Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix outer concatenation along axis=1 for missing layers #1291

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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