Skip to content

Commit 7f2cfcd

Browse files
authored
Vectorize core computations and expand property-based test suite (#555)
* Vectorize core computations and expand property-based test suite * Format with ruff * Fix ty check diagnostics in test_invariants.py
1 parent e5a896b commit 7f2cfcd

8 files changed

Lines changed: 234 additions & 26 deletions

File tree

‎scrise/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .opq import OPQQuantizer, find_optimal_opq
2121
from .rank_selection import bicv
2222

23-
__version__ = "1.2.0"
23+
__version__ = "1.3.0"
2424

2525
__all__ = [
2626
"CellTypeAlignmentResults",

‎scrise/alignment_stats.py‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def compute_eta_squared(
118118

119119
y_mean = np.mean(y)
120120
ss_total = np.sum((y - y_mean) ** 2)
121-
if ss_total <= 0.0:
121+
if ss_total <= 1e-12:
122122
return 0.0
123123

124124
counts = np.bincount(cell_type_codes, minlength=n_types).astype(float)
@@ -216,21 +216,19 @@ def _permutation_p_values(
216216
"""Empirical one-sided p-values against a rank-permutation null."""
217217
ranks = sp.rankdata(y, method="average")
218218
counts = np.bincount(codes, minlength=n_types).astype(float)
219+
n0 = n_cells - counts
220+
valid = (counts > 0) & (n0 > 0)
221+
denom = np.where(valid, counts * n0, 1.0)
222+
u_offset = counts * (counts + 1.0) / 2.0
219223
null_counts = np.zeros(n_types, dtype=int)
220224

221225
for _ in range(n_permutations):
222226
perm_ranks = rng.permutation(ranks)
223227
perm_sums = np.bincount(codes, weights=perm_ranks, minlength=n_types).astype(
224228
float
225229
)
226-
for k in range(n_types):
227-
n1 = counts[k]
228-
n0 = n_cells - n1
229-
if n1 > 0 and n0 > 0:
230-
u_null = perm_sums[k] - n1 * (n1 + 1.0) / 2.0
231-
auc_null = u_null / (n1 * n0)
232-
if auc_null >= aurocs[k]:
233-
null_counts[k] += 1
230+
auc_null = (perm_sums - u_offset) / denom
231+
null_counts += (auc_null >= aurocs) & valid
234232

235233
return (1.0 + null_counts) / (1.0 + n_permutations)
236234

‎scrise/factorization.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,18 @@ def correct_conditions(X: anndata.AnnData):
3535
numpy.ndarray
3636
Corrected condition factors normalized by sequencing depth
3737
"""
38-
sgIndex = X.obs["condition_unique_idxs"]
39-
40-
counts = np.zeros((np.amax(sgIndex.to_numpy()) + 1, 1))
41-
38+
sgIndex = np.asarray(X.obs["condition_unique_idxs"])
4239
cond_mean = gmean(X.uns["Pf2_A"], axis=1)
4340

4441
if X.X is None:
4542
raise TypeError("X.X must not be None.")
4643
# X.X's declared type is a large union of array-like/backed-storage types
4744
# from the AnnData stub; at runtime this is always a dense or sparse
4845
# in-memory array supporting `.sum`.
49-
x_count = np.asarray(cast(Any, X.X).sum(axis=1))
46+
x_count = np.asarray(cast(Any, X.X).sum(axis=1)).ravel()
5047

51-
for ii in range(counts.size):
52-
counts[ii] = np.sum(x_count[X.obs["condition_unique_idxs"] == ii])
48+
n_conds = int(np.amax(sgIndex)) + 1
49+
counts = np.bincount(sgIndex, weights=x_count, minlength=n_conds).reshape(-1, 1)
5350

5451
lr = LinearRegression()
5552
lr.fit(counts, cond_mean.reshape(-1, 1))

‎scrise/opq.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,9 @@ def find_optimal_opq(
209209
_, D = P_arr.shape
210210

211211
# Candidate M values to evaluate (increasing order for compression)
212-
candidate_Ms = sorted({1, 2, 4, 5, 8, 10, 12, 15, 20, 25, 30, D})
213-
candidate_Ms = [m for m in candidate_Ms if m <= D]
214-
if D not in candidate_Ms:
215-
candidate_Ms.append(D)
212+
candidate_Ms = sorted(
213+
{m for m in (1, 2, 4, 5, 8, 10, 12, 15, 20, 25, 30, D) if m <= D}
214+
)
216215

217216
best_quantizer = None
218217
best_codes = None

‎scrise/rank_selection.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ def _max_feasible_rank(
5959
and enough train genes remain for a BiCV trial to be well-posed."""
6060
cond_idx = X.obs["condition_unique_idxs"].to_numpy().astype(int)
6161
n_cond = int(cond_idx.max()) + 1
62+
cond_counts = np.bincount(cond_idx, minlength=n_cond)
6263
min_train_cells = min(
6364
max(
6465
1,
65-
int(np.sum(cond_idx == c))
66-
- max(1, round(np.sum(cond_idx == c) * held_out_cell_frac)),
66+
int(n_c) - max(1, round(n_c * held_out_cell_frac)),
6767
)
68-
for c in range(n_cond)
68+
for n_c in cond_counts
6969
)
7070
n_train_genes = X.n_vars - max(1, round(X.n_vars * held_out_gene_frac))
7171
return int(min(min_train_cells, n_train_genes))

‎scrise/tests/test_invariants.py‎

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@
1010
elsewhere in the suite.
1111
"""
1212

13+
import tempfile
14+
from collections.abc import Mapping, Sequence
15+
from typing import Any, cast
16+
1317
import anndata
1418
import numpy as np
19+
import pandas as pd
1520
import pytest
1621
from hypothesis import HealthCheck, given, settings
1722
from hypothesis import strategies as st
1823
from hypothesis.extra.numpy import arrays
1924

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
2032
from ..factorization import (
2133
canonical_component_signs,
2234
correct_conditions,
@@ -207,3 +219,205 @@ def test_opq_reconstruction_r2_nondecreasing_in_num_subquantizers(D):
207219
# violated by noise near saturation (r2 close to 1).
208220
assert r2 >= prev_r2 - 0.05
209221
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+
)

‎scrise/tests/test_parafac2.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_root_api_exports():
132132
assert callable(prepare_dataset)
133133
assert hasattr(plotting, "plot_condition_factors")
134134
assert hasattr(plotting, "plot_gene_factors")
135-
assert __version__ == "1.2.0"
135+
assert __version__ == "1.3.0"
136136
assert "bicv" in scrise.__all__
137137
assert "prepare_dataset" in scrise.__all__
138138
assert "plotting" in scrise.__all__

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)