Skip to content

Commit 6c4d9b6

Browse files
Merge pull request #25 from HiDiHlabs/22-localmax-function-is-doubled-up
22 localmax function is doubled up
2 parents 533325a + b95239b commit 6c4d9b6

File tree

2 files changed

+23
-52
lines changed

2 files changed

+23
-52
lines changed

ovrlpy/_ovrlp.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
_compute_divergence_patched,
2121
_create_histogram,
2222
_create_knn_graph,
23-
_determine_localmax,
23+
_determine_localmax_and_sample,
2424
_fill_color_axes,
2525
_get_knn_expression,
2626
_get_spatial_subsample_mask,
@@ -274,7 +274,7 @@ def get_pseudocell_locations(
274274
df, genes=genes, min_expression=min_expression, KDE_bandwidth=KDE_bandwidth
275275
)
276276

277-
pseudocell_locations_x, pseudocells_y, _ = _determine_localmax(
277+
pseudocell_locations_x, pseudocells_y, _ = _determine_localmax_and_sample(
278278
hist, min_distance=min_distance, min_expression=min_expression
279279
)
280280

@@ -443,7 +443,7 @@ def detect_doublets(
443443
if integrity_sigma is not None:
444444
integrity_map = gaussian_filter(integrity_map, integrity_sigma)
445445

446-
dist_x, dist_y, dist_t = _determine_localmax(
446+
dist_x, dist_y, dist_t = _determine_localmax_and_sample(
447447
(1 - integrity_map) * (signal_map > minimum_signal_strength),
448448
min_distance=min_distance,
449449
min_expression=integrity_threshold,
@@ -787,7 +787,6 @@ def transform(self, coordinate_df: pd.DataFrame):
787787
self.pca_2d,
788788
embedder_2d=self.embedder_2d,
789789
embedder_3d=self.embedder_3d,
790-
colors_min_max=self.colors_min_max,
791790
)
792791
subsample_embedding_color, _ = _fill_color_axes(
793792
subsample_embedding_color, self.pca_3d
@@ -825,8 +824,8 @@ def pseudocell_df(self) -> pd.DataFrame:
825824

826825
def plot_region_of_interest(
827826
self,
828-
subsample,
829-
subsample_embedding_color,
827+
subsample: pd.DataFrame,
828+
subsample_embedding_color: np.ndarray,
830829
x: float = None,
831830
y: float = None,
832831
window_size: int = None,
@@ -839,7 +838,7 @@ def plot_region_of_interest(
839838
----------
840839
subsample : pandas.DataFrame
841840
A dataframe of molecule coordinates and gene assignments.
842-
subsample_embedding_color : Optional[pandas.DataFrame]
841+
subsample_embedding_color : pandas.DataFrame
843842
A list of rgb values for each molecule.
844843
x : float
845844
Center x-coordinate for the region-of-interest.

ovrlpy/_utils.py

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
from concurrent.futures import ThreadPoolExecutor, as_completed
22

3-
# create circular kernel:
4-
# draw outlines around artist:
53
import matplotlib.patheffects as PathEffects
64
import matplotlib.pyplot as plt
75
import numpy as np
86
import pandas as pd
97
import tqdm
10-
from scipy.ndimage import gaussian_filter, maximum_filter
8+
from scipy.ndimage import gaussian_filter
119
from sklearn.decomposition import PCA
1210
from sklearn.neighbors import NearestNeighbors
1311

14-
from ._ssam2 import kde_2d
12+
from ._ssam2 import find_local_maxima, kde_2d
1513

1614

17-
def _draw_outline(ax, artist, lw=2, color="black"):
15+
def _draw_outline(artist, lw=2, color="black"):
16+
"Draws outlines around the (text) artists for better legibility."
1817
_ = artist.set_path_effects(
1918
[PathEffects.withStroke(linewidth=lw, foreground=color), PathEffects.Normal()]
2019
)
@@ -43,33 +42,12 @@ def _plot_scalebar(
4342
)
4443

4544
if edge_color is not None:
46-
_draw_outline(ax, plot_artist[0], lw=5, color=edge_color)
47-
_draw_outline(ax, text_artist, lw=5, color=edge_color)
45+
_draw_outline(plot_artist[0], lw=5, color=edge_color)
46+
_draw_outline(text_artist, lw=5, color=edge_color)
4847

4948
return plot_artist, text_artist
5049

5150

52-
def _create_circular_kernel(r):
53-
"""
54-
Creates a circular kernel of radius r.
55-
56-
Parameters
57-
----------
58-
r : int
59-
The radius of the kernel.
60-
61-
Returns
62-
-------
63-
kernel : np.array
64-
A 2d array of the circular kernel.
65-
66-
"""
67-
68-
span = np.linspace(-1, 1, r * 2)
69-
X, Y = np.meshgrid(span, span)
70-
return (X**2 + Y**2) ** 0.5 <= 1
71-
72-
7351
def _get_kl_divergence(p, q):
7452
# mask = (p!=0) * (q!=0)
7553
output = np.zeros(p.shape)
@@ -78,7 +56,7 @@ def _get_kl_divergence(p, q):
7856
return output
7957

8058

81-
def _determine_localmax(distribution, min_distance=3, min_expression=5):
59+
def _determine_localmax_and_sample(distribution, min_distance=3, min_expression=5):
8260
"""
8361
Returns a list of local maxima in a kde of the data frame.
8462
@@ -99,12 +77,8 @@ def _determine_localmax(distribution, min_distance=3, min_expression=5):
9977
A list of y coordinates of local maxima.
10078
10179
"""
102-
localmax_kernel = _create_circular_kernel(min_distance)
103-
localmax_projection = distribution == maximum_filter(
104-
distribution, footprint=localmax_kernel
105-
)
10680

107-
rois_x, rois_y = np.where((distribution > min_expression) & localmax_projection)
81+
rois_x, rois_y = find_local_maxima(distribution, min_distance, min_expression)
10882

10983
return rois_x, rois_y, distribution[rois_x, rois_y]
11084

@@ -148,15 +122,15 @@ def _min_to_max(arr, arr_min=None, arr_max=None):
148122

149123
# define a function that fits expression data to into the umap embeddings:
150124
def _transform_embeddings(
151-
expression, pca, embedder_2d, embedder_3d, colors_min_max=[None, None]
125+
expression,
126+
pca,
127+
embedder_2d,
128+
embedder_3d,
152129
):
153130
factors = pca.transform(expression)
154131

155132
embedding = embedder_2d.transform(factors)
156133
embedding_color = embedder_3d.transform(factors)
157-
# embedding_color = embedder_3d.transform(embedding)
158-
159-
# embedding_color = _min_to_max(embedding_color,colors_min_max[0],colors_min_max[1])
160134

161135
return embedding, embedding_color
162136

@@ -192,12 +166,12 @@ def _plot_embeddings(
192166
)
193167

194168
text_artists = []
195-
for i in range(len(celltypes)):
169+
for i, celltype in enumerate(celltypes):
196170
if not np.isnan(celltype_centers[i, 0]):
197171
t = ax.text(
198172
np.nan_to_num((celltype_centers[i, 0])),
199173
np.nan_to_num(celltype_centers[i, 1]),
200-
celltypes[i],
174+
celltype,
201175
color="k",
202176
fontsize=12,
203177
)
@@ -364,6 +338,9 @@ def _compute_divergence_embedded(
364338
metric="cosine_similarity",
365339
pca_divergence=0.8,
366340
):
341+
"""This is a legacy function, replaced by _compute_divergence_patched. It contains other similarity measures than cosine similarity.
342+
To be integrated into the patch-based divergence computation later.
343+
"""
367344
signal = _create_histogram(
368345
df,
369346
genes,
@@ -381,9 +358,6 @@ def _compute_divergence_embedded(
381358
df_top = df[df.z_delim < df.z]
382359
df_bot = df[df.z_delim > df.z]
383360

384-
# dr_bottom = np.zeros((df_bottom.shape[0],df_bottom.shape[1], pca.components_.shape[0]))
385-
# dr_top = np.zeros((df_bottom.shape[0],df_bottom.shape[1], pca.components_.shape[0]))
386-
387361
hists_top = np.zeros((mask.sum(), pca.components_.shape[0]))
388362
hists_bot = np.zeros((mask.sum(), pca.components_.shape[0]))
389363

@@ -481,8 +455,6 @@ def pearson_cross_correlation(a, b):
481455

482456

483457
def _compute_embedding_vectors(subset_df, signal_mask, factor):
484-
# for i,g in tqdm.tqdm(enumerate(genes),total=len(genes)):
485-
486458
if len(subset_df) < 2:
487459
return None, None
488460

0 commit comments

Comments
 (0)