Skip to content

Commit

Permalink
Backport PR scverse#1291: Fix outer concatenation along axis=1 for mi…
Browse files Browse the repository at this point in the history
…ssing layers (scverse#1292)

Co-authored-by: Isaac Virshup <[email protected]>
  • Loading branch information
meeseeksmachine and ivirshup authored Jan 11, 2024
1 parent c63461b commit 770e97b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion anndata/_core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,13 @@ def outer_concat_aligned_mapping(
result = {}
ns = [m.parent.shape[axis] for m in mappings]

def missing_element(n: int, axis: Literal[0, 1] = 0) -> np.ndarray:
"""Generates value to use when there is a missing element."""
if axis == 0:
return np.zeros((n, 0), dtype=bool)
else:
return np.zeros((0, n), dtype=bool)

for k in union_keys(mappings):
els = [m.get(k, MissingVal) for m in mappings]
if reindexers is None:
Expand All @@ -924,7 +931,7 @@ def outer_concat_aligned_mapping(
# We should probably just handle missing elements for all types
result[k] = concat_arrays(
[
el if not_missing(el) else np.zeros((n, 0), dtype=bool)
el if not_missing(el) else missing_element(n, axis=axis)
for el, n in zip(els, ns)
],
cur_reindexers,
Expand Down
19 changes: 19 additions & 0 deletions anndata/tests/test_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,3 +1550,22 @@ def test_error_on_mixed_device():

for p in permutations([cp_adata, cp_sparse_adata]):
concat(p)


def test_concat_on_var_outer_join(array_type):
# https://github.com/scverse/anndata/issues/1286
a = AnnData(
obs=pd.DataFrame(index=[f"cell_{i:02d}" for i in range(10)]),
var=pd.DataFrame(index=[f"gene_{i:02d}" for i in range(10)]),
layers={
"X": array_type(np.ones((10, 10))),
},
)
b = AnnData(
obs=pd.DataFrame(index=[f"cell_{i:02d}" for i in range(10)]),
var=pd.DataFrame(index=[f"gene_{i:02d}" for i in range(10, 20)]),
)

# This shouldn't error
# TODO: specify expected result while accounting for null value
_ = concat([a, b], join="outer", axis=1)
2 changes: 2 additions & 0 deletions docs/release-notes/0.10.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
```{rubric} Bugfix
```

* Fix outer concatenation along variables when only a subset of objects had an entry in layers {pr}`1291` {user}`ivirshup`

```{rubric} Documentation
```

Expand Down

0 comments on commit 770e97b

Please sign in to comment.