haesleinhuepf / napari-assistant

BSD 3-Clause "New" or "Revised" License
20 stars 5 forks source link

4D data handling #15

Open Cryaaa opened 2 years ago

Cryaaa commented 2 years ago

At the moment 4D data is automatically converted to 3D (or 2D data if z = 1) in the ._gui._category_widget.py call_op function (see lines 141 - 151):

 # transfer data to gpu
    if timepoint is None:
        i0 = inputs[0].data
        gpu_ins = [i.data if i is not None else i0 for i in inputs]
    else:
        i0 = inputs[0].data[timepoint] if len(inputs[0].data.shape) == 4 else inputs[0].data
        gpu_ins = [(i.data[timepoint] if len(i.data.shape) == 4 else i.data if i is not None else i0) for i in inputs]

    # convert 3d-1-slice-data into 2d data
    # to support 2d timelapse data
    gpu_ins = [i if len(i.shape) != 3 or i.shape[0] != 1 else i [0] for i in gpu_ins]

This means that functions only see a 3D (or 2D) image in the arguments which are given to napari workflows (which is causing issues for the undo functionality with 4D data). As we discussed handling of 4D data should not be set by the assistant but by the functions themselves which should be wrapped in the new dask timeslicer wrapper if they cannot handle 4D data. This issue needs to be solved before we can continue with #12

Cryaaa commented 2 years ago

The workaround for now is that the Workflowmanager does not support undo in the case of images that cannot be pinpointed to a specific layer and throws a lot of warnings. The changes can be found in this PR: ._undo_redo_functionality.py lines 61 - 71:

            workflows_differ = False
            try:
                workflows_differ = (self.workflow._tasks != (self.undo_stack[-1])._tasks)
            except ValueError:
                warnings.warn("Cannot determine layer from image - Undo functionality impaired")

            if workflows_differ:  
                self.undo_stack.append(
                    copy_workflow_state(self.workflow)
                )
                self.redo_stack.clear()

The comparison only works if the workflows contain layer names and not arrays which is not the case at the moment as time slices are passed to the workflowmanager and not the whole images which is why the workflowmanager cannot determine which layer it is from. Tagging @haesleinhuepf so you know what is going on here.

haesleinhuepf commented 2 years ago

I managed to remove the code shown above, but there are downstream issues related to the time-slicer. If a time-slicer annotated function receives a 4D image, it processes internally a 3D image and also stores this 3D image in the workflow. Thus, the workflow cannot know which 4D image this corresponded to. I won't dive deeper into this, because I presume this issue will go away, once we managed to make 4D dask-images using the time-slicer. Instead I'm cancelling Undo/Redo now in case of 4D data. We should come back to this after making this work:

Cryaaa commented 2 years ago

@haesleinhuepf the PR you mentioned above already works and as you said should solve the issue! I created the dask Version as a new function so it won’t replace the old one. Anyway we should talk about this once more in Person as soon as you have some time!

haesleinhuepf commented 2 years ago

I created the dask Version as a new function so it won’t replace the old one.

I know, but I'd like to avoid this kind of code-duplication, The dask-version brings the functionality as it's supposed to be and it almost works, see:

Anyway we should talk about this once more in Person as soon as you have some time!

Yes, definitely! Will work this week I guess.