Skip to content

Conversation

@vabhravi
Copy link

@vabhravi vabhravi commented Jan 31, 2026

Description
The test_spi.py tests were failing during collection because PWM_FERQUENCY was attempting to perform arithmetic on the SPIMaster._frequency method itself, rather than its return value.

Changes

  • Removed the module-level calculation of PWM_FERQUENCY.
  • Moved the frequency calculation inside the test function using the master fixture.
  • Added () to correctly call the _frequency method.

Summary by Sourcery

Document and clarify analog calibration and waveform normalization behavior in the analog instrument module.

Enhancements:

  • Add documentation to the internal calibration routine to clarify how scaling between ADC values and voltages is computed.
  • Expose waveform range normalization logic as a dedicated helper for reuse and clarity.

Documentation:

  • Add a docstring explaining the side effects and purpose of the analog calibration method.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 31, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors analog signal calibration internals by documenting and slightly restructuring the _calibrate helper and the waveform range normalization helper for analog outputs, without changing their core numerical behavior.

Class diagram for updated analog calibration and waveform normalization

classDiagram
    class AnalogChannel {
        - str _name
        - float _gain
        - int _resolution
        - np.poly1d _scale
        - np.poly1d _unscale
        - tuple RANGE
        - np.ndarray _waveform_table
        + resolution(value int) void
        - _calibrate() void
        + lowres_waveform_table() np.ndarray
        - _range_normalize(x np.ndarray, norm int) np.ndarray
    }

    class INPUT_RANGES {
    }

    AnalogChannel --> INPUT_RANGES : uses

    class np_poly1d {
    }

    AnalogChannel --> np_poly1d : sets_scale_and_unscale

    class numpy_ndarray {
    }

    AnalogChannel --> numpy_ndarray : uses_waveform_table
Loading

File-Level Changes

Change Details Files
Document and clarify the internal ADC calibration helper while preserving its behavior.
  • Replaced the bare _calibrate implementation with a version that includes a descriptive docstring explaining its role and side effects.
  • Retained the existing gain- and range-based computation of slope, intercept, and the poly1d scale/unscale callables for converting between raw ADC codes and voltages.
pslab/instrument/analog.py
Tidy up the waveform range normalization helper for analog outputs.
  • Kept the lowres_waveform_table logic that sub-samples the waveform table and normalizes it to a 6-bit (0–63) duty-cycle range.
  • Adjusted the definition/indentation of the _range_normalize helper while preserving its normalization formula and int16 rounding behavior.
pslab/instrument/analog.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 found 1 issue, and left some high level feedback:

  • The _calibrate method has lost one level of indentation and is no longer aligned with the other methods on the class; as written it will be defined at the wrong scope and won’t be available as an instance method.
  • _range_normalize has also been dedented out of the class while lowres_waveform_table still calls self._range_normalize, which will raise an error at runtime—either re-indent it as a method or adjust the call site accordingly.
  • The implementation changes in analog.py don’t match the pull request description about fixing a SPI test frequency calculation; double-check that you’ve committed the intended file changes for this PR.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `_calibrate` method has lost one level of indentation and is no longer aligned with the other methods on the class; as written it will be defined at the wrong scope and won’t be available as an instance method.
- `_range_normalize` has also been dedented out of the class while `lowres_waveform_table` still calls `self._range_normalize`, which will raise an error at runtime—either re-indent it as a method or adjust the call site accordingly.
- The implementation changes in `analog.py` don’t match the pull request description about fixing a SPI test frequency calculation; double-check that you’ve committed the intended file changes for this PR.

## Individual Comments

### Comment 1
<location> `pslab/instrument/analog.py:146` </location>
<code_context>
         self._resolution = 2**value - 1
         self._calibrate()

-    def _calibrate(self):
-        A = INPUT_RANGES[self._name][0] / self._gain
-        B = INPUT_RANGES[self._name][1] / self._gain
</code_context>

<issue_to_address>
**issue (bug_risk):** Fix the indentation of `_calibrate` to match the surrounding methods.

`_calibrate` is indented with three spaces instead of the four used for other methods, which can cause misalignment or an indentation error. Please align `def _calibrate(...)` with the other methods (four leading spaces) and indent the docstring consistently under it.
</issue_to_address>

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.

@vabhravi vabhravi changed the title Fix: Resolve TypeError in test_spi frequency calculation Fix: Correct indentation in analog.py and Resolve SPI test TypeError. Jan 31, 2026
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