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

VEC function integration into smact.properties with a test file to serve as an example for its use. #305

Merged
merged 10 commits into from
Aug 22, 2024

Conversation

ryannduma
Copy link
Collaborator

@ryannduma ryannduma commented Aug 22, 2024

VEC function Pull Request

Description

Hey, I hope this PR finds you well, I have now completed the integration of the VEC function succintly into the smact.properties.py module and attached a relevant example of it's use. I would also like to indicate that the function takes two arguments - the compound in question and a valence file - allowing for the user to select any file say magpie, or any existing valence file that they would like to use, in this case the example uses the modified file you shared.

I would like to highlight that this is a New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Test Configuration: VECexample.py

  • Python version: 3.12.0
  • Operating System: MacOS

Reviewers

@AntObi - Admin and Maintainer

Checklist:

  • [y ] My code follows the style guidelines of this project
  • [ y] I have performed a self-review of my own code
  • [ y] I have commented my code, particularly in hard-to-understand areas
  • [n ] I have made corresponding changes to the documentation
  • [y ] My changes generate no new warnings
  • [ y] I have added tests that prove my fix is effective or that my feature works
  • [ n] New and existing unit tests pass locally with my changes
  • [ y] Any dependent changes have been merged and published in downstream modules
  • [ y] I have checked my code and corrected any misspellings

Summary by CodeRabbit

  • New Features

    • Introduced scripts for calculating the Valence Electron Count (VEC) for specified compounds.
    • Added a function to compute VEC from a compound formula with enhanced parsing capabilities.
  • Bug Fixes

    • Implemented error handling for invalid chemical elements in the compound formula.
  • Tests

    • Enhanced test coverage to validate the accuracy of the VEC calculations for various compounds.

@ryannduma ryannduma requested a review from AntObi August 22, 2024 10:39
Copy link
Contributor

coderabbitai bot commented Aug 22, 2024

Warning

Rate limit exceeded

@ryannduma has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 18 minutes and 36 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between f8b7a8a and 55ba0db.

Walkthrough

The changes introduce two new scripts, VECexample.py and examples/vec_example.py, for calculating the Valence Electron Count (VEC) of different chemical compounds. This functionality is facilitated by the new valence_electron_count function in the smact/properties.py module, which includes parsing capabilities and error handling. Test coverage is enhanced with a new test function in the smact/tests/test_core.py file, ensuring proper validation of the new functionality.

Changes

Files Change Summary
VECexample.py, examples/vec_example.py Introduced scripts for VEC calculation for "Ba5In4Bi5" and "Fe2O3" using the valence_electron_count function from smact.properties.
smact/properties.py Added valence_electron_count function for computing VEC with parsing and error handling.
smact/tests/test_core.py Added test_valence_electron_count to validate the functionality of the valence_electron_count function, including expected outputs and error handling.
.pre-commit-config.yaml Updated pre-commit hooks, modifying nbQA version and Python compatibility argument.
examples/Crystal_Space/1_reduction.ipynb Changed import of Iterable from typing to collections.abc for better compatibility.

Poem

In a garden of formulas, oh what a sight,
A rabbit hops joyfully, under moonlight.
Valence counts dancing, a new script in play,
For compounds and elements, come join the fray!
With data from CSV, the VEC's now bright,
A leap for the science, oh what pure delight! 🐇✨


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
Contributor

@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: 3

Outside diff range, codebase verification and nitpick comments (1)
VECexample.py (1)

1-11: Enhance error handling and validation.

The example script lacks error handling for file operations and validation for the input compound. Consider adding try-except blocks to handle potential exceptions from the valence_electron_count function, such as FileNotFoundError and ValueError.

try:
    vec = valence_electron_count(compound, valence_file)
    print(f"The Valence Electron Count (VEC) for {compound} is: {vec:.2f}")
except FileNotFoundError:
    print(f"Error: The file {valence_file} was not found.")
except ValueError as e:
    print(f"Error: {e}")
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 92e9bc9 and c84e475.

Files ignored due to path filters (1)
  • elements_valence_modified.csv is excluded by !**/*.csv
Files selected for processing (2)
  • VECexample.py (1 hunks)
  • smact/properties.py (2 hunks)
Additional context used
Ruff
smact/properties.py

204-204: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

smact/properties.py Outdated Show resolved Hide resolved
smact/properties.py Outdated Show resolved Hide resolved
smact/properties.py Outdated Show resolved Hide resolved
@AntObi
Copy link
Collaborator

AntObi commented Aug 22, 2024

Thank you for submitting this PR @ryannduma . A few things before I can accept this PR.

  • Install pre-commit (pip install pre-commit) and run pre-commit run --all-files: This automatically lints and formats any updated code to make it compliant with some coding conventions/styles. This will make the PR pass the QA stage of the CI
  • Add a unit test for this function in smact/tests/test_core.py
  • While allowing the user to supply their own valence files is option to have, the function should work with data that's already built into SMACT, so default behaviour should be to access the valence data through Element('Fe').num_valence_modified, which is available on this develop branch.
  • Move the example into the example folders for now

Copy link
Contributor

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c84e475 and c085b49.

Files selected for processing (3)
  • VECexample.py (1 hunks)
  • smact/properties.py (2 hunks)
  • smact/tests/test_core.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • VECexample.py
Additional context used
Ruff
smact/properties.py

1-1: csv imported but unused

Remove unused import: csv

(F401)


197-197: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

Additional comments not posted (3)
smact/properties.py (2)

184-191: Consider refactoring parse_formula for reusability.

The parse_formula function is useful for extracting elements and stoichiometries from chemical formulas. Consider refactoring it into a standalone utility function for broader use within the module.


193-197: Consider refactoring get_element_valence for reusability.

The get_element_valence function is useful for retrieving valence data from CSV rows. Consider refactoring it into a standalone utility function for broader use within the module.

Tools
Ruff

197-197: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

smact/tests/test_core.py (1)

94-98: Test function test_valence_electron_count is well-structured.

The test function effectively validates the valence_electron_count function, covering both correct calculations and error handling for invalid inputs.

@@ -1,3 +1,6 @@
import csv
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove unused import csv.

The csv module is imported but not used in the file. Removing it will clean up the code.

-import csv
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import csv
Tools
Ruff

1-1: csv imported but unused

Remove unused import: csv

(F401)

smact/properties.py Outdated Show resolved Hide resolved
@ryannduma
Copy link
Collaborator Author

Hey Anthony,

I have resolved all conflicts and the code now passes the qa stage of the CI except its failing all the other tests of the CI, how can I resolve that?

Copy link
Contributor

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c085b49 and 80ccabf.

Files selected for processing (3)
  • examples/vec_example.py (1 hunks)
  • smact/properties.py (2 hunks)
  • smact/tests/test_core.py (2 hunks)
Files skipped from review due to trivial changes (1)
  • examples/vec_example.py
Files skipped from review as they are similar to previous changes (1)
  • smact/tests/test_core.py
Additional context used
Ruff
smact/properties.py

1-1: csv imported but unused

Remove unused import: csv

(F401)

Additional comments not posted (1)
smact/properties.py (1)

1-1: Remove unused import csv.

The csv module is imported but not used in the file. Removing it will clean up the code.

Apply this diff to remove the unused import:

-import csv

Likely invalid or redundant comment.

Tools
Ruff

1-1: csv imported but unused

Remove unused import: csv

(F401)

smact/properties.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@AntObi AntObi left a comment

Choose a reason for hiding this comment

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

See comments

smact/properties.py Outdated Show resolved Hide resolved
smact/tests/test_core.py Show resolved Hide resolved
elements_valence_modified.csv Outdated Show resolved Hide resolved
Copy link
Contributor

@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

Outside diff range, codebase verification and nitpick comments (1)
.pre-commit-config.yaml (1)

7-7: Ensure consistent formatting for isort arguments.

The formatting of the args for the isort hook has been adjusted. Ensure that this change aligns with the project's style guidelines.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 80ccabf and 8377670.

Files selected for processing (4)
  • .pre-commit-config.yaml (2 hunks)
  • examples/Crystal_Space/1_reduction.ipynb (1 hunks)
  • smact/properties.py (2 hunks)
  • smact/tests/test_core.py (2 hunks)
Files skipped from review due to trivial changes (1)
  • examples/Crystal_Space/1_reduction.ipynb
Files skipped from review as they are similar to previous changes (1)
  • smact/tests/test_core.py
Additional context used
Ruff
smact/properties.py

1-1: csv imported but unused

Remove unused import: csv

(F401)

Additional comments not posted (4)
.pre-commit-config.yaml (1)

19-23: Update nbQA version and compatibility.

The nbQA version has been updated to 1.8.7, and the Python compatibility for nbqa-pyupgrade has been changed to --py39-plus. Ensure that these changes are compatible with the project's requirements and do not introduce any issues.

smact/properties.py (3)

1-1: Remove unused import csv.

The csv module is imported but not used in the file. Removing it will clean up the code.

Tools
Ruff

1-1: csv imported but unused

Remove unused import: csv

(F401)


165-219: Consider refactoring helper functions for reusability.

The valence_electron_count function is well-implemented with appropriate error handling. However, consider refactoring the parse_formula and get_element_valence helper functions into standalone utility functions for broader use within the module.


193-199: Improve exception handling in get_element_valence.

Use raise ... from None to distinguish it from errors in exception handling.

smact/properties.py Outdated Show resolved Hide resolved
Copy link
Contributor

@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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8377670 and f8b7a8a.

Files selected for processing (1)
  • smact/tests/test_core.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • smact/tests/test_core.py

Copy link

codecov bot commented Aug 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.67%. Comparing base (f7a72b7) to head (55ba0db).
Report is 24 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #305      +/-   ##
===========================================
+ Coverage    75.47%   75.67%   +0.19%     
===========================================
  Files           24       24              
  Lines         2361     2183     -178     
===========================================
- Hits          1782     1652     -130     
+ Misses         579      531      -48     

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

@AntObi AntObi merged commit b6f4a7b into WMD-group:develop Aug 22, 2024
16 checks passed
@AntObi
Copy link
Collaborator

AntObi commented Aug 22, 2024

Merged!

@ryannduma
Copy link
Collaborator Author

Hey @AntObi , the code passed the tests after a bunch of minor tweaks here and there to the types of errors raised, I noticed the Lr0 element check passed as a ValueError when passed to the function instead of a TypeError as presumed so I edited that part of the test to match, and it passed, though I dont know if thats exactly right, same to the Xx which isnt a valid element but I guess when parsed over is accepted because of the following codeblock, which ofc down the line, is found to not have a Valence number and then returns a ValueError instead of a NameError - please let me know what your thoughts are on this and whether additional tweaks need to be made.

Thank you and have a good evening

def parse_formula(formula): pattern = re.compile(r'([A-Z][a-z]*)(\d*)') elements = pattern.findall(formula) element_stoich = defaultdict(int) for (element, count) in elements: count = int(count) if count else 1 element_stoich[element] += count return element_stoich

@ryannduma ryannduma deleted the VEC branch August 27, 2024 13:21
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.

2 participants