Skip to content

refactor: Remove magic numbers in ScienceLab.temperature#268

Open
Architrb1795 wants to merge 2 commits intofossasia:developfrom
Architrb1795:refactor-sciencelab-temperature-calibration
Open

refactor: Remove magic numbers in ScienceLab.temperature#268
Architrb1795 wants to merge 2 commits intofossasia:developfrom
Architrb1795:refactor-sciencelab-temperature-calibration

Conversation

@Architrb1795
Copy link

@Architrb1795 Architrb1795 commented Feb 7, 2026

Fixes #272

Summary

This PR addresses the # TODO: Get rid of magic numbers in
ScienceLab.temperature by extracting calibration constants into a
named class-level dictionary.

Changes

  • Extracted CTMU temperature calibration values into
    _CTMU_TEMPERATURE_CALIBRATION
  • Refactored temperature property to use a dictionary lookup
  • Removed hardcoded numeric literals
  • Preserved existing calculation logic exactly

Verification

  • tox -e lint

Summary by Sourcery

Refactor CTMU-based MCU temperature measurement to use named calibration constants instead of hardcoded literals, adding validation for unsupported current sources.

Enhancements:

  • Extract CTMU temperature calibration parameters into a class-level mapping for clearer, centralized configuration.
  • Refactor the temperature property to compute values via calibration lookup while preserving existing calculation behavior.
  • Add explicit error handling for unsupported CTMU current source values in temperature computation.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 7, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the ScienceLab.temperature property to replace hardcoded CTMU calibration magic numbers with a centralized class-level calibration table and adds explicit error handling for unsupported current-source values, while preserving the existing temperature calculation behavior.

Class diagram for updated ScienceLab temperature calibration

classDiagram
class ScienceLab {
    +_CTMU_TEMPERATURE_CALIBRATION: dict[int, tuple[float, float]]
    +temperature() float
    +_get_ctmu_voltage(channel: int, current_range: int, tgen: bool) float
}
Loading

File-Level Changes

Change Details Files
Centralize CTMU temperature calibration constants and update temperature calculation to use them with explicit error handling.
  • Introduce a class-level _CTMU_TEMPERATURE_CALIBRATION dictionary mapping CTMU current-source IDs to (offset, slope) calibration tuples.
  • Refactor the temperature property to look up offset and slope from the calibration dictionary instead of using inline magic numbers in conditional branches.
  • Add a ValueError raised when an unsupported CTMU current source is used, preserving the original calculation formula for supported sources.
pslab/sciencelab.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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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

Copy link

@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 - I've left some high level feedback:

  • Consider adding a type annotation to _CTMU_TEMPERATURE_CALIBRATION (e.g., dict[int, tuple[float, float]]) to make the expected key/value types explicit and help with static analysis.
  • Since cs is effectively an enum of supported current sources, you might improve readability by introducing named constants or an Enum for the allowed current source values instead of using bare integers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding a type annotation to `_CTMU_TEMPERATURE_CALIBRATION` (e.g., `dict[int, tuple[float, float]]`) to make the expected key/value types explicit and help with static analysis.
- Since `cs` is effectively an enum of supported current sources, you might improve readability by introducing named constants or an `Enum` for the allowed current source values instead of using bare integers.

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.

@Architrb1795
Copy link
Author

Hi maintainers 👋
I noticed the # TODO: Get rid of magic numbers while exploring the codebase
and implemented this refactor proactively.

Happy to adjust the approach if you’d prefer a different structure.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors ScienceLab.temperature to centralize CTMU temperature calibration constants, aiming to improve readability and reduce inline “magic numbers” in the temperature calculation.

Changes:

  • Added a class-level _CTMU_TEMPERATURE_CALIBRATION mapping for CTMU calibration parameters.
  • Updated temperature to compute via calibration lookup and raise a clear error for unsupported values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 58 to 66
cs = 3
V = self._get_ctmu_voltage(0b11110, cs, 0)

if cs == 1:
return (646 - V * 1000) / 1.92 # current source = 1
elif cs == 2:
return (701.5 - V * 1000) / 1.74 # current source = 2
elif cs == 3:
return (760 - V * 1000) / 1.56 # current source = 3
try:
offset, slope = self._CTMU_TEMPERATURE_CALIBRATION[cs]
except KeyError as exc:
msg = f"Unsupported CTMU current source: {cs}"
raise ValueError(msg) from exc
return (offset - V * 1000) / slope
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

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

PR description says hardcoded numeric literals were removed from ScienceLab.temperature, but the method still contains several unexplained literals (cs = 3, the CTMU channel 0b11110, and the * 1000 unit conversion). Consider extracting these into named constants (or adding a brief comment explaining the units/meaning) so the "remove magic numbers" goal is fully met.

Copilot uses AI. Check for mistakes.
Comment on lines 47 to 52
# Calibration parameters for CTMU temperature measurement.
# Format: {current_source: (offset, slope)}
_CTMU_TEMPERATURE_CALIBRATION = {
1: (646, 1.92),
2: (701.5, 1.74),
3: (760, 1.56),
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

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

The calibration mapping comment uses the term current_source, but the value used as the key (cs) is passed as current_range to _get_ctmu_voltage (documented as {0,1,2,3} ranges). This mismatch is confusing—please rename the comment/format description (or the local variable) to consistently reflect what the keys represent (e.g., current_range).

Copilot uses AI. Check for mistakes.
Copy link
Member

@mariobehling mariobehling left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Centralizing the CTMU calibration constants is a good cleanup and the lookup plus explicit error for unsupported values improves maintainability.

Before we can merge, please do the following:

  1. Open an issue first and link it from the PR description using GitHub best practice, for example Fixes #<issue-number>. This helps track why we are changing calibration logic and what behavior we expect.

  2. Please align naming and remove remaining ambiguity. The calibration mapping comment refers to “current source”, but the key is passed as current_range to _get_ctmu_voltage. Please rename the comment and or the local variable so it is consistent and clear.

  3. The “remove magic numbers” goal is only partial. cs = 3, 0b11110, and * 1000 are still unexplained. Either extract them into named constants or add brief comments describing what they represent and the units.

  4. Add proof of behavior. Please attach a short screencast showing this running on your machine, including the exact command. Minimum: tox or pytest run passing. Ideally also show a tiny snippet printing ScienceLab().temperature with your setup, so we can confirm the code path is exercised.

Once these are in place, this becomes a clean refactor we can merge confidently.

@mariobehling
Copy link
Member

Small process note.

We have automatic Copilot PR reviews enabled on this repository. These reviews are only triggered if the contributor has GitHub Copilot enabled and an active license on their own account.

Please enable Copilot in your GitHub settings if you have access. In many regions, free licenses are available through educational institutions or developer programs. Enabling Copilot helps us speed up the auto review process and reduces manual review overhead for the core team.

The team will review your PR. Thank you for your contribution and cooperation.

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