1+ import math
12from typing import Literal
23
34import numpy as np
45from anndata import AnnData
6+ from sklearn .linear_model import LinearRegression # type: ignore
57from sklearn .metrics import pairwise_distances
68from tqdm import tqdm
79
1719]
1820
1921
20- def calculate_weight_matrix (
21- adata : AnnData ,
22- adata_imputed : AnnData | None = None ,
23- pseudo_spots : bool = False ,
24- platform : _PLATFORM = "Visium" ,
25- ) -> AnnData | None :
26- import math
27-
28- from sklearn .linear_model import LinearRegression
29-
22+ def row_col_by_platform (
23+ adata , platform
24+ ) -> tuple [LinearRegression , LinearRegression , float ]:
3025 rate : float
3126 if platform == "Visium" :
3227 img_row = adata .obs ["imagerow" ]
@@ -46,64 +41,61 @@ def calculate_weight_matrix(
4641 { platform !r} does not support.
4742 """
4843 )
49-
50- reg_row = LinearRegression ().fit (array_row .values .reshape (- 1 , 1 ), img_row )
51-
52- reg_col = LinearRegression ().fit (array_col .values .reshape (- 1 , 1 ), img_col )
53-
54- if pseudo_spots and adata_imputed :
55- pd = pairwise_distances (
56- adata_imputed .obs [["imagecol" , "imagerow" ]],
57- adata .obs [["imagecol" , "imagerow" ]],
58- metric = "euclidean" ,
59- )
60- unit = math .sqrt (reg_row .coef_ ** 2 + reg_col .coef_ ** 2 )
61- pd_norm = np .where (pd >= unit , 0 , 1 )
62-
63- md = 1 - pairwise_distances (
64- adata_imputed .obsm ["X_morphology" ],
65- adata .obsm ["X_morphology" ],
66- metric = "cosine" ,
67- )
68- md [md < 0 ] = 0
69-
70- adata_imputed .uns ["physical_distance" ] = pd_norm
71- adata_imputed .uns ["morphological_distance" ] = md
72-
73- adata_imputed .uns ["weights_matrix_all" ] = (
74- adata_imputed .uns ["physical_distance" ]
75- * adata_imputed .uns ["morphological_distance" ]
76- )
77-
78- else :
79- pd = pairwise_distances (adata .obs [["imagecol" , "imagerow" ]], metric = "euclidean" )
80- unit = math .sqrt (reg_row .coef_ ** 2 + reg_col .coef_ ** 2 )
81- pd_norm = np .where (pd >= rate * unit , 0 , 1 )
82-
83- md = 1 - pairwise_distances (adata .obsm ["X_morphology" ], metric = "cosine" )
84- md [md < 0 ] = 0
85-
86- gd = 1 - pairwise_distances (adata .obsm ["X_pca" ], metric = "correlation" )
87- adata .uns ["gene_expression_correlation" ] = gd
88- adata .uns ["physical_distance" ] = pd_norm
89- adata .uns ["morphological_distance" ] = md
90-
91- adata .uns ["weights_matrix_all" ] = (
92- adata .uns ["physical_distance" ]
93- * adata .uns ["morphological_distance" ]
94- * adata .uns ["gene_expression_correlation" ]
95- )
96- adata .uns ["weights_matrix_pd_gd" ] = (
97- adata .uns ["physical_distance" ] * adata .uns ["gene_expression_correlation" ]
98- )
99- adata .uns ["weights_matrix_pd_md" ] = (
100- adata .uns ["physical_distance" ] * adata .uns ["morphological_distance" ]
101- )
102- adata .uns ["weights_matrix_gd_md" ] = (
103- adata .uns ["gene_expression_correlation" ]
104- * adata .uns ["morphological_distance" ]
105- )
106- return adata
44+ regression = LinearRegression ()
45+ reg_row : LinearRegression = regression .fit (array_row .values .reshape (- 1 , 1 ), img_row ) # type: ignore
46+ reg_col : LinearRegression = regression .fit (array_col .values .reshape (- 1 , 1 ), img_col ) # type: ignore
47+ return reg_col , reg_row , rate
48+
49+
50+ def weight_matrix (adata , platform ):
51+ reg_col , reg_row , rate = row_col_by_platform (adata , platform )
52+ pd = pairwise_distances (adata .obs [["imagecol" , "imagerow" ]], metric = "euclidean" )
53+ unit = math .sqrt (reg_row .coef_ [0 ] ** 2 + reg_col .coef_ [0 ] ** 2 )
54+ pd_norm = np .where (pd >= rate * unit , 0 , 1 )
55+ md = 1 - pairwise_distances (adata .obsm ["X_morphology" ], metric = "cosine" )
56+ md [md < 0 ] = 0
57+ gd = 1 - pairwise_distances (adata .obsm ["X_pca" ], metric = "correlation" )
58+ adata .uns ["gene_expression_correlation" ] = gd
59+ adata .uns ["physical_distance" ] = pd_norm
60+ adata .uns ["morphological_distance" ] = md
61+ adata .uns ["weights_matrix_all" ] = (
62+ adata .uns ["physical_distance" ]
63+ * adata .uns ["morphological_distance" ]
64+ * adata .uns ["gene_expression_correlation" ]
65+ )
66+ adata .uns ["weights_matrix_pd_gd" ] = (
67+ adata .uns ["physical_distance" ] * adata .uns ["gene_expression_correlation" ]
68+ )
69+ adata .uns ["weights_matrix_pd_md" ] = (
70+ adata .uns ["physical_distance" ] * adata .uns ["morphological_distance" ]
71+ )
72+ adata .uns ["weights_matrix_gd_md" ] = (
73+ adata .uns ["gene_expression_correlation" ] * adata .uns ["morphological_distance" ]
74+ )
75+
76+
77+ def weight_matrix_imputed (adata , adata_imputed , platform ):
78+ reg_col , reg_row , _ = row_col_by_platform (adata , platform )
79+
80+ pd = pairwise_distances (
81+ adata_imputed .obs [["imagecol" , "imagerow" ]],
82+ adata .obs [["imagecol" , "imagerow" ]],
83+ metric = "euclidean" ,
84+ )
85+ unit = math .sqrt (reg_row .coef_ [0 ] ** 2 + reg_col .coef_ [0 ] ** 2 )
86+ pd_norm = np .where (pd >= unit , 0 , 1 )
87+ md = 1 - pairwise_distances (
88+ adata_imputed .obsm ["X_morphology" ],
89+ adata .obsm ["X_morphology" ],
90+ metric = "cosine" ,
91+ )
92+ md [md < 0 ] = 0
93+ adata_imputed .uns ["physical_distance" ] = pd_norm
94+ adata_imputed .uns ["morphological_distance" ] = md
95+ adata_imputed .uns ["weights_matrix_all" ] = (
96+ adata_imputed .uns ["physical_distance" ]
97+ * adata_imputed .uns ["morphological_distance" ]
98+ )
10799
108100
109101def impute_neighbour (
0 commit comments