labsyspharm / scimap

Spatial Single-Cell Analysis Toolkit
https://scimap.xyz/
MIT License
74 stars 26 forks source link

return_data not working for plots #115

Open LukasHats opened 2 months ago

LukasHats commented 2 months ago

Dear @ajitjohnson ,

after running:

for add in adata_list:
    for r in [30, 50, 75, 100]:
        sm.tl.spatial_interaction (add, 
                                method='radius', 
                                radius=r, 
                                label='spatial_interaction_r'+str(r),
                                phenotype='Phenotype2',
                                imageid='image_ID',
                                permutation=500,
                                )

and plotting with:

int_r50 = sm.pl.spatial_interaction (adata_mmnobd,
                           spatial_interaction='spatial_interaction_r50',
                           summarize_plot=True,
                           binary_view=False,
                           return_data=True,
)

I get the error message:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[27], [line 1](vscode-notebook-cell:?execution_count=27&line=1)
----> [1](vscode-notebook-cell:?execution_count=27&line=1) sm.pl.spatial_interaction (adata_mmnobd,
      [2](vscode-notebook-cell:?execution_count=27&line=2)                            spatial_interaction='spatial_interaction_r50',
      [3](vscode-notebook-cell:?execution_count=27&line=3)                            summarize_plot=True,
      [4](vscode-notebook-cell:?execution_count=27&line=4)                            binary_view=False,
      [5](vscode-notebook-cell:?execution_count=27&line=5)                            return_data=True,
      [6](vscode-notebook-cell:?execution_count=27&line=6) )

File ~/miniforge3/envs/scimap/lib/python3.10/site-packages/scimap/plotting/spatial_interaction.py:309, in spatial_interaction(adata, spatial_interaction, summarize_plot, p_val, row_cluster, col_cluster, cmap, nonsig_color, subset_phenotype, subset_neighbour_phenotype, binary_view, return_data, fileName, saveDir, **kwargs)
    [307](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/scimap/plotting/spatial_interaction.py:307) p_val_data.reset_index(inplace=True)
    [308](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/scimap/plotting/spatial_interaction.py:308) # remove the first two colums
--> [309](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/scimap/plotting/spatial_interaction.py:309) map_data = map_data.drop(['phenotype', 'neighbour_phenotype'], axis=1)
    [310](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/scimap/plotting/spatial_interaction.py:310) p_val_data = p_val_data.drop(['phenotype', 'neighbour_phenotype'], axis=1)
    [311](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/scimap/plotting/spatial_interaction.py:311) p_val_data.columns = map_data.columns

File ~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5581, in DataFrame.drop(self, labels, axis, index, columns, level, inplace, errors)
   [5433](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5433) def drop(
   [5434](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5434)     self,
   [5435](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5435)     labels: IndexLabel | None = None,
   (...)
   [5442](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5442)     errors: IgnoreRaise = "raise",
   [5443](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5443) ) -> DataFrame | None:
   [5444](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/frame.py:5444)     """
...
-> [7070](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/indexes/base.py:7070)         raise KeyError(f"{labels[mask].tolist()} not found in axis")
   [7071](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/indexes/base.py:7071)     indexer = indexer[~mask]
   [7072](https://file+.vscode-resource.vscode-cdn.net/Users/lukashat/Documents/PhD_Schapiro/Projects/Myeloma_Standal/github/myeloma_standal/src/downstream/~/miniforge3/envs/scimap/lib/python3.10/site-packages/pandas/core/indexes/base.py:7072) return self.delete(indexer)

KeyError: "['neighbour_phenotype'] not found in axis"
Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?ab828ad5-d8f5-44e1-ade2-ca62d1570c80) or open in a [text editor](command:workbench.action.openLargeOutput?ab828ad5-d8f5-44e1-ade2-ca62d1570c80). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...

I also realized that since a version change from 2.05, it is no longer possible to get the axis from the figure object that scimap produces. IIRC on 2.05 I could plot and access axes and modify labels like this:

sm.pl.spatial_distance (adata, 
                        imageid='disease2', 
                        phenotype='Phenotype2',
                        method='numeric',
                        distance_from='Osteoclasts',
                        distance_to= ['CD8+Tcells', 'Plasma Cells/MM cells', 'CD4+Tcells'],
                        x_axis='group',
                        y_axis='distance',
                        plot_type='violin',
                        facet_by='imageid',
                        #log=True,
                        )
fig = plt.gcf()
for ax in fig.axes:
    ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')

This now just results in: <Figure size 640x480 with 0 Axes>

Thanks in advance for your help!

ajitjohnson commented 2 months ago

hi @LukasHats I am currently in the middle of working with grant deadlines. I can only investigate this mid October. If you are able to troubleshoot it in the mean time, would be happy to look through a PR. thank you.