BiomedicalMachineLearning / stLearn

A novel machine learning pipeline to analyse spatial transcriptomics data
Other
190 stars 25 forks source link

Problem with creating "stlearn object" #232

Closed suvranath1 closed 1 year ago

suvranath1 commented 1 year ago

Hello,

I am trying to create my own "stlearn" object with my own spatial spot count matrix and associated 3D coordinates (x,y and z). (also with 2D coordinates) However, when I try to create the "stlearn" object as per the tutorial mentioned here: https://stlearn.readthedocs.io/en/latest/tutorials/Read_any_data.html as

import stlearn as st adata = st.create_stlearn(count=a,spatial=b,library_id="Th",scale=1, background_color="white")

It is giving me following error:

KeyError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key) 3651 try: -> 3652 return self._engine.get_loc(casted_key) 3653 except KeyError as err:

~\Anaconda3\lib\site-packages\pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

~\Anaconda3\lib\site-packages\pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'imagecol'

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

KeyError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_19372\2878763128.py in 1 import stlearn as st ----> 2 adata = st.create_stlearn(count=a,spatial=b,library_id="Th",scale=1, background_color="white")

~\Anaconda3\lib\site-packages\stlearn\wrapper\read.py in create_stlearn(count, spatial, library_id, image_path, scale, quality, spot_diameter_fullres, background_color) 634 scale = 2000 / max_coor 635 --> 636 adata.obs["imagecol"] = spatial["imagecol"].values scale 637 adata.obs["imagerow"] = spatial["imagerow"].values scale 638

~\Anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key) 3759 if self.columns.nlevels > 1: 3760 return self._getitem_multilevel(key) -> 3761 indexer = self.columns.get_loc(key) 3762 if is_integer(indexer): 3763 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key) 3652 return self._engine.get_loc(casted_key) 3653 except KeyError as err: -> 3654 raise KeyError(key) from err 3655 except TypeError: 3656 # If we have a listlike key, _check_indexing_error will raise

KeyError: 'imagecol'

I am pretty beginner with coding with python and any suggestions would be immensely helpful to me.

Thanks, Suvra

duypham2108 commented 1 year ago

Can you make sure the column in your b variable as a data frame contains the "imagecol" and "imagerow" columns? If it's not working, you can send me your code here and print the a and b here

suvranath1 commented 1 year ago

Hello Duy Pham,

Thanks for your reply.

My count matrix looks like this:

image

And my coordinate matrix looks like this after changing my coordinate matrix’s x and y column with “imagecol” and “imagerow” respectively.

image

Now I can execute the command, but facing following issue:

Thank you so much for your suggestions & help.

Regards, Suvra

duypham2108 commented 1 year ago

For the first issue, you can add this line before you run the plotting function

%matplotlib inline

For the second question, we currently do not support 3D visualization. I recommend you use plotly for this purpose, they have a built-in function for 3D scatter plot. You can check it here https://plotly.com/python/3d-scatter-plots/

suvranath1 commented 1 year ago

Hello Duy Pham,

It works now. Thank you once again for your suggestions!

Regards, Suvra