(
matrix: &IMArrayElement,
feature_selection_method: Option<FeatureSelectionMethod>,
center: Option<bool>,
verbose: Option<bool>,
n_components: Option<usize>,
alpha: Option<f64
| 197 | /// - SVD computation fails (e.g., insufficient rank) |
| 198 | #[allow(clippy::too_many_arguments)] |
| 199 | pub fn run_pca_sparse_masked<T>( |
| 200 | matrix: &IMArrayElement, |
| 201 | feature_selection_method: Option<FeatureSelectionMethod>, |
| 202 | center: Option<bool>, |
| 203 | verbose: Option<bool>, |
| 204 | n_components: Option<usize>, |
| 205 | alpha: Option<f64>, |
| 206 | random_seed: Option<u32>, |
| 207 | svd_method: Option<SVDMethod>, |
| 208 | ) -> anyhow::Result<PCAResult<T>> |
| 209 | where |
| 210 | T: FloatOpsTS, |
| 211 | { |
| 212 | let feature_selection_method = |
| 213 | feature_selection_method.unwrap_or(FeatureSelectionMethod::RandomSelection(1000)); |
| 214 | let shape = matrix.get_shape()?; |
| 215 | let ncols = shape[1]; |
| 216 | let center = center.unwrap_or(false); |
| 217 | let verbose = verbose.unwrap_or(false); |
| 218 | let n_components = n_components.unwrap_or(50); |
| 219 | let random_seed = random_seed.unwrap_or(42); |
| 220 | let svd_method = svd_method.unwrap_or_default(); |
| 221 | let selected = match feature_selection_method { |
| 222 | FeatureSelectionMethod::FullFeatures => { |
| 223 | vec![true; ncols] |
| 224 | } |
| 225 | FeatureSelectionMethod::HighlyVariableSelection(vec) => vec, |
| 226 | FeatureSelectionMethod::RandomSelection(num_genes) => { |
| 227 | generate_random_mask(ncols, num_genes) |
| 228 | } |
| 229 | }; |
| 230 | let read_guard = matrix.0.read_inner(); |
| 231 | let data = read_guard.deref(); |
| 232 | match data { |
| 233 | ArrayData::CsrMatrix(dyn_csr) => { |
| 234 | match dyn_csr { |
| 235 | DynCsrMatrix::F32(csr) => { |
| 236 | let mut masked_pca = MaskedSparsePCABuilder::new() |
| 237 | .mask(selected) |
| 238 | .center(center) |
| 239 | .verbose(verbose) |
| 240 | .alpha(alpha.unwrap_or(1.0) as f32) |
| 241 | .n_components(n_components) |
| 242 | .random_seed(random_seed) |
| 243 | .svd_method(svd_method) |
| 244 | .build(); |
| 245 | masked_pca.fit(csr)?; |
| 246 | let transformed = masked_pca.transform(csr)?; |
| 247 | let explained_variance_ratio = masked_pca.explained_variance_ratio()?; |
| 248 | let cumulative_explained_variance_ratio = masked_pca.cumulative_explained_variance_ratio()?; |
| 249 | let feature_importance = masked_pca.feature_importances()?; |
| 250 | |
| 251 | let transformed: Array2<T> = arr2_conversion(transformed)?; |
| 252 | let explained_variance_ratio: Array1<T> = arr1_conversion(explained_variance_ratio)?; |
| 253 | let cumulative_explained_variance_ratio: Array1<T> = arr1_conversion(cumulative_explained_variance_ratio)?; |
| 254 | let feature_importance: Array2<T> = arr2_conversion(feature_importance)?; |
| 255 | let res = PCAResult { |
| 256 | transformed, |
nothing calls this directly
no test coverage detected