Skip to content

Simplify dev - #11

Merged
adamfreedman merged 3 commits into
mainfrom
simplify_dev
Jul 17, 2026
Merged

Simplify dev#11
adamfreedman merged 3 commits into
mainfrom
simplify_dev

Conversation

@adamfreedman

Copy link
Copy Markdown
Contributor

code review simplification and bug detection

adamfreedman and others added 3 commits July 16, 2026 10:15
…, hoist invariant loop work

- tests/downsampling/utils.py: parameterize assert_stability_table with
  expected_bootstraps, and add a shared run_downsample_clusters_rule() helper
- test_downsample_clusters_rule.py / test_downsample_scaling.py: use the new
  shared helpers instead of duplicating the Snakemake-invocation and
  stability-table-assertion logic
- downsample_clusters.R: compute vars_to_regress once and call SCTransform a
  single time instead of duplicating the call in an if/else; hoist the
  per-replicate-invariant `dat` tibble out of GetJaccardMaxByCluster's cluster
  loop
- make_medium_fixture.R: jitter only the sparse matrix's nonzero values
  in place instead of densifying the whole count matrix; drop a
  PercentageFeatureSet() data.frame check that can't fail; regenerate the
  medium fixture with the fixed generator

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…fixture generator

The prior simplify pass assumed PercentageFeatureSet() always returns a
data.frame and replaced a defensive is.data.frame() check with an
unconditional [[1]]. Confirmed by direct execution that this repo's
installed Seurat (5.5.0) returns a plain numeric vector instead, so [[1]]
extracted only the first cell's value and R silently broadcast it across
every cell's percent.mt. Masked in this fixture only because it has no
genes matching "^MT-" (percent.mt is 0 either way). Restored the
is.data.frame() branch.

Also, switching the jitter step to only perturb the sparse matrix's nonzero
(@x) entries meant cells resampled more than once from the same source cell
had a non-negligible chance (P(X=0)~=0.98 per jittered entry) of staying
byte-identical, reintroducing exact-duplicate cells the jitter exists to
avoid (measured: 163/2500 cells, 6.5%). Added explicit de-duplication that
bumps one entry per repeat draw of the same source index, independent of
jitter's randomness. Regenerated fixture verified to have 0 duplicate cells
(down from 163) and 0 all-zero genes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…rize the bump

Code review found the prior de-dup loop's bump row `((k-1) %% nrow)+1` was
keyed on a global repeat-draw counter and, for the real 300->2500 fixture,
wraps (2200 repeat draws > 2000 genes) so rows 1..200 are reused. Two repeat
draws of the same source cell whose positions differed by exactly nrow could
then get bumped at the same row and, if their jitter also coincided, stay
byte-identical -- the loop never re-checked, and the "guarantee distinctness"
comment overstated a heuristic. The shipped fixture happened to be duplicate-
free at seed 12345, but a different seed/size could silently reintroduce the
duplicate cells this logic exists to prevent.

Key the bump row on the per-source occurrence rank instead (Nth draw of a
source bumped at row N-1), which is injective within each source group and
bounded by the max multiplicity (17 here) so it never wraps; assert it stays
under the gene count. Replace the per-element sparse [<- loop (O(repeats x
nnz) dgCMatrix reallocations) with a single vectorized matrix-index
assignment, and correct the comment to describe best-effort separation rather
than a guarantee.

Regenerated fixture verified: 0 duplicate cells, occurrence ranks peak at 17
(<< 2000 genes, no wrap). Full downsampling suite (rule + workflow + scaling)
passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@adamfreedman
adamfreedman merged commit 85d33ab into main Jul 17, 2026
12 checks passed
@adamfreedman
adamfreedman deleted the simplify_dev branch July 17, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant