diff --git a/anndata/_core/merge.py b/anndata/_core/merge.py index 4463399bc..4429a7f15 100644 --- a/anndata/_core/merge.py +++ b/anndata/_core/merge.py @@ -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: @@ -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, diff --git a/anndata/tests/test_concatenate.py b/anndata/tests/test_concatenate.py index 17c11ef70..4a51c5230 100644 --- a/anndata/tests/test_concatenate.py +++ b/anndata/tests/test_concatenate.py @@ -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) diff --git a/docs/release-notes/0.10.5.md b/docs/release-notes/0.10.5.md index cfb4c1e36..0aed44ae4 100644 --- a/docs/release-notes/0.10.5.md +++ b/docs/release-notes/0.10.5.md @@ -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 ```