saeyslab / napari-sparrow

Other
17 stars 0 forks source link

KeyError: "'None' not found in sdata.images or sdata.labels" #171

Closed Lucas-Maciel closed 5 months ago

Lucas-Maciel commented 6 months ago

Hi,

So I updated today my sparrow version and I'm having some issues that I didn't have in the past. This happens when I'm correcting the Illumination:

$ sdata = sp.im.min_max_filtering( sdata=sdata, output_layer='min_max_filtered' , size_min_max_filter= 45,img_layer="tiling_correction",overwrite=True )
$ sp.pl.plot_image( sdata, img_layer='min_max_filtered', crd=crd, figsize=( 8,8 ) )
$ sdata = sp.im.enhance_contrast( sdata=sdata, output_layer='clahe', contrast_clip=3.5, chunks=20000 )
$ sp.pl.plot_image( sdata, img_layer='clahe', crd=crd, figsize=( 8,8 ) )

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[12], line 3
      1 sdata = sp.im.min_max_filtering( sdata=sdata, output_layer='min_max_filtered' , size_min_max_filter= 45,img_layer="tiling_correction",overwrite=True )
      2 sp.pl.plot_image( sdata, img_layer='min_max_filtered', crd=crd, figsize=( 8,8 ) )
----> 3 sdata = sp.im.enhance_contrast( sdata=sdata, output_layer='clahe', contrast_clip=3.5, chunks=20000 )
      4 sp.pl.plot_image( sdata, img_layer='clahe', crd=crd, figsize=( 8,8 ) )

File D:\Lucas\napari-sparrow-main\src\sparrow\image\_contrast.py:108, in enhance_contrast(sdata, img_layer, contrast_clip, chunks, depth, output_layer, crd, scale_factors, overwrite)
    104         image = image[None, None, ...]
    106     return image
--> 108 se = _get_spatial_element(sdata, img_layer)
    110 if isinstance(contrast_clip, Iterable):
    111     assert len(contrast_clip) == len(
    112         se.c.data
    113     ), f"If 'contrast_clip' is provided as a list, it should match the number of channels in '{se}' ({len(se.c.data)})"

File D:\Lucas\napari-sparrow-main\src\sparrow\image\_image.py:140, in _get_spatial_element(sdata, layer)
    138     si = sdata.labels[layer]
    139 else:
--> 140     raise KeyError(f"'{layer}' not found in sdata.images or sdata.labels")
    141 if isinstance(si, SpatialImage):
    142     return si

KeyError: "'None' not found in sdata.images or sdata.labels"

Once I provide the layer, it works fine. The same happens if I don't provide img_layer in for the min_max_filtering function.

ArneDefauw commented 6 months ago

Hi Lucas, thanks for opening the issue! I see what is happening, if img_layer was not provided, we used to fall back to the last image layer (given by[*sdata.images][-1] ) insdata, to perform our processing on. But this has been left out forsp.im.enhance_contrast and sp.im.min_max_filtering , and is indeed not very consitent (and results in an unclear error message) because for other sparrow functions we still have a fall back implemented.

I am not completely sure what the best solution is. I think adding this fall back again for sp.im.enhance_contrast and sp.im.min_max_filtering seems to be the best choice.

ArneDefauw commented 5 months ago

fixed by https://github.com/saeyslab/napari-sparrow/pull/177