Re: ENH: add bounding_box function to SciPy.ndimage

"Tomer Noy" <[email protected]>
Newsgroups gmane.comp.python.scientific.devel
Message-ID <[email protected]>
Ralf Gommers wrote:
> On Mon, Mar 20, 2023 at 2:06 PM [email protected] wrote:
> > Link to PR: https://github.com/scipy/scipy/pull/18170
> > I would like to propose a new function for the ndimage measurements
> > module: `bounding_box()`
> > This function finds the bounding box of the nonzero elements in the input
> > array. It accepts any dimensionality and dtype input.
> > Why do I think this is needed?
> > It is true that same result can be obtained by:
> > 
> > numpy: `nz=np.nonzero(arr)  ; nz[0].min(), nz[0].max(),
> > 
> > nz[1].min(),....`   : SLOW
> > 
> > scipy: `ndi.find_objects(arr!=0,max_label=1)`   : Faster, implemented in
> > 
> > C
> > I actually implemented `bounding_box` simply by adapting the existing
> > implementation of `find_objects`, so the logic is fundamentally the same.
> > However, it runs significantly faster - namely between 10% and 30% faster
> > than `find_objects`, per my tests locally.
> > This is mainly due to the fact that `bounding_box` runs *in-place* on the
> > input array, while running `find_objects(arr!=0) requires allocation of a
> > new array, to store the boolean result of `arr!=0`, which is then passed as
> > the argument to `find_objects`.
> > Of course, avoiding this allocation also means memory footprint is
> > significantly reduced, and in case of large multidimensional arrays this
> > can be crucial.
> > The only similar functionality I have found in Python libraries is
> > `PIL.Image.getbbox()` :
> > https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image...
> > This is limited to 2D images.
> > Thanks, this seems like a reasonable function to add to me. The one
> question that came to mind is whether the functionality should be supported
> for floating point dtypes, or only for bool and integer dtypes. Your
> implementation uses `!= 0`, and that may not work as expected for -0.0
> values for example. In general these kinds of exact equality checks for
> floats should be avoided.
> Cheers,
> Ralf

Thanks for your feedback, Ralf.

Your question regarding floating point types is an important one.
One could make the claim that comparing `!=0` does make sense in some cases for this function, suggesting that a value that is in any way different from `np.zeros([...])`, is "nonempty".

Maybe we can address the more general case by adding an optional argument, specifying the max absolute value to be considered "zero" in the floating point case, e.g:
`ndi.bounding_box(arr, epsilon=1e-6)`

Tomer
_______________________________________________
SciPy-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/scipy-dev.python.org/
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.