Skip to content

Commit

Permalink
Merge pull request #289 from BiomedicalMachineLearning/fix_bug/cci
Browse files Browse the repository at this point in the history
fix: fix several bugs of CCI and zoom
  • Loading branch information
duypham2108 authored Mar 19, 2024
2 parents bf91758 + 1c8b40c commit 439692b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion stlearn/image_preprocessing/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions stlearn/plotting/cci_plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 #
Expand Down
2 changes: 1 addition & 1 deletion stlearn/plotting/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions stlearn/tools/microenv/cci/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
----------
Expand Down
2 changes: 1 addition & 1 deletion stlearn/tools/microenv/cci/het.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stlearn/tools/microenv/cci/het_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions stlearn/tools/microenv/cci/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -349,7 +350,6 @@ def permutation(
background: np.array = None,
**kwargs,
) -> AnnData:

"""Permutation test for merged result
Parameters
----------
Expand Down
7 changes: 1 addition & 6 deletions stlearn/wrapper/read.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Reading and Writing
"""

from pathlib import Path, PurePath
from typing import Optional, Union
from anndata import AnnData
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -209,7 +209,6 @@ def ReadOldST(
quality: str = "hires",
spot_diameter_fullres: float = 50,
) -> AnnData:

"""\
Read Old Spatial Transcriptomics data
Expand Down Expand Up @@ -258,7 +257,6 @@ def ReadSlideSeq(
spot_diameter_fullres: float = 50,
background_color: _background = "white",
) -> AnnData:

"""\
Read Slide-seq data
Expand Down Expand Up @@ -340,7 +338,6 @@ def ReadMERFISH(
spot_diameter_fullres: float = 50,
background_color: _background = "white",
) -> AnnData:

"""\
Read MERFISH data
Expand Down Expand Up @@ -423,7 +420,6 @@ def ReadSeqFish(
spot_diameter_fullres: float = 50,
background_color: _background = "white",
) -> AnnData:

"""\
Read SeqFish data
Expand Down Expand Up @@ -510,7 +506,6 @@ def ReadXenium(
spot_diameter_fullres: float = 15,
background_color: _background = "white",
) -> AnnData:

"""\
Read Xenium data
Expand Down

0 comments on commit 439692b

Please sign in to comment.