BodenmillerGroup / steinbock

A toolkit for processing multiplexed tissue images
https://bodenmillergroup.github.io/steinbock
MIT License
49 stars 14 forks source link

Error when trying to execute "Image visualization" in napari.ipynb #158

Closed marleneweiss closed 1 year ago

marleneweiss commented 1 year ago

Hi, I get this error when I try to execute the "Image visualization" chunk in the napari python notebook. Thank you for your help!

KeyError                                  Traceback (most recent call last)
File ~\anaconda3\envs\napari\lib\site-packages\pandas\core\indexes\base.py:3803, in Index.get_loc(self, key, method, tolerance)
   3802 try:
-> 3803     return self._engine.get_loc(casted_key)
   3804 except KeyError as err:

File ~\anaconda3\envs\napari\lib\site-packages\pandas\_libs\index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()

File ~\anaconda3\envs\napari\lib\site-packages\pandas\_libs\index.pyx:165, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'keep'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[9], line 13
      6 viewer.scale_bar.visible = True
      7 viewer.scale_bar.unit = "um"
      9 img_layers = viewer.add_image(
     10     data=img,
     11     channel_axis=0,
     12     colormap="gray",
---> 13     name=panel.loc[panel["keep"] == 1, "name"],
     14     blending="additive",
     15     visible=False,
     16 )
     18 mask_layer = viewer.add_labels(
     19     data=mask,
     20     name="Cells",
     21     blending="translucent",
     22     visible=False,
     23 )

File ~\anaconda3\envs\napari\lib\site-packages\pandas\core\frame.py:3805, in DataFrame.__getitem__(self, key)
   3803 if self.columns.nlevels > 1:
   3804     return self._getitem_multilevel(key)
-> 3805 indexer = self.columns.get_loc(key)
   3806 if is_integer(indexer):
   3807     indexer = [indexer]

File ~\anaconda3\envs\napari\lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key, method, tolerance)
   3803     return self._engine.get_loc(casted_key)
   3804 except KeyError as err:
-> 3805     raise KeyError(key) from err
   3806 except TypeError:
   3807     # If we have a listlike key, _check_indexing_error will raise
   3808     #  InvalidIndexError. Otherwise we fall through and re-raise
   3809     #  the TypeError.
   3810     self._check_indexing_error(key)

KeyError: 'keep'
jwindhager commented 1 year ago

Hi @marleneweiss

That likely means that your panel.csv does not contain a keep column. If you kept all channels during image extraction (no keep column or keep=1 for all channels), you can replace

name=panel.loc[panel["keep"] == 1, "name"],

by

name=panel["name"],
marleneweiss commented 1 year ago

Hi @jwindhager

I did keep the column and selected 2 channels = 0. panel

Even when replacing like you suggested, I still get an error message:

KeyError                                  Traceback (most recent call last)
File ~\anaconda3\envs\napari\lib\site-packages\pandas\core\indexes\base.py:3803, in Index.get_loc(self, key, method, tolerance)
   3802 try:
-> 3803     return self._engine.get_loc(casted_key)
   3804 except KeyError as err:

File ~\anaconda3\envs\napari\lib\site-packages\pandas\_libs\index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()

File ~\anaconda3\envs\napari\lib\site-packages\pandas\_libs\index.pyx:165, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'name'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[13], line 13
      6 viewer.scale_bar.visible = True
      7 viewer.scale_bar.unit = "um"
      9 img_layers = viewer.add_image(
     10     data=img,
     11     channel_axis=0,
     12     colormap="gray",
---> 13     name=panel["name"],
     14     blending="additive",
     15     visible=False,
     16 )
     18 mask_layer = viewer.add_labels(
     19     data=mask,
     20     name="Cells",
     21     blending="translucent",
     22     visible=False,
     23 )

File ~\anaconda3\envs\napari\lib\site-packages\pandas\core\frame.py:3805, in DataFrame.__getitem__(self, key)
   3803 if self.columns.nlevels > 1:
   3804     return self._getitem_multilevel(key)
-> 3805 indexer = self.columns.get_loc(key)
   3806 if is_integer(indexer):
   3807     indexer = [indexer]

File ~\anaconda3\envs\napari\lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key, method, tolerance)
   3803     return self._engine.get_loc(casted_key)
   3804 except KeyError as err:
-> 3805     raise KeyError(key) from err
   3806 except TypeError:
   3807     # If we have a listlike key, _check_indexing_error will raise
   3808     #  InvalidIndexError. Otherwise we fall through and re-raise
   3809     #  the TypeError.
   3810     self._check_indexing_error(key)

KeyError: 'name'

Any other idea?

Thank you.

jwindhager commented 1 year ago

Hi @marleneweiss,

As said, this error means that the panel.csv file you are loading does not contain the columns name or keep.

Please:

You can check whether you are loading the correct panel file by visualizing the contents of the loaded panel file directly in Jupyter Lab (just add a new cell that only contains panel and execute it).

If that doesn't help, as this issue doesn't directly relate to steinbock, please reach out via jonas.windhager@uzh.ch. Thanks!

jwindhager commented 1 year ago

Hi @marleneweiss, has this been resolved? Thanks

Milad4849 commented 1 year ago

Inactive, Closing the issue.