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

Define ElementType enum in core/periodic_table.py #3726

Merged
merged 4 commits into from
Mar 29, 2024
Merged

Conversation

janosh
Copy link
Member

@janosh janosh commented Mar 29, 2024

@unique
class ElementType(Enum):
    """Enum for element types."""

    noble_gas = "noble_gas"  # He, Ne, Ar, Kr, Xe, Rn
    transition_metal = "transition_metal"  # Sc-Zn, Y-Cd, La-Hg, Ac-Cn
    post_transition_metal = "post_transition_metal"  # Al, Ga, In, Tl, Sn, Pb, Bi, Po
    rare_earth_metal = "rare_earth_metal"  # Ce-Lu, Th-Lr
    metal = "metal"
    metalloid = "metalloid"  # B, Si, Ge, As, Sb, Te, Po
    alkali = "alkali"  # Li, Na, K, Rb, Cs, Fr
    alkaline = "alkaline"  # Be, Mg, Ca, Sr, Ba, Ra
    halogen = "halogen"  # F, Cl, Br, I, At
    chalcogen = "chalcogen"  # O, S, Se, Te, Po
    lanthanoid = "lanthanoid"  # La-Lu
    actinoid = "actinoid"  # Ac-Lr
    quadrupolar = "quadrupolar"
    s_block = "s-block"
    p_block = "p-block"
    d_block = "d-block"
    f_block = "f-block"

defined as above to avoid breaking changes in Composition.contains_element_type. definitely think this could be improved. we can make (breaking) changes later up to 1 or 2 releases down the road when adoption is still low

def contains_element_type(self, category: str) -> bool:
"""Check if Composition contains any elements matching a given category.
Args:
category (str): one of "noble_gas", "transition_metal",
"post_transition_metal", "rare_earth_metal", "metal", "metalloid",
"alkali", "alkaline", "halogen", "chalcogen", "lanthanoid",
"actinoid", "quadrupolar", "s-block", "p-block", "d-block", "f-block"
Returns:
bool: True if any elements in Composition match category, otherwise False
"""
allowed_categories = (
"noble_gas",
"transition_metal",
"post_transition_metal",
"rare_earth_metal",
"metal",
"metalloid",
"alkali",
"alkaline",

Summary by CodeRabbit

  • New Features
    • Introduced ElementType enum to classify elements such as noble gas, transition metal, metalloid, etc.
  • Improvements
    • Optimized the condition for checking user input in scripts.
    • Enhanced the parse_shannon_radii function for better dictionary initialization.
    • Updated the contains_element_type method for improved element type validation.
  • Tests
    • Added and modified tests to cover new scenarios related to element types and composition validity.
    • Improved test clarity by renaming variables and testing additional cases.

Copy link

coderabbitai bot commented Mar 29, 2024

Walkthrough

The recent updates focus on optimizing and extending functionality across several scripts and modules within a development environment. Key enhancements include improved data structure handling, such as the use of tuples and setdefault for efficiency and clarity, and the introduction of the ElementType enum to categorize elements more effectively. These changes streamline operations, enhance readability, and expand the capabilities for handling chemical and periodic table data.

Changes

Files Change Summary
.../get_plane_permutations_optimized.py Modified condition to check user input from a list to a tuple.
.../update_pt_data.py Replaced a conditional check with setdefault for initializing nested dictionaries.
pymatgen/core/composition.py Updated imports and methods for handling ElementType, improving error messages and efficiency.
pymatgen/core/periodic_table.py, tests/core/test_periodic_table.py Added ElementType enum and tests for new element categories.
tests/core/test_composition.py Enhanced test cases for Composition class, including element types and charge removal.

A whimsical ode by a rabbit to code:

In a burrow deep, where the code does sleep,
Changes stir, in the digital deep.
🐇 With a hop and a skip, on the keyboard I tap,
Enums and tuples, in Python's lap.
From elements to lists, our code does leap,
Crafting a world, where the data does creep.
So here's to the changes, both big and small,
In the heart of the code, a rabbit stands tall. 🌟

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-tests 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 tests 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 tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@janosh janosh added enhancement A new feature or improvement to an existing one linting Linting and quality assurance core Pymatgen core labels Mar 29, 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: 1

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3a49dd9 and d97a62e.
Files selected for processing (6)
  • dev_scripts/chemenv/get_plane_permutations_optimized.py (1 hunks)
  • dev_scripts/update_pt_data.py (1 hunks)
  • pymatgen/core/composition.py (2 hunks)
  • pymatgen/core/periodic_table.py (1 hunks)
  • tests/core/test_composition.py (1 hunks)
  • tests/core/test_periodic_table.py (2 hunks)
Additional Context Used
Additional comments not posted (8)
dev_scripts/update_pt_data.py (1)

165-165: The use of setdefault here is a good improvement, making the code more concise and potentially more efficient by avoiding explicit checks. This is a nice example of Pythonic code.

dev_scripts/chemenv/get_plane_permutations_optimized.py (1)

282-282: Changing the condition check from a list to a tuple is a good practice for static value checks, offering a slight performance improvement due to the immutability of tuples.

tests/core/test_periodic_table.py (1)

607-612: The test for the ElementType enum checks its existence and the number of categories, which is a good approach for verifying the enum's integration. However, the enum definition itself is not included in the provided code.

tests/core/test_composition.py (3)

676-678: The test case for Composition("Y3N@C80") correctly asserts that the composition created from the string is equivalent to the composition created from the dictionary. This is a good practice for ensuring that the parser correctly handles special formulas like metallofullerenes.


681-696: The tests for contains_element_type method are comprehensive, covering various element types and blocks. However, it's important to ensure that the ElementType enum and its integration are correctly implemented in the Composition class for these tests to be valid. Additionally, the use of pytest.raises to check for an invalid category is a good practice for error handling tests.


715-731: The test_remove_charges method correctly tests the functionality of removing charges from compositions. It's good to see that both simple and mixed-valence compositions are tested. This ensures that the method works as expected in various scenarios. However, it would be beneficial to add a comment explaining the purpose of calling remove_charges multiple times (lines 720 and 723) to clarify if it's testing idempotency.

Consider adding a comment explaining the repeated calls to remove_charges to clarify the test's intent, such as testing the method's idempotency.

pymatgen/core/composition.py (2)

21-21: The import statement has been modified to include ElementType from pymatgen.core.periodic_table. This change is consistent with the PR's objective to introduce and utilize the ElementType enum for categorizing elements. Ensure that the ElementType enum is properly defined and implemented in the referenced module to avoid import errors.


504-507: The modification in the contains_element_type method to use a list comprehension for extracting values from ElementType for allowed_categories is a good approach for ensuring that the method can dynamically check against the newly introduced categories of elements. However, it's important to ensure that the ElementType enum members have a .value attribute that correctly represents the category strings expected by this method. Additionally, the error message has been updated to guide users towards valid categories, which enhances usability.

One potential improvement is to cache the allowed_categories list if this method is called frequently, to avoid recomputing the list every time the method is invoked.

Consider caching the allowed_categories list if performance becomes a concern due to frequent calls to contains_element_type.

Comment on lines +1503 to +1523
@unique
class ElementType(Enum):
"""Enum for element types."""

noble_gas = "noble_gas" # He, Ne, Ar, Kr, Xe, Rn
transition_metal = "transition_metal" # Sc-Zn, Y-Cd, La-Hg, Ac-Cn
post_transition_metal = "post_transition_metal" # Al, Ga, In, Tl, Sn, Pb, Bi, Po
rare_earth_metal = "rare_earth_metal" # Ce-Lu, Th-Lr
metal = "metal"
metalloid = "metalloid" # B, Si, Ge, As, Sb, Te, Po
alkali = "alkali" # Li, Na, K, Rb, Cs, Fr
alkaline = "alkaline" # Be, Mg, Ca, Sr, Ba, Ra
halogen = "halogen" # F, Cl, Br, I, At
chalcogen = "chalcogen" # O, S, Se, Te, Po
lanthanoid = "lanthanoid" # La-Lu
actinoid = "actinoid" # Ac-Lr
quadrupolar = "quadrupolar"
s_block = "s-block"
p_block = "p-block"
d_block = "d-block"
f_block = "f-block"
Copy link

Choose a reason for hiding this comment

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

The ElementType enum introduces clear categorizations for elements based on their properties and positions in the periodic table. However, the inclusion of a generic 'metal' category could be ambiguous, as many other categories also represent metals. Consider refining this category or providing additional documentation on its intended use to avoid confusion.

Additionally, while the enum member names are descriptive, Python's convention for enum names is to use uppercase letters with underscores separating words (e.g., NOBLE_GAS instead of noble_gas). Adhering to this convention would improve consistency with Python standards.

-    noble_gas = "noble_gas"
+    NOBLE_GAS = "noble_gas"
-    transition_metal = "transition_metal"
+    TRANSITION_METAL = "transition_metal"
-    post_transition_metal = "post_transition_metal"
+    POST_TRANSITION_METAL = "post_transition_metal"
-    rare_earth_metal = "rare_earth_metal"
+    RARE_EARTH_METAL = "rare_earth_metal"
-    metal = "metal"
+    METAL = "metal"
-    metalloid = "metalloid"
+    METALLOID = "metalloid"
-    alkali = "alkali"
+    ALKALI = "alkali"
-    alkaline = "alkaline"
+    ALKALINE = "alkaline"
-    halogen = "halogen"
+    HALOGEN = "halogen"
-    chalcogen = "chalcogen"
+    CHALCOGEN = "chalcogen"
-    lanthanoid = "lanthanoid"
+    LANTHANOID = "lanthanoid"
-    actinoid = "actinoid"
+    ACTINOID = "actinoid"
-    quadrupolar = "quadrupolar"
+    QUADRUPOLAR = "quadrupolar"
-    s_block = "s-block"
+    S_BLOCK = "s-block"
-    p_block = "p-block"
+    P_BLOCK = "p-block"
-    d_block = "d-block"
+    D_BLOCK = "d-block"
-    f_block = "f-block"
+    F_BLOCK = "f-block"

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.

Suggested change
@unique
class ElementType(Enum):
"""Enum for element types."""
noble_gas = "noble_gas" # He, Ne, Ar, Kr, Xe, Rn
transition_metal = "transition_metal" # Sc-Zn, Y-Cd, La-Hg, Ac-Cn
post_transition_metal = "post_transition_metal" # Al, Ga, In, Tl, Sn, Pb, Bi, Po
rare_earth_metal = "rare_earth_metal" # Ce-Lu, Th-Lr
metal = "metal"
metalloid = "metalloid" # B, Si, Ge, As, Sb, Te, Po
alkali = "alkali" # Li, Na, K, Rb, Cs, Fr
alkaline = "alkaline" # Be, Mg, Ca, Sr, Ba, Ra
halogen = "halogen" # F, Cl, Br, I, At
chalcogen = "chalcogen" # O, S, Se, Te, Po
lanthanoid = "lanthanoid" # La-Lu
actinoid = "actinoid" # Ac-Lr
quadrupolar = "quadrupolar"
s_block = "s-block"
p_block = "p-block"
d_block = "d-block"
f_block = "f-block"
@unique
class ElementType(Enum):
"""Enum for element types."""
NOBLE_GAS = "noble_gas" # He, Ne, Ar, Kr, Xe, Rn
TRANSITION_METAL = "transition_metal" # Sc-Zn, Y-Cd, La-Hg, Ac-Cn
POST_TRANSITION_METAL = "post_transition_metal" # Al, Ga, In, Tl, Sn, Pb, Bi, Po
RARE_EARTH_METAL = "rare_earth_metal" # Ce-Lu, Th-Lr
METAL = "metal"
METALLOID = "metalloid" # B, Si, Ge, As, Sb, Te, Po
ALKALI = "alkali" # Li, Na, K, Rb, Cs, Fr
ALKALINE = "alkaline" # Be, Mg, Ca, Sr, Ba, Ra
HALOGEN = "halogen" # F, Cl, Br, I, At
CHALCOGEN = "chalcogen" # O, S, Se, Te, Po
LANTHANOID = "lanthanoid" # La-Lu
ACTINOID = "actinoid" # Ac-Lr
QUADRUPOLAR = "quadrupolar"
S_BLOCK = "s-block"
P_BLOCK = "p-block"
D_BLOCK = "d-block"
F_BLOCK = "f-block"

@janosh janosh merged commit 8e6acdd into master Mar 29, 2024
22 checks passed
@janosh janosh deleted the elem-type-enum branch March 29, 2024 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Pymatgen core enhancement A new feature or improvement to an existing one linting Linting and quality assurance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant