scverse / anndata

Annotated data.
http://anndata.readthedocs.io
BSD 3-Clause "New" or "Revised" License
577 stars 154 forks source link

anndata initializer does not verify indices match on layers #1769

Open amcpherson opened 2 hours ago

amcpherson commented 2 hours ago

Please make sure these conditions are met

Report

The AnnData Initializer verifies index and columns of the input X match var and obs, but does not do the same for layers resulting in possible bugs especially when initializing with layers but no X.

Code:

import numpy as np
import pandas as pd
import anndata as ad

var = pd.DataFrame(index=["gene1", "gene2"])
different_indices = ["gene2", "gene1"]
layer_data = pd.DataFrame(np.array([[5, 6], [7, 8]]), columns=different_indices)
layers = {"layer1": layer_data}
adata = ad.AnnData(obs=obs, var=var, layers=layers)

Versions

-----
anndata             0.11.1.dev10+gaf6480e
session_info        1.0.0
-----
cython_runtime      NA
dateutil            2.9.0.post0
exceptiongroup      1.2.2
h5py                3.12.1
natsort             8.4.0
numpy               2.0.2
packaging           24.2
pandas              2.2.3
pytz                2024.2
scipy               1.14.1
six                 1.16.0
zoneinfo            NA
-----
Python 3.10.15 | packaged by conda-forge | (main, Oct 16 2024, 01:24:20) [Clang 17.0.6 ]
macOS-14.6.1-arm64-arm-64bit
-----
Session information updated at 2024-11-15 21:08
amcpherson commented 2 hours ago

Happy to try to fix and submit a PR given some advice on how to proceed