klippain-shaketune icon indicating copy to clipboard operation
klippain-shaketune copied to clipboard

Bye, Bye, Bye (SciPy)

Open mjonuschat opened this issue 6 months ago • 2 comments

Replace SciPy functionality with NumPy equivalents to escape dependency hell.

libatlas is a dependency for installing precompiled SciPy packages for python, but the latest Raspbian/Debian 13 no longer have that package. Instead of requiring the heavy SciPy dependency replace the functionality with direct NumPy calls / a little bit of wrapper code.

  • refactor: Replace SciPy pearsonr correlation coefficent with NumPy functionality
  • refactor: Use numpy for linear regression calculations
  • refactor: Replace scipy.spectrogram with numpy fft
  • refactor: Bump numpy version, remove scipy dependency
  • refactor: Stop installing scipy sytem dependencies
  • docs: Remove scipy from list of dependencies

Summary by Sourcery

Remove SciPy dependency by reimplementing correlation, regression, and spectrogram functionality with pure NumPy, update dependency versions and installation scripts, and clean up documentation accordingly

Enhancements:

  • Replace Pearson correlation using SciPy with NumPy's corrcoef
  • Replace linear regression using SciPy stats with NumPy polynomial fitting
  • Implement FFT-based spectrogram using NumPy in a new helper module instead of SciPy signal
  • Remove installation of SciPy and related system libraries (libatlas, libopenblas)

Build:

  • Bump NumPy version constraints and drop SciPy from requirements.txt

Documentation:

  • Remove SciPy from the list of dependencies in documentation

mjonuschat avatar Oct 19 '25 06:10 mjonuschat

Reviewer's Guide

This PR removes the SciPy dependency by migrating spectral, regression, and correlation routines to NumPy-based implementations and streamlines dependency management by updating requirements, install scripts, and documentation.

Class diagram for updated spectrogram computation (NumPy-based)

classDiagram
    class SpectrogramHelper {
        +compute_spectrogram(data)
        +spectrogram(x, fs, window, nperseg, noverlap)
        +_fft_helper(x, window, nperseg, noverlap, nfft, sides)
        +_detrend_constant(data)
    }
    SpectrogramHelper <|-- compute_spectrogram
    SpectrogramHelper <|-- spectrogram
    SpectrogramHelper <|-- _fft_helper
    SpectrogramHelper <|-- _detrend_constant

Class diagram for updated linear regression direction calculation

classDiagram
    class AxesMapComputation {
        +_linear_regression_direction(position_x, position_y, position_z)
    }
    class Polynomial {
        +fit(time, position, 1)
        +convert()
    }
    AxesMapComputation --> Polynomial: uses

Class diagram for updated correlation calculation in BeltsComputation

classDiagram
    class BeltsComputation {
        +compute()
    }
    BeltsComputation --> numpy: uses corrcoef

File-Level Changes

Change Details Files
Migrate spectrogram generation from SciPy to a custom NumPy FFT–based implementation
  • Remove SciPy import and compute_spectrogram in common_func
  • Introduce new helper module with _fft_helper, spectrogram, and compute_spectrogram functions using NumPy
shaketune/helpers/common_func.py
shaketune/helpers/spectrogram.py
Replace SciPy linear regression with NumPy Polynomial.fit
  • Swap stats.linregress calls for Polynomial.fit.convert
  • Update intercept and slope assignments from the fit result
shaketune/graph_creators/computations/axes_map_computation.py
Swap SciPy’s pearsonr with NumPy’s corrcoef for correlation computation
  • Replace pearsonr call with np.corrcoef
  • Adjust similarity_factor calculation to use the corrcoef output
shaketune/graph_creators/computations/belts_computation.py
Remove SciPy from dependencies and clean up installation steps
  • Delete SciPy entries and add conditional NumPy version constraints in requirements.txt
  • Remove system package installation logic in install.sh and update docs to exclude SciPy
requirements.txt
install.sh
docs/cli_usage.md

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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Oct 19 '25 06:10 sourcery-ai[bot]

@Frix-x when will this be accepted?

EricZimmerman avatar Nov 21 '25 17:11 EricZimmerman

Happy to test this on my machine if we think it's ready

schutztj avatar Nov 25 '25 16:11 schutztj

Thank you so much @mjonuschat for this contribution! 🙏

As we discussed on Discord, I've created a hybrid solution merging your more generic approach with some low-RAM optimizations targeting Raspberry Pi (using a pre-allocated buffer approach instead of stride tricks for the spectrogram).

Your changes are now merged into the develop branch. Thanks again for the collaboration!

Frix-x avatar Dec 07 '25 14:12 Frix-x