Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty color values in images after polygon_query from Xenium data #117

Closed
Tracked by #477
Bo-UT opened this issue Mar 4, 2024 · 11 comments
Closed
Tracked by #477

Empty color values in images after polygon_query from Xenium data #117

Bo-UT opened this issue Mar 4, 2024 · 11 comments

Comments

@Bo-UT
Copy link

Bo-UT commented Mar 4, 2024

Hi,

I have multiple samples in the same FOV in Xenium data. I am trying to subset each sample with polygon_query.

cropped = polygon_query(
    sdata=sdata,
    polygons=polygon,
    target_coordinate_system="global",
    images=True, 
)  

Then I plot the image

cropped.pl.render_images('morphology_mip').pl.show(), but no data show up in the plot.

Screenshot 2024-03-04 at 4 18 20 PM

Here is the data from cropped.images. The c is just 0.

Screenshot 2024-03-04 at 4 19 21 PM

Can you please point out what's the problem there?
Thank you!!

Bo

@LucaMarconato
Copy link
Member

LucaMarconato commented Mar 8, 2024

Hi, I think this could be due to a bug of polygon_query() that we fixed in the spaitaldata package here: scverse/spatialdata#422.

Can you please try the latest main? Unfortunately we haven't made a release for it yet.

Please notice that you may need to also use the latest main from spatialdata-io.

@Bo-UT
Copy link
Author

Bo-UT commented Mar 8, 2024

Hi, thanks for reply!

I created a new Conda environment and reinstalled it from git
pip install git+https://github.com/scverse/spatialdata

croppedRegion = polygon_query( spadata, polygon, target_coordinate_system="global", shapes = True, points = True, images = True, labels = True )

But it looks the shapes and points are not included
Screenshot 2024-03-08 at 12 04 16 PM

I see they're deprecated from polygon_query. Can you please let me know how to get them back?

Then I tried to plot the image
croppedRegion.pl.render_images('morphology_mip').pl.show()

but got a ValueError

Screenshot 2024-03-08 at 12 08 49 PM

However, the imagecodecs has already been installed.

Screenshot 2024-03-08 at 12 10 22 PM

Thanks so much for your help!

Conda environment:
aiobotocore 2.5.4 pypi_0 pypi
aiohttp 3.9.3 pypi_0 pypi
aioitertools 0.11.0 pypi_0 pypi
aiosignal 1.3.1 pypi_0 pypi
anndata 0.10.5.post1 pyhd8ed1ab_0 conda-forge
annotated-types 0.6.0 pypi_0 pypi
aom 3.8.1 h078ce10_0 conda-forge
appnope 0.1.4 pyhd8ed1ab_0 conda-forge
arpack 3.8.0 nompi_ha3438d0_101 conda-forge
array-api-compat 1.5 pyhd8ed1ab_0 conda-forge
asciitree 0.3.3 pypi_0 pypi
asttokens 2.4.1 pyhd8ed1ab_0 conda-forge
async-timeout 4.0.3 pypi_0 pypi
attrs 23.2.0 pypi_0 pypi
botocore 1.31.17 pypi_0 pypi
brotli 1.1.0 hb547adb_1 conda-forge
brotli-bin 1.1.0 hb547adb_1 conda-forge
bzip2 1.0.8 h80987f9_5
c-ares 1.27.0 h93a5062_0 conda-forge
ca-certificates 2024.2.2 hf0a4a13_0 conda-forge
cached-property 1.5.2 hd8ed1ab_1 conda-forge
cached_property 1.5.2 pyha770c72_1 conda-forge
cairo 1.18.0 hd1e100b_0 conda-forge
certifi 2024.2.2 py310hca03da5_0
charset-normalizer 3.3.2 pypi_0 pypi
click 8.1.7 pypi_0 pypi
click-plugins 1.1.1 pypi_0 pypi
cligj 0.7.2 pypi_0 pypi
cloudpickle 3.0.0 pypi_0 pypi
colorama 0.4.6 pyhd8ed1ab_0 conda-forge
colorcet 3.1.0 pypi_0 pypi
comm 0.2.1 pyhd8ed1ab_0 conda-forge
contourpy 1.2.0 py310hd137fd4_0 conda-forge
cycler 0.12.1 pyhd8ed1ab_0 conda-forge
dask 2024.2.1 pypi_0 pypi
dask-image 2023.8.1 pypi_0 pypi
datashader 0.16.0 pypi_0 pypi
dav1d 1.2.1 h80987f9_0
debugpy 1.8.1 py310h692a8b6_0 conda-forge
decorator 5.1.1 pyhd8ed1ab_0 conda-forge
distributed 2024.2.1 pypi_0 pypi
docrep 0.3.2 pypi_0 pypi
exceptiongroup 1.2.0 pyhd8ed1ab_2 conda-forge
executing 2.0.1 pyhd8ed1ab_0 conda-forge
expat 2.6.1 hebf3989_0 conda-forge
fasteners 0.19 pypi_0 pypi
fcsparser 0.2.8 pypi_0 pypi
ffmpeg 6.1.1 gpl_h31ea89b_104 conda-forge
fiona 1.9.6 pypi_0 pypi
font-ttf-dejavu-sans-mono 2.37 hd3eb1b0_0
font-ttf-inconsolata 2.001 hcb22688_0
font-ttf-source-code-pro 2.030 hd3eb1b0_0
font-ttf-ubuntu 0.83 h8b1ccd4_0
fontconfig 2.14.2 h82840c6_0 conda-forge
fonts-anaconda 1 h8fa9717_0
fonts-conda-ecosystem 1 hd3eb1b0_0
fonttools 4.49.0 py310hd125d64_0 conda-forge
freetype 2.12.1 hadb7bae_2 conda-forge
fribidi 1.0.10 h1a28f6b_0
frozenlist 1.4.1 pypi_0 pypi
fsspec 2023.6.0 pypi_0 pypi
geopandas 0.14.3 pypi_0 pypi
get-annotations 0.1.2 pyhd8ed1ab_0 conda-forge
gettext 0.21.1 h0186832_0 conda-forge
glpk 5.0 h6d7a090_0 conda-forge
gmp 6.3.0 h965bd2d_0 conda-forge
gnutls 3.7.9 hd26332c_0 conda-forge
graphite2 1.3.14 hc377ac9_1
h5py 3.10.0 nompi_py310h49e257e_101 conda-forge
harfbuzz 8.3.0 h8f0ba13_0 conda-forge
hdf5 1.14.3 nompi_h5bb55e9_100 conda-forge
icu 73.2 hc8870d7_0 conda-forge
idna 3.6 pypi_0 pypi
igraph 0.10.10 h48be1ad_0 conda-forge
imagecodecs 2024.1.1 pypi_0 pypi
imageio 2.34.0 pypi_0 pypi
importlib-metadata 7.0.2 pyha770c72_0 conda-forge
importlib_metadata 7.0.2 hd8ed1ab_0 conda-forge
inflect 7.0.0 pypi_0 pypi
ipykernel 6.29.3 pyh3cd1d5f_0 conda-forge
ipython 8.22.2 pyh707e725_0 conda-forge
jasper 4.2.1 h7c0e182_0 conda-forge
jedi 0.19.1 pyhd8ed1ab_0 conda-forge
jinja2 3.1.3 pypi_0 pypi
jmespath 1.0.1 pypi_0 pypi
joblib 1.3.2 pyhd8ed1ab_0 conda-forge
jupyter_client 8.6.0 pyhd8ed1ab_0 conda-forge
jupyter_core 5.7.1 py310hbe9552e_0 conda-forge
kiwisolver 1.4.5 py310h38f39d4_1 conda-forge
krb5 1.21.2 h92f50d5_0 conda-forge
lame 3.100 h1a28f6b_0
lamin-utils 0.13.0 pypi_0 pypi
lazy-loader 0.3 pypi_0 pypi
lcms2 2.16 ha0e7c42_0 conda-forge
leidenalg 0.10.2 py310h692a8b6_0 conda-forge
lerc 4.0.0 h9a09cb3_0 conda-forge
libabseil 20240116.1 cxx17_hebf3989_2 conda-forge
libaec 1.1.2 h13dd4ca_1 conda-forge
libass 0.17.1 hf7da4fe_1 conda-forge
libblas 3.9.0 21_osxarm64_openblas conda-forge
libbrotlicommon 1.1.0 hb547adb_1 conda-forge
libbrotlidec 1.1.0 hb547adb_1 conda-forge
libbrotlienc 1.1.0 hb547adb_1 conda-forge
libcblas 3.9.0 21_osxarm64_openblas conda-forge
libcurl 8.5.0 h2d989ff_0 conda-forge
libcxx 16.0.6 h4653b0c_0 conda-forge
libdeflate 1.19 hb547adb_0 conda-forge
libedit 3.1.20191231 hc8eb9b7_2 conda-forge
libev 4.33 h93a5062_2 conda-forge
libexpat 2.6.1 hebf3989_0 conda-forge
libffi 3.4.4 hca03da5_0
libgfortran 5.0.0 13_2_0_hd922786_3 conda-forge
libgfortran5 13.2.0 hf226fd6_3 conda-forge
libglib 2.78.4 h0a96307_0
libhwloc 2.9.3 default_h4394839_1009 conda-forge
libiconv 1.17 h0d3ecfb_2 conda-forge
libidn2 2.3.4 h80987f9_0
libjpeg-turbo 3.0.0 hb547adb_1 conda-forge
liblapack 3.9.0 21_osxarm64_openblas conda-forge
liblapacke 3.9.0 21_osxarm64_openblas conda-forge
libleidenalg 0.11.1 h1995070_0 conda-forge
libllvm14 14.0.6 hd1a9a77_4 conda-forge
libnghttp2 1.58.0 ha4dd798_1 conda-forge
libopenblas 0.3.26 openmp_h6c19121_0 conda-forge
libopencv 4.9.0 py310h63e2394_8 conda-forge
libopenvino 2023.3.0 he6dadac_2 conda-forge
libopenvino-arm-cpu-plugin 2023.3.0 he6dadac_2 conda-forge
libopenvino-auto-batch-plugin 2023.3.0 hc9f00d9_2 conda-forge
libopenvino-auto-plugin 2023.3.0 hc9f00d9_2 conda-forge
libopenvino-hetero-plugin 2023.3.0 hf483cef_2 conda-forge
libopenvino-ir-frontend 2023.3.0 hf483cef_2 conda-forge
libopenvino-onnx-frontend 2023.3.0 h9363200_2 conda-forge
libopenvino-paddle-frontend 2023.3.0 h9363200_2 conda-forge
libopenvino-pytorch-frontend 2023.3.0 hebf3989_2 conda-forge
libopenvino-tensorflow-frontend 2023.3.0 h64b43cf_2 conda-forge
libopenvino-tensorflow-lite-frontend 2023.3.0 hebf3989_2 conda-forge
libopus 1.3.1 h27ca646_1 conda-forge
libpng 1.6.43 h091b4b1_0 conda-forge
libprotobuf 4.25.2 hbfab5d5_1 conda-forge
libsodium 1.0.18 h27ca646_1 conda-forge
libsqlite 3.45.1 h091b4b1_0 conda-forge
libssh2 1.11.0 h7a5bd25_0 conda-forge
libtasn1 4.19.0 h80987f9_0
libtiff 4.6.0 ha8a6c65_2 conda-forge
libunistring 0.9.10 h1a28f6b_0
libvpx 1.13.1 h313beb8_0
libwebp-base 1.3.2 hb547adb_0 conda-forge
libxcb 1.15 hf346824_0 conda-forge
libxml2 2.12.5 h0d0cfa8_0 conda-forge
libzlib 1.2.13 h53f4e23_5 conda-forge
llvm-openmp 17.0.6 hcd81f8e_0 conda-forge
llvmlite 0.42.0 pypi_0 pypi
locket 1.0.0 pypi_0 pypi
markdown-it-py 3.0.0 pypi_0 pypi
markupsafe 2.1.5 pypi_0 pypi
matplotlib-base 3.8.3 py310h2439c42_0 conda-forge
matplotlib-inline 0.1.6 pyhd8ed1ab_0 conda-forge
matplotlib-scalebar 0.8.1 pypi_0 pypi
mdurl 0.1.2 pypi_0 pypi
msgpack 1.0.8 pypi_0 pypi
multidict 6.0.5 pypi_0 pypi
multipledispatch 1.0.0 pypi_0 pypi
multiscale-spatial-image 0.11.2 pypi_0 pypi
munkres 1.1.4 pyh9f0ad1d_0 conda-forge
natsort 8.4.0 pyhd8ed1ab_0 conda-forge
ncurses 6.4 h313beb8_0
nest-asyncio 1.6.0 pyhd8ed1ab_0 conda-forge
nettle 3.9.1 h40ed0f5_0 conda-forge
networkx 3.2.1 pyhd8ed1ab_0 conda-forge
numba 0.59.0 py310hdf1f89a_1 conda-forge
numcodecs 0.12.1 pypi_0 pypi
numpy 1.23.4 pypi_0 pypi
ome-zarr 0.8.3 pypi_0 pypi
omnipath 1.0.8 pypi_0 pypi
opencv 4.9.0 py310h748fdf9_8 conda-forge
openh264 2.4.1 hebf3989_0 conda-forge
openjpeg 2.5.2 h9f1df11_0 conda-forge
openssl 3.2.1 h0d3ecfb_0 conda-forge
p11-kit 0.24.1 h29577a5_0 conda-forge
packaging 23.2 pyhd8ed1ab_0 conda-forge
pandas 2.2.1 py310h401b61c_0 conda-forge
param 2.0.2 pypi_0 pypi
parso 0.8.3 pyhd8ed1ab_0 conda-forge
partd 1.4.1 pypi_0 pypi
patsy 0.5.6 pyhd8ed1ab_0 conda-forge
pcre2 10.42 hb066dcc_0
pexpect 4.9.0 pyhd8ed1ab_0 conda-forge
pickleshare 0.7.5 py_1003 conda-forge
pillow 10.2.0 py310hfae7ebd_0 conda-forge
pims 0.6.1 pypi_0 pypi
pip 23.3.1 py310hca03da5_0
pixman 0.43.4 hebf3989_0 conda-forge
platformdirs 4.2.0 pyhd8ed1ab_0 conda-forge
prompt-toolkit 3.0.42 pyha770c72_0 conda-forge
psutil 5.9.8 py310hd125d64_0 conda-forge
pthread-stubs 0.4 h27ca646_1001 conda-forge
ptyprocess 0.7.0 pyhd3deb0d_0 conda-forge
pugixml 1.14 h13dd4ca_0 conda-forge
pure_eval 0.2.2 pyhd8ed1ab_0 conda-forge
py-opencv 4.9.0 py310h784d225_8 conda-forge
pyarrow 15.0.1 pypi_0 pypi
pyct 0.5.0 pypi_0 pypi
pydantic 2.6.3 pypi_0 pypi
pydantic-core 2.16.3 pypi_0 pypi
pygeos 0.14 pypi_0 pypi
pygments 2.17.2 pyhd8ed1ab_0 conda-forge
pynndescent 0.5.11 pyhca7485f_0 conda-forge
pyparsing 3.1.2 pyhd8ed1ab_0 conda-forge
pyproj 3.6.1 pypi_0 pypi
python 3.10.13 h2469fbe_1_cpython conda-forge
python-dateutil 2.9.0.post0 pypi_0 pypi
python-igraph 0.11.4 py310hae504d7_0 conda-forge
python-tzdata 2024.1 pyhd8ed1ab_0 conda-forge
python_abi 3.10 4_cp310 conda-forge
pytz 2024.1 pyhd8ed1ab_0 conda-forge
pyyaml 6.0.1 pypi_0 pypi
pyzmq 25.1.2 py310hbb13138_0 conda-forge
readfcs 1.1.7 pypi_0 pypi
readline 8.2 h1a28f6b_0
requests 2.31.0 pypi_0 pypi
rich 13.7.1 pypi_0 pypi
s3fs 2023.6.0 pypi_0 pypi
scanpy 1.9.8 pyhd8ed1ab_0 conda-forge
scikit-image 0.22.0 pypi_0 pypi
scikit-learn 1.4.1.post1 py310h7ef31dd_0 conda-forge
scipy 1.12.0 py310hf4b343e_2 conda-forge
seaborn 0.13.2 hd8ed1ab_0 conda-forge
seaborn-base 0.13.2 pyhd8ed1ab_0 conda-forge
session-info 1.0.0 pyhd8ed1ab_0 conda-forge
setuptools 68.2.2 py310hca03da5_0
shapely 2.0.3 pypi_0 pypi
six 1.16.0 pyh6c4a22f_0 conda-forge
slicerator 1.1.0 pypi_0 pypi
snappy 1.1.10 h313beb8_1
sortedcontainers 2.4.0 pypi_0 pypi
spatial-image 0.3.0 pypi_0 pypi
spatialdata 0.0.16.dev43+ge359d2e pypi_0 pypi
spatialdata-io 0.0.10.dev164+g829519d pypi_0 pypi
spatialdata-plot 0.1.1.dev1+g5965a3e pypi_0 pypi
sqlite 3.41.2 h80987f9_0
squidpy 1.4.1 pypi_0 pypi
stack_data 0.6.2 pyhd8ed1ab_0 conda-forge
statsmodels 0.14.1 py310h50ce23c_0 conda-forge
stdlib-list 0.10.0 pyhd8ed1ab_0 conda-forge
svt-av1 1.8.0 h463b476_0 conda-forge
tbb 2021.11.0 h2ffa867_1 conda-forge
tblib 3.0.0 pypi_0 pypi
texttable 1.7.0 pyhd8ed1ab_0 conda-forge
threadpoolctl 3.3.0 pyhc1e730c_0 conda-forge
tifffile 2024.2.12 pypi_0 pypi
tk 8.6.13 h5083fa2_1 conda-forge
toolz 0.12.1 pypi_0 pypi
tornado 6.4 py310hd125d64_0 conda-forge
tqdm 4.66.2 pyhd8ed1ab_0 conda-forge
traitlets 5.14.1 pyhd8ed1ab_0 conda-forge
typing_extensions 4.10.0 pyha770c72_0 conda-forge
tzdata 2024a h04d1e81_0
umap-learn 0.5.5 py310hbe9552e_1 conda-forge
unicodedata2 15.1.0 py310h2aa6e3c_0 conda-forge
urllib3 1.26.18 pypi_0 pypi
validators 0.22.0 pypi_0 pypi
wcwidth 0.2.13 pyhd8ed1ab_0 conda-forge
wheel 0.41.2 py310hca03da5_0
wrapt 1.16.0 pypi_0 pypi
x264 1!164.3095 h57fd34a_2 conda-forge
x265 3.5 hbc6ce65_3 conda-forge
xarray 2023.12.0 pypi_0 pypi
xarray-dataclasses 1.7.0 pypi_0 pypi
xarray-datatree 0.0.14 pypi_0 pypi
xarray-schema 0.0.3 pypi_0 pypi
xarray-spatial 0.3.7 pypi_0 pypi
xorg-libxau 1.0.11 hb547adb_0 conda-forge
xorg-libxdmcp 1.1.3 h27ca646_0 conda-forge
xz 5.4.6 h80987f9_0
yarl 1.9.4 pypi_0 pypi
zarr 2.17.1 pypi_0 pypi
zeromq 4.3.5 hebf3989_1 conda-forge
zict 3.0.0 pypi_0 pypi
zipp 3.17.0 pyhd8ed1ab_0 conda-forge
zlib 1.2.13 h53f4e23_5 conda-forge
zstd 1.5.5 h4f39d0f_0 conda-forge

@Bo-UT
Copy link
Author

Bo-UT commented Mar 8, 2024

Both spatialdata-io and spatialdata-plot are latest from main. Thanks!

Screenshot 2024-03-08 at 12 19 19 PM

@LucaMarconato
Copy link
Member

LucaMarconato commented Mar 9, 2024

Thanks for the update. Answering point by point.

Here is the data from cropped.images. The c is just 0.

This is actually correct, because the initial image has one channel, so there is only c=0 as valid coordinate.

cropped.pl.render_images('morphology_mip').pl.show(), but no data show up in the plot.

Below I will suggest a way to make the plot being shown. After this, can you please check if the plot is empty also with the new installation? If spatialdata-plot doesn't work, you can also use napari-spatialdata, but still after following the recommendation explained below (save to Zarr and reload the data).

I see they're deprecated from polygon_query. Can you please let me know how to get them back?

The new APIs doesn't require to have points=True, shapes=True as they are considered by default. If you want to exclude them you can subset the SpatialData object (this can be done conveniently with the new subset() API); but here you want to include them so it should work.

But it looks the shapes and points are not included

This is unexpected. Can you please share some code/data to reproduce? We used queries to crop Xenium data multiple times and we didn't have problems. A reproducible example would help understand what is happening please.

Then I tried to plot the image
croppedRegion.pl.render_images('morphology_mip').pl.show()

but got a ValueError

The problem may be due to using the raw data with Dask. Please try to save the data with sdata.write() and then reload it with spatialdta.read_zarr().

  • We will make the info that data should be saved to Zarr and reloaded to take advantage of the performant and general storage format, more visible in the tutorial.

@Bo-UT
Copy link
Author

Bo-UT commented Mar 11, 2024

Thanks so much for clarification!
I used the data from 10x website Xenium Output Bundle.

I selected a region (circled). The coordinates are coordinates.csv

Screenshot 2024-03-10 at 8 53 37 PM

For installation,
pip install git+https://github.com/scverse/spatialdata
pip install git+https://github.com/scverse/spatialdata-io.git@main
pip install git+https://github.com/scverse/spatialdata-plot.git@main

For polygon-query

selected_region = pd.read_csv('/path/to/coordinates.csv',skiprows=[0,1])
_X = selected_region['X']
_Y = selected_region['Y']
coords = [(x,y) for x, y in zip(_X, _Y)]

from shapely.geometry import MultiPolygon, Polygon
polygon = Polygon(coords)

croppedRegion = polygon_query(
       spdata_test,
       polygon,
       target_coordinate_system="global",
   )

Looks points and shapes are still not included.

Screenshot 2024-03-10 at 10 03 35 PM

I tried to save it to zarr,
croppedRegion.write('/path/to/cropped_region.zarr')

but I got a ValueError.

Screenshot 2024-03-10 at 9 10 13 PM Screenshot 2024-03-10 at 9 10 47 PM Screenshot 2024-03-10 at 9 11 01 PM Screenshot 2024-03-10 at 9 11 12 PM

Thanks so much of your help!!

Bo

@LucaMarconato
Copy link
Member

Thank you for the additional details. I have tried visualizing the data using napari and I get this (the polygon is the one in red):
image

The reason why the polygon is off (and thus doesn't overlap any shape) is that the polygon coordinates that are saved using Xenium Explorer are in a different coordinate system. The fix is very easy, and is implemented in this function:

def xenium_explorer_selection(path: str | Path, pixel_size: float = 0.2125) -> Polygon:
.
The function is not very visible, apologies for that; @quentinblampey and I curated a new tutorial for Xenium data that uses this function and shows additional use cases. I'll add a link of the tutorial to the docs.

The rechunking behavior is instead a bug; a quick fix is to use this code after the query operation:

from spatialdata._utils import multiscale_spatial_image_from_data_tree
from dask.array.core import Array as DaskArray
from multiscale_spatial_image import MultiscaleSpatialImage


def rechunk(msi: MultiscaleSpatialImage) -> DaskArray:
    for scale in msi.keys():
        msi[scale]["image"].data = msi[scale]["image"].data.rechunk()


rechunk(query_result["morphology_focus"])
rechunk(query_result["morphology_mip"])

I will open a PR for that.

@Bo-UT
Copy link
Author

Bo-UT commented Mar 13, 2024

This is very helpful. Thanks a lot!

I assume this should work. My current notebook kernel died and I tried to reinstall spatial data-io,
pip install git+https://github.com/scverse/spatialdata-io.git@main, but encountered an error

NotImplementedError NotImplementedError Traceback (most recent call last) Cell In[4], [line 1](vscode-notebook-cell:?execution_count=4&line=1) ----> [1](vscode-notebook-cell:?execution_count=4&line=1) spdata_test = spatialdata_io.xenium('[/Users/bzhao2/Downloads/Xenium_V1_hPancreas_Cancer_Add_on_FFPE_outs](https://file+.vscode-resource.vscode-cdn.net/Users/bzhao2/Downloads/Xenium_V1_hPancreas_Cancer_Add_on_FFPE_outs)') [2](vscode-notebook-cell:?execution_count=4&line=2) polygon = spatialdata_io.xenium_explorer_selection("[/Users/bzhao2/Downloads/selected_region/coordinates.csv](https://file+.vscode-resource.vscode-cdn.net/Users/bzhao2/Downloads/selected_region/coordinates.csv)") [4](vscode-notebook-cell:?execution_count=4&line=4) query_sdata = sdata_test.query.polygon(polygon, target_coordinate_system="global")

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata_io/_utils.py:46, in deprecation_alias..deprecation_decorator..wrapper(*args, **kwargs)
44 class_name = f.qualname
45 rename_kwargs(f.name, kwargs, aliases, class_name)
---> 46 return f(*args, **kwargs)

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata_io/readers/xenium.py:132, in xenium(path, n_jobs, cells_as_circles, nucleus_boundaries, transcripts, morphology_mip, morphology_focus, imread_kwargs, image_models_kwargs)
130 points = {}
131 if transcripts:
--> 132 points["transcripts"] = _get_points(path, specs)
134 images = {}
135 if morphology_mip:

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata_io/readers/xenium.py:189, in _get_points(path, specs)
184 table["feature_name"] = table["feature_name"].apply(
185 lambda x: x.decode("utf-8") if isinstance(x, bytes) else str(x), meta=("feature_name", "object")
186 )
188 transform = Scale([1.0 / specs["pixel_size"], 1.0 / specs["pixel_size"]], axes=("x", "y"))
--> 189 points = PointsModel.parse(
190 table,
191 coordinates={"x": XeniumKeys.TRANSCRIPTS_X, "y": XeniumKeys.TRANSCRIPTS_Y, "z": XeniumKeys.TRANSCRIPTS_Z},
192 feature_key=XeniumKeys.FEATURE_NAME,
193 instance_key=XeniumKeys.CELL_ID,
194 transformations={"global": transform},
195 )
196 return points

File ~/miniconda3/envs/single-cell/lib/python3.10/functools.py:926, in singledispatchmethod.get.._method(*args, **kwargs)
924 def _method(*args, **kwargs):
925 method = self.dispatcher.dispatch(args[0].class)
--> 926 return method.get(obj, cls)(*args, **kwargs)

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata/models/models.py:507, in PointsModel.parse(cls, data, **kwargs)
469 @singledispatchmethod
470 @classmethod
471 def parse(cls, data: Any, **kwargs: Any) -> DaskDataFrame:
472 """
473 Validate (or parse) points data.
474
(...)
505 :class:dask.dataframe.core.DataFrame
506 """
--> 507 raise NotImplementedError()

NotImplementedError:

The versions are
spatialdata-io 0.0.15
spatialdata-io 0.0.10.dev164+g829519d
dask 2024.3.0

I created a new Conda environment, but the errors persists. It seems the author of dask just released this new version 2024.3.0 yesterday. I am not sure if this causes the error, as the installation worked well several days before.

I downgraded dask to 2024.2.1, and import spatialdata_io, but another error emerged,

ValueError
NotImplementedError Traceback (most recent call last)
Cell In[4], line 1
----> 1 spdata_test = spatialdata_io.xenium('/Users/bzhao2/Downloads/Xenium_V1_hPancreas_Cancer_Add_on_FFPE_outs')
2 polygon = spatialdata_io.xenium_explorer_selection("/Users/bzhao2/Downloads/selected_region/coordinates.csv")
4 query_sdata = sdata_test.query.polygon(polygon, target_coordinate_system="global")

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata_io/_utils.py:46, in deprecation_alias..deprecation_decorator..wrapper(*args, **kwargs)
44 class_name = f.qualname
45 rename_kwargs(f.name, kwargs, aliases, class_name)
---> 46 return f(*args, **kwargs)

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata_io/readers/xenium.py:132, in xenium(path, n_jobs, cells_as_circles, nucleus_boundaries, transcripts, morphology_mip, morphology_focus, imread_kwargs, image_models_kwargs)
130 points = {}
131 if transcripts:
--> 132 points["transcripts"] = _get_points(path, specs)
134 images = {}
135 if morphology_mip:

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata_io/readers/xenium.py:189, in _get_points(path, specs)
184 table["feature_name"] = table["feature_name"].apply(
185 lambda x: x.decode("utf-8") if isinstance(x, bytes) else str(x), meta=("feature_name", "object")
186 )
188 transform = Scale([1.0 / specs["pixel_size"], 1.0 / specs["pixel_size"]], axes=("x", "y"))
--> 189 points = PointsModel.parse(
190 table,
191 coordinates={"x": XeniumKeys.TRANSCRIPTS_X, "y": XeniumKeys.TRANSCRIPTS_Y, "z": XeniumKeys.TRANSCRIPTS_Z},
192 feature_key=XeniumKeys.FEATURE_NAME,
193 instance_key=XeniumKeys.CELL_ID,
194 transformations={"global": transform},
195 )
196 return points

File ~/miniconda3/envs/single-cell/lib/python3.10/functools.py:926, in singledispatchmethod.get.._method(*args, **kwargs)
924 def _method(*args, **kwargs):
925 method = self.dispatcher.dispatch(args[0].class)
--> 926 return method.get(obj, cls)(*args, **kwargs)

File ~/miniconda3/envs/single-cell/lib/python3.10/site-packages/spatialdata/models/models.py:507, in PointsModel.parse(cls, data, **kwargs)
469 @singledispatchmethod
470 @classmethod
471 def parse(cls, data: Any, **kwargs: Any) -> DaskDataFrame:
472 """
473 Validate (or parse) points data.
474
(...)
505 :class:dask.dataframe.core.DataFrame
506 """
--> 507 raise NotImplementedError()

NotImplementedError:

Thanks so much for your help!

@LucaMarconato
Copy link
Member

LucaMarconato commented Mar 13, 2024

Exactly, Dask now uses dask_expr as a backend for Dask DataFrames; we haven't updated the repository to use that (as you observed is a very recent change).

Anyway I am a bit surprised that you get the second error after a downgrade. Can you please set a breakpoint/put a print(type(table)) in xenium.py right before calling PointsModel.parse()?

You should get a dask.dataframe.core.DataFrame, but in your case you are getting a different type.

@Bo-UT
Copy link
Author

Bo-UT commented Mar 14, 2024

Thanks for your prompt response.
I made spatialdata-io working by installing dask 2024.2.1 first. Looks the cropped image is square not a circle, but this is very great. Thank you so much for your help!

image
image

@melonora
Copy link
Collaborator

melonora commented Mar 14, 2024

Hi @Bo-UT, we are soon pushing a new release of SpatialData in which for now we have constrained the version of dask being used until we have adjusted the code base to work with the new dask-expr

@LucaMarconato
Copy link
Member

Great to hear that it worked!

Looks the cropped image is square not a circle,
Yes this is the intended behavior; a discussion around this (a bit technical) is in this comment: scverse/spatialdata#151 (comment).

Anyway when the rasterize() APIs will be implemented for vector data, we could add a helper function to compute a raster mask from the polygon so that one can then use this mask to set to 0 the pixels outside the queried region.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants