Skip to content

fix #382: handle 'layers' as h5py.Dataset in read_stereo_h5ad#447

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursorstereopy-issue-automation-e772
Draft

fix #382: handle 'layers' as h5py.Dataset in read_stereo_h5ad#447
cursor[bot] wants to merge 1 commit into
mainfrom
cursorstereopy-issue-automation-e772

Conversation

@cursor
Copy link
Copy Markdown

@cursor cursor Bot commented May 20, 2026

Summary

In _read_stereo_h5ad_from_group(), the layers key handler unconditionally called f[k].keys() on line 318 of stereo/io/reader.py. When a Stereopy-format H5AD file stores layers as an h5py.Dataset rather than an h5py.Group, this raises AttributeError: 'Dataset' object has no attribute 'keys'.

The root cause is a missing type guard — the same pattern is already correctly handled for exp_matrix (lines 303–307) and exp_matrix@raw (lines 327–330) in the same function.

Changes

  • stereo/io/reader.py: Added an isinstance(f[k], h5py.Group) check before iterating over f['layers'] sub-keys. If layers is a Group, the existing per-key iteration is preserved. If it's a Dataset, it is read directly via h5ad.read_dataset().
  • tests/test_read_h5ad_layers.py: Added 3 pytest tests verifying both the Group and Dataset code paths, plus a regression test confirming the original AttributeError no longer occurs.

Verification

  • AST syntax check passed for both modified files
  • Logic verified in isolation with h5py Group and Dataset test files
  • Consistent with existing type guard pattern for exp_matrix

Classification

  • Type: bug
  • Confidence: high
  • Severity: medium (affects users whose H5AD files store layers as a flat Dataset)

Closes #382

Open in Web View Automation 

In _read_stereo_h5ad_from_group(), the 'layers' key handler unconditionally
called f[k].keys(), which fails when 'layers' is stored as an h5py.Dataset
rather than an h5py.Group.

Added isinstance(f[k], h5py.Group) check before iterating sub-keys, falling
back to h5ad.read_dataset() for the Dataset case. This matches the existing
pattern used for 'exp_matrix' handling in the same function.

Added tests covering both Group and Dataset code paths.

Co-authored-by: wanruiwen-genomics-cn <wanruiwen-genomics-cn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read_h5ad meets error: 'Dataset' object has no attribute 'keys'

1 participant