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

support slicing stacks in arbitrary dimension #42

Closed
wants to merge 1 commit into from

Conversation

martinschorb
Copy link
Contributor

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
Copy link
Owner

constantinpape commented Nov 12, 2023

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 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.

@martinschorb
Copy link
Contributor Author

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
Copy link
Owner

constantinpape commented Nov 13, 2023

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:

  • by default the output will be created with the full shape of the input, but only the data within the roi will be copied. You can additionally set fit_to_roi: True in the config and then it will also crop the output shape to the roi. (This is probably the behavior you want).
  • I have only used this for spatial rois so far, so there might be some issues when you use it for channels. But this is a valid use-case, so I would def. want to fix those errors so that subselecting channels is also supported. (As usual a minimal reproducible example would be very helpful if something does not work.)

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 constantinpape/elf#81 introduces it on a level that does not quite fit, it is already supported in ImageStackDataset.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants