Skip to content

fix(system): isolate sliced system metadata#1014

Open
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/issue-985
Open

fix(system): isolate sliced system metadata#1014
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/issue-985

Conversation

@njzjz-bot

@njzjz-bot njzjz-bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #985.

Deep-copy frame-selected arrays and frame-independent metadata so subsystem slices cannot mutate their source through NumPy views or shared lists.

Tests: cd tests && python -m unittest test_system_append

Why existing tests missed it: The existing tests only compared sliced values; they never mutated a slice, so aliasing remained invisible.

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

Summary by CodeRabbit

  • Bug Fixes

    • Improved subsystem and append operations to prevent unintended data sharing.
    • Changes made to a subsystem or appended system no longer unexpectedly modify the original source data.
  • Tests

    • Added regression coverage for data ownership during subsystem creation and append operations.

Deep-copy both frame-selected arrays and frame-independent metadata when creating a subsystem so slice views and mutable values cannot alias the source. Add regressions for coordinates, cells, names, and atom types, which existing comparison tests did not mutate.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. dpdata labels Jul 16, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 2 untouched benchmarks


Comparing njzjz-bot:fix/issue-985 (8a6b8b9) with master (0416b54)

Open in CodSpeed

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

System.sub_system now deep-copies extracted data, with tests covering mutation isolation for subsystem slices and systems populated by first append.

Changes

System ownership isolation

Layer / File(s) Summary
Deep-copy subsystem data
dpdata/system.py
System.sub_system deep-copies both frame-dependent and frame-independent fields when constructing a subsystem.
Ownership regression coverage
tests/test_system_append.py
Tests verify that subsystem mutations do not affect the parent and that first-append targets do not alias source metadata or coordinates.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: njzjz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Mentions the main slicing-related fix, though it omits the first-append deepcopy regression covered by the PR.
Linked Issues check ✅ Passed The patch deep-copies sub_system slices and first append data, matching #985's requirements and regression tests.
Out of Scope Changes check ✅ Passed Changes stay focused on ownership and aliasing fixes in System.sub_system(), append behavior, and regression tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_system_append.py`:
- Around line 45-54: Update System.append() so its first-append path deep-copies
system.data instead of using a shallow dictionary copy, ensuring nested lists
and NumPy arrays do not alias the source. Preserve the existing append behavior
for non-empty systems.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c713429b-f5d4-4b18-b072-81d23c410825

📥 Commits

Reviewing files that changed from the base of the PR and between 0416b54 and 8a6b8b9.

📒 Files selected for processing (2)
  • dpdata/system.py
  • tests/test_system_append.py

Comment on lines +45 to +54
def test_first_append_does_not_alias_source(self):
source = dpdata.System("poscars/POSCAR.oh.d", fmt="vasp/poscar")
target = dpdata.System()
target.append(source)

source.data["atom_names"][0] = "X"
source.data["coords"][0, 0, 0] = 123.0
self.assertEqual(target.data["atom_names"][0], "O")
self.assertNotEqual(target.data["coords"][0, 0, 0], 123.0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Missing implementation for first-append deep-copy.

While this test correctly asserts that appending to an empty system should deep-copy the source data to prevent aliasing, the corresponding fix in dpdata/system.py was omitted from this PR. Currently, System.append() uses self.data = system.data.copy(), which performs a shallow copy of the dictionary. Consequently, lists and numpy arrays remain aliased, and this test will fail when executed.

To fix this, update System.append() in dpdata/system.py (around line 489) to use deepcopy:

        elif not len(self.data["atom_numbs"]):
            # this system is non-converged but the system to append is converged
-           self.data = system.data.copy()
+           self.data = deepcopy(system.data)
            return False

Based on the PR objectives, the first append into an empty system must deep-copy source data to prevent mutations from propagating.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_system_append.py` around lines 45 - 54, Update System.append() so
its first-append path deep-copies system.data instead of using a shallow
dictionary copy, ensuring nested lists and NumPy arrays do not alias the source.
Preserve the existing append behavior for non-empty systems.

@wanghan-iapcm wanghan-iapcm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes: this fixes the sub_system half of #985 but leaves the first-append()-into-empty path aliased, so the PR's own test_first_append_does_not_alias_source fails and CI (build 3.10 / 3.13) is red. See the inline note for the one-line fix.


source.data["atom_names"][0] = "X"
source.data["coords"][0, 0, 0] = 123.0
self.assertEqual(target.data["atom_names"][0], "O")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_first_append_does_not_alias_source exercises the first-append()-into-empty path, but that path was not changed — dpdata/system.py:489 still does self.data = system.data.copy(), a shallow dict.copy() that keeps the source's arrays/lists aliased. So this test fails as shipped (CI build 3.10 / 3.13 is red). Applying self.data = deepcopy(system.data) there completes the #985 fix and turns the test green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dpdata size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] System slicing and first append share mutable data with the source

2 participants