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

pref: lazy import modules #658

Merged
merged 4 commits into from
May 16, 2024
Merged

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented May 16, 2024

Fix #526.

Summary by CodeRabbit

  • Refactor

    • Removed unnecessary import statements and restructured import handling for improved code organization and readability.
    • Reorganized imports within functions to localize dependencies and enhance code modularity.
  • New Features

    • Introduced conditional imports based on TYPE_CHECKING for better resource management and efficiency.
    • Added a new method from_dict to the System class for constructing instances from a data dictionary.
  • Chores

    • Updated linting rules in pyproject.toml to include TID253 for banned module-level imports.
    • Modified import statements in test files to comply with the new linting rules for better code quality.
  • Style

    • Added # noqa: TID253 comments to specific import statements to adhere to new linting rules and ensure clean code styling.

Fix deepmodeling#526.

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Copy link

coderabbitai bot commented May 16, 2024

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
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 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codspeed-hq bot commented May 16, 2024

CodSpeed Performance Report

Merging #658 will not alter performance

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

Summary

✅ 2 untouched benchmarks

njzjz added 2 commits May 15, 2024 21:03
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Copy link

codecov bot commented May 16, 2024

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 ⚠️
dpdata/gaussian/gjf.py 66.66% 2 Missing ⚠️
dpdata/plugins/ase.py 71.42% 2 Missing ⚠️
dpdata/deepmd/hdf5.py 75.00% 1 Missing ⚠️
dpdata/plugins/deepmd.py 85.71% 1 Missing ⚠️
dpdata/plugins/rdkit.py 80.00% 1 Missing ⚠️
dpdata/rdkit/utils.py 50.00% 1 Missing ⚠️
dpdata/system.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            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     

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

@njzjz
Copy link
Member Author

njzjz commented May 16, 2024

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

@njzjz njzjz marked this pull request as ready for review May 16, 2024 01:16
@njzjz njzjz linked an issue May 16, 2024 that may be closed by this pull request
@njzjz njzjz requested a review from wanghan-iapcm May 16, 2024 01:59
@wanghan-iapcm wanghan-iapcm merged commit a7bf93d into deepmodeling:devel May 16, 2024
10 of 11 checks passed
@njzjz njzjz mentioned this pull request May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

slow to import dpdata
2 participants