22
33import shutil
44from pathlib import Path
5+ from typing import TYPE_CHECKING
56from unittest .mock import patch
67
78import h5py
89import numpy as np
10+ import pandas as pd
911import pytest
1012
1113import scanpy as sc
1214
15+ if TYPE_CHECKING :
16+ from typing import Literal
17+
18+
1319ROOT = Path (__file__ ).parent
1420ROOT = ROOT / "_data" / "10x_data"
1521VISIUM_ROOT = Path (__file__ ).parent / "_data" / "visium_data"
@@ -28,15 +34,19 @@ def assert_anndata_equal(a1, a2):
2834 pytest .param (
2935 ROOT / "1.2.0" / "filtered_gene_bc_matrices" / "hg19_chr21" ,
3036 ROOT / "1.2.0" / "filtered_gene_bc_matrices_h5.h5" ,
37+ id = "1.2.0" ,
3138 ),
3239 pytest .param (
3340 ROOT / "3.0.0" / "filtered_feature_bc_matrix" ,
3441 ROOT / "3.0.0" / "filtered_feature_bc_matrix.h5" ,
42+ id = "3.0.0" ,
3543 ),
3644 ],
3745)
38- @pytest .mark .parametrize ("prefix" , [None , "prefix_" ])
39- def test_read_10x (tmp_path , mtx_path , h5_path , prefix ):
46+ @pytest .mark .parametrize ("prefix" , [None , "prefix_" ], ids = ["no_prefix" , "prefix" ])
47+ def test_read_10x (
48+ tmp_path : Path , mtx_path : Path , h5_path : Path , prefix : str | None
49+ ) -> None :
4050 if prefix is not None :
4151 # Build files named "prefix_XXX.xxx" in a temporary directory.
4252 mtx_path_orig = mtx_path
@@ -66,6 +76,27 @@ def test_read_10x(tmp_path, mtx_path, h5_path, prefix):
6676 assert_anndata_equal (sc .read_h5ad (from_mtx_pth ), sc .read_h5ad (from_h5_pth ))
6777
6878
79+ @pytest .mark .parametrize (
80+ ("genes" , "col_dtypes" ),
81+ [
82+ pytest .param ("symbols" , dict (gene_ids = "int64" ), id = "symbols" ),
83+ pytest .param ("ids" , dict (gene_symbols = "str" ), id = "ids" ),
84+ ],
85+ )
86+ def test_read_10x_mtx_int (
87+ genes : Literal ["symbols" , "ids" ], col_dtypes : dict [str , str ]
88+ ) -> None :
89+ str_dt = "str" if pd .options .future .infer_string else "object"
90+ col_dtypes = {k : str_dt if v == "str" else v for k , v in col_dtypes .items ()}
91+
92+ adata = sc .read_10x_mtx (
93+ ROOT / "int-ids" , var_names = f"gene_{ genes } " , compressed = False
94+ )
95+
96+ assert adata .var .index .dtype == str_dt
97+ assert dict (adata .var .dtypes ) == dict (feature_types = str_dt , ** col_dtypes )
98+
99+
69100def test_read_10x_h5_v1 ():
70101 spec_genome_v1 = sc .read_10x_h5 (
71102 ROOT / "1.2.0" / "filtered_gene_bc_matrices_h5.h5" ,
0 commit comments