1+ import warnings
2+
3+ import anndata as ad
14import numpy as np
25import pandas as pd
36from sklearn .decomposition import NMF , LatentDirichletAllocation
47from spatialdata import SpatialData
58
69
7- import numpy as np
8- import scanpy as sc
9- import anndata as ad
10- from sklearn .decomposition import NMF , LatentDirichletAllocation
11- import warnings
12-
1310def latent_factor (
1411 sdata : SpatialData ,
1512 method : str = "NMF" ,
@@ -55,7 +52,6 @@ def latent_factor(
5552 sdata : SpatialData or None
5653 Modified SpatialData object or None if copy=False.
5754 """
58-
5955 adata = sdata [layer ]
6056 counts = adata .X .copy ()
6157
@@ -74,15 +70,12 @@ def latent_factor(
7470 try :
7571 from drvi .model import DRVI
7672 from drvi .utils .tools import (
77- traverse_latent ,
7873 calculate_differential_vars ,
7974 set_latent_dimension_stats ,
75+ traverse_latent ,
8076 )
8177 except ImportError :
82- raise ImportError (
83- "The 'drvi' package is required for method='DRVI'. "
84- "Please install it with: pip install drvi"
85- )
78+ raise ImportError ("The 'drvi' package is required for method='DRVI'. Please install it with: pip install drvi" )
8679
8780 # DRVI-specific parameters
8881 encoder_dims = kwargs .pop ("encoder_dims" , [128 , 128 ])
@@ -145,6 +138,7 @@ def latent_factor(
145138 sdata [layer ] = adata
146139 return sdata if copy else None
147140
141+
148142def combine_loadings_arrays (gene_loadings_pos : np .ndarray , gene_loadings_neg : np .ndarray ) -> np .ndarray :
149143 if gene_loadings_pos .shape != gene_loadings_neg .shape :
150144 raise ValueError ("Input arrays must have the same shape." )
@@ -157,8 +151,7 @@ def combine_loadings_arrays(gene_loadings_pos: np.ndarray, gene_loadings_neg: np
157151 # Raise warning if conflicts found
158152 if np .any (conflict_mask ):
159153 conflict_indices = np .argwhere (conflict_mask )
160- warnings .warn (f"Conflicts found at { len (conflict_indices )} locations. "
161- f"Example: { conflict_indices [:5 ].tolist ()} " )
154+ warnings .warn (f"Conflicts found at { len (conflict_indices )} locations. Example: { conflict_indices [:5 ].tolist ()} " )
162155
163156 # Initialize combined output
164157 combined = np .zeros_like (gene_loadings_pos )
@@ -168,7 +161,6 @@ def combine_loadings_arrays(gene_loadings_pos: np.ndarray, gene_loadings_neg: np
168161 return combined
169162
170163
171-
172164def factors_to_cells (
173165 sdata : SpatialData , extracellular_layer : str = "segmentation_free_table" , cellular_layer : str = "table" , copy : bool | None = None
174166) -> SpatialData :
0 commit comments