dynverse / anndata

Annotated multivariate observation data in R
https://anndata.dynverse.org
Other
43 stars 4 forks source link

AttributeError: 'dict' object has no attribute 'dtype' #24

Closed pati-ni closed 1 year ago

pati-ni commented 1 year ago

When I try to load an anndata h5ad to R I get the following error:

ann <- read_h5ad("/tmp/test.h5ad")
Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
  AttributeError: 'dict' object has no attribute 'dtype'
Run `reticulate::py_last_error()` for details.

Here is the backtrace:

> reticulate::py_last_error()

── Python Exception Message ─────────────────────────────────────────────────────────
Traceback (most recent call last):
  File "/home/main/analysis/miniconda3/envs/Renv/lib/python3.9/site-packages/anndata/readwrite/read.py", line 447, in read_h5ad
    constructor_args = _read_args_from_h5ad(filename=filename, chunk_size=chunk_size)
  File "/home/main/analysis/miniconda3/envs/Renv/lib/python3.9/site-packages/anndata/readwrite/read.py", line 502, in _read_args_from_h5ad
    return AnnData._args_from_dict(d)
  File "/home/main/analysis/miniconda3/envs/Renv/lib/python3.9/site-packages/anndata/core/anndata.py", line 2157, in _args_from_dict
    if key in d_true_keys[true_key].dtype.names:
AttributeError: 'dict' object has no attribute 'dtype'

── R Traceback ──────────────────────────────────────────────────────────────────────
    ▆
 1. └─anndata::read_h5ad("/tmp/test.h5ad")
 2.   ├─anndata:::py_to_r_ifneedbe(...)
 3.   └─python_anndata$read_h5ad(filename = filename, backed = backed)
 4.     └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)

Digging into the exception:

        # transform recarray to dataframe
        for true_key, keys in AnnData._H5_ALIASES_NAMES.items():
            if d_true_keys[true_key] is not None:
                for key in keys:
                    if key in d_true_keys[true_key].dtype.names:
                        d_true_keys[true_key] = pd.DataFrame.from_records(
                            d_true_keys[true_key], index=key)
                        break
                d_true_keys[true_key].index = d_true_keys[true_key].index.astype('U')
                # transform to unicode string
                # TODO: this is quite a hack
                for c in d_true_keys[true_key].columns:
                    if is_string_dtype(d_true_keys[true_key][c]):
                        d_true_keys[true_key][c] = pd.Index(
                            d_true_keys[true_key][c]).astype('U').values

Upon failure, true_key keys are equal to obs and {'row_names', 'smp_names', 'index', 'obs_names'} when the code fails.

Is there something wrong with my object and it errors over there or is it a bug?

pati-ni commented 1 year ago

Reinstalling the packages in a separate environment sorted the issue.