- 
                Notifications
    You must be signed in to change notification settings 
- Fork 177
fix: fancy indexing fixes backed h5py error #2066
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
          
     Merged
      
      
            ilan-gold
  merged 47 commits into
  scverse:main
from
sjfleming:sf-backed-hdf5-fancy-indexing
  
      
      
   
  Oct 22, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            47 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      7dc2af2
              
                fixes #2064
              
              
                sjfleming 4c2d47d
              
                undo accidental message changes
              
              
                sjfleming d451c74
              
                remove comment
              
              
                sjfleming 080050e
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 77956e9
              
                remove redundant code
              
              
                sjfleming dae91ab
              
                test the new function explicitly
              
              
                sjfleming c99ec17
              
                additional test case
              
              
                sjfleming 989cca5
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 887641e
              
                simplify code
              
              
                sjfleming eefb639
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] eb55618
              
                ruff check
              
              
                sjfleming 07c4ab9
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                flying-sheep be9cec0
              
                address PR comments
              
              
                sjfleming 4368f0a
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 58b990b
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                sjfleming 5ee3658
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                sjfleming 7c23963
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                sjfleming 28b874c
              
                fix subset type
              
              
                flying-sheep b669f71
              
                use correct type
              
              
                flying-sheep 9cc31e2
              
                compact
              
              
                flying-sheep 78c6141
              
                some more compacting
              
              
                flying-sheep b2b68c3
              
                early return
              
              
                flying-sheep 8c4ce4e
              
                fix types
              
              
                flying-sheep b4d70d7
              
                fix type hint and provide comment
              
              
                sjfleming 8fde064
              
                simplify
              
              
                flying-sheep df7a9ea
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                flying-sheep ee30fa6
              
                slice with most selective index first
              
              
                sjfleming e3c16b7
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 31f5bc5
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                sjfleming 08f2cef
              
                move out
              
              
                flying-sheep 64af43e
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                flying-sheep e2b9bdc
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                flying-sheep bf8f2a2
              
                implement suggestion to remove extra unique call
              
              
                sjfleming bdab900
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                sjfleming 86cb5ff
              
                chore(deps): bump astral-sh/setup-uv from 6 to 7 (#2151)
              
              
                dependabot[bot] fdc7e30
              
                ci: pre-commit autoupdate (#2150)
              
              
                pre-commit-ci[bot] c6310ff
              
                chore: Backport PR #2154 on branch main ((chore): generate 0.12.3 rel…
              
              
                meeseeksmachine 2d5efbc
              
                fix: explicit error for `pandas.MultiIndex` (#2096)
              
              
                ilan-gold 78fd77e
              
                added benchmarks for backed hdf5
              
              
                sjfleming 66b25d6
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 9734678
              
                slightly improve reliability of peak mem calc
              
              
                sjfleming c558d7e
              
                revert the benchmark utils change
              
              
                sjfleming ffa1a97
              
                pare down benchmarks
              
              
                sjfleming fd9f374
              
                ci: pre-commit autoupdate (#2160)
              
              
                pre-commit-ci[bot] a99b78f
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] ec3cbb8
              
                modify new benchmarks, fix ruff errors
              
              
                sjfleming 655c3ba
              
                Merge branch 'main' into sf-backed-hdf5-fancy-indexing
              
              
                sjfleming File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| from __future__ import annotations | ||
|  | ||
| import tempfile | ||
| from pathlib import Path | ||
|  | ||
| import numpy as np | ||
| import pandas as pd | ||
| from scipy import sparse | ||
|  | ||
| import anndata as ad | ||
|  | ||
|  | ||
| class BackedHDF5: | ||
| def setup(self): | ||
| n_obs, n_vars = 10000, 5000 | ||
| self.n_obs = n_obs | ||
| self.n_vars = n_vars | ||
|  | ||
| # Create count matrix | ||
| X = sparse.random(n_obs, n_vars, density=0.1, format="csr") | ||
| X.data = np.random.poisson(5, X.data.shape).astype(np.float32) | ||
|  | ||
| # Create obs and var dataframes | ||
| obs = pd.DataFrame( | ||
| { | ||
| "cell_type": pd.Categorical( | ||
| np.random.choice(["TypeA", "TypeB", "TypeC"], n_obs) | ||
| ), | ||
| "total_counts": np.random.randint(1000, 5000, n_obs), | ||
| }, | ||
| index=[f"cell_{i}" for i in range(n_obs)], | ||
| ) | ||
|  | ||
| var = pd.DataFrame( | ||
| { | ||
| "gene_name": [f"gene_{i}" for i in range(n_vars)], | ||
| }, | ||
| index=[f"ENSG_{i:08d}" for i in range(n_vars)], | ||
| ) | ||
|  | ||
| # Create AnnData object and save to HDF5 | ||
| adata = ad.AnnData(X=X, obs=obs, var=var) | ||
|  | ||
| # Create temporary file | ||
| self.temp_path = Path(tempfile.mkdtemp()) / "test_data.h5ad" | ||
| adata.write_h5ad(self.temp_path) | ||
|  | ||
| # Open as backed | ||
| self.adata_backed = ad.read_h5ad(self.temp_path, backed="r") | ||
|  | ||
| # Prepare indices for duplicate index testing | ||
| self.obs_idx_with_dupes = np.array([0, 1, 0, 2, 1] * (n_obs // 100 + 1))[ | ||
| : (n_obs // 10) | ||
| ] | ||
| self.var_idx_with_dupes = np.array([0, 1, 2, 0, 3] * (n_vars // 100 + 1))[ | ||
| : (n_vars // 10) | ||
| ] | ||
| self.obs_idx_no_dupes = np.arange(0, n_obs, 10) | ||
| self.var_idx_no_dupes = np.arange(0, n_vars, 10) | ||
|  | ||
| def time_slice_obs(self, *_): | ||
| """Time slicing observations from backed HDF5""" | ||
| self.adata_backed[0 : (self.n_obs // 2), :] | ||
|  | ||
| def time_slice_obs_to_memory(self, *_): | ||
| """Time slicing observations from backed HDF5""" | ||
| self.adata_backed[0 : (self.n_obs // 2), :].to_memory() | ||
|  | ||
| def peakmem_slice_obs(self, *_): | ||
| """Peak memory for slicing observations from backed HDF5""" | ||
| self.adata_backed[0 : (self.n_obs // 2), :] | ||
|  | ||
| def time_fancy_index_no_dupes(self, *_): | ||
| """Time fancy indexing without duplicates""" | ||
| self.adata_backed[self.obs_idx_no_dupes, self.var_idx_no_dupes] | ||
|  | ||
| def peakmem_fancy_index_no_dupes(self, *_): | ||
| """Peak memory for fancy indexing without duplicates""" | ||
| self.adata_backed[self.obs_idx_no_dupes, self.var_idx_no_dupes] | ||
|  | ||
| def time_fancy_index_no_dupes_to_memory(self, *_): | ||
| """Time fancy indexing without duplicates""" | ||
| self.adata_backed[self.obs_idx_no_dupes, self.var_idx_no_dupes].to_memory() | ||
|  | ||
| def time_index_with_dupes_obs(self, *_): | ||
| """Time fancy indexing with duplicate observation indices""" | ||
| self.adata_backed[self.obs_idx_with_dupes, :] | ||
|  | ||
| def peakmem_index_with_dupes_obs(self, *_): | ||
| """Peak memory for fancy indexing with duplicate observation indices""" | ||
| self.adata_backed[self.obs_idx_with_dupes, :] | ||
|  | ||
| def time_to_memory_subset(self, *_): | ||
| """Time converting subset to memory""" | ||
| subset = self.adata_backed[0 : (self.n_obs // 4), 0 : (self.n_vars // 4)] | ||
| subset.to_memory() | ||
|  | ||
| def peakmem_to_memory_subset(self, *_): | ||
| """Peak memory for converting subset to memory""" | ||
| subset = self.adata_backed[0 : (self.n_obs // 4), 0 : (self.n_vars // 4)] | ||
| subset.to_memory() | ||
|  | ||
| def teardown(self, *_): | ||
| """Clean up temporary files""" | ||
| if hasattr(self, "adata_backed"): | ||
| self.adata_backed.file.close() | ||
| if hasattr(self, "temp_path") and self.temp_path.exists(): | ||
| self.temp_path.unlink() | ||
| self.temp_path.parent.rmdir() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't
orderalready sorted soargsortwould just do anarange?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
orderis not already sorted here, it's just the index order that sortsaxis_idx. I did try the above suggested change, but the result was that tests would no longer pass.