Repository navigation
fix(concat_on_disk): retain aligned-mapping keys under join="outer" - #2558
alejandro-publius wants to merge 7 commits into
Conversation
`concat_on_disk` concatenated the aligned mappings (`.obsm`/`.varm` and `.layers`) over the intersection of their keys regardless of `join`. Under `join="outer"` this silently dropped any key present in only some inputs (and, when retained keys had differing shapes, raised in the dense path), diverging from in-memory `anndata.concat`, which keeps the union and fills the gaps. The concatenation-axis mapping writer now mirrors `outer_concat_aligned_mapping`: it takes the union of keys for outer joins, generates outer reindexers to pad each key's free axis, and fills keys missing from some objects with `fill_value` (promoting integer dtypes where needed) by materializing just those keys through the same `concat_arrays` path the in-memory concat uses. Keys present in every object that need no reindexing keep the memory-efficient streaming path. Closes scverse#2394
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2558 +/- ##
==========================================
- Coverage 87.49% 85.64% -1.85%
==========================================
Files 49 49
Lines 8058 8097 +39
==========================================
- Hits 7050 6935 -115
- Misses 1008 1162 +154
|
…paths Use numeric dense/sparse/df obsm with an unshared key and differing widths, so the test exercises union-of-keys, outer reindexing, and the fill paths for all three element types while staying writable on the minimum supported h5py (the previous fixture had a NaN-filled string column that older h5py cannot write).
530db38 to
04aa7a8
Compare
| if reindexers is None: | ||
| reindexers = gen_outer_reindexers(elems, ns, new_index=index, axis=axis) | ||
| arrays = [ | ||
| (elem if isinstance(elem, pd.DataFrame) else to_memory(elem)) |
There was a problem hiding this comment.
Why do these need to be brought into memory?
There was a problem hiding this comment.
Is it to avoid rewriting _write_concat_sequence to handle missing elements?
There was a problem hiding this comment.
Yes—that was the reason in the original implementation, but I agree that materializing the present arrays undermined concat_on_disk.
I’ve since rewritten that path so it remains streaming: missing dense entries use lazy Dask fill blocks, missing sparse entries use shaped sparse fill blocks, and present arrays remain backed and flow through _write_concat_sequence. I also added coverage for custom fill values and axis-1 sparse layers while verifying the result still matches anndata.concat.
Does the revised approach address your concern?
…ng into memory Addresses review feedback on scverse#2558. The previous approach routed every outer-join `.obsm`/`.varm`/`.layers` key that needed filling through `to_memory()` + in-memory `concat_arrays`, pulling the present objects' full arrays into RAM and defeating the point of `concat_on_disk`. Instead, keep the existing streaming writers and teach them about missing keys directly: `write_concat_dense` contributes a lazy `da.full` block and `write_concat_sparse` contributes an empty sparse block (via `_sparse_fill_block`) for each object missing the key, while present objects stay backed/streamed exactly as they do for `X`. `_write_concat_mappings` now streams every key through `_write_concat_sequence`, so `_concat_missing_mapping_key` is gone. Fill shapes come from `ns` (per-object size along the concat axis) and `_target_free_width` (the reindexed width). Result is byte-identical to `anndata.concat` (regression tests unchanged and still pass) but no longer materializes present arrays. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TPXJeYVpsPJtcG92Zrc44B
…ls for missing keys Follow-up to the streaming rewrite. Adds two outer-join regression tests: - a non-default `fill_value` with keys missing from the leading object, which exercises the materialized (non-empty) sparse fill and the fill built for the first object. - a sparse `.layers` key missing under `axis=1`, which drives the fill along the layer's column axis. Marks the width-inference guard in `_target_free_width` as unreachable (`# pragma: no cover`): a key only reaches it via the union/intersection of the mappings, so at least one object always carries it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TPXJeYVpsPJtcG92Zrc44B
…mappings # Conflicts: # src/anndata/experimental/merge.py
|
Sorry for the slow reply here. To answer the question directly, yes, that was the reason, and it was the wrong call. Bringing the present elements into memory was avoiding the work of teaching the write path to handle missing elements. That has since been rewritten. I have also just merged main in, since the mypy adoption in #2577 and the compat cleanup in #2574 had touched the same code paths. Two things there are worth a look, since they are judgement calls rather than mechanical conflict resolution.
The Locally |
|
@ilan-gold gentle ping on this one. Since my last comment the in-memory fallback is gone: an absent key now contributes a lazily built fill block that stacks onto the other reindexed blocks, so nothing is brought into memory. CI is green and it applies cleanly to I have also opened #2636 ( |
concat_on_diskwithjoin='outer'doesn't retain all.obsmfields #2394Problem
concat_on_diskwrites the aligned mappings (.obsm/.varmand.layers) over the intersection of their keys, regardless ofjoin. Underjoin="outer"this silently drops any mapping key present in only some of the inputs — unlike in-memoryanndata.concat, which keeps the union and fills the missing entries. When the retained keys additionally have differing shapes, the dense path raisesValueError: Shapes do not aligninstead of padding.Fix
_write_concat_mappingsnow mirrors the in-memoryouter_concat_aligned_mapping:join="outer"(intersection for"inner", unchanged);fill_value(promoting integer dtypes to float where the fill isNaN), by materializing just those keys through the sameconcat_arrayspath the in-memory concat uses.Keys present in every object that need no reindexing keep the existing memory-efficient streaming path, so the common case is unchanged. As a side effect this also fixes the inner-join case where differing
.obsmwidths previously raised.Tests
test_concat_on_disk_outer_mapping_missing_keys—.obsmwith dense / sparse / dataframe values, one key missing from an object and shared keys of differing widths.test_concat_on_disk_outer_layers_missing_keys— a.layerskey missing from one object with a differingvaraxis (exercises the reindexed path).Both assert the on-disk result equals in-memory
concat. The fulltests/test_concatenate_disk.pysuite passes (498 tests).