deepmodeling / dpgen

The deep potential generator to generate a deep-learning based model of interatomic potential energy and force field
https://docs.deepmodeling.com/projects/dpgen/
GNU Lesser General Public License v3.0
298 stars 173 forks source link

chore: ban module-level import of pymatgen #1597

Closed njzjz closed 2 months ago

njzjz commented 2 months ago

Pymatgen is still imcompatbile with NumPy 2. Move the import into runtime to fix the documentation builds and prevent potential errors when one uses dpgen without using pymatgen.

Signed-off-by: Jinzhe Zeng jinzhe.zeng@rutgers.edu

Summary by CodeRabbit

coderabbitai[bot] commented 2 months ago
Walkthrough ## Walkthrough The updates primarily involved restructuring import statements to improve code readability and maintainability. Imports were moved from the global scope to within specific methods or functions, localizing dependencies where they are used. This change impacts various files related to elasticity calculations, VASP input/output handling, crystal structure generation, and defect generation. The codebase now ensures more modular and efficient import management. ## Changes | File Path | Change Summary | |------------------------------------|-------------------------------------------------------------------------------------------------------------| | `dpgen/auto_test/Elastic.py` | Reorganized imports within methods for elasticity calculations and VASP handling. | | `dpgen/auto_test/Gamma.py` | Moved imports to within `make_confs` and `__gen_slab_ase`, added path existence check in `make_confs`. | | `dpgen/auto_test/Interstitial.py` | Localized imports within `make_confs` method. | | `dpgen/auto_test/Surface.py` | Moved imports into `make_confs` method. | | `dpgen/auto_test/VASP.py` | Localized imports within `make_potential_files` and `make_input_file` methods. | | `dpgen/auto_test/Vacancy.py` | Localized imports within `make_confs` method. | | `dpgen/auto_test/gen_confs.py` | Localized imports within `test_fit`, `gen_element`, and `gen_alloy` functions. | | `dpgen/auto_test/lib/abacus.py` | Removed unused import, localized import within `stru2Structure` function. | | `dpgen/auto_test/lib/crys.py` | Localized imports within various functions for creating crystal structures. | | `dpgen/auto_test/lib/vasp.py` | Localized imports within `make_vasp_kpoints_from_incar` function. | | `dpgen/auto_test/mpdb.py` | Localized imports within `check_apikey` function. | | `dpgen/data/gen.py` | Localized imports within various functions for generating supercells and VASP input files. | | `dpgen/data/surf.py` | Localized imports within `poscar_elong` and `make_super_cell_pymatgen` functions. | | `dpgen/database/entry.py` | Localized import within `__init__` method of `Entry` class. | | `dpgen/database/vasp.py` | Localized imports within methods related to VASP input files handling. | | `dpgen/generator/lib/ele_temp.py` | Localized imports within `_get_incar_ele_temp` and `_get_incar_nbands` methods. | | `dpgen/generator/lib/vasp.py` | Localized import within `incar_upper` function. | | `dpgen/generator/run.py` | Localized imports within `make_vasp_incar_ele_temp` and `make_fp_vasp_kp` functions. | | `pyproject.toml` | Updated linting configurations, added banned module-level import for `pymatgen`, and extended per-file ignores for rule `TID253`. |

Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between 8f7829477c1d2d1b3ecc414685ebbec84a09f4bd and 8c541b7eb7b6130ee1eb9e8ba15db63301cda75e.
Files selected for processing (19) * dpgen/auto_test/Elastic.py (4 hunks) * dpgen/auto_test/Gamma.py (3 hunks) * dpgen/auto_test/Interstitial.py (2 hunks) * dpgen/auto_test/Surface.py (2 hunks) * dpgen/auto_test/VASP.py (3 hunks) * dpgen/auto_test/Vacancy.py (2 hunks) * dpgen/auto_test/gen_confs.py (4 hunks) * dpgen/auto_test/lib/abacus.py (2 hunks) * dpgen/auto_test/lib/crys.py (5 hunks) * dpgen/auto_test/lib/vasp.py (2 hunks) * dpgen/auto_test/mpdb.py (1 hunks) * dpgen/data/gen.py (4 hunks) * dpgen/data/surf.py (4 hunks) * dpgen/database/entry.py (2 hunks) * dpgen/database/vasp.py (4 hunks) * dpgen/generator/lib/ele_temp.py (2 hunks) * dpgen/generator/lib/vasp.py (2 hunks) * dpgen/generator/run.py (3 hunks) * pyproject.toml (2 hunks)
Files skipped from review due to trivial changes (1) * dpgen/auto_test/lib/abacus.py
Additional comments not posted (41)
dpgen/auto_test/mpdb.py (1)
`9-10`: **LGTM! Import localized within the function.** The import of `MPRester` and `MPRestError` within the function scope improves module loading efficiency and avoids unnecessary imports when the function is not called.
dpgen/auto_test/lib/crys.py (7)
`5-6`: **LGTM! Import localized within the function.** The import of `Structure` within the function scope improves module loading efficiency and avoids unnecessary imports when the function is not called. --- `13-15`: **LGTM! Imports localized within the function.** The imports of `Lattice` and `Structure` within the function scope improve module loading efficiency and avoid unnecessary imports when the function is not called. --- `25-27`: **LGTM! Imports localized within the function.** The imports of `Lattice` and `Structure` within the function scope improve module loading efficiency and avoid unnecessary imports when the function is not called. --- `33-35`: **LGTM! Imports localized within the function.** The imports of `Lattice` and `Structure` within the function scope improve module loading efficiency and avoid unnecessary imports when the function is not called. --- `50-52`: **LGTM! Imports localized within the function.** The imports of `Lattice` and `Structure` within the function scope improve module loading efficiency and avoid unnecessary imports when the function is not called. --- `66-68`: **LGTM! Imports localized within the function.** The imports of `Lattice` and `Structure` within the function scope improve module loading efficiency and avoid unnecessary imports when the function is not called. --- `87-89`: **LGTM! Imports localized within the function.** The imports of `Lattice` and `Structure` within the function scope improve module loading efficiency and avoid unnecessary imports when the function is not called.
dpgen/generator/lib/ele_temp.py (2)
`88-89`: **LGTM! Import localized within the method.** The import of `Incar` within the method scope improves module loading efficiency and avoids unnecessary imports when the method is not called. --- `95-96`: **LGTM! Import localized within the method.** The import of `Incar` within the method scope improves module loading efficiency and avoids unnecessary imports when the method is not called.
dpgen/generator/lib/vasp.py (1)
`133-134`: **LGTM! Import localized within the function.** The import of `Incar` within the function scope improves module loading efficiency and avoids unnecessary imports when the function is not called.
dpgen/database/entry.py (1)
`54-55`: **Good practice: Localized import.** Moving the import of `Composition` to within the `__init__` method ensures that `pymatgen` is only imported when an instance of `Entry` is created. This can help avoid unnecessary imports and potential import errors if `pymatgen` is not installed.
pyproject.toml (3)
`92-92`: **Good practice: Enforcing linting rules.** Adding the linting rule `TID253` to the `select` list ensures that module-level imports of banned modules are flagged by the linter, promoting better import practices. --- `116-119`: **Good practice: Banning module-level imports.** Adding `pymatgen` to the `banned-module-level-imports` list ensures that module-level imports of `pymatgen` are flagged, aligning with the PR objective to move such imports to runtime. --- `121-122`: **Good practice: Extending per-file ignores.** Adding the rule `TID253` to the per-file ignores for files under `tests/` ensures that test files are not flagged for module-level imports, which might be necessary for testing purposes.
dpgen/auto_test/gen_confs.py (3)
`20-21`: **Good practice: Localized import.** Moving the import of `StructureMatcher` to within the `test_fit` function ensures that `pymatgen` is only imported when the function is called. This can help avoid unnecessary imports if the function is not used. --- `52-54`: **Good practice: Localized imports.** Moving the imports of `StructureMatcher` and `MPRester` to within the `gen_element` function ensures that `pymatgen` is only imported when the function is called. This can help avoid unnecessary imports if the function is not used. --- `98-99`: **Good practice: Localized import.** Moving the import of `MPRester` to within the `gen_alloy` function ensures that `pymatgen` is only imported when the function is called. This can help avoid unnecessary imports if the function is not used.
dpgen/database/vasp.py (3)
`20-21`: **Good practice: Localized imports.** Moving the imports of `Potcar` and `PotcarSingle` to within the `__init__` method ensures that `pymatgen` is only imported when an instance of `DPPotcar` is created. This can help avoid unnecessary imports if the class is not used. --- `83-84`: **Good practice: Localized import.** Moving the import of `Potcar` to within the `from_file` method ensures that `pymatgen` is only imported when the method is called. This can help avoid unnecessary imports if the method is not used. --- `183-184`: **Good practice: Localized imports.** Moving the imports of `Incar`, `Kpoints`, and `Poscar` to within the `from_directory` method ensures that `pymatgen` is only imported when the method is called. This can help avoid unnecessary imports if the method is not used.
dpgen/auto_test/VASP.py (2)
`23-24`: **LGTM! Localized import for `Structure`.** The import of `Structure` has been moved to within the method, which is a good practice for optimizing imports. --- `72-73`: **LGTM! Localized imports for `Incar` and `Kpoints`.** The imports of `Incar` and `Kpoints` have been moved to within the method, which is a good practice for optimizing imports.
dpgen/auto_test/Vacancy.py (1)
`78-80`: **LGTM! Localized imports for `VacancyGenerator` and `Structure`.** The imports of `VacancyGenerator` and `Structure` have been moved to within the method, which is a good practice for optimizing imports.
dpgen/auto_test/Elastic.py (3)
`51-53`: **LGTM! Localized imports for `DeformedStructureSet`, `Strain`, and `Structure`.** The imports of `DeformedStructureSet`, `Strain`, and `Structure` have been moved to within the method, which is a good practice for optimizing imports. --- `190-191`: **LGTM! Localized imports for `Incar` and `Kpoints`.** The imports of `Incar` and `Kpoints` have been moved to within the method, which is a good practice for optimizing imports. --- `253-255`: **LGTM! Localized imports for `ElasticTensor` and `Stress`.** The imports of `ElasticTensor` and `Stress` have been moved to within the method, which is a good practice for optimizing imports.
dpgen/auto_test/Surface.py (1)
`86-88`: **LGTM! Localized imports for `Structure` and `generate_all_slabs`.** The imports of `Structure` and `generate_all_slabs` have been moved to within the method, which is a good practice for optimizing imports.
dpgen/auto_test/lib/vasp.py (1)
`505-506`: **LGTM! Imports moved inside the function.** The import statements for `Incar` and `Kpoints` were moved inside the `make_vasp_kpoints_from_incar` function to avoid module-level imports of `pymatgen`. This change ensures that the import is only done when necessary.
dpgen/auto_test/Gamma.py (2)
`95-96`: **LGTM! Import moved inside the method.** The import statement for `Structure` was moved inside the `make_confs` method to avoid module-level imports of `pymatgen`. This change ensures that the import is only done when necessary. --- `290-291`: **LGTM! Import moved inside the method.** The import statement for `AseAtomsAdaptor` was moved inside the `__gen_slab_ase` method to avoid module-level imports of `pymatgen`. This change ensures that the import is only done when necessary.
dpgen/data/surf.py (3)
`166-167`: **LGTM! Import moved inside the function.** The import statement for `Structure` was moved inside the `poscar_elong` function to avoid module-level imports of `pymatgen`. This change ensures that the import is only done when necessary. --- `215-217`: **LGTM! Imports moved inside the function.** The import statements for `Element`, `Structure`, and `AseAtomsAdaptor` were moved inside the `make_super_cell_pymatgen` function to avoid module-level imports of `pymatgen`. This change ensures that the imports are only done when necessary. --- `404-405`: **LGTM! Import moved inside the function.** The import statement for `Poscar` was moved inside the `poscar_scale` function to avoid module-level imports of `pymatgen`. This change ensures that the import is only done when necessary.
dpgen/auto_test/Interstitial.py (1)
`79-81`: **LGTM! Imports moved inside the method.** The import statements for `InterstitialGenerator` and `Structure` were moved inside the `make_confs` method to avoid module-level imports of `pymatgen`. This change ensures that the imports are only done when necessary.
dpgen/data/gen.py (4)
`301-302`: **LGTM! Import moved to runtime.** The import of `Structure` from `pymatgen.core` has been moved to runtime, which is appropriate to avoid module-level import issues. --- `345-346`: **LGTM! Import moved to runtime.** The import of `Structure` from `pymatgen.core` has been moved to runtime, which is appropriate to avoid module-level import issues. --- `1449-1450`: **LGTM! Import moved to runtime.** The import of `Incar` from `pymatgen.io.vasp` has been moved to runtime, which is appropriate to avoid module-level import issues. --- `1449-1450`: **LGTM! Import moved to runtime.** The import of `Incar` from `pymatgen.io.vasp` has been moved to runtime, which is appropriate to avoid module-level import issues.
dpgen/generator/run.py (2)
`3080-3081`: **LGTM!** The import statement for `Incar` is correctly moved to within the function to avoid unnecessary module-level imports. --- `3159-3160`: **LGTM!** The import statements for `Incar` and `Kpoints` are correctly moved to within the function to avoid unnecessary module-level imports.
--- 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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code 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 testing code 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 testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` 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 an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configuration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - 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/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
codecov[bot] commented 2 months ago

Codecov Report

Attention: Patch coverage is 54.90196% with 23 lines in your changes missing coverage. Please review.

Project coverage is 49.51%. Comparing base (a33e270) to head (8c541b7). Report is 2 commits behind head on devel.

Files Patch % Lines
dpgen/auto_test/lib/crys.py 0.00% 13 Missing :warning:
dpgen/auto_test/gen_confs.py 0.00% 4 Missing :warning:
dpgen/auto_test/Elastic.py 60.00% 2 Missing :warning:
dpgen/data/gen.py 33.33% 2 Missing :warning:
dpgen/auto_test/lib/vasp.py 0.00% 1 Missing :warning:
dpgen/auto_test/mpdb.py 0.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## devel #1597 +/- ## ========================================== - Coverage 49.59% 49.51% -0.08% ========================================== Files 83 83 Lines 14851 14870 +19 ========================================== - Hits 7365 7363 -2 - Misses 7486 7507 +21 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.