_mtp.py from_config() feature update#618
Conversation
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
WalkthroughThe recent updates focus on improving element ordering control in Changes
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 7
Out of diff range and nitpick comments (9)
tests/apps/pes/test_mtp.py (9)
Line range hint
1-1: Add a module-level docstring to provide an overview of the module's purpose and functionality.
Line range hint
25-25: Add a docstring to thesetUpClassmethod to explain its functionality, particularly how it sets up the test environment.
Line range hint
31-31: Add a docstring to thetearDownClassmethod to describe its role in cleaning up after tests are run.
Line range hint
35-35: Add a docstring to thesetUpmethod to explain how it initializes the conditions for each test.
Line range hint
49-49: Add a docstring to thetest_write_read_cfgsmethod to describe what this test verifies.
Line range hint
60-60: ReplaceassertAlmostEqualwith a regularassertfor consistency with other tests and to simplify the code.- self.assertAlmostEqual(energy1, energy2) + assert energy1 == energy2
Line range hint
66-66: Add a docstring to thetest_trainmethod to explain what aspects of thetrainmethod it tests.
Line range hint
79-79: Add a docstring to thetest_evaluatemethod to describe its purpose and the conditions under which it operates.
Line range hint
99-99: Add a docstring to thetest_predict_efsmethod to explain what this test checks and how it uses thepredict_efsmethod.
| mtp = MTPotential.from_config(config_file, elements=["Mo"]) | ||
| assert mtp.param is not None | ||
| mtp = MTPotential.from_config(config_file_4, elements=['Nb','O','Li','Cl']) | ||
| assert mtp.param is not None and mtp.elements == ['Li','Nb','Cl','O'] |
There was a problem hiding this comment.
Break down the assertion into multiple parts for clarity and to isolate which part of the assertion might fail.
- assert mtp.param is not None and mtp.elements == ['Li','Nb','Cl','O']
+ assert mtp.param is not None
+ assert mtp.elements == ['Li','Nb','Cl','O']Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
| assert mtp.param is not None and mtp.elements == ['Li','Nb','Cl','O'] | |
| assert mtp.param is not None | |
| assert mtp.elements == ['Li','Nb','Cl','O'] |
| mtp = MTPotential.from_config(config_file_4, elements=['Nb','O','Li','Cl']) | ||
| assert mtp.param is not None and mtp.elements == ['Li','Nb','Cl','O'] | ||
| mtp = MTPotential.from_config(config_file_4, elements=['Nb','O','Li','Cl'], default_element_ordering=False) | ||
| assert mtp.param is not None and mtp.elements == ['Nb','O','Li','Cl'] |
There was a problem hiding this comment.
Break down the assertion into multiple parts for clarity and to isolate which part of the assertion might fail.
- assert mtp.param is not None and mtp.elements == ['Nb','O','Li','Cl']
+ assert mtp.param is not None
+ assert mtp.elements == ['Nb','O','Li','Cl']Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
| assert mtp.param is not None and mtp.elements == ['Nb','O','Li','Cl'] | |
| assert mtp.param is not None | |
| assert mtp.elements == ['Nb','O','Li','Cl'] |
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
| f"Order for the elements has been altered from {elements} to {ordered_elements} to ensure" | ||
| + " consistency with default element ordering in maml during MTP fitting. Change the" |
There was a problem hiding this comment.
Consider using implicit string concatenation for better readability.
- f"Order for the elements has been altered from {elements} to {ordered_elements} to ensure"
- + " consistency with default element ordering in maml during MTP fitting. Change the"
+ f"Order for the elements has been altered from {elements} to {ordered_elements} to ensure consistency with default element ordering in maml during MTP fitting. Change the"Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
| f"Order for the elements has been altered from {elements} to {ordered_elements} to ensure" | |
| + " consistency with default element ordering in maml during MTP fitting. Change the" | |
| f"Order for the elements has been altered from {elements} to {ordered_elements} to ensure consistency with default element ordering in maml during MTP fitting. Change the" |
|
Thanks for the PR David @dsun980701. Please try to make the linting tests pass, and then we can ask Shyue Ping for a review. |
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
|
Thank you for reviewing @JiQi535. I have changed my code to address the linting issue. However, as of now, the linting is failing not from my changes, but from "maml/apps/symbolic/init." and "maml/utils/_signal_processing.py". Please let me know if there is anything that I can do about these issues! |
Signed-off-by: David <91859764+dsun980701@users.noreply.github.com>
|
Hi @dsun980701, can you please try to pull the latest changes from the main branch? I think I have fixed the united tests and now at least the pytest should work. |
shyuep
left a comment
There was a problem hiding this comment.
Automated PR review generated by Claude (Opus 4.7) on behalf of @shyuep. Based purely on the diff — no code was executed locally.
Thanks @dsun980701 for the contribution. The intent — making MTPotential.from_config() reorder elements to a canonical order so a trained .mtp file isn't misapplied — is a real footgun worth fixing. A few issues need addressing before this can merge:
1. The sort is by atomic number, not Pauling electronegativity. The PR description and docstring both say "Pauling electronegativity", but sorted([Element(x) for x in elements]) uses Element.__lt__, which sorts by Z (atomic number). If the canonical convention you want is electronegativity (e.g. consistent with how MLIP/MTP fitting code orders species), you need:
ordered_elements = [str(x) for x in sorted([Element(x) for x in elements], key=lambda e: (e.X, e.Z))](using Z as a tiebreaker for elements with the same / undefined X). Otherwise either change the docstring/warning to say "atomic number" or fix the implementation. Worth confirming with the upstream MTP/MLIP convention — please double-check what order the fitting actually emits.
2. Wrong warning category. ImportWarning is reserved for issues raised at module import time (PEP 8). For a runtime user-facing notice about argument reordering, use UserWarning (default) or RuntimeWarning. As written, this warning will be silenced by default in most contexts because ImportWarning is suppressed unless python -W is set.
3. species_count parsing is brittle. num_species = -1 is initialized then only overwritten if a "species_count" line is found. If the line is missing (malformed file), the validation message becomes "Inconsistent number of species between the provided .mtp file and the elements argument" even though the real problem is a missing key. Either raise a clearer "could not find species_count in file" error, or use next(...) with a sentinel.
4. No safeguard when default_element_ordering=False. The whole point of the PR is to prevent the user mis-ordering elements. With the new flag set to False, the user is back to the original footgun and the mtp's actual species order is never validated against the user-provided list. Consider always emitting a warning if the user-provided order differs from the canonical one, even when ordering is disabled.
5. Test file tests/apps/pes/MTP/fitted_4.mtp is 117 lines of opaque numerical data. Could you add a single concise test that exercises the reordering branch (input a deliberately mis-ordered elements list and assert the warning fires + the final mtp.elements is canonical), and one that exercises default_element_ordering=False?
6. Unrelated change in _signal_processing.py. The Callable[..., Any] widening is a separate concern from the _mtp.py feature. Not a blocker, but in the future please split unrelated touch-ups into their own PR.
7. Branch is very stale (last activity Aug 2024) and CI hasn't run on the head commit. Please rebase onto master and push so CI can produce a fresh signal — the matgl-related test failures we're seeing on PR #705 may also affect this PR independently.
|
🤖 Automated PR review (generated by Claude on behalf of @shyuep)
Generated by Claude Code |
Summary
Major changes:
Checklist
ruff.mypy.duecredit@due.dcitedecorators to reference relevant papers by DOI (example)Tip: Install
pre-commithooks to auto-check types and linting before every commit:Summary by CodeRabbit
New Features
MTPotentialclass based on Pauling electronegativity, enhancing consistency in molecular simulations.Bug Fixes
get_sp_methodfunction in the signal processing module to accept a more flexible range of callable types, improving its utility and compatibility.Tests
MTPotentialclass to ensure robustness and reliability.