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

Improve function pert_scaled #1563

Merged
merged 28 commits into from
Aug 27, 2024
Merged

Improve function pert_scaled #1563

merged 28 commits into from
Aug 27, 2024

Conversation

thangckt
Copy link

@thangckt thangckt commented May 30, 2024

My suggestion:

  • Separate VASP and ABACUS into separated functions (and may be separated files). This will helpful to someone who want to implement a new DFT engine and also convenient for maintain.

Summary by CodeRabbit

  • New Features

    • Enhanced the pert_scaled function to support different initialization styles ("VASP" or "ABACUS").
  • Refactor

    • Improved loop logic and file handling for better performance and reliability in perturbation processes.
    • Streamlined the make_vasp_relax and make_scale functions for improved readability and clarity.
    • Simplified the construction of perturbation commands in the pert_scaled function.

Copy link

coderabbitai bot commented May 30, 2024

Walkthrough

Walkthrough

Recent changes to dpgen involve modifications to the pert_scaled function in both gen.py and surf.py. Enhancements include improved handling of different initialization styles, refined construction of perturbation commands, and clearer loop logic for processing systems and scales. Additionally, updates to file management and import statements contribute to more efficient data handling during perturbation operations.

Changes

Files Change Summaries
dpgen/data/gen.py, dpgen/data/surf.py Updated pert_scaled function: enhanced handling for "VASP" and "ABACUS" styles, improved perturbation command construction, and clarified loop structures. Refactored import statements and streamlined file existence assertions in make_vasp_relax and make_scale functions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant pert_scaled
    participant Systems
    participant Scales
    participant Command

    User ->> pert_scaled: Call pert_scaled()
    pert_scaled ->> Systems: Iterate over systems
    loop for each system
        Systems ->> pert_scaled: Process system
        pert_scaled ->> Scales: Iterate over scales
        loop for each scale
            Scales ->> pert_scaled: Process scale
            alt Initialization Style: VASP
                pert_scaled ->> Command: Construct VASP pert_cmd
            else Initialization Style: ABACUS
                pert_scaled ->> Command: Construct ABACUS pert_cmd
            end
            pert_scaled ->> Command: Handle perturbation
        end
    end
    pert_scaled -->> User: Return results
Loading

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5ad52f3 and cc0efbd.

Files selected for processing (2)
  • dpgen/data/gen.py (5 hunks)
  • dpgen/data/surf.py (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • dpgen/data/gen.py
  • dpgen/data/surf.py

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.
    • @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 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

@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.

Actionable comments posted: 1

dpgen/data/tools/create_random_disturb.py Outdated Show resolved Hide resolved
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.

Actionable comments posted: 1

tests/data/test_gen_bulk.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented May 30, 2024

Codecov Report

Attention: Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.

Project coverage is 49.55%. Comparing base (08c8f9e) to head (cc0efbd).
Report is 2 commits behind head on devel.

Files Patch % Lines
dpgen/data/surf.py 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #1563      +/-   ##
==========================================
- Coverage   49.56%   49.55%   -0.01%     
==========================================
  Files          83       83              
  Lines       14860    14850      -10     
==========================================
- Hits         7365     7359       -6     
+ Misses       7495     7491       -4     

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

@thangckt thangckt changed the title Simplify function pert_scaled Improve function pert_scaled May 30, 2024
@thangckt thangckt requested a review from Liu-RX June 1, 2024 09:53
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.

Actionable comments posted: 2

Outside diff range and nitpick comments (1)
dpgen/data/gen.py (1)

Line range hint 729-842: The implementation of the pert_scaled function has been significantly refactored to accommodate different initialization styles and to handle perturbations more flexibly. However, there are a couple of points that need attention:

  1. Error Handling: The use of assert statements (e.g., line 746) for checking if directories exist is generally not recommended for production code as it can cause the program to crash without a graceful recovery option. Consider using if-statements with proper exceptions or error messages.

  2. Command Injection Risk: The construction of the pert_cmd command string (lines 764-769) directly incorporates user input (pert_box, fp_style, poscar_name, pert_numb, pert_atom) which could potentially lead to command injection if not properly sanitized. Ensure that these inputs are validated or sanitized before being used in command line contexts.

  3. File Handling: The function modifies the current working directory frequently (e.g., lines 746, 775, 779). This can lead to hard-to-track bugs and is generally not a good practice in a multi-threaded environment where the current directory is shared among all threads. Consider using absolute paths and avoid changing the global state.

  4. Resource Management: The function uses os.remove(pos_in) inside a loop (line 811) which might lead to errors if the file does not exist or has already been removed in a previous iteration. Adding checks before attempting to delete can prevent runtime errors.

Overall, the function's logic appears sound, but these improvements could enhance robustness and security.

dpgen/data/surf.py Show resolved Hide resolved
dpgen/data/surf.py Show resolved Hide resolved
@njzjz njzjz closed this Jul 2, 2024
@njzjz njzjz reopened this Jul 2, 2024
Copy link
Member

@njzjz njzjz left a comment

Choose a reason for hiding this comment

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

I am not familiar with this piece of code. I can merge if @Liu-RX approves the PR.

@njzjz
Copy link
Member

njzjz commented Aug 1, 2024

@Liu-RX could you take a review? Thanks!

dpgen/data/surf.py Show resolved Hide resolved
dpgen/data/surf.py Show resolved Hide resolved
@Liu-RX
Copy link
Collaborator

Liu-RX commented Aug 2, 2024

@Liu-RX could you take a review? Thanks!

Sorry for the late reply!!!

@thangckt thangckt closed this Aug 23, 2024
@thangckt thangckt deleted the simplify branch August 23, 2024 17:03
@thangckt thangckt restored the simplify branch August 23, 2024 17:14
@thangckt thangckt reopened this Aug 23, 2024
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.

Actionable comments posted: 2

Outside diff range, codebase verification and nitpick comments (2)
dpgen/data/gen.py (2)

Line range hint 782-813: Avoid repeated code for file handling.

The code for handling files based on fp_style is repeated. Consider refactoring to reduce duplication.


Line range hint 814-844: Clarify the purpose of the special case handling.

The handling of the special case (unperturbed) is not clearly documented. Consider adding comments to explain its purpose.

dpgen/data/gen.py Show resolved Hide resolved
dpgen/data/gen.py Outdated Show resolved Hide resolved
@thangckt thangckt requested review from njzjz and Liu-RX August 23, 2024 19:12
@njzjz njzjz merged commit e285ab3 into deepmodeling:devel Aug 27, 2024
7 checks passed
thangckt added a commit to thangckt/dpgen that referenced this pull request Aug 28, 2024
@thangckt thangckt deleted the simplify branch August 28, 2024 00:37
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.

3 participants