Skip to content

fix: build pymatgen Molecule species from atom_types order (#994)#1010

Merged
njzjz merged 1 commit into
deepmodeling:masterfrom
CedricConday:fix/pymatgen-molecule-species-order-994
Jul 7, 2026
Merged

fix: build pymatgen Molecule species from atom_types order (#994)#1010
njzjz merged 1 commit into
deepmodeling:masterfrom
CedricConday:fix/pymatgen-molecule-species-order-994

Conversation

@CedricConday

@CedricConday CedricConday commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #994.

Summary

PyMatgenMoleculeFormat.to_system() built the species list by expanding grouped atom_names/atom_numbs (e.g. H, H, O), but passed coords in their atom_types order (e.g. H, O, H). When atoms are not grouped by type, the resulting pymatgen.core.Molecule assigns species to the wrong coordinates.

Change

Build species per-atom from atom_types, matching the coordinate order — the same construction the sibling Structure exporter already uses in this file:

species = [data["atom_names"][tt] for tt in data["atom_types"]]

This is the fix suggested in the issue.

Testing

  • Added a regression test (test_ungrouped_atom_types_species_match_coords) using the reporter's reproducer: species now follow the coordinate order (H, O, H) and each site sits on its original coordinate. It fails on master and passes with this change.
  • All 50 pymatgen-related tests pass; ruff check and ruff format --check clean.

Disclosure: authored with AI assistance (Claude Code); reviewed by me and I'll shepherd it through review.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed molecule conversion so atom species now follow the actual per-atom type order.
    • Ensured converted molecule coordinates continue to match the source structure for each frame.
  • Tests

    • Added a regression test covering ungrouped atom types to verify species ordering and coordinate mapping during molecule conversion.

…ing#994)

PyMatgenMoleculeFormat.to_system() built the species list by expanding
grouped atom_names/atom_numbs, but passed coords in atom_types order. When
atoms are not grouped by type, species were assigned to the wrong
coordinates. Build species per-atom from atom_types, matching the coord
order and the sibling Structure exporter (line 37).

Adds a regression test that fails on master (['H','H','O'] vs ['H','O','H'])
and passes with this change.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jul 6, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 6, 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 CedricConday:fix/pymatgen-molecule-species-order-994 (7e073a4) with master (7092ff0)

Open in CodSpeed

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.87%. Comparing base (7092ff0) to head (7e073a4).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1010      +/-   ##
==========================================
- Coverage   86.87%   86.87%   -0.01%     
==========================================
  Files          89       89              
  Lines        8266     8264       -2     
==========================================
- Hits         7181     7179       -2     
  Misses       1085     1085              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6bf3e812-43aa-43b3-bc9c-c319f044d29a

📥 Commits

Reviewing files that changed from the base of the PR and between 7092ff0 and 7e073a4.

📒 Files selected for processing (2)
  • dpdata/plugins/pymatgen.py
  • tests/test_pymatgen_molecule.py

📝 Walkthrough

Walkthrough

This PR fixes a bug in PyMatgenMoleculeFormat.to_system where the species list was built from grouped atom_names/atom_numbs counts instead of per-atom atom_types, causing species-coordinate misalignment for ungrouped atom types. A regression test is added.

Changes

Species ordering fix

Layer / File(s) Summary
Fix species-coordinate mismatch
dpdata/plugins/pymatgen.py
Species list is now built by mapping each atom_types index to its corresponding atom_names entry, ensuring species order matches the coordinate order instead of grouping by type counts.
Regression test for species ordering
tests/test_pymatgen_molecule.py
Adds a test constructing a dpdata.System with ungrouped atom types, converting to pymatgen/molecule, and asserting species order and coordinates match the input.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Sequence Diagram(s)

Not applicable; the change is a localized bug fix without multi-component interaction flow.

Suggested labels: bug, tests

Suggested reviewers: none identified from the provided context

Poem:
A rabbit hopped through atoms small,
Found species scrambled, out of thrall.
With atom_types now leading the way,
Each H and O finds its rightful place to stay.
Coordinates matched, no more mix-up call! 🐇✨

🚥 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 The title clearly states the pymatgen Molecule species-order fix and references the issue.
Linked Issues check ✅ Passed The PR builds species from atom_types order and adds a regression test for the H,O,H case described in #994.
Out of Scope Changes check ✅ Passed Changes are limited to the pymatgen molecule fix and its regression test, matching the stated objective.
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.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 7, 2026
@njzjz njzjz merged commit 221f254 into deepmodeling:master Jul 7, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] pymatgen Molecule export can assign species to the wrong coordinates

2 participants