Teichlab / TissueTag

Python package to interactively annotate histological images within a jupyter notebook
BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

Error returned for "map_annotations_to_target" function #22

Closed bryanrhelm closed 3 months ago

bryanrhelm commented 3 months ago

Hello, Very interesting tool that you've built here. I'm currently working to implement this tool on some thymic samples for a group that has done Visium sequencing of human/mouse samples, and I've run into some issues that I was wondering if you could help with.

The main issue that I'm having (and haven't been able to resolve) is that the "map_annotations_to_target" function that is demoed in the example analyses does not work when I've implemented it on my own samples. I'm running the code directly from your example in the label transfer demo. In Jupyter, the function successfully plots the areas that I'm trying to map, but it returns this error:

"--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[79], line 5 3 _,ppm_vis,vis_df = tt.read_visium(spaceranger_dir_path=path,plot=False,in_tissue=True,header=0,use_resolution="hires") 4 vis_df.rename(columns={'pxl_col': "x", 'pxl_row': "y"},inplace=True) # for new spaceranger ----> 5 vis_df = tt.map_annotations_to_target(
6 df_target=vis_df,
7 df_source=df_anno, 8 ppm_target= ppm_vis, 9 plot=True, 10 how="nearest" 11 ) 12 vis_df

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\tissue_tag\tissue_tag.py:1737, in map_annotations_to_target(df_source, df_target, ppm_target, ppm_source, plot, how, max_distance) 1734 print('Migrating source annotation - ' + k + ' to target space.') 1736 # Interpolation -> 1737 df_target[k] = griddata(points=a.T, values=df_source[k], xi=b.T, method=how) 1739 # Create KDTree 1740 tree = cKDTree(a.T)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\interpolate_ndgriddata.py:321, in griddata(points, values, xi, method, fill_value, rescale) 319 elif method == 'nearest': 320 ip = NearestNDInterpolator(points, values, rescale=rescale) --> 321 return ip(xi) 322 elif method == 'linear': 323 ip = LinearNDInterpolator(points, values, fill_value=fill_value, 324 rescale=rescale)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\interpolate_ndgriddata.py:158, in NearestNDInterpolator.call(self, *args, **query_options) 155 else: 156 interp_values = np.full(interp_shape, np.nan) --> 158 interp_values[valid_mask] = self.values[i[valid_mask], ...] 160 if self.values.ndim > 1: 161 new_shape = original_shape[:-1] + self.values.shape[1:]

ValueError: could not convert string to float: 'Cortex'"

I'm at a loss about how to resolve this and any help would be appreciated! Thanks! B

nadavyayon commented 3 months ago

Hey thanks for trying this out!

Yes I also stumbled upon this recently and I'm investigating the cause. Probably a version thing. In the meantime what worked for me is changing the categories to integers (1,2,3..) and after migration assigning them their original names. I know this is far from ideal but hopefully a partial fix until I fix it!

bryanrhelm commented 3 months ago

Hi Nadav, Thanks for looking into this, and I'm glad that it wasn't just on my end. I've tried changing the factors to integers, but I'm not able to get the code to work properly. I was wondering if you could provide an example script of your solution? (Python is not my typical coding language, so I'm a little daft on customizing it on my own.)

nadavyayon commented 3 months ago

Hey, sorry about that we are working on a new version that will correct this issue but i'm currently on AL so will not be ready until a couple of weeks probably. in the meantime this is how I bypass this, i hope this works for you!


df_anno is the tissue annotation grid from tissuetag

adata is the visium anndata object

vis_df = pd.DataFrame(adata.obsm['spatial']) # get spatial coordinates from visium
vis_df.rename(columns={0: "x", 1: "y"},inplace=True) # for new spaceranger
vis_df = tt.map_annotations_to_target(
df_target=vis_df,
df_source=df_anno.drop(columns='annotations'),# remove the column with categories ppm_target=ppm, plot=True, ) # map annotations

now lets map the annotations back to the visium object

vis_df.index = adata.obs.index annotation_dict = df_anno.set_index('annotations_number')['annotations'].to_dict() # you should have for any annotation that is generated with tissuetag this extra "_number" column that you can use to map the annotations if not you can generate this dictionary manually vis_df['annotations'] = vis_df['annotations_number'].map(annotation_dict) # adding back names adata.obs = pd.concat([adata.obs,vis_df.iloc[:,2:]],axis=1) # add new annotations back to the visium object

bryanrhelm commented 3 months ago

Hi! Thank you for sharing that code--I was able to get it to label appropriately with the code you shared. I ended up doing it slightly differently because I mostly want the barcodes + labels + cma for import into Seurat, but your example above definitely helped me get there.

Rather than importing a whole anndata object, I used the info that was already imported with the tissue_tag.read_visium function (part of full script)

df is the dataframe imported by 'tt.read_visium' in the main tutorial

vis_df = df[['pxl_col','pxl_row']] # get spatial coordinates from visium vis_df = vis_df.rename(columns={'pxl_col':"x",'pxl_row':"y"}) # Rename locations from hires vis_df = tt.map_annotations_to_target( df_target=vis_df, df_source=df_anno.drop(columns='annotations'),# remove the column with categories ppm_target=ppm_anno, #ppm_anno from tt.read_visium plot=True, ) # map annotations

I then directly remapped labels using your code from above and exported: annotation_dict = df_anno.set_index('annotations_number')['annotations'].to_dict() # annotation dictionary defined as in tutorial vis_df['annotations'] = vis_df['annotations_number'].map(annotation_dict) # adding back names vis_df.to_csv("test.csv")

From there imported into Seurat and worked like a charm! Very appreciative for your help on this! B

nadavyayon commented 3 months ago

amazing! and thanks for not giving up! I will try to fix this in the next version and also simplify some of the steps which are probably an unnecessary complication.

Best

Nadav