From 1c8b40c0f656f777083f6aaf9c6d0e3de6037e44 Mon Sep 17 00:00:00 2001 From: duypham2108 Date: Tue, 19 Mar 2024 01:07:08 +0000 Subject: [PATCH] fix: fix several bugs --- stlearn/image_preprocessing/segmentation.py | 2 +- stlearn/plotting/cci_plot_helpers.py | 19 +++++++++++++++---- stlearn/plotting/classes.py | 2 +- stlearn/tools/microenv/cci/base.py | 7 +++---- stlearn/tools/microenv/cci/het.py | 2 +- stlearn/tools/microenv/cci/het_helpers.py | 4 ++-- stlearn/tools/microenv/cci/permutation.py | 4 ++-- stlearn/wrapper/read.py | 7 +------ 8 files changed, 26 insertions(+), 21 deletions(-) diff --git a/stlearn/image_preprocessing/segmentation.py b/stlearn/image_preprocessing/segmentation.py index 2cc0d642..76023058 100644 --- a/stlearn/image_preprocessing/segmentation.py +++ b/stlearn/image_preprocessing/segmentation.py @@ -158,7 +158,7 @@ def _calculate_morph_stats(tile_path): min_nucleus_area = 60 im_nuclei_seg_mask = htk.segmentation.label.area_open( labels, min_nucleus_area - ).astype(np.int) + ).astype(np.int64) # compute nuclei properties objProps = skimage.measure.regionprops(im_nuclei_seg_mask) diff --git a/stlearn/plotting/cci_plot_helpers.py b/stlearn/plotting/cci_plot_helpers.py index a4554ecd..045612e0 100644 --- a/stlearn/plotting/cci_plot_helpers.py +++ b/stlearn/plotting/cci_plot_helpers.py @@ -198,9 +198,11 @@ def rank_scatter( y[ranks_], alpha=alpha, c=highlight_color, - s=None - if type(point_sizes) == type(None) - else (point_sizes[ranks_] ** point_size_exp), + s=( + None + if type(point_sizes) == type(None) + else (point_sizes[ranks_] ** point_size_exp) + ), edgecolors=color, ) ranks = ranks_ if not show_all else ranks @@ -754,7 +756,16 @@ def chordDiagram(X, ax, colors=None, width=0.1, pad=2, chordwidth=0.7, lim=1.1): # This draws the outter ring # # IdeogramArc(start=start, end=end, radius=1.0, ax=ax, # color=colors[i], width=width) - a = Arc((0, 0), diam, diam, 0, start, end, color=colors[i], lw=10) + a = Arc( + xy=(0, 0), + width=diam, + height=diam, + angle=0, + theta1=start, + theta2=end, + color=colors[i], + lw=10, + ) ax.add_patch(a) start, end = pos[(i, i)] # This draws the paths to itself # diff --git a/stlearn/plotting/classes.py b/stlearn/plotting/classes.py index f2c4a073..e60c7a0e 100644 --- a/stlearn/plotting/classes.py +++ b/stlearn/plotting/classes.py @@ -194,7 +194,7 @@ def _crop_image(self, main_ax: _AxesSubplot, margin: float): def _zoom_image(self, main_ax: _AxesSubplot, zoom_coord: Optional[float]): main_ax.set_xlim(zoom_coord[0], zoom_coord[1]) - main_ax.set_ylim(zoom_coord[2], zoom_coord[3]) + main_ax.set_ylim(zoom_coord[3], zoom_coord[2]) def _add_color_bar(self, plot, color_bar_label: str = ""): cb = plt.colorbar( diff --git a/stlearn/tools/microenv/cci/base.py b/stlearn/tools/microenv/cci/base.py index 1b0afb82..ecea7ecc 100644 --- a/stlearn/tools/microenv/cci/base.py +++ b/stlearn/tools/microenv/cci/base.py @@ -16,7 +16,6 @@ def lr( neighbours: list = None, fast: bool = True, ) -> AnnData: - """Calculate the proportion of known ligand-receptor co-expression among the neighbouring spots or within spots Parameters ---------- @@ -111,7 +110,7 @@ def get_lrs_scores( lrs: np.array lr pairs from the database in format ['L1_R1', 'LN_RN'] """ if type(spot_indices) == type(None): - spot_indices = np.array(list(range(len(adata))), dtype=np.int_) + spot_indices = np.array(list(range(len(adata))), dtype=np.int32) spot_lr1s = get_spot_lrs( adata, lr_pairs=lrs, lr_order=True, filter_pairs=filter_pairs @@ -208,7 +207,7 @@ def calc_neighbours( distance, ) if index: - n_index = np.array(n_index, dtype=np.int_) + n_index = np.array(n_index, dtype=np.int32) neighbours.append(n_index[n_index != i]) else: n_spots = adata.obs_names[n_index] @@ -286,6 +285,7 @@ def lr_pandas( ------- lr_scores: numpy.ndarray Cells*LR-scores. """ + # function to calculate mean of lr2 expression between neighbours or within spot (distance==0) for each spot def mean_lr2(x): # get lr2 expressions from the neighbour(s) @@ -352,7 +352,6 @@ def lr_grid( radius: int = 1, verbose: bool = True, ) -> AnnData: - """Calculate the proportion of known ligand-receptor co-expression among the neighbouring grids or within each grid Parameters ---------- diff --git a/stlearn/tools/microenv/cci/het.py b/stlearn/tools/microenv/cci/het.py index e7d3cffe..bc6fb221 100644 --- a/stlearn/tools/microenv/cci/het.py +++ b/stlearn/tools/microenv/cci/het.py @@ -348,7 +348,7 @@ def get_interactions( A_gene1_sig_bool = np.logical_and(A_gene1_bool, sig_bool) n_true = A_gene1_sig_bool.sum() - A_gene1_sig_indices = np.zeros((1, n_true), dtype=np.int_)[ + A_gene1_sig_indices = np.zeros((1, n_true), dtype=np.int32)[ 0, : ] # np.where(A_gene1_sig_bool)[0] index = 0 diff --git a/stlearn/tools/microenv/cci/het_helpers.py b/stlearn/tools/microenv/cci/het_helpers.py index 8faa2464..270e811c 100644 --- a/stlearn/tools/microenv/cci/het_helpers.py +++ b/stlearn/tools/microenv/cci/het_helpers.py @@ -218,7 +218,7 @@ def get_data_for_counting(adata, use_label, mix_mode, all_set): cell_data = np.zeros((len(cell_labels), len(all_set)), dtype=np.float64) for i, cell_type in enumerate(all_set): cell_data[:, i] = ( - (cell_labels == cell_type).astype(np.int_).astype(np.float64) + (cell_labels == cell_type).astype(np.int32).astype(np.float64) ) spot_bcs = adata.obs_names.values.astype(str) @@ -254,7 +254,7 @@ def get_data_for_counting_OLD(adata, use_label, mix_mode, all_set): cell_data = np.zeros((len(cell_labels), len(all_set)), dtype=np.float64) for i, cell_type in enumerate(all_set): cell_data[:, i] = ( - (cell_labels == cell_type).astype(np.int_).astype(np.float64) + (cell_labels == cell_type).astype(np.int32).astype(np.float64) ) spot_bcs = adata.obs_names.values.astype(str) diff --git a/stlearn/tools/microenv/cci/permutation.py b/stlearn/tools/microenv/cci/permutation.py index 42cda543..6ca6ce12 100644 --- a/stlearn/tools/microenv/cci/permutation.py +++ b/stlearn/tools/microenv/cci/permutation.py @@ -13,6 +13,7 @@ from .merge import merge from .perm_utils import get_lr_features, get_lr_bg + # Newest method # def perform_spot_testing( adata: AnnData, @@ -74,7 +75,7 @@ def perform_spot_testing( ######## Background per LR, but only for spots where LR has a score ######## # Determine the indices of the spots where each LR has a score # cols = ["n_spots", "n_spots_sig", "n_spots_sig_pval"] - lr_summary = np.zeros((lr_scores.shape[1], 3), np.int) + lr_summary = np.zeros((lr_scores.shape[1], 3), np.int32) pvals = np.ones(lr_scores.shape, dtype=np.float64) pvals_adj = np.ones(lr_scores.shape, dtype=np.float64) log10pvals_adj = np.zeros(lr_scores.shape, dtype=np.float64) @@ -349,7 +350,6 @@ def permutation( background: np.array = None, **kwargs, ) -> AnnData: - """Permutation test for merged result Parameters ---------- diff --git a/stlearn/wrapper/read.py b/stlearn/wrapper/read.py index 89a96f43..a66bf512 100644 --- a/stlearn/wrapper/read.py +++ b/stlearn/wrapper/read.py @@ -1,5 +1,6 @@ """Reading and Writing """ + from pathlib import Path, PurePath from typing import Optional, Union from anndata import AnnData @@ -27,7 +28,6 @@ def Read10X( quality: _QUALITY = "hires", image_path: Union[str, Path] = None, ) -> AnnData: - """\ Read Visium data from 10X (wrap read_visium from scanpy) @@ -209,7 +209,6 @@ def ReadOldST( quality: str = "hires", spot_diameter_fullres: float = 50, ) -> AnnData: - """\ Read Old Spatial Transcriptomics data @@ -258,7 +257,6 @@ def ReadSlideSeq( spot_diameter_fullres: float = 50, background_color: _background = "white", ) -> AnnData: - """\ Read Slide-seq data @@ -340,7 +338,6 @@ def ReadMERFISH( spot_diameter_fullres: float = 50, background_color: _background = "white", ) -> AnnData: - """\ Read MERFISH data @@ -423,7 +420,6 @@ def ReadSeqFish( spot_diameter_fullres: float = 50, background_color: _background = "white", ) -> AnnData: - """\ Read SeqFish data @@ -510,7 +506,6 @@ def ReadXenium( spot_diameter_fullres: float = 15, background_color: _background = "white", ) -> AnnData: - """\ Read Xenium data