constantinpape / cluster_tools

Distributed segmentation for bio-image-analysis
MIT License
34 stars 14 forks source link

support slicing stacks in arbitrary dimension #42

Closed martinschorb closed 6 months ago

martinschorb commented 1 year ago

Hi, this should enable extracting only sub-dimensional parts of stacks. (-> single channels of multi-channel stacks for MoBIE etc...)

I have not checked how it can be tested yet.

constantinpape commented 9 months ago

I think it would be better to solve this via the roi_begin and roi_end parameters, which can be set via the config file for the downscaling task already.

See the example script for how to write the config file.

It is then read here.

Note that rois for 4d inputs (i.e. data with channels) is not supported yet. But it should not be too difficult to implement this.

For this we would need to update the code here so that rois for 4d inputs are allowed, and potentially change a few other cases that might lead to errors when using a 4d roi.

See my comments in https://github.com/constantinpape/elf/pull/81 why I would rather not encode this in the key (because the key is only meant to select the dataset for h5py, zarr etc.) and because we already have the roi functionality here for arbitrary slicing.

martinschorb commented 9 months ago

See my comments in constantinpape/elf#81 why I would rather not encode this in the key (because the key is only meant to select the dataset for h5py, zarr etc.) and because we already have the roi functionality here for arbitrary slicing.

Do I understand it correctly, that for a 2D multichannel image, one could simply choose roi_begin: [2,0,0], roi_end: [2,-1,-1] to select the 3rd channel (if channel is encoded in dim 0) and return a 2D output image?

That would indeed already be the required functionality and ideally elf would take these rois as parameters

constantinpape commented 9 months ago

Do I understand it correctly, that for a 2D multichannel image, one could simply choose roi_begin: [2,0,0], roi_end: [2,-1,-1] to select the 3rd channel (if channel is encoded in dim 0) and return a 2D output image?

That's the idea, but the roi you give is not quite correct. To get the 3rd channel you would need to set

roi_begin: [2, 0, 0], roi_end: [3, image.shape[1], image.shape[2]]

The roi arguments are used to create a slice like this: (This code is not actually used, but the equivalent thing happens)

np.s_[roi_begin[0]:roi_end[0], roi_begin[1]:roi_end[1], ...]

So most of the rules of regular slicing in python apply. (And the roi you gave would have resulted in not selecting anything across dim0 and selecting everything up to the last pixel in the other dimensions).

Two more notes:

That would indeed already be the required functionality and ideally elf would take these rois as parameters.

The way this is handled is a bit more complex, since we need to translate these rois to the blocking mechanism that is used for parallelization by cluster_tools. elf already supports slicing as it is (as I said, your PR in https://github.com/constantinpape/elf/pull/81 introduces it on a level that does not quite fit, it is already supported in ImageStackDataset.)