Save layer back to spatialdata from napari

I tried to follow the landmark alignment tutorial and created a “points” layer that highlights some landmark features of the image as shown in the tutorial.

How can I save the points layer back in the spatialdata object? The tutorial suggests Ctrl+E, but that doesn’t seem to do anything.

This happens irrespective of if I open a spatialdata zarr file in napari, or open it by using Interactive().

Versions:
spatialdata v0.0.12
napari_spatialdata v0.2.6
on Linux installed via pip into a conda env

Hi Gregor,

Thanks for reporting!

Please use this branch GitHub - scverse/napari-spatialdata at spatialdata for enabling the feature. Unfortunately there have been some delays in merging the PR.

I’ll update the docs accordingly if we don’t manage to merge it soon enough.

Thanks for the quick response! Now at least something is happening, but pressing Ctrl+E raises the following error:

Traceback (most recent call last):
  File "<path redacted>lib/python3.10/site-packages/napari/_qt/widgets/qt_viewer_dock_widget.py", line 230, in keyPressEvent
    return self._ref_qt_viewer().keyPressEvent(event)
  File "<path redacted>lib/python3.10/site-packages/napari/_qt/qt_viewer.py", line 1250, in keyPressEvent
    self.canvas._backend._keyEvent(self.canvas.events.key_press, event)
  File "<path redacted>lib/python3.10/site-packages/vispy/app/backends/_qt.py", line 622, in _keyEvent
    func(native=ev, key=key, text=str(ev.text()), modifiers=mod)
  File "<path redacted>lib/python3.10/site-packages/vispy/util/event.py", line 453, in __call__
    self._invoke_callback(cb, event)
  File "<path redacted>lib/python3.10/site-packages/vispy/util/event.py", line 471, in _invoke_callback
    _handle_exception(self.ignore_callback_errors,
  File "<path redacted>lib/python3.10/site-packages/vispy/util/event.py", line 469, in _invoke_callback
    cb(event)
  File "<path redacted>lib/python3.10/site-packages/napari/utils/key_bindings.py", line 545, in on_key_press
    self.press_key(combo)
  File "<path redacted>lib/python3.10/site-packages/napari/utils/key_bindings.py", line 470, in press_key
    generator_or_callback = func()
  File "<path redacted>lib/python3.10/site-packages/napari_spatialdata/_view.py", line 305, in export
    assert len(set(row)) == 1
TypeError: 'numpy.int64' object is not iterable

Thanks for reporting back. I have just tried again the notebook and I don’t get the error, could you please try the following?

  1. run the first 2 cells of the notebook
  2. the 3rd cell (Interactive(visium_sdata)) needs to be set as a code cell
  3. run the 3rd cell
  4. napari opens, now create a new points layer
  5. add a few points (n >= 1)
  6. press Shift-E. You will see a tooltip in the bottom right interface of napari saying that the data was saved. Note: if you are running napari in a Jupyter Notebook you may see no tooltip, we haven’t investigated yet why this happens.
  7. try to print the sdata object (print(visium_sdata)), you should see the points.

In your case are you doing something substantially different from this?
Thanks for reporting.

I think I did exactly this, but on my own data. I’ll try again with the example data and keep you posted!

Hi @LucaMarconato,

I double-checked: doing exactly what you described with the example data from the notebook results in the same error.

I installed napari-spatialdata using

pip install git+https://github.com/scverse/napari-spatialdata.git@spatialdata

as you suggested.

Napari info

napari: 0.4.18
Platform: Linux-3.10.0-1160.11.1.el7.x86_64-x86_64-with-glibc2.17
Python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:40:32) [GCC 12.3.0]
Qt: 5.15.2
PyQt5: 5.15.9
NumPy: 1.22.4
SciPy: 1.11.1
Dask: 2023.7.1
VisPy: 0.12.2
magicgui: 0.7.3
superqt: 0.5.4
in-n-out: 0.1.8
app-model: 0.2.1
npe2: 0.7.2

OpenGL:
- GL version: 4.6.0 NVIDIA 470.129.06
- MAX_TEXTURE_SIZE: 32768

Screens:
- screen 1: resolution 1920x1080, scale 1.0

@grst we merge this PR to main (the shortcut is Shift+E, no need to do Shift+L before). Saving / exporting new elements back to SpatialData by melonora · Pull Request #168 · scverse/napari-spatialdata · GitHub Can you please try to see if it works for you now?

1 Like