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

Add environ module and refactor unit handling #96

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

Conversation

Routhleck
Copy link
Collaborator

@Routhleck Routhleck commented Jan 16, 2025

Summary by Sourcery

Introduce the environ module to enable switching between SI and non-SI computation modes. Remove commented-out code blocks for enhanced readability and maintainability.

New Features:

  • Add environ module to control computation in SI or non-SI units.

Tests:

  • Update tests to accommodate the new environ module and computation modes.

Copy link
Contributor

sourcery-ai bot commented Jan 16, 2025

Reviewer's Guide by Sourcery

This pull request introduces an environ module to manage computation modes (SI or non-SI) within the brainunit library. It also includes code cleanup by removing commented-out lines and adds logic to adjust the mantissa and unit based on the compute mode.

Sequence diagram for SI mode computation

sequenceDiagram
    participant C as Client Code
    participant E as Environ Module
    participant Q as Quantity

    C->>E: context(compute_mode='si')
    activate E
    E->>Q: Convert quantities to SI units
    Q->>Q: factorless()
    Q->>Q: Adjust mantissa
    Q-->>E: Return SI quantities
    E-->>C: Execute in SI context
    deactivate E
Loading

Class diagram for environ module

classDiagram
    class DefaultContext {
        +Dict settings
        +defaultdict contexts
        +Dict functions
    }

    class EnvironModule {
        +SI_MODE: str
        +NON_SI_MODE: str
        +set(compute_mode: str)
        +get(key: str): Any
        +context(**kwargs): ContextManager
        +get_compute_mode(): str
        +all(): dict
    }

    DefaultContext --* EnvironModule: uses
Loading

State diagram for compute mode transitions

stateDiagram-v2
    [*] --> NON_SI_MODE: Default
    NON_SI_MODE --> SI_MODE: set(compute_mode='si')
    SI_MODE --> NON_SI_MODE: set(compute_mode='non_si')
    NON_SI_MODE --> NON_SI_MODE: context(compute_mode='non_si')
    SI_MODE --> SI_MODE: context(compute_mode='si')
    state SI_MODE {
        [*] --> ConvertUnits
        ConvertUnits --> AdjustMantissa
        AdjustMantissa --> UseFactorless
    }
Loading

File-Level Changes

Change Details Files
Added environ module to control computation in SI or non-SI mode.
  • Added environ to the list of imports and the __all__ list in brainunit/__init__.py.
  • Imported get_compute_mode, SI_MODE, and NON_SI_MODE from the new environ module in brainunit/_base.py.
  • Added logic to adjust the mantissa and unit based on the compute mode in the __init__ method of Quantity class in brainunit/_base.py.
  • Created brainunit/environ.py to manage the compute mode.
  • Created brainunit/environ_test.py to test the compute mode.
brainunit/__init__.py
brainunit/_base.py
brainunit/environ.py
brainunit/environ_test.py
Removed commented-out code.
  • Removed commented-out lines of code across multiple functions in brainunit/_base.py.
  • Removed commented-out lines of code in brainunit/math/_fun_remove_unit.py.
  • Removed commented-out lines of code in brainunit/math/_fun_accept_unitless.py.
  • Removed commented-out lines of code in brainunit/math/_fun_change_unit.py.
  • Removed commented-out lines of code in brainunit/math/_einops.py.
  • Removed commented-out lines of code in brainunit/math/_fun_keep_unit.py.
brainunit/_base.py
brainunit/math/_fun_remove_unit.py
brainunit/math/_fun_accept_unitless.py
brainunit/math/_fun_change_unit.py
brainunit/math/_einops.py
brainunit/math/_fun_keep_unit.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Routhleck Routhleck changed the title [feat] Add convert_in_si Function [feat] Add convert_in_si Function Jan 16, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Routhleck - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The convert_in_si() function modifies global state by changing variables in the caller's scope. Consider adding a warning in the docstring about potential side effects and thread-safety concerns when using this function.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

brainunit/_base.py Outdated Show resolved Hide resolved
@Routhleck Routhleck changed the title [feat] Add convert_in_si Function [feat] Add environ module to control computation in SI or non-SI mode Jan 20, 2025
@Routhleck
Copy link
Collaborator Author

@sourcery-ai review

@Routhleck Routhleck changed the title [feat] Add environ module to control computation in SI or non-SI mode @sourcery-ai Jan 20, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Routhleck - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding more comprehensive tests for the environ module, particularly around error cases and nested context manager usage
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 3 issues found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -1808,7 +1809,7 @@ def __mul__(self, other) -> 'Unit' | Quantity:
dim,
scale=scale,
base=self.base,
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Bug: Using self.factor instead of the calculated factor variable could lead to incorrect unit conversions

The factor parameter should use the local factor variable that was calculated earlier in the method, not self.factor

brainunit/environ_test.py Show resolved Hide resolved
brainunit/environ_test.py Show resolved Hide resolved
brainunit/environ_test.py Show resolved Hide resolved
brainunit/environ.py Show resolved Hide resolved
brainunit/environ.py Outdated Show resolved Hide resolved
brainunit/environ.py Outdated Show resolved Hide resolved
brainunit/environ.py Outdated Show resolved Hide resolved
brainunit/environ.py Outdated Show resolved Hide resolved
@sourcery-ai sourcery-ai bot changed the title @sourcery-ai Add environ module and remove commented-out code Jan 20, 2025
@sourcery-ai sourcery-ai bot changed the title Add environ module and remove commented-out code Add environ module and refactor unit handling Jan 20, 2025
Routhleck and others added 3 commits January 21, 2025 00:10
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
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