Skip to content

Use semantikon annotation for units in a random number of functions #467

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

samwaseda
Copy link
Member

@samwaseda samwaseda commented May 6, 2025

Following @jan-janssen's request, I opened this PR for the type annotations with units using semantikon. I guess the fastest is to take a look at the actual code changes

Summary by CodeRabbit

  • New Features
    • Introduced unit-aware handling for various calculation and helper functions, ensuring that physical quantities such as force tolerance, temperature, pressure, damping times, timestep, and displacement are now explicitly associated with their respective units.
  • Chores
    • Added a new dependency to support unit-aware functionality.

@samwaseda samwaseda requested a review from jan-janssen as a code owner May 6, 2025 18:10
Copy link
Contributor

coderabbitai bot commented May 6, 2025

Walkthrough

This update introduces unit-aware typing to several scientific functions by applying the @units decorator and u type annotations from the semantikon package. These changes affect molecular dynamics, optimization, and phonon helper functions, ensuring parameters are explicitly typed with physical units. The dependency on semantikon is also added to the environment configuration.

Changes

File(s) Change Summary
.ci_support/environment.yml, .ci_support/environment-old.yml Added semantikon==0.0.16 to the list of dependencies.
atomistics/calculators/lammps/libcalculator.py Imported units and u from semantikon. Decorated multiple calculation and optimization functions with @units. Updated relevant float parameters to use u(float, units=...) for unit-aware typing.
atomistics/workflows/phonons/helper.py Imported units and u from semantikon. Decorated generate_structures_helper with @units. Changed displacement parameter to u(float, units="angstrom").

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Function
    participant Semantikon

    User->>Function: Call (with parameters)
    Function->>Semantikon: Validate & convert units via @units decorator
    Semantikon-->>Function: Return unit-checked parameters
    Function-->>User: Execute calculation with unit-safe values
Loading

Poem

🐰
With units in tow, we hop ahead,
No more guessing what physicists said.
Semantikon guides each float and type,
Ensuring our values are always right.
From angstroms to bars, our code’s now precise—
Oh, what a scientific delight!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dfacb26 and 278634e.

📒 Files selected for processing (2)
  • .ci_support/environment-old.yml (1 hunks)
  • atomistics/calculators/lammps/libcalculator.py (8 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .ci_support/environment-old.yml
🚧 Files skipped from review as they are similar to previous changes (1)
  • atomistics/calculators/lammps/libcalculator.py
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: unittest_qe
  • GitHub Check: unittest_gpaw
  • GitHub Check: notebooks
  • GitHub Check: coverage
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

codecov bot commented May 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.34%. Comparing base (a427432) to head (278634e).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #467      +/-   ##
==========================================
+ Coverage   85.27%   85.34%   +0.06%     
==========================================
  Files          43       43              
  Lines        2581     2593      +12     
==========================================
+ Hits         2201     2213      +12     
  Misses        380      380              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

🧹 Nitpick comments (3)
atomistics/workflows/phonons/helper.py (1)

280-280: Update docstring to reflect unit annotation.

The docstring currently describes displacement as "float, optional" but should be updated to mention the unit.

-        displacement (float, optional): The displacement distance. Defaults to 0.01.
+        displacement (float, optional): The displacement distance in angstroms. Defaults to 0.01.
atomistics/calculators/lammps/libcalculator.py (2)

338-338: Remove debug print statement.

This debug print statement should be removed before merging as it appears to be leftover from development.

-    print("Force", timestep)

390-390: Add unit annotation to timestep parameter.

For consistency with other functions, the timestep parameter in this function should also use the unit annotation pattern.

-    timestep: float = 0.001,
+    timestep: u(float, units="picosecond") = 0.001,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3455a10 and dfacb26.

📒 Files selected for processing (3)
  • .ci_support/environment.yml (1 hunks)
  • atomistics/calculators/lammps/libcalculator.py (8 hunks)
  • atomistics/workflows/phonons/helper.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
atomistics/workflows/phonons/helper.py (1)
atomistics/workflows/quasiharmonic.py (1)
  • generate_structures_helper (367-431)
atomistics/calculators/lammps/libcalculator.py (1)
atomistics/shared/output.py (2)
  • OutputMolecularDynamics (42-51)
  • keys (11-18)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: notebooks
  • GitHub Check: coverage
  • GitHub Check: unittest_gpaw
🔇 Additional comments (11)
.ci_support/environment.yml (1)

19-19: Adding semantikon dependency - looks good.

This addition is necessary to support the unit-aware typing and conversion features introduced in the other files.

atomistics/workflows/phonons/helper.py (2)

8-9: Adding required semantikon imports - good.

These imports provide the necessary functionality for unit-aware typing and conversion.


265-269: Appropriate use of the @Units decorator.

The decorator is correctly placed before the function definition, which enables the unit conversion functionality for this function.

atomistics/calculators/lammps/libcalculator.py (8)

9-10: Adding required semantikon imports - good.

These imports provide the necessary functionality for unit-aware typing and conversion.


45-51: Good unit specification for force tolerance.

The ftol parameter is appropriately annotated with eV/angstrom units, which is the correct unit for force in this context.


88-94: Good unit specification for force tolerance.

The ftol parameter is consistently annotated with the same units as in the previous function.


150-159: Good unit specifications for temperature, damping, and timestep parameters.

The parameters are appropriately annotated with relevant units (kelvin for temperature, picosecond for damping time and timestep).


205-217: Good unit specifications for NPT parameters.

All relevant parameters (temperature, pressure, damping) are appropriately annotated with their respective units.


266-276: Good unit specifications for NPH parameters.

Temperature, pressure and damping parameters are appropriately annotated with their respective units.


322-331: Good unit specifications for Langevin dynamics parameters.

Temperature, damping and timestep parameters are correctly annotated with appropriate units.


380-393: Good unit specifications for thermal expansion parameters.

Temperature, pressure and damping parameters are correctly annotated with appropriate units.

def generate_structures_helper(
structure: Atoms,
primitive_matrix: Optional[np.ndarray] = None,
displacement: float = 0.01,
displacement: u(float, units="angstrom") = 0.01,
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Good unit specification for displacement parameter.

The displacement parameter is appropriately annotated with angstrom units, which clearly communicates its physical meaning and enables automatic unit conversion.

There is a similar function in atomistics/workflows/quasiharmonic.py that also has a displacement parameter. Consider adding unit annotations there as well for consistency:


🏁 Script executed:

#!/bin/bash
# Check if there are other similar functions that might benefit from unit annotations
grep -rn "def generate_structures_helper" --include="*.py" .

Length of output: 368


🏁 Script executed:

#!/bin/bash
# Display the signature of generate_structures_helper in each file to check for displacement unit annotations
sed -n '1,40p' atomistics/workflows/elastic/helper.py
sed -n '180,240p' atomistics/workflows/evcurve/helper.py
sed -n '357,407p' atomistics/workflows/quasiharmonic.py

Length of output: 5925


Add unit annotation for displacement in QuasiHarmonic helper

The phonons helper correctly uses

displacement: u(float, units="angstrom") = 0.01

to convey units, but the QuasiHarmonic generate_structures_helper still has a plain float:

• File: atomistics/workflows/quasiharmonic.py
• Around line 367 in the def generate_structures_helper(...) signature

Update the parameter to match the phonons helper:

-    displacement: float = 0.01,
+    displacement: u(float, units="angstrom") = 0.01,

And add the corresponding import for u if it’s not already present:

from atomistics.shared.units import u

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.

1 participant