ckmah / bento-tools

A Python toolkit for subcellular analysis of spatial transcriptomics data
https://bento-tools.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
56 stars 6 forks source link

bento.io.prep KeyError: 'spatialdata_attrs' #143

Open pakiessling opened 1 month ago

pakiessling commented 1 month ago

Hi I am trying to follow the tutorial but I receive the following error with my data:


sdata = bt.io.prep(sdata)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[3], line 2
      1 sdata = sd.read_zarr("kidney_core.zarr")
----> 2 sdata = bt.io.prep(sdata)

File ~/mambaforge/envs/bento/lib/python3.11/site-packages/bento/io/_io.py:70, in prep(sdata, points_key, feature_key, instance_key, shape_keys)
     67         shape_sjoin.append(shape_key)
     69 # Set instance key for points
---> 70 sdata.points[points_key].attrs["spatialdata_attrs"]["instance_key"] = instance_key
     72 pbar = tqdm(total=3)
     73 if len(point_sjoin) > 0:

KeyError: 'spatialdata_attrs'

It seems like my data does not have that attribute:

sdata.points["transcripts"].attrs
{'transform': {'global': Scale (x, y, z)
      [4.70588235 4.70588235 1.   ]}}
-----
bento                2.1.2
dask                2024.7.0
matplotlib          3.9.1
session_info        1.0.0
spatialdata         0.2.1
-----
IPython             8.26.0
jupyter_client      8.6.2
jupyter_core        5.7.2
-----
Python 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:34:54) [Clang 16.0.6 ]
macOS-14.4.1-arm64-arm-64bit
ckmah commented 1 month ago

For now, add this line before calling bt.io.prep(sdata), assuming "transcripts" is the key for your point coordinates:

sdata.points["transcripts"].attrs["spatialdata_attrs"] = {}

I will make sure this is addressed in the next version, thanks for catching this.