deepmodeling / dpdata

Manipulating multiple atomic simulation data formats, including DeePMD-kit, VASP, LAMMPS, ABACUS, etc.
https://docs.deepmodeling.com/projects/dpdata/
GNU Lesser General Public License v3.0
188 stars 128 forks source link

pref: lazy import modules #658

Closed njzjz closed 1 month ago

njzjz commented 1 month ago

Fix #526.

Summary by CodeRabbit

coderabbitai[bot] commented 1 month ago

Walkthrough

The changes across the dpdata library primarily focus on optimizing import statements to reduce the initial loading time when importing the library. This includes moving imports to within functions where they are used, restructuring conditional imports, and removing unnecessary or redundant imports. Additionally, the linting rules have been updated to enforce these changes, ensuring that module-level imports are minimized.

Changes

Files/Modules Change Summary
dpdata/__init__.py Removed fakemodule class, adjusted lzma handling, restructured USE_RDKIT logic.
dpdata/amber/md.py Moved netcdf_file import from scipy.io within the read_amber_traj function.
dpdata/ase_calculator.py Added # noqa: TID253 comment to ase.calculators.calculator import for linting.
dpdata/bond_order_system.py Moved Conformer import from rdkit.Chem within the to_fmt_obj method.
dpdata/deepmd/hdf5.py Conditional import of h5py based on TYPE_CHECKING, local import of globfilter.
dpdata/gaussian/gjf.py Reorganized imports, used try-except for openbabel, local import of csr_matrix and connected_components.
dpdata/periodic_table.py Switched from monty.serialization.loadfn to json.load for loading periodic table data.
dpdata/plugins/ase.py Restructured imports related to ASE modules, added necessary imports within specific functions.
dpdata/plugins/deepmd.py Conditional import of h5py based on TYPE_CHECKING, local import within _from_system function.
dpdata/plugins/rdkit.py Moved rdkit.Chem imports to specific methods within classes MolFormat and SdfFormat.
dpdata/pymatgen/molecule.py Moved Molecule import from pymatgen.core within the to_system_data function.
dpdata/pymatgen/structure.py Removed Structure import and associated try-except block.
dpdata/rdkit/sanitize.py Removed openbabel import, added rdkit.Chem and BondType imports within specific functions.
dpdata/rdkit/utils.py Moved Chem import from rdkit to the top of the file.
dpdata/system.py Removed inheritance from MSONable, added from_dict method and related imports.
dpdata/unit.py Added # noqa: TID253 comment to constants import from scipy for linting.
pyproject.toml Introduced new linting rule TID253 for banned module-level imports.
tests/test_custom_data_type.py Added # noqa: TID253 comment to h5py import for linting.
tests/test_to_pymatgen_entry.py Added # noqa: TID253 comment to loadfn import from monty.serialization.

Assessment against linked issues

Objective Addressed Explanation
Reduce import time by optimizing module imports (#526) The changes focus on moving imports to within functions and restructuring conditional imports, which minimizes the initial loading time.

These changes effectively address the primary objective of reducing the import time for the dpdata library, ensuring a more efficient loading process.


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.` 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. - 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.
codspeed-hq[bot] commented 1 month ago

CodSpeed Performance Report

Merging #658 will not alter performance

Comparing njzjz:move-imports (a236787) with devel (02309f7)

Summary

✅ 2 untouched benchmarks

codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 80.30303% with 13 lines in your changes are missing coverage. Please review.

Project coverage is 83.86%. Comparing base (02309f7) to head (a236787).

Files Patch % Lines
dpdata/rdkit/sanitize.py 75.00% 4 Missing :warning:
dpdata/gaussian/gjf.py 66.66% 2 Missing :warning:
dpdata/plugins/ase.py 71.42% 2 Missing :warning:
dpdata/deepmd/hdf5.py 75.00% 1 Missing :warning:
dpdata/plugins/deepmd.py 85.71% 1 Missing :warning:
dpdata/plugins/rdkit.py 80.00% 1 Missing :warning:
dpdata/rdkit/utils.py 50.00% 1 Missing :warning:
dpdata/system.py 87.50% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## devel #658 +/- ## ========================================== + Coverage 83.63% 83.86% +0.22% ========================================== Files 81 81 Lines 7009 6996 -13 ========================================== + Hits 5862 5867 +5 + Misses 1147 1129 -18 ```

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

njzjz commented 1 month ago

For the unknown reason, the benchmark doesn't work at all...