From 10b105a20c8c2cfdcf77a187b80e043a2283f405 Mon Sep 17 00:00:00 2001 From: duypham2108 Date: Wed, 8 Mar 2023 10:32:36 +1000 Subject: [PATCH] fix: cci and interactive plot and type matplotlib --- stlearn/plotting/cci_plot.py | 5 ++-- stlearn/plotting/cci_plot_helpers.py | 8 +++++-- stlearn/plotting/classes.py | 14 ++++++------ stlearn/plotting/classes_bokeh.py | 34 ++++++++++++++-------------- stlearn/plotting/cluster_plot.py | 2 +- stlearn/plotting/feat_plot.py | 2 +- stlearn/plotting/gene_plot.py | 2 +- stlearn/utils.py | 2 +- stlearn/wrapper/read.py | 4 ++++ 9 files changed, 40 insertions(+), 33 deletions(-) diff --git a/stlearn/plotting/cci_plot.py b/stlearn/plotting/cci_plot.py index 8a87bd18..06d997f8 100644 --- a/stlearn/plotting/cci_plot.py +++ b/stlearn/plotting/cci_plot.py @@ -434,7 +434,7 @@ def lr_result_plot( title: Optional["str"] = None, figsize: Optional[Tuple[float, float]] = None, cmap: Optional[str] = "Spectral_r", - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -919,7 +919,7 @@ def het_plot( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -1069,7 +1069,6 @@ def ccinet_plot( # Either plotting overall interactions, or just for a particular LR # int_df, title = get_int_df(adata, lr, use_label, sig_interactions, title) - # Creating the interaction graph # all_set = int_df.index.values int_matrix = int_df.values diff --git a/stlearn/plotting/cci_plot_helpers.py b/stlearn/plotting/cci_plot_helpers.py index f282d9f6..a4554ecd 100644 --- a/stlearn/plotting/cci_plot_helpers.py +++ b/stlearn/plotting/cci_plot_helpers.py @@ -417,19 +417,23 @@ def add_arrows_by_edges( def get_int_df(adata, lr, use_label, sig_interactions, title): """Retrieves the relevant interaction count matrix.""" no_title = type(title) == type(None) + labels_ordered = adata.obs[use_label].cat.categories if type(lr) == type(None): # No LR inputted, so just use all int_df = ( adata.uns[f"lr_cci_{use_label}"] if sig_interactions else adata.uns[f"lr_cci_raw_{use_label}"] - ) + )[labels_ordered].loc[labels_ordered] title = "Cell-Cell LR Interactions" if no_title else title else: + + labels_ordered = adata.obs[use_label].cat.categories int_df = ( adata.uns[f"per_lr_cci_{use_label}"][lr] if sig_interactions else adata.uns[f"per_lr_cci_raw_{use_label}"][lr] - ) + )[labels_ordered].loc[labels_ordered] + title = f"Cell-Cell {lr} interactions" if no_title else title return int_df, title diff --git a/stlearn/plotting/classes.py b/stlearn/plotting/classes.py index 91bd3f9f..f2c4a073 100644 --- a/stlearn/plotting/classes.py +++ b/stlearn/plotting/classes.py @@ -41,7 +41,7 @@ def __init__( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -244,7 +244,7 @@ def __init__( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -457,7 +457,7 @@ def __init__( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -627,7 +627,7 @@ def __init__( cmap: Optional[str] = "default", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -981,7 +981,7 @@ def __init__( cmap: Optional[str] = "jet", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -1135,7 +1135,7 @@ def __init__( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, @@ -1205,7 +1205,7 @@ def __init__( figsize: Optional[Tuple[float, float]] = None, cmap: Optional[str] = "Spectral_r", list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, diff --git a/stlearn/plotting/classes_bokeh.py b/stlearn/plotting/classes_bokeh.py index cdbc5881..7a75b2df 100644 --- a/stlearn/plotting/classes_bokeh.py +++ b/stlearn/plotting/classes_bokeh.py @@ -75,7 +75,7 @@ def __init__( view = self.image.view(dtype=np.uint8).reshape((self.ydim, self.xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view[:, :, :] = np.flipud(np.asarray(img_pillow)) + view[:, :, :] = np.asarray(img_pillow) # Display the 32-bit RGBA image self.dim = max(self.xdim, self.ydim) @@ -159,7 +159,7 @@ def make_fig(self): fig = figure( title=self.gene_select.value, - x_range=(0, self.dim - 150), + x_range=(0, self.dim), y_range=(self.dim, 0), output_backend=self.output_backend.value, name="GenePlot", @@ -173,7 +173,7 @@ def make_fig(self): fig.image_rgba( image=[self.image], x=0, - y=self.xdim, + y=0, dw=self.ydim, dh=self.xdim, global_alpha=self.tissue_alpha.value, @@ -251,11 +251,11 @@ def add_violin(self): view2 = image2.view(dtype=np.uint8).reshape((ydim, xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view2[:, :, :] = np.flipud(np.asarray(img_pillow2)) + view2[:, :, :] = np.asarray(img_pillow2) p = figure( - plot_width=910, - plot_height=int(910 / xdim * ydim) + 5, + width=910, + height=int(910 / xdim * ydim) + 5, output_backend=self.output_backend.value, ) @@ -346,7 +346,7 @@ def __init__( view = self.image.view(dtype=np.uint8).reshape((self.ydim, self.xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view[:, :, :] = np.flipud(np.asarray(img_pillow)) + view[:, :, :] = np.asarray(img_pillow) # Display the 32-bit RGBA image self.dim = max(self.xdim, self.ydim) @@ -556,7 +556,7 @@ def make_fig(self): fig.image_rgba( image=[self.image], x=0, - y=self.xdim, + y=0, dw=self.ydim, dh=self.xdim, global_alpha=self.tissue_alpha.value, @@ -682,11 +682,11 @@ def add_dea(self): view2 = image2.view(dtype=np.uint8).reshape((ydim, xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view2[:, :, :] = np.flipud(np.asarray(img_pillow2)) + view2[:, :, :] = np.asarray(img_pillow2) p = figure( - plot_width=910, - plot_height=int(910 / xdim * ydim) + 5, + width=910, + height=int(910 / xdim * ydim) + 5, output_backend=self.output_backend.value, ) @@ -796,7 +796,7 @@ def __init__( view = self.image.view(dtype=np.uint8).reshape((self.ydim, self.xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view[:, :, :] = np.flipud(np.asarray(img_pillow)) + view[:, :, :] = np.asarray(img_pillow) # Display the 32-bit RGBA image self.dim = max(self.xdim, self.ydim) @@ -883,7 +883,7 @@ def make_fig(self): fig.image_rgba( image=[self.image], x=0, - y=self.xdim, + y=0, dw=self.ydim, dh=self.xdim, global_alpha=self.tissue_alpha.value, @@ -975,7 +975,7 @@ def __init__( view = self.image.view(dtype=np.uint8).reshape((self.ydim, self.xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view[:, :, :] = np.flipud(np.asarray(img_pillow)) + view[:, :, :] = np.asarray(img_pillow) # Display the 32-bit RGBA image self.dim = max(self.xdim, self.ydim) @@ -1072,7 +1072,7 @@ def make_fig(self): fig.image_rgba( image=[self.image], x=0, - y=self.xdim, + y=0, dw=self.ydim, dh=self.xdim, global_alpha=self.tissue_alpha.value, @@ -1264,7 +1264,7 @@ def __init__( view = self.image.view(dtype=np.uint8).reshape((self.ydim, self.xdim, 4)) # Copy the RGBA image into view, flipping it so it comes right-side up # with a lower-left origin - view[:, :, :] = np.flipud(np.asarray(img_pillow)) + view[:, :, :] = np.asarray(img_pillow) # Display the 32-bit RGBA image self.dim = max(self.xdim, self.ydim) @@ -1320,7 +1320,7 @@ def make_fig(self): fig.image_rgba( image=[self.image], x=0, - y=self.xdim, + y=0, dw=self.ydim, dh=self.xdim, global_alpha=self.tissue_alpha.value, diff --git a/stlearn/plotting/cluster_plot.py b/stlearn/plotting/cluster_plot.py index e4d1be19..0cd6645a 100644 --- a/stlearn/plotting/cluster_plot.py +++ b/stlearn/plotting/cluster_plot.py @@ -30,7 +30,7 @@ def cluster_plot( cmap: Optional[str] = "default", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, diff --git a/stlearn/plotting/feat_plot.py b/stlearn/plotting/feat_plot.py index 1352cc6e..7df51516 100644 --- a/stlearn/plotting/feat_plot.py +++ b/stlearn/plotting/feat_plot.py @@ -35,7 +35,7 @@ def feat_plot( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, diff --git a/stlearn/plotting/gene_plot.py b/stlearn/plotting/gene_plot.py index cd074aa1..c755d12b 100644 --- a/stlearn/plotting/gene_plot.py +++ b/stlearn/plotting/gene_plot.py @@ -33,7 +33,7 @@ def gene_plot( cmap: Optional[str] = "Spectral_r", use_label: Optional[str] = None, list_clusters: Optional[list] = None, - ax: Optional[matplotlib.axes._subplots.Axes] = None, + ax: Optional[matplotlib.axes.Axes] = None, fig: Optional[matplotlib.figure.Figure] = None, show_plot: Optional[bool] = True, show_axis: Optional[bool] = False, diff --git a/stlearn/utils.py b/stlearn/utils.py index 97180035..0ea54262 100644 --- a/stlearn/utils.py +++ b/stlearn/utils.py @@ -26,7 +26,7 @@ class Empty(Enum): from abc import ABC -class _AxesSubplot(Axes, axes.SubplotBase, ABC): +class _AxesSubplot(Axes, axes.SubplotBase): """Intersection between Axes and SubplotBase: Has methods of both""" diff --git a/stlearn/wrapper/read.py b/stlearn/wrapper/read.py index 3bbcac4d..89a96f43 100644 --- a/stlearn/wrapper/read.py +++ b/stlearn/wrapper/read.py @@ -193,6 +193,10 @@ def Read10X( adata.obs["imagerow"] = image_coor[:, 1] adata.uns["spatial"][library_id]["use_quality"] = quality + adata.obs["array_row"] = adata.obs["array_row"].astype(int) + adata.obs["array_col"] = adata.obs["array_col"].astype(int) + adata.obsm["spatial"] = adata.obsm["spatial"].astype("int64") + return adata