|
10 | 10 | elsewhere in the suite. |
11 | 11 | """ |
12 | 12 |
|
| 13 | +import tempfile |
| 14 | +from collections.abc import Mapping, Sequence |
| 15 | +from typing import Any, cast |
| 16 | + |
13 | 17 | import anndata |
14 | 18 | import numpy as np |
| 19 | +import pandas as pd |
15 | 20 | import pytest |
16 | 21 | from hypothesis import HealthCheck, given, settings |
17 | 22 | from hypothesis import strategies as st |
18 | 23 | from hypothesis.extra.numpy import arrays |
19 | 24 |
|
| 25 | +from ..alignment_stats import ( |
| 26 | + compute_auroc_per_cell_type, |
| 27 | + compute_eta_squared, |
| 28 | + compute_kruskal_epsilon_squared, |
| 29 | + compute_tau, |
| 30 | +) |
| 31 | +from ..factor_io import export_factors, load_factors |
20 | 32 | from ..factorization import ( |
21 | 33 | canonical_component_signs, |
22 | 34 | correct_conditions, |
@@ -207,3 +219,205 @@ def test_opq_reconstruction_r2_nondecreasing_in_num_subquantizers(D): |
207 | 219 | # violated by noise near saturation (r2 close to 1). |
208 | 220 | assert r2 >= prev_r2 - 0.05 |
209 | 221 | prev_r2 = r2 |
| 222 | + |
| 223 | + |
| 224 | +@given( |
| 225 | + n_cells=st.integers(min_value=10, max_value=80), |
| 226 | + n_types=st.integers(min_value=2, max_value=5), |
| 227 | + seed=st.integers(min_value=0, max_value=2**31 - 1), |
| 228 | +) |
| 229 | +@_slow_settings |
| 230 | +def test_alignment_stats_cell_permutation_invariance_and_negation( |
| 231 | + n_cells, n_types, seed |
| 232 | +): |
| 233 | + """Cell ordering should not affect AUROC, tau, eta^2, or Kruskal epsilon^2. |
| 234 | + Negating loadings must invert AUROC (AUROC(-y) = 1 - AUROC(y)).""" |
| 235 | + rng = np.random.default_rng(seed) |
| 236 | + y = rng.normal(size=n_cells) |
| 237 | + codes = rng.integers(0, n_types, size=n_cells) |
| 238 | + |
| 239 | + # Ensure all types have at least one cell |
| 240 | + for k in range(min(n_types, n_cells)): |
| 241 | + codes[k] = k |
| 242 | + |
| 243 | + # Baseline statistics |
| 244 | + aurocs = compute_auroc_per_cell_type(y, codes, n_types) |
| 245 | + tau = compute_tau(aurocs) |
| 246 | + eta2 = compute_eta_squared(y, codes, n_types) |
| 247 | + eps2 = compute_kruskal_epsilon_squared(y, codes, n_types) |
| 248 | + |
| 249 | + # Permute cell order |
| 250 | + perm = rng.permutation(n_cells) |
| 251 | + y_perm = y[perm] |
| 252 | + codes_perm = codes[perm] |
| 253 | + |
| 254 | + aurocs_perm = compute_auroc_per_cell_type(y_perm, codes_perm, n_types) |
| 255 | + tau_perm = compute_tau(aurocs_perm) |
| 256 | + eta2_perm = compute_eta_squared(y_perm, codes_perm, n_types) |
| 257 | + eps2_perm = compute_kruskal_epsilon_squared(y_perm, codes_perm, n_types) |
| 258 | + |
| 259 | + np.testing.assert_allclose(aurocs_perm, aurocs, rtol=1e-5, atol=1e-6) |
| 260 | + assert abs(tau_perm - tau) < 1e-6 |
| 261 | + assert abs(eta2_perm - eta2) < 1e-6 |
| 262 | + assert abs(eps2_perm - eps2) < 1e-6 |
| 263 | + |
| 264 | + # Negation property: AUROC(-y) = 1 - AUROC(y) |
| 265 | + aurocs_neg = compute_auroc_per_cell_type(-y, codes, n_types) |
| 266 | + counts = np.bincount(codes, minlength=n_types) |
| 267 | + valid = (counts > 0) & ((n_cells - counts) > 0) |
| 268 | + np.testing.assert_allclose( |
| 269 | + aurocs_neg[valid] + aurocs[valid], 1.0, rtol=1e-5, atol=1e-6 |
| 270 | + ) |
| 271 | + |
| 272 | + |
| 273 | +@given( |
| 274 | + n_cells=st.integers(min_value=5, max_value=50), |
| 275 | + n_types=st.integers(min_value=2, max_value=4), |
| 276 | +) |
| 277 | +@_slow_settings |
| 278 | +def test_alignment_stats_constant_loading_degeneracy(n_cells, n_types): |
| 279 | + """Constant loadings carry zero information: AUROC should be 0.5 and all effect |
| 280 | + sizes should be 0.0.""" |
| 281 | + y = np.full(n_cells, 3.14) |
| 282 | + codes = np.arange(n_cells) % n_types |
| 283 | + |
| 284 | + aurocs = compute_auroc_per_cell_type(y, codes, n_types) |
| 285 | + np.testing.assert_allclose(aurocs, 0.5) |
| 286 | + assert compute_tau(aurocs) == 0.0 |
| 287 | + assert compute_eta_squared(y, codes, n_types) == 0.0 |
| 288 | + assert compute_kruskal_epsilon_squared(y, codes, n_types) == 0.0 |
| 289 | + |
| 290 | + |
| 291 | +@given( |
| 292 | + n_genes=st.integers(min_value=5, max_value=20), |
| 293 | + rank_low=st.integers(min_value=2, max_value=4), |
| 294 | + seed=st.integers(min_value=0, max_value=2**31 - 1), |
| 295 | +) |
| 296 | +@_slow_settings |
| 297 | +def test_match_components_across_ranks_permutation_equivariance( |
| 298 | + n_genes, rank_low, seed |
| 299 | +): |
| 300 | + """Permuting the columns of C_high must permute the matched column indices.""" |
| 301 | + rng = np.random.default_rng(seed) |
| 302 | + C_low = rng.normal(size=(n_genes, rank_low)) + np.eye(n_genes, rank_low) * 10.0 |
| 303 | + C_low = C_low * canonical_component_signs(C_low) |
| 304 | + |
| 305 | + # Make extra columns orthogonal / distinct so matching has a unique optimum |
| 306 | + extra_cols = rng.normal(size=(n_genes, 2)) |
| 307 | + Q, _ = np.linalg.qr(C_low) |
| 308 | + extra_cols = extra_cols - Q @ (Q.T @ extra_cols) |
| 309 | + extra_cols = extra_cols * canonical_component_signs(extra_cols) |
| 310 | + C_high = np.concatenate([C_low, extra_cols], axis=1) |
| 311 | + rank_high = C_high.shape[1] |
| 312 | + |
| 313 | + pairs_orig, unmatched_orig = match_components_across_ranks( |
| 314 | + C_low, C_high, threshold=0.5 |
| 315 | + ) |
| 316 | + |
| 317 | + # Permute columns of C_high |
| 318 | + perm = rng.permutation(rank_high) |
| 319 | + C_high_perm = C_high[:, perm] |
| 320 | + |
| 321 | + pairs_perm, unmatched_perm = match_components_across_ranks( |
| 322 | + C_low, C_high_perm, threshold=0.5 |
| 323 | + ) |
| 324 | + |
| 325 | + # Low rank indices match identically |
| 326 | + np.testing.assert_array_equal(pairs_perm[:, 0], pairs_orig[:, 0]) |
| 327 | + # Permuted high indices match the original column indices |
| 328 | + np.testing.assert_array_equal(perm[pairs_perm[:, 1]], pairs_orig[:, 1]) |
| 329 | + np.testing.assert_array_equal( |
| 330 | + np.sort(perm[unmatched_perm]), np.sort(unmatched_orig) |
| 331 | + ) |
| 332 | + |
| 333 | + |
| 334 | +@given( |
| 335 | + n_genes=st.integers(min_value=5, max_value=15), |
| 336 | + rank_low=st.integers(min_value=2, max_value=4), |
| 337 | + seed=st.integers(min_value=0, max_value=2**31 - 1), |
| 338 | +) |
| 339 | +@_slow_settings |
| 340 | +def test_match_components_across_ranks_threshold_monotonicity(n_genes, rank_low, seed): |
| 341 | + """Increasing threshold must yield a subset of matched pairs.""" |
| 342 | + rng = np.random.default_rng(seed) |
| 343 | + C_low = rng.normal(size=(n_genes, rank_low)) |
| 344 | + C_high = np.concatenate( |
| 345 | + [C_low[:, :2], rng.normal(size=(n_genes, rank_low))], axis=1 |
| 346 | + ) |
| 347 | + |
| 348 | + pairs_low_thresh, _ = match_components_across_ranks(C_low, C_high, threshold=0.3) |
| 349 | + pairs_high_thresh, _ = match_components_across_ranks(C_low, C_high, threshold=0.8) |
| 350 | + |
| 351 | + set_low = set(map(tuple, pairs_low_thresh)) |
| 352 | + set_high = set(map(tuple, pairs_high_thresh)) |
| 353 | + assert set_high.issubset(set_low) |
| 354 | + |
| 355 | + |
| 356 | +@given( |
| 357 | + n_cells=st.integers(min_value=20, max_value=60), |
| 358 | + n_genes=st.integers(min_value=10, max_value=25), |
| 359 | + n_conditions=st.integers(min_value=2, max_value=5), |
| 360 | + rank=st.integers(min_value=2, max_value=4), |
| 361 | + seed=st.integers(min_value=0, max_value=2**31 - 1), |
| 362 | +) |
| 363 | +@_slow_settings |
| 364 | +def test_export_load_factors_preserves_invariants( |
| 365 | + n_cells, n_genes, n_conditions, rank, seed |
| 366 | +): |
| 367 | + """export_factors -> load_factors roundtrip must preserve factor matrices, |
| 368 | + obs_names, and ensure weighted_projections = projections @ Pf2_B.""" |
| 369 | + rng = np.random.default_rng(seed) |
| 370 | + P, _ = np.linalg.qr(rng.normal(size=(n_cells, rank))) |
| 371 | + P = P.astype(np.float32) |
| 372 | + A = rng.normal(size=(n_conditions, rank)).astype(np.float32) |
| 373 | + B = rng.normal(size=(rank, rank)).astype(np.float32) |
| 374 | + C = rng.normal(size=(n_genes, rank)).astype(np.float32) |
| 375 | + weights = rng.uniform(0.5, 2.0, size=rank).astype(np.float32) |
| 376 | + |
| 377 | + barcodes = [f"cell_barcode_{seed}_{i}" for i in range(n_cells)] |
| 378 | + obs = pd.DataFrame( |
| 379 | + { |
| 380 | + "condition_unique_idxs": pd.Categorical( |
| 381 | + [i % n_conditions for i in range(n_cells)] |
| 382 | + ), |
| 383 | + }, |
| 384 | + index=pd.Index(barcodes), |
| 385 | + ) |
| 386 | + var = pd.DataFrame( |
| 387 | + {"gene_name": [f"gene_{j}" for j in range(n_genes)]}, |
| 388 | + index=[f"gene_{j}" for j in range(n_genes)], |
| 389 | + ) |
| 390 | + |
| 391 | + orig_adata = anndata.AnnData( |
| 392 | + X=rng.normal(size=(n_cells, n_genes)).astype(np.float32), |
| 393 | + obs=obs, |
| 394 | + var=var, |
| 395 | + uns={"Pf2_A": A, "Pf2_B": B, "Pf2_weights": weights}, |
| 396 | + varm=cast(Mapping[str, Sequence[Any]], {"Pf2_C": C}), |
| 397 | + obsm=cast(Mapping[str, Sequence[Any]], {"projections": P}), |
| 398 | + ) |
| 399 | + |
| 400 | + with tempfile.NamedTemporaryFile(suffix=".h5ad") as tmp: |
| 401 | + export_factors(orig_adata, tmp.name, fidelity_threshold=0.90, random_state=seed) |
| 402 | + loaded = load_factors(tmp.name) |
| 403 | + |
| 404 | + # 1. Barcode names roundtrip preserved exactly |
| 405 | + assert loaded.obs_names.tolist() == barcodes |
| 406 | + |
| 407 | + # 2. Factor matrices preserved in float32 |
| 408 | + np.testing.assert_allclose(loaded.uns["Pf2_A"], A, atol=1e-6) |
| 409 | + np.testing.assert_allclose(loaded.uns["Pf2_B"], B, atol=1e-6) |
| 410 | + np.testing.assert_allclose(loaded.uns["Pf2_weights"], weights, atol=1e-6) |
| 411 | + np.testing.assert_allclose(np.asarray(loaded.varm["Pf2_C"]), C, atol=1e-6) |
| 412 | + |
| 413 | + # 3. Projections shape preserved and reconstructed |
| 414 | + assert loaded.obsm["projections"].shape == (n_cells, rank) |
| 415 | + assert loaded.obsm["projections"].dtype == np.float32 |
| 416 | + |
| 417 | + # 4. Weighted projections invariant: WP == P @ B |
| 418 | + expected_wp = np.asarray(loaded.obsm["projections"]) @ np.asarray( |
| 419 | + loaded.uns["Pf2_B"] |
| 420 | + ) |
| 421 | + np.testing.assert_allclose( |
| 422 | + np.asarray(loaded.obsm["weighted_projections"]), expected_wp, atol=1e-5 |
| 423 | + ) |
0 commit comments