Repository navigation
Add boundary_mode parameter to binary_blobs - #7909
Conversation
|
Thanks for the suggestion. I'm not opposed to adding this parameter or something similar. Though, I'd like to understand why and how this parameter would be useful to you. :) |
|
Of course @lagru! I am using the binary blobs function in my research as it provides the structures I need. The domain I research requiers periodic boundary conditions and the 'wrap' mode provides this perfectly. Adding this to the function instead of implementing it seperatly cleans up my work and lets me more easily use the structures later :) |
|
Ah, I was initially confused why a |
| wrap_blobs = data.binary_blobs(length=64, mode="wrap") | ||
| assert wrap_blobs.shape == (64, 64) | ||
| assert wrap_blobs.dtype == bool |
There was a problem hiding this comment.
Is there some way to test the periodicity? As it is now, this does just test that the new parameter works and returns "some" boolean array...
There was a problem hiding this comment.
There are statistical ways to do this. I am not very familiar with how a methods statistics can be tested in a project like this?
There was a problem hiding this comment.
Maybe we can check, that the edge slices in each respective dimension are similar to each other within a given tolerance. For example, compare blobs[1, :] with blobs[-1, :]. 🤔
There was a problem hiding this comment.
Yes. This tends to be true:
def is_periodic(arr, threshold=0.3):
for axis in range(arr.ndim):
first_slice = np.take(arr, 0, axis=axis)
last_slice = np.take(arr, -1, axis=axis)
mismatch = np.mean(first_slice != last_slice)
if mismatch >= threshold:
return False
return TrueHowever, I tested it for various modes in the gaussian and both "wrap" and "constant" return true, while "reflect" and "nearest" return false.
|
|
||
| def binary_blobs( | ||
| length=512, blob_size_fraction=0.1, n_dim=2, volume_fraction=0.5, rng=None | ||
| length=512, |
There was a problem hiding this comment.
Unrelated, but such a missed opportunity to use shape as a parameter here. That would allow creating array with different lengths in each dimension.
Maybe something to change in skimage2? @scikit-image/core
There was a problem hiding this comment.
That is very true! Shape would be cleaner.
There was a problem hiding this comment.
Yes, let's put this on the 2.0 list.
|
Yes! I use the structures to study transport properties of 2d porous media. :) |
Co-authored-by: Lars Grüter <[email protected]>
I found it a bit tricky to describe the boundary modes in terms of their effects on the resulting blobs. It's tempting to delegate to the docstring of `gaussian` but that isn't really helpful to users without being aware of the actual algorithm or implementation. I opted to not support all modes that gaussian supports. I'm not sure how useful they would really be in addition to 'nearest' and 'wrap'. If I could switch defaults, I would probably switch to 'wrap' or 'constant' over 'nearest'. 'nearest' has a significant effect but only for blobs whose center lies exactly on the edge.
lagru
left a comment
There was a problem hiding this comment.
@Tensorboy2 I took the liberty to push a few new suggestions. Their commit message(s) add more context on the why. I'd be curious what you think about the current state and if you agree with only supporting the existing default and the new 'wrap' mode.
Feel totally welcome to add to this, revert or ask for clarification!
If the CI stays green and you are happy to, I think this is ready to merge.
|
I very much agree with limiting the modes. My choice of default would be "wrap", but keeping the gaussian default is probably best for clarity. |
boundary_mode parameter to binary_blobs
stefanv
left a comment
There was a problem hiding this comment.
Not sure about the clamping; the rest looks good to me.
|
|
||
| def binary_blobs( | ||
| length=512, blob_size_fraction=0.1, n_dim=2, volume_fraction=0.5, rng=None | ||
| length=512, |
There was a problem hiding this comment.
Yes, let's put this on the 2.0 list.
| raise ValueError(f"unsupported `boundary_mode`: {boundary_mode!r}") | ||
|
|
||
| blob_size = blob_size_fraction * length | ||
| if blob_size < 0.1: |
There was a problem hiding this comment.
This clamping feels a bit magical; why not just limit the blob size fraction to somewhere between 0.1 and 1?
There was a problem hiding this comment.
For small blob_size_fraction it will try to allocate an array of size (n_dim, int(1.0 / blob_size_fraction) ** n_dim). This can lead to unexpectedly large arrays being allocated.
I chose blob_size_fraction * length < 0.1 as a cut-off because its results is very probably unexpected. At that point the returned array will mostly consist of 1s everywhere because almost every pixel has been marked as a seed.
There was a problem hiding this comment.
But maybe I shouldn't call this blob_size as the actual size of blobs is influenced by volume_fraction too.
There was a problem hiding this comment.
Maybe something like blob_pixel_ratio?
Co-authored-by: Stefan van der Walt <[email protected]>
Co-authored-by: Stefan van der Walt <[email protected]>
Co-authored-by: Stefan van der Walt <[email protected]>
Co-authored-by: Stefan van der Walt <[email protected]>
Co-authored-by: Stefan van der Walt <[email protected]>
…" in doc of binary_blobs
stefanv
left a comment
There was a problem hiding this comment.
- Discussed on weekly call: we probably want to get this in without the clipping, but Lars will do one more scan to ensure this wasn't done to avoid high memory use.
- Check that
shapeissue has been captured around
| f"Clamping to `blob_size_fraction={clamped_size_fraction}` and a blob size " | ||
| f"of {clamped_blob_size} pixels to avoid allocating excessive memory.", |
There was a problem hiding this comment.
@stefanv, avoiding the allocation of excessive memory was indeed the reason for the clamping.
|
Thanks for your contribution, @Tensorboy2! |

Description
binary_blobs now supports a
modeparameter for the Gaussian filter, allowing periodic boundary conditions withmode="wrap".Checklist
Update to _binary_blobs doc string:
Default remains the default of the Gaussian filter.
Addition to test_binary_blobs:
Release note