scverse / napari-spatialdata

Interactive visualization of spatial omics data
https://spatialdata.scverse.org/projects/napari/en/latest/
BSD 3-Clause "New" or "Revised" License
39 stars 17 forks source link

mportError: cannot import name '_get_element_annotators' from 'spatialdata._core.query.relational_query' #230

Closed Nazim0001 closed 6 months ago

Nazim0001 commented 6 months ago

Hi, I did not able to run napari-spatialdata. I saw this error: ImportError: cannot import name '_get_element_annotators' from 'spatialdata._core.query.relational_query' (/home/ben/anaconda3/envs/v2/lib/python3.11/site-packages/spatialdata/_core/query/relational_query.py)

Do you have a solution for that please? Thank you in advance Best Nazim

melonora commented 6 months ago

Hi @Nazim0001,

Could you please tell me how you installed spatialdata? I tried to reproduce but am able to import so perhaps you have not updated the spatialdata version.

Nazim0001 commented 6 months ago

Hi, I installed using pip initially. I tried to switch to conda processes later, but it didn't work. My configuration is: Ubuntu 22.04.3 and Python 3.11.07. Best Nazim

melonora commented 6 months ago

Could you check the version of spatialdata and tell me please? Or try reinstalling solely SpatialData itself. We had a release last week in which this function was added.

Nazim0001 commented 6 months ago

0.0.15

melonora commented 6 months ago

This is indeed an outdated version. We are at 0.1.2. Please upgrade the library, pip install --upgrade spatialdata should do the trick.

Nazim0001 commented 6 months ago

Thank you very much, it works, but apparently, it a conflict with this package:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. dask-expr 1.0.6 requires dask==2024.4.0, but you have dask 2024.2.1 which is incompatible When I changed it, two conflicts appeared: ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. distributed 2024.2.1 requires dask==2024.2.1, but you have dask 2024.4.0 which is incompatible. spatialdata 0.1.2 requires dask<=2024.2.1, but you have dask 2024.4.0 which is incompatible.

=> Which one I need to prioritize please?

I have another question, because I would open napari with my Visium Data but I don't have any zarr file for visium_sdata = sd.read_zarr(visium_zarr_path). I have dat files.

=> How can I read my Visium data please?

Best

melonora commented 6 months ago

We require dask<2024.2.1 because recently dask adopted to dask_expr which we do not yet support. This will soon change but for the moment we have a constraint for the version of dask. Perhaps better to install everything into a clean environment that automatically takes care of these constraints when installing.

Regarding question 2, this does not seem like standard visium output if I am not mistaken. Perhaps @LucaMarconato can chime in as he has more experience working with all the versions of Visium data. For now I would refer you to our tutorial on how to construct a SpatialData object that can be written as .zarr: https://spatialdata.scverse.org/en/latest/tutorials/notebooks/notebooks/examples/models1.html and https://spatialdata.scverse.org/en/latest/tutorials/notebooks/notebooks/examples/models2.html. If anything is unclear please ask.

Nazim0001 commented 6 months ago

ok thanks. I install al packages in a clean environment and all work perfectly.

For zarr file, I tried this:

`import scanpy as sc import anndata import zarr

Set paths to data

sp_data_folder = "/media/ben/Elements/Recherche/ETUDE230 visium plag1/data/Visium PA data/nazim/N1Abis"

adata = sc.read_visium( sp_data_folder, count_file="filtered_feature_bc_matrix.h5", load_images=True )

adata.write("/home/ben/Documents/VISIUM/PA/lame V1b/N1Ab.h5ad")

Chargement du fichier H5AD

adata = anndata.read_h5ad("/home/ben/Documents/VISIUM/PA/lame V1b/N1Ab.h5ad")

Conversion en Zarr

zarr_path = "/home/ben/Documents/VISIUM/PA/lame V1b/N1Ab.zarr" adata.write_zarr(zarr_path)

Vérification du fichier Zarr

zarr.open(zarr_path, mode="r")`

But, I did not have any spatialdata object, nor coordinate system: sdata = sd.read_zarr(zarr_path) print(sdata)

Response : SpatialData object with: with coordinate systems:

What I need to do to overcome this please?

Best

LucaMarconato commented 6 months ago

Hi @Nazim0001, thanks for reaching out. Please use the function visium() from spatialdata-io like this:

from spatialdata_io import visium
import spatialdata as sd

sdata = visium('path/to/visium/files')
sdata.write('data.zarr')
# re-read so that the performant Zarr storage is being used
sdata = sd.read_zarr('data.zarr')
print(sdata)

The function sc.read_visium() is not compatible with spatialdata and will probably get deprecated soon (scanpy will probably directly use the one in spatialdata-io, unless we find some blockers with Python dependencies).

Nazim0001 commented 6 months ago

Hey, Thanks you so much. It works now ;) . The system resquested a dataset_id that is worked like that.

`from spatialdata_io import visium import spatialdata as sd

sdata = visium('path/to/visium/files', 'dataset_id='name of repertory') sdata.write('data.zarr')

re-read so that the performant Zarr storage is being used

sdata = sd.read_zarr('data.zarr') print(sdata)`

I don't know if you have the solution, because I lost the link of Qt for napari as you can see : `--------------------------------------------------------------------------- QtBindingsNotFoundError Traceback (most recent call last) File ~/anaconda3/envs/spana/lib/python3.11/site-packages/napari/_qt/init.py:8 7 try: ----> 8 from qtpy import API_NAME, QtCore 9 except Exception as e:

File ~/anaconda3/envs/spana/lib/python3.11/site-packages/qtpy/init.py:287 286 except ImportError: --> 287 raise QtBindingsNotFoundError from None 288 else:

QtBindingsNotFoundError: No Qt bindings could be found

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

ImportError Traceback (most recent call last) Cell In[25], line 7 4 viewer = napari.Viewer() 6 # Afficher la fenêtre de Napari ----> 7 napari.run()

File ~/anaconda3/envs/spana/lib/python3.11/site-packages/napari/_event_loop.py:12, in run(kwargs) 11 def run(kwargs): ---> 12 raise exc

[... skipping hidden 1 frame]

Cell In[25], line 7 4 viewer = napari.Viewer() 6 # Afficher la fenêtre de Napari ----> 7 napari.run()

File ~/anaconda3/envs/spana/lib/python3.11/site-packages/napari/_event_loop.py:12, in run(kwargs) 11 def run(kwargs): ---> 12 raise exc

[... skipping hidden 1 frame]

Cell In[25], line 7 4 viewer = napari.Viewer() 6 # Afficher la fenêtre de Napari ----> 7 napari.run()

File ~/anaconda3/envs/spana/lib/python3.11/site-packages/napari/_event_loop.py:12, in run(kwargs) 11 def run(kwargs): ---> 12 raise exc

File ~/anaconda3/envs/spana/lib/python3.11/site-packages/napari/_event_loop.py:2 1 try: ----> 2 from napari._qt.qt_event_loop import gui_qt, run 4 # qtpy raises a RuntimeError if no Qt bindings can be found 5 except (ImportError, RuntimeError) as e:

File ~/anaconda3/envs/spana/lib/python3.11/site-packages/napari/_qt/init.py:18 12 from inspect import cleandoc 14 installed_withconda = list( 15 Path(sys.prefix, "conda-meta").glob("napari-*.json") 16 ) ---> 18 raise ImportError( 19 trans.( 20 cleandoc( 21 """ 22 No Qt bindings could be found. 23 24 napari requires either PyQt5 (default) or PySide2 to be installed in the environment. 25 26 With pip, you can install either with: 27 $ pip install -U 'napari[all]' # default choice 28 $ pip install -U 'napari[pyqt5]' 29 $ pip install -U 'napari[pyside2]' 30 31 With conda, you need to do: 32 $ conda install -c conda-forge pyqt 33 $ conda install -c conda-forge pyside2 34 35 Our heuristics suggest you are using '{tool}' to manage your packages. 36 """ 37 ), 38 deferred=True, 39 tool="conda" if installed_with_conda else "pip", 40 ) 41 ) from e 42 raise 45 if API_NAME == 'PySide2': 46 # Set plugin path appropriately if using PySide2. This is a bug fix 47 # for when both PyQt5 and Pyside2 are installed

ImportError: No Qt bindings could be found.

napari requires either PyQt5 (default) or PySide2 to be installed in the environment.

With pip, you can install either with: $ pip install -U 'napari[all]' # default choice $ pip install -U 'napari[pyqt5]' $ pip install -U 'napari[pyside2]'

With conda, you need to do: $ conda install -c conda-forge pyqt $ conda install -c conda-forge pyside2

Our heuristics suggest you are using 'pip' to manage your packages.`

and for anaconda:

(base) ben@ben:~$ anaconda-navigator Traceback (most recent call last): File "/home/ben/anaconda3/bin/anaconda-navigator", line 7, in <module> from anaconda_navigator.app.main import main File "/home/ben/anaconda3/lib/python3.11/site-packages/anaconda_navigator/app/main.py", line 19, in <module> from anaconda_navigator.app.start import start_app File "/home/ben/anaconda3/lib/python3.11/site-packages/anaconda_navigator/app/start.py", line 17, in <module> from qtpy import QtCore File "/home/ben/anaconda3/lib/python3.11/site-packages/qtpy/__init__.py", line 287, in <module> raise QtBindingsNotFoundError from None qtpy.QtBindingsNotFoundError: No Qt bindings could be found

Do you any idea of what I need to do please? Thank you best

melonora commented 6 months ago

pip install pyqt fixes this:) or the conda install -c conda-forge pyqt if you use conda

Nazim0001 commented 6 months ago

Hi, Thank you ;)+++, It worked for me after : pip install pyqt5 pip install pyqtwebengine export QT_SELECTION=/usr/bin/qmake

Sorry, I have another issue. It is ok with your script, I was able to create a zarr folder:

from spatialdata_io import visium import spatialdata as sd sdata = visium( "/home/ben/Documents/VISIUM/ETUDE230 visium plag1/data/Visium PA data/nazim/N1Abis/", dataset_id="v1", ) sdata.write("/home/ben/Documents/VISIUM/PA/Lame V1/wr/V1.zarr") sdata = sd.read_zarr("/home/ben/Documents/VISIUM/PA/Lame V1/wr/V1.zarr") print(sdata)

SpatialData object with: ├── Images │ ├── 'v1_hires_image': SpatialImage[cyx] (4, 1890, 2000) │ └── 'v1_lowres_image': SpatialImage[cyx] (4, 567, 600) ├── Shapes │ └── 'v1': GeoDataFrame shape: (3935, 2) (2D shapes) └── Tables └── 'table': AnnData (3935, 17943) with coordinate systems: ▸ 'downscaled_hires', with elements: v1_hires_image (Images), v1 (Shapes) ▸ 'downscaled_lowres', with elements: v1_lowres_image (Images), v1 (Shapes) ▸ 'global', with elements: v1 (Shapes)

I tried to open it with this:

sdata = sd.read_zarr("/home/ben/Documents/VISIUM/PA/Lame V1/wr/V1.zarr") interactive = Interactive(sdata) interactive.run()

It opens without displaying any images. I don't understand the problem. Do you have any ideas on how to debug this issue?

Thank you Best Nazim

melonora commented 6 months ago

What does the graphical user interface look like? Note that all elements live in coordinate systems. We have a widget displaying those. If you double click on a particular coordinate system in this widget, you will see that the element widget will display the elements living in the coordinate system you clicked on. You can then click on any of the elements to open them as a layer in napari.

Another option is to use the API to add elements as layer to the viewer. interactive.add_element(element: str, element_coordinate_system: str, view_element_system: bool = False) Put this before run or after run. The element here is the name of the element, element_coordinate_system is the coordinate system in which you want to display the element. The element must be available in this coordinate system. view_element_system is whether you want to switch the viewer to the coordinate system in which the element is to be displayed.