@@ -28,6 +28,7 @@ class SpatialGraph():
28
28
:param n_neighbors: number of nearset neighbors to infer, defaults to 10
29
29
:type n_neighbors: int, optional
30
30
"""
31
+
31
32
def __init__ (self , sdata , n_neighbors = 10 ) -> None :
32
33
33
34
self .sdata = sdata
@@ -74,6 +75,14 @@ def neighbor_types(self):
74
75
self .n_neighbors )
75
76
return self ._neighbor_types [:, :self .n_neighbors ]
76
77
78
+ @property
79
+ def umap_0 (self ):
80
+ return self ._umap [:, 0 ]
81
+
82
+ @property
83
+ def umap_1 (self ):
84
+ return self ._umap [:, 1 ]
85
+
77
86
@property
78
87
def umap (self ):
79
88
"""umap: Returns the coordinates of the UMAP representation of the source data.
@@ -216,7 +225,7 @@ def plot_entropy(self, n_neighbors=4):
216
225
linestyle = 'dotted' )
217
226
fig .add_artist (con )
218
227
219
- def _determine_counts (self , bandwidth = 1 , kernel = None ):
228
+ def _determine_counts (self , bandwidth = 1 , regularization = 0 , kernel = None ):
220
229
"""_determine_counts: Determines the count distributions of genes around each molecule, thus effectively generating models of the immediate environment.
221
230
222
231
:param bandwidth: Bandwidth of the default Gaussian kernel, defaults to 1
@@ -234,9 +243,14 @@ def kernel(x): return np.exp(-x**2/(2*bandwidth**2))
234
243
for i in range (0 , self .n_neighbors ):
235
244
counts [np .arange (len (self .sdata )), self .neighbor_types [:, i ]
236
245
] += kernel (self .distances [:, i ])
246
+
247
+ assert (all (counts .sum (1 )) > 0 )
248
+ counts [np .arange (len (self .sdata )),
249
+ self .sdata .gene_ids ] += regularization - 1
250
+
237
251
return counts
238
252
239
- def run_umap (self , bandwidth = 1 , kernel = None , metric = 'euclidean' , zero_weight = 1.0 , cutoff = None , * args , ** kwargs ):
253
+ def run_umap (self , bandwidth = 1 , kernel = None , metric = 'euclidean' , zero_weight = 1.0 , cutoff = None , * args , ** kwargs ):
240
254
"""run_umap: Creates a UMAP representation of recurring local contexts in the source data.
241
255
242
256
:param bandwidth: Bandwidth of the default Gaussian kernel used to build local environment models, defaults to 1
@@ -249,10 +263,8 @@ def run_umap(self, bandwidth=1, kernel=None, metric='euclidean', zero_weight=1.0
249
263
:type zero_weight: float, optional
250
264
"""
251
265
# print(kwargs)
252
- counts = self ._determine_counts (bandwidth = bandwidth , kernel = kernel )
253
- assert (all (counts .sum (1 )) > 0 )
254
- counts [np .arange (len (self .sdata )),
255
- self .sdata .gene_ids ] += zero_weight - 1
266
+ counts = self ._determine_counts (
267
+ bandwidth = bandwidth , kernel = kernel , regularization = zero_weight )
256
268
257
269
if cutoff is not None :
258
270
@@ -272,7 +284,6 @@ def run_umap(self, bandwidth=1, kernel=None, metric='euclidean', zero_weight=1.0
272
284
# del facs,ica
273
285
print ('Calculating UMAP embedding.' )
274
286
# counts[:,-1]=facs[:,cutoff:].sum(1)
275
-
276
287
277
288
umap = UMAP (metric = metric , * args , ** kwargs )
278
289
self ._umap = umap .fit_transform (counts )
@@ -364,16 +375,7 @@ def umap_js(self, color_prop='c_genes'):
364
375
365
376
n_bars = 20
366
377
367
- if False :
368
- f_scatter = go .FigureWidget (px .imshow (np .repeat (self .sdata .background .data [:, :, None ], 3 , axis = - 1 ,),
369
- x = np .linspace (
370
- self .sdata .background .extent [0 ], self .sdata .background .extent [1 ], self .sdata .background .data .shape [0 ]),
371
- y = np .linspace (
372
- self .sdata .background .extent [2 ], self .sdata .background .extent [3 ], self .sdata .background .data .shape [1 ])
373
- ),
374
- layout = Layout (border = 'solid 4px' , width = '100%' ))
375
-
376
- trace_scatter = go .Scattergl (x = self .sdata .x ,
378
+ trace_scatter = go .Scattergl (x = self .sdata .x ,
377
379
y = self .sdata .y ,
378
380
mode = 'markers' ,
379
381
marker = dict (
@@ -383,20 +385,7 @@ def umap_js(self, color_prop='c_genes'):
383
385
'color' : 'black' , 'opacity' : 0.2 }},
384
386
)
385
387
386
- f_scatter .add_trace (trace_scatter )
387
- else :
388
- trace_scatter = go .Scattergl (x = self .sdata .x ,
389
- y = self .sdata .y ,
390
- mode = 'markers' ,
391
- marker = dict (
392
- color = self .sdata .obsc .project (color_prop )),
393
- hoverinfo = 'none' , meta = {'name' : 'tissue-scatter' },
394
- unselected = {'marker' : {
395
- 'color' : 'black' , 'opacity' : 0.2 }},
396
- )
397
-
398
- f_scatter = go .FigureWidget (trace_scatter ,)
399
-
388
+ f_scatter = go .FigureWidget (trace_scatter ,)
400
389
401
390
f_umap = go .FigureWidget (go .Scattergl (x = self .sdata .graph .umap [:, 0 ],
402
391
y = self .sdata .graph .umap [:, 1 ],
@@ -417,7 +406,9 @@ def umap_js(self, color_prop='c_genes'):
417
406
marker = {
418
407
'color' : ['rgb' + str (tuple ((np .array (c )* 256 ).astype (int ))) for c in colors ]},
419
408
)
420
- f_bars = go .FigureWidget (w_bars )
409
+ f_bars = go .FigureWidget (w_bars ,
410
+ layout = {'title' : 'n (selected):' },
411
+ )
421
412
422
413
f_bars .data [0 ]['showlegend' ] = False
423
414
@@ -426,7 +417,8 @@ def umap_js(self, color_prop='c_genes'):
426
417
marker = {
427
418
'color' : ['rgb' + str (tuple ((np .array (c )* 256 ).astype (int ))) for c in colors ]},
428
419
)
429
- f_bars_ratio_up = go .FigureWidget (w_bars_ratio_up )
420
+ f_bars_ratio_up = go .FigureWidget (w_bars_ratio_up ,
421
+ layout = {'title' : 'n (selected) / total:' })
430
422
431
423
f_bars_ratio_up .data [0 ]['showlegend' ] = False
432
424
@@ -435,7 +427,9 @@ def umap_js(self, color_prop='c_genes'):
435
427
marker = {
436
428
'color' : ['rgb' + str (tuple ((np .array (c )* 256 ).astype (int ))) for c in colors ]},
437
429
)
438
- f_bars_binom = go .FigureWidget (w_bars_binom )
430
+ f_bars_binom = go .FigureWidget (w_bars_binom ,
431
+ layout = {
432
+ 'title' : 'binomcdf (n (selected),p(total)):' },)
439
433
440
434
f_bars_binom .data [0 ]['showlegend' ] = False
441
435
@@ -521,12 +515,12 @@ def store_selection(event):
521
515
return widgets .VBox ([widgets .HBox ([f_scatter , f_umap ], layout = Layout (display = 'flex' , width = '100%' , height = '80%' , border = 'red solid 1px' , align_items = 'stretch' , justify_content = 'space-around' , flex_direction = 'row' )),
522
516
widgets .HBox ([widgets .HBox ([f_bars , f_bars_ratio_up , f_bars_binom ], layout = Layout (display = 'flex' , width = '80%' )),
523
517
widgets .VBox ([widgets .HBox ([text_field ,
524
- ]), widgets .HBox ([store_button ,reset_button ])], layout = Layout (display = 'flex' , width = '20%' , height = '100%' , border = 'red solid 1px' , justify_content = 'space-around' , flex_direction = 'column' )
518
+ ]), widgets .HBox ([store_button , reset_button ])], layout = Layout (display = 'flex' , width = '20%' , height = '100%' , border = 'red solid 1px' , justify_content = 'space-around' , flex_direction = 'column' )
525
519
)]
526
520
)], layout = Layout (width = '100%' , height = '80vh' , background = 'red' , border = 'solid 1px' ))
527
521
528
522
def map_and_umap (self , color_prop = None , scalebar = True , cmap = 'jet' ,
529
- ** kwargs ):
523
+ ** kwargs ):
530
524
"""map_and_umap: Plots a side-by-side representation of the available UMAP- and coordinate data, with styling arguments passed to both plotting functions.
531
525
532
526
:param color_prop: Property to color the individual markers by. Needs to be a column in self.sdata.obsc, defaults to None
@@ -547,12 +541,11 @@ def map_and_umap(self, color_prop=None, scalebar=True, cmap='jet',
547
541
548
542
ax1 = plt .subplot2grid ((3 , 2 ), (0 , 0 ), 2 , 1 )
549
543
550
- sc2 , _ , _ = self .sdata .scatter (axd = ax1 ,scalebar = scalebar ,cmap = cmap , ** kwargs )
544
+ sc2 , _ , _ = self .sdata .scatter (
545
+ axd = ax1 , scalebar = scalebar , cmap = cmap , ** kwargs )
551
546
552
547
ax2 = plt .subplot2grid ((3 , 2 ), (0 , 1 ), 2 , 1 )
553
- self .sdata .graph .plot_umap (cmap = cmap ,** kwargs )
554
-
555
-
548
+ self .sdata .graph .plot_umap (cmap = cmap , ** kwargs )
556
549
557
550
def _untangle_text (self , cogs , untangle_rounds = 50 , min_distance = 0.5 ):
558
551
knn = NearestNeighbors (n_neighbors = 2 )
0 commit comments