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

Further refactor spline filters #20519

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

Conversation

ev-br
Copy link
Member

@ev-br ev-br commented Apr 18, 2024

Reference issue

Based off #18926, will need a rebase when that one lands.
Supersedes and closes #14558
closes #13643

I initially meant to fix #9209, but turns out it is better addressed separately.
#12691 is related, but no change to it in this PR.

What does this implement/fix?

  • refactor _splinemodule from tempita-templated C into C++;
  • use standard C++ complex arithmetics instead of GNU extensions, remove #ifdef GNUC guards
  • consolidate the python side: merge _splines.py and _bsplines.py. Previously it was not clear what is where and why; The resulting python module is under 1kLOC, so it's OK, I think;
  • incrementally improve test coverage of sepfir2d and spline_filter.

Additional information

This is pure maintenance in the end. I think it makes sense to land this separately from gh-18926: the latter does non-trivial algorithmic improvements, and this one just shuffles code around.

The main improvement is probably the complex-valued arithmetics: #13643 was due to the implementation probably predating C99; this PR uses standard tools instead; the python <-> C++ glue parrots what scipy.special does (thanks @steppi for the explanation!).

Here's a bigger picture, as far as I understand it:

  • signal.spline_filter is IIUC used for image denoising
  • spline_filter itself is a thin wrapper over signal.symirrorder{1,2} and signal.sepfir2d
  • @andfoy refactored symiirorder filters in ENH: Move symiirorder1/2, cspline2d, qspline2d and spline_filter to use sosfilt/lfilter #18926 to use lfilter / sosfilt instead of... uhm... bespoke manual implementations from the multipack era.
  • this PR does a technical refactor of the internals on top of Edgar's PR, plus some small cleanups of the python side.
    sepfir2d is still using C code from 2003, only minimally modernized.

A larger clean-up of sepfir2d could be to take a look at what CuPy does: it delegates a large chunk of signal convolution/filtering to ndimage (https://github.com/cupy/cupy/blob/v13.0.0/cupyx/scipy/signal/_bsplines.py#L72). Maybe SciPy can follow suit and remove a large amount of C code in scipy.signal.
Note that this is where gh-13643 originates.
This will require figuring out what CuPy actually does, and a careful testing, benchmarking etc etc. So it's a somewhat sizeable project, and is best done separately.

andfoy and others added 30 commits April 14, 2024 17:48
Compare to what scipy 1.9.1 / numpy 1.20.x returns
The test exercises spline_filter of complex argument, and has useful
info about spline_filter working in single precision for complex inputs
(the reason is still unclear).
- merge _splines.py and _bsplines.py
- merge tests/test_splines.py and tests/test_bsplines.py
- rename the merged implementation file into _spline_filters.py

None of this is user visible.
Run `$ clang-format -i ` on both files with
.clang-format from scipy/special/special
@ev-br ev-br added scipy.signal maintenance Items related to regular maintenance tasks labels Apr 18, 2024
@github-actions github-actions bot added C/C++ Items related to the internal C/C++ code base Meson Items related to the introduction of Meson as the new build system for SciPy labels Apr 18, 2024
@andfoy
Copy link
Contributor

andfoy commented Apr 18, 2024

It looks good to me! Thanks @ev-br

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C/C++ Items related to the internal C/C++ code base maintenance Items related to regular maintenance tasks Meson Items related to the introduction of Meson as the new build system for SciPy scipy.signal
Projects
None yet
3 participants