Skip to content

fix: keep legacy graph metrics usable without anndata.acc - #4532

Merged
flying-sheep merged 4 commits into
scverse:mainfrom
rpinho:fix-metrics-legacy-anndata
Nov 26, 2026
Merged

flying-sheep merged 4 commits into
scverse:mainfrom
rpinho:fix-metrics-legacy-anndata

Conversation

@rpinho

@rpinho rpinho commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor
  • Closes #
  • Tests included or not required because:
  • Release notes not necessary because:
  • LLM or coding agent used: GPT-6.1 (OpenAI) via Codex wrote the patch and the test; Claude Opus 5.5 via Claude Code reproduced the bug separately and opened this PR
  • If one was used, I, a human, have reviewed, understood and tested every changed line

morans_i and gearys_c raise ModuleNotFoundError for AnnData input when anndata.acc is unavailable, including supported anndata 0.12.14. Calling either metric on AnnData with a connectivities graph reproduces it, while passing the graph and expression array directly works. The patch imports the accessor parser only when vals is a string that needs parsing. The test blocks the accessor import for the AnnData call and compares the result with the array call for both metrics.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

❌ 4 Tests Failed:

Tests completed Failed Passed Skipped
2654 4 2650 1400
View the top 3 failed test(s) by shortest run time
tests/plotting/legacy/embedded/test_embeddings.py::test_missing_values_continuous[spatial-na_color.black_tup-vbounds.vcenter]
Stack Traces | 0.957s run time
image_dir = PosixPath('.../plotting/legacy/_images')
request = <FixtureRequest for <Function test_missing_values_continuous[spatial-na_color.black_tup-vbounds.vcenter]>>
plot_cmp = functools.partial(<function plot_cmp.<locals>.save_and_compare at 0x75b4c4bec3b0>, root=PosixPath('.../plotting/legacy/_images/embedding-missing-values'))
adata = AnnData object with n_obs × n_vars = 2600 × 20
    obs: 'label', 'label_missing', '1_missing'
    uns: 'spatial', 'pca', 'label_colors', 'label_missing_colors'
    obsm: 'spatial', 'X_pca'
    varm: 'PCs'
    layers: None (.X)
plotfunc = functools.partial(<function spatial at 0x75b4acf5dfe0>, show=False)
na_color = (0, 0, 0, 1)
vbounds = {'vmin': 0, 'vmax': 'p99', 'vcenter': 0.1, 'norm': None}

    #x1B[0m#x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_missing_values_continuous#x1B[39;49;00m(#x1B[90m#x1B[39;49;00m
        *,#x1B[90m#x1B[39;49;00m
        image_dir: Path,#x1B[90m#x1B[39;49;00m
        request: pytest.FixtureRequest,#x1B[90m#x1B[39;49;00m
        plot_cmp,#x1B[90m#x1B[39;49;00m
        adata,#x1B[90m#x1B[39;49;00m
        plotfunc,#x1B[90m#x1B[39;49;00m
        na_color,#x1B[90m#x1B[39;49;00m
        vbounds,#x1B[90m#x1B[39;49;00m
    ):#x1B[90m#x1B[39;49;00m
        plot_cmp = partial(plot_cmp, root=image_dir / #x1B[33m"#x1B[39;49;00m#x1B[33membedding-missing-values#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
        base_name = request.node.name#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
        #x1B[90m# Passing through a dict so it's easier to use default values#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
        kwargs = {}#x1B[90m#x1B[39;49;00m
        kwargs.update(vbounds)#x1B[90m#x1B[39;49;00m
        #x1B[94mif#x1B[39;49;00m na_color #x1B[95mis#x1B[39;49;00m #x1B[95mnot#x1B[39;49;00m #x1B[94mNone#x1B[39;49;00m:#x1B[90m#x1B[39;49;00m
            kwargs[#x1B[33m"#x1B[39;49;00m#x1B[33mna_color#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] = na_color#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
>       plotfunc(adata, color=[#x1B[33m"#x1B[39;49;00m#x1B[33m1#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m, #x1B[33m"#x1B[39;49;00m#x1B[33m1_missing#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m], **kwargs)#x1B[90m#x1B[39;49;00m

#x1B[1m#x1B[.../legacy/embedded/test_embeddings.py#x1B[0m:147: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

adata = AnnData object with n_obs × n_vars = 2600 × 20
    obs: 'label', 'label_missing', '1_missing'
    uns: 'spatial', 'pca', 'label_colors', 'label_missing_colors'
    obsm: 'spatial', 'X_pca'
    varm: 'PCs'
    layers: None (.X)
kwargs = {'show': False, 'color': ['1', '1_missing'], 'vmin': 0, 'vmax': 'p99', ...}

    #x1B[0m#x1B[37m@wraps#x1B[39;49;00m(request.param)#x1B[90m#x1B[39;49;00m
    #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mf#x1B[39;49;00m(adata, **kwargs):#x1B[90m#x1B[39;49;00m
>       #x1B[94mwith#x1B[39;49;00m pytest.warns(#x1B[96mFutureWarning#x1B[39;49;00m, match=#x1B[33mr#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m#x1B[33m.*Use .*`squidpy.*` instead#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m):#x1B[90m#x1B[39;49;00m
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE       ResourceWarning: Implicitly cleaning up <HTTPError 403: 'Forbidden'>#x1B[0m

#x1B[1m#x1B[.../legacy/embedded/test_embeddings.py#x1B[0m:47: ResourceWarning
src/scanpy/datasets/_ebi_expression_atlas.py::scanpy.datasets._ebi_expression_atlas.ebi_expression_atlas
Stack Traces | 1.1s run time
133     labels with a single or :attr:`~anndata.AnnData.n_obs` distinct values.
134 
135 Returns
136 -------
137 Annotated data matrix.
138 
139 Example
140 -------
141 >>> import scanpy as sc
142 >>> sc.datasets.ebi_expression_atlas("E-ENAD-19")  # doctest: +ELLIPSIS
UNEXPECTED EXCEPTION: <HTTPError 403: 'Forbidden'>
Traceback (most recent call last):
  File "<doctest scanpy.datasets._ebi_expression_atlas.ebi_expression_atlas[1]>", line 1, in <module>
  File ".../scanpy/datasets/_ebi_expression_atlas.py", line 159, in ebi_expression_atlas
    download_experiment(accession)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File ".../scanpy/datasets/_utils.py", line 22, in wrapper
    return f(*args, **kwargs)
  File ".../scanpy/datasets/_ebi_expression_atlas.py", line 48, in download_experiment
    sniff_url(accession)
    ~~~~~~~~~^^^^^^^^^^^
  File ".../scanpy/datasets/_ebi_expression_atlas.py", line 39, in sniff_url
    with urlopen(base_url):  # Check if server up/ dataset exists
         ~~~~~~~^^^^^^^^^^
  File ".../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py", line 187, in urlopen
    return opener.open(url, data, timeout)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File ".../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py", line 493, in open
    response = meth(req, response)
  File ".../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py", line 602, in http_response
    response = self.parent.error(
        'http', request, response, code, msg, hdrs)
  File ".../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py", line 531, in error
    return self._call_chain(*args)
           ~~~~~~~~~~~~~~~~^^^^^^^
  File ".../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py", line 464, in _call_chain
    result = func(*args)
  File ".../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py", line 611, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
https://www.ebi.ac..../sc/experiments/E-ENAD-19/
#x1B[1m#x1B[31m.../scanpy/datasets/_ebi_expression_atlas.py#x1B[0m:142: UnexpectedException
src/scanpy/datasets/_datasets.py::scanpy.datasets._datasets.pbmc3k_processed
Stack Traces | 1.7s run time
511 - :func:`~scanpy.tl.leiden` clusters in `.obs["leiden"]`
512 
513 Returns
514 -------
515 Annotated data matrix.
516 
517 Examples
518 --------
519 >>> import scanpy as sc
520 >>> sc.datasets.pbmc3k_processed()
Differences (unified diff with -expected +actual):
    @@ -1,2 +1,6 @@
    +ResourceWarning: unclosed <ssl.SSLSocket [closed] fd=19, family=2, type=1, proto=6>
    +    obj, end = self.scan_once(s, idx)
    +ResourceWarning: Implicitly cleaning up <HTTPError 403: 'Forbidden'>
    +    _warnings.warn(self.warn_message, ResourceWarning)
     AnnData object with n_obs × n_vars = 2638 × 1838
         obs: 'n_genes', 'percent_mito', 'n_counts', 'louvain'

#x1B[1m#x1B[.../scanpy/datasets/_datasets.py#x1B[0m:520: DocTestFailure
tests/test_datasets.py::test_ebi_expression_atlas
Stack Traces | 5.11s run time
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x75b460dba430>

    #x1B[0m#x1B[37m@pytest#x1B[39;49;00m.mark.internet#x1B[90m#x1B[39;49;00m
    #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_ebi_expression_atlas#x1B[39;49;00m(monkeypatch: pytest.MonkeyPatch):#x1B[90m#x1B[39;49;00m
        #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscanpy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mdatasets#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m _ebi_expression_atlas #x1B[94mas#x1B[39;49;00m ea_mod#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
        #x1B[90m# make sure we use chunks when testing.#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
        #x1B[90m# This dataset has ~31k entries, so 20k entries/chunk = 2 chunks#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
        #x1B[94massert#x1B[39;49;00m #x1B[96mhasattr#x1B[39;49;00m(ea_mod, #x1B[33m"#x1B[39;49;00m#x1B[33mCHUNK_SIZE#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
        monkeypatch.setattr(ea_mod, #x1B[33m"#x1B[39;49;00m#x1B[33mCHUNK_SIZE#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m, #x1B[94m20_000#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
        #x1B[90m# a tiny dataset (~160kB), since EBI’s downloads can be very slow#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
>       adata = sc.datasets.ebi_expression_atlas(#x1B[33m"#x1B[39;49;00m#x1B[33mE-ENAD-19#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m

#x1B[1m#x1B[31mtests/test_datasets.py#x1B[0m:110: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
#x1B[1m#x1B[.../scanpy/datasets/_ebi_expression_atlas.py#x1B[0m:159: in ebi_expression_atlas
    #x1B[0mdownload_experiment(accession)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../scanpy/datasets/_utils.py#x1B[0m:22: in wrapper
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m f(*args, **kwargs)#x1B[90m#x1B[39;49;00m
           ^^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../scanpy/datasets/_ebi_expression_atlas.py#x1B[0m:48: in download_experiment
    #x1B[0msniff_url(accession)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../scanpy/datasets/_ebi_expression_atlas.py#x1B[0m:39: in sniff_url
    #x1B[0m#x1B[94mwith#x1B[39;49;00m urlopen(base_url):  #x1B[90m# Check if server up/ dataset exists#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
         ^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31m../..................../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py#x1B[0m:187: in urlopen
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m opener.open(url, data, timeout)#x1B[90m#x1B[39;49;00m
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31m../..................../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py#x1B[0m:493: in open
    #x1B[0mresponse = meth(req, response)#x1B[90m#x1B[39;49;00m
               ^^^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31m../..................../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py#x1B[0m:602: in http_response
    #x1B[0mresponse = #x1B[96mself#x1B[39;49;00m.parent.error(#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31m../..................../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py#x1B[0m:531: in error
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m #x1B[96mself#x1B[39;49;00m._call_chain(*args)#x1B[90m#x1B[39;49;00m
           ^^^^^^^^^^^^^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31m../..................../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py#x1B[0m:464: in _call_chain
    #x1B[0mresult = func(*args)#x1B[90m#x1B[39;49;00m
             ^^^^^^^^^^^#x1B[90m#x1B[39;49;00m
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <urllib.request.HTTPDefaultErrorHandler object at 0x75b4c4395be0>
req = <urllib.request.Request object at 0x75b463652f10>
fp = <http.client.HTTPResponse object at 0x75b461397d30>, code = 403
msg = 'Forbidden', hdrs = <http.client.HTTPMessage object at 0x75b460d551d0>

    #x1B[0m#x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mhttp_error_default#x1B[39;49;00m(#x1B[96mself#x1B[39;49;00m, req, fp, code, msg, hdrs):#x1B[90m#x1B[39;49;00m
>       #x1B[94mraise#x1B[39;49;00m HTTPError(req.full_url, code, msg, hdrs, fp)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE       urllib.error.HTTPError: HTTP Error 403: Forbidden#x1B[0m
#x1B[1m#x1B[31mE       https://www.ebi.ac..../sc/experiments/E-ENAD-19/#x1B[0m

#x1B[1m#x1B[31m../..................../_temp/uv-python-dir/cpython-3.14.10-linux-x86_64-gnu/lib/python3.14/urllib/request.py#x1B[0m:611: HTTPError

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@Zethson

Zethson commented Nov 18, 2026

Copy link
Copy Markdown
Member

Please fill out our PR template.

@rpinho

rpinho commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Sorry about that. The template is filled in now.

@flying-sheep flying-sheep added this to the 1.13.1 milestone Nov 26, 2026
@flying-sheep

flying-sheep commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

whoops, indeed that import was in the wrong spot!

no need for a test for something this trivial though.

@flying-sheep
flying-sheep merged commit 54b9f64 into scverse:main Nov 26, 2026
6 of 11 checks passed
flying-sheep pushed a commit that referenced this pull request Nov 26, 2026
…able without anndata.acc) (#4551)

Co-authored-by: Ricardo Pinho <[email protected]>
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.

3 participants