Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define needs_u_correction(comp: CompositionLike) -> set[str] utility function #3703

Merged
merged 5 commits into from Mar 26, 2024

Conversation

janosh
Copy link
Member

@janosh janosh commented Mar 23, 2024

Checks if a CompositionLike is Hubbard U-corrected in the Materials Project 2020 GGA/GGA+U mixing scheme.

would be great to get other's opinions on this @rkingsbury @JaGeo @Andrew-S-Rosen

@janosh janosh added enhancement A new feature or improvement to an existing one tests Issues with or changes to the pymatgen test suite mixing-schemes About mixing energies from different DFT functionals labels Mar 23, 2024
Copy link

coderabbitai bot commented Mar 26, 2024

Walkthrough

The update brings a mix of enhancements across different components: the pre-commit configuration, pymatgen library updates, and test suite improvements. It introduces a new argument for the ruff hook, expands pymatgen's functionality with additional imports and compatibility checks, and enriches the test suite with new checks and constants. These changes collectively enhance code quality, compatibility handling, and testing robustness, reflecting a commitment to maintaining high standards in software development.

Changes

Files Summary
.pre-commit-config.yaml Updated ruff-pre-commit revision to v0.3.4 and added --unsafe-fixes argument.
pymatgen/entries/compatibility.py Enhanced compatibility checks, added imports, and introduced needs_u_correction function.
tests/entries/.../test_compatibility.py Expanded tests with new imports, constants, and a method to check u_correction.

🐰✨
In the realm of code, where changes abound,
A rabbit hopped in, improvements it found.
With a tweak and a fix, in files it did roam,
Enhancing the code, in its digital home.
So here's to the changes, both big and small,
May they make our code stronger, bugs to appall.
🌟📜🐇

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@janosh janosh enabled auto-merge (squash) March 26, 2024 06:33
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b1e5023 and 20e60b2.
Files selected for processing (3)
  • .pre-commit-config.yaml (1 hunks)
  • pymatgen/entries/compatibility.py (7 hunks)
  • tests/entries/test_compatibility.py (4 hunks)
Additional comments: 8
.pre-commit-config.yaml (1)
  • 11-11: The update to ruff-pre-commit from v0.3.3 to v0.3.4 is noted and seems appropriate for staying up-to-date with dependencies. However, the addition of --unsafe-fixes to the ruff hook arguments warrants careful consideration. While this argument can enable more aggressive automatic fixes, it may also introduce changes that could potentially alter the intended behavior of the code. It's recommended to thoroughly review the changes made by this option during the pre-commit phase to ensure they align with the project's standards and expectations.
pymatgen/entries/compatibility.py (5)
  • 51-54: The assertion here checks for consistency in Hubbard U corrections for O and F. Consider adding a more informative error message to guide users on how to resolve the issue if this assertion fails. For example, you could suggest checking the MP2020Compatibility.yaml file for correctness.
  • 1472-1495: The needs_u_correction function could benefit from an enhanced docstring that provides more context about its return value. Specifically, it would be helpful to explain the significance of the returned set of elements and how it indicates the need for U-correction. This additional information will make the function more understandable to users who may not be familiar with the concept of U-corrections.
  • 802-808: The addition of a deprecation warning in the MaterialsProjectCompatibility class is a good practice for informing users about outdated functionality. Ensure that all references to this class in documentation and example code are updated to guide users towards the recommended alternatives.
  • 909-918: In the MaterialsProject2020Compatibility class, consider adding error handling or logging for cases where the configuration file might be missing or corrupted. This will improve the robustness of the class and provide clearer feedback to users encountering issues with the configuration.
  • 1470-1472: The MaterialsProjectAqueousCompatibility class introduces a significant enhancement with the aqueous energy referencing scheme. Ensure thorough testing, especially for the get_adjustments method, to validate the accuracy of the adjustments applied and to handle edge cases gracefully. Consider adding unit tests that cover a variety of compositions, including edge cases like compositions without explicit oxidation states.
tests/entries/test_compatibility.py (2)
  • 2044-2063: The test class TestCorrectionErrors2020Compatibility and its method test_errors are correctly implemented to check the correction uncertainties of various entries. Good use of approx for floating-point comparison.
  • 2044-2065: The standalone function test_needs_u_correction is correctly implemented to test various compositions against expected results for needing U correction. Proper use of pytest.mark.parametrize for testing multiple scenarios.

Comment on lines +1085 to +1088
expected_u = float(u_settings.get(symbol, 0))
actual_u = float(calc_u.get(symbol, 0))
if actual_u != expected_u:
raise CompatibilityError(f"Invalid U value of {actual_u:.1f} on {symbol}, expected {expected_u:.1f}")
raise CompatibilityError(f"Invalid U value of {actual_u:.3} on {symbol}, expected {expected_u:.3}")
Copy link

Choose a reason for hiding this comment

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

In the MaterialsProject2020Compatibility.get_adjustments method, the comparison between expected_u and actual_u uses floating-point equality. Due to the nature of floating-point arithmetic, this might lead to unexpected results. Consider using a tolerance-based comparison method to avoid potential issues with floating-point precision.

- if actual_u != expected_u:
+ if not math.isclose(actual_u, expected_u, rel_tol=1e-9):

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.

Suggested change
expected_u = float(u_settings.get(symbol, 0))
actual_u = float(calc_u.get(symbol, 0))
if actual_u != expected_u:
raise CompatibilityError(f"Invalid U value of {actual_u:.1f} on {symbol}, expected {expected_u:.1f}")
raise CompatibilityError(f"Invalid U value of {actual_u:.3} on {symbol}, expected {expected_u:.3}")
expected_u = float(u_settings.get(symbol, 0))
actual_u = float(calc_u.get(symbol, 0))
if not math.isclose(actual_u, expected_u, rel_tol=1e-9):
raise CompatibilityError(f"Invalid U value of {actual_u:.3} on {symbol}, expected {expected_u:.3}")

@janosh janosh merged commit 167171f into master Mar 26, 2024
22 checks passed
@janosh janosh deleted the needs_u_correction branch March 26, 2024 06:44
@rkingsbury
Copy link
Contributor

Sorry to be late to the party here @janosh . This looks useful to me. It's easy to forget that we only correct certain TMs when they occur as oxides or fluorides so it'll be nice to have a check like this. Thanks for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement to an existing one mixing-schemes About mixing energies from different DFT functionals tests Issues with or changes to the pymatgen test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants