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
ENH: ndimage: 1D rank filter speed up #20026
Comments
There certainly is interest, I'd say! Ideally the perf improvement happens in ndimage --- the duplication between scipy.ndimage and scipy.signal filtering routines is mostly historical, and would be nice to reduce where reasonable (see e.g. [1]). This is not set in stone, however, so if there's a reason to keep duplicating things, let's hear them. |
Well, for both rank filter and median filter, a reasonable 1D use case in signal processing can have a 21 window length. Meaning, that for each step we will replace 1 element from 21. I am not familiar with the image processing but it seems to me that for a window of 6x6=36, every step will have new 6 elements. Therefore a ratio between the new and the current number of elements is smaller and the current code is reasonable (in my tests the advantage os from a widow size of 7 which is larger than 6). |
@ev-br , in your opinion, assuming I am covering all the boundary conditions (symetric, anti-symetric, zero-pad, constant, etc), what are the additional tests required for the function? |
1 step at a time is good. So the story along the lines of "scipy.signal.medfilt does better in 1D and delegates to ndimage otherwise" sounds reasonable. As a first step at least :-). Re tests. Good question. Both ndimage and scipy.signal versions are documented to operate on general N-D arrays, so ideally there are tests to clarify and fix the behavior. Also the question of backwards compatibility is going to come up, so you'll need to be able to show it's preserved (or clearly show where it is not). I'm not entirely sure the current coverage is stellar, so maybe think of how to help in this. |
Seems like the median filters in both modules are running on top of the rank filter. I suggest renaming the issue to rank filter speed up and handle this function for the 1D case by integrating to the source code and show improvement in performance. Is it ok with you @ev-br ?
I figured out that though it though the documentation says that the kernal should be odd, it accepts even values too. Assuming we move to handling the rank filter it will not bother us and I will modify the code to consider it.
Assuming we are moving to handle the background functions only, it will be transparent to the user, ok? |
PS: a second thought about when it should be more efficient in 2D: I assume that it will outperform the current implementation in the cases where ratio of the computation time is higher than the width of the kernel. For example: all the kernels with a width of 2 and a total kernel size larger than 11 should be beneficial. Or all the kernels with a width of 3 and a total kernel size larger than 19. |
Finished handling all the modes/boundary coditions and implemented all the required modifications to handle the rank_filter. Current results are attached. Now I will start moving from ctypes API to Python/C API. |
A couple of quick superficial comments:
|
Thanks for your support.
|
@ev-br I have created a pull request. Will you please review it and tell me what you think of it? |
Is your feature request related to a problem? Please describe.
During working on the Hampel filter, I implemented a median filter and tested it vs the current implementation. Obviously, there are some tests that I did not implement. Nevertheless, seems like for any window bigger than 5, the new implementation is faster. I checked vs the faster implementation scipy.ndimage.median_filter rather than the slower signal.medfilt
I wonder if I am missing something. Otherwise, is in the community's interest that I will continue the development and replace the current implementation?
Why do I think that the current implementation is slower? I think that they did not use the median heap. Obviously, in case I am right, it might lead to new implementations of rank filter too.
The testing code and the implementation are here:
Describe the solution you'd like.
No response
Describe alternatives you've considered.
No response
Additional context (e.g. screenshots, GIFs)
No response
The text was updated successfully, but these errors were encountered: