Skip to content

Commit b8798f0

Browse files
latest tutorial
1 parent 92cdca5 commit b8798f0

File tree

4 files changed

+2032
-7089
lines changed

4 files changed

+2032
-7089
lines changed

plankton/stats.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from weakref import ref
12
from scipy.signal import fftconvolve
23
import numpy as np
34
from scipy import signal, fft as sp_fft
@@ -65,6 +66,19 @@ def get_histograms(sdata, category=None,resolution=5):
6566

6667
import pdb;
6768

69+
def _interpolate(x,y,step):
70+
"""_interpolat generates linear interpolation from the x,y to create mutual distance curves in um units
71+
72+
:param x: distances with available values
73+
:type x: np.array
74+
:param y: values at x
75+
:type y: np array
76+
:param step: step size of x,y
77+
:type step: float
78+
"""
79+
80+
return np.apply_along_axis(lambda x_: np.interp(np.arange(0,x.max()*step),x*step,x_,),2,y)
81+
6882
def co_occurrence(sdata, resolution=5.0, max_radius=400, linear_steps=5, category=None):
6983
"""co_occurrence _summary_
7084
@@ -118,7 +132,7 @@ def co_occurrence(sdata, resolution=5.0, max_radius=400, linear_steps=5, categor
118132
co_occurrences[i,j] = h2_product.sum(axis=(1,2))
119133
co_occurrences[j,i]= co_occurrences[i,j]
120134

121-
return radii, co_occurrences
135+
return _interpolate(radii, co_occurrences,resolution)
122136

123137
def ripleys_k(sdata, resolution=5, max_radius=400, linear_steps=5, category=None):
124138

@@ -159,3 +173,26 @@ def ripleys_k(sdata, resolution=5, max_radius=400, linear_steps=5, category=None
159173
co_occurrences[j,i]= co_occurrences[i,j]
160174

161175
return radii, co_occurrences
176+
177+
def mor_normalize(stats1,stats2):
178+
"""mor_normalize _summary_
179+
180+
:param stats1: _description_
181+
:type stats1: _type_
182+
:param stats2: _description_
183+
:type stats2: _type_
184+
"""
185+
186+
gmean = (stats1.counts*stats2.counts)**0.5
187+
188+
ref_ratios_1 = (stats1.counts/gmean).fillna(0)
189+
ref_ratios_2 = (stats2.counts/gmean).fillna(0)
190+
191+
norm_factor_1 = np.median(ref_ratios_1)
192+
norm_factor_2 = np.median(ref_ratios_2)
193+
194+
normalized_1 = stats1.counts/norm_factor_1
195+
normalized_2 = stats2.counts/norm_factor_2
196+
197+
return normalized_1,normalized_2
198+

plankton/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ def get_histograms(sdata, mins=None, maxs=None, category=None, resolution=5):
248248

249249

250250
def crosscorr(x, y):
251-
x -= x.mean(1)[:, None]
252-
y -= y.mean(1)[:, None]
251+
x -= np.array(x.mean(1))[:, None]
252+
y -= np.array(y.mean(1))[:, None]
253253
c = (np.dot(x, y.T)/x.shape[1]).squeeze()
254254

255255
return np.nan_to_num(np.nan_to_num(c/np.array(x.std(1))[:, None])/np.array(y.std(1))[None, :])
@@ -338,7 +338,7 @@ def ssam(sdata, signatures=None, adata_obs_label='celltype', kernel_bandwidth=2.
338338

339339

340340
def localmax_sampling(sdata, n_clusters=10, min_distance=3, bandwidth=4):
341-
n_bins = np.array(sdata.spatial.shape)
341+
n_bins = np.array(sdata.spatial.shape).astype(int)
342342

343343
vf = (gaussian_filter(np.histogram2d(
344344
*sdata.coordinates.T, bins=n_bins)[0], 2))
@@ -357,9 +357,9 @@ def kernel(x): return np.exp(-x**2/(2*bandwidth**2))
357357
counts[np.arange(dists.shape[0]), neighbor_types[:, i]
358358
] += kernel(dists[:, i])
359359

360-
assert (all(counts.sum(1)) > 0)
360+
# assert (all(counts.sum(1)) > 0)
361361

362-
counts=counts/counts.sum(1)[:,None]
362+
counts=np.nan_to_num(counts/counts.sum(1)[:,None])
363363

364364
ica = FastICA(n_components=30)
365365
facs = ica.fit_transform(counts)

0 commit comments

Comments
 (0)