Replace deepcopy with coord save/restore in CIF writing#15
Replace deepcopy with coord save/restore in CIF writing#15longleo17 wants to merge 1 commit intobytedance:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes CIF dumping during inference by avoiding copy.deepcopy(atom_array) and instead temporarily swapping AtomArray.coord to the predicted coordinates while writing, then restoring the original coordinates.
Changes:
- Updated
save_structure_cif()in the runner dumper to use coord save/restore instead of deep-copying the fullAtomArray. - Added a test to enforce that
copy.deepcopy()is no longer used inpxdesign/runner/dumper.pyand to validate the basic coord swap/restore pattern.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pxdesign/runner/dumper.py |
Replaces deep-copy-based CIF writing with coord swapping for improved performance. |
tests/test_avoid_deepcopy_cif.py |
Adds regression tests to prevent reintroduction of copy.deepcopy() and to validate coord restoration behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| original_coord = atom_array.coord | ||
| atom_array.coord = pred_coordinate.cpu().numpy() | ||
| save_atoms_to_cif( | ||
| output_fpath, | ||
| pred_atom_array, | ||
| atom_array, | ||
| entity_poly_type, | ||
| pdb_id, | ||
| ) | ||
| atom_array.coord = original_coord |
There was a problem hiding this comment.
atom_array.coord is restored only on the happy path. If save_atoms_to_cif() raises (I/O error, invalid structure, etc.), the function exits early and leaves atom_array mutated for the rest of the run. Wrap the coord swap in a try/finally so the original coordinates are restored even on exceptions.
Summary
Replaces
copy.deepcopy(atom_array)with a coord save/restore pattern insave_structure_cif(). For each of N_sample outputs (100-500), the old code deep-copied the entire AtomArray just to set new coordinates. The swap pattern just reassigns the.coordattribute reference — no data is copied.Measured impact (synthetic benchmark)
Changes
pxdesign/runner/dumper.py—save_structure_cif(): save original coords, assign predicted coords, write CIF, restore originalTest plan
pytest tests/test_avoid_deepcopy_cif.py -v