Skip to content

Commit 2227ce4

Browse files
committed
enh: allow access to PyBIDS' magic get_*
This patch allows users to access the automated querying of the layout with the ``get_*`` magic (and ``ls_*``, for TF), e.g.: ```Python >>> from templateflow import api >>> api.ls_atlases(template="MNI152NLin6Asym") ['DiFuMo', 'Schaefer2018', 'HOCPA', 'HCP', 'HOCPAL', 'HOSPA'] ``` The existing ``get_*`` methods are not overshadowed: ```Python >>> api.get_citations(template="MNI152NLin6Asym") ['https://doi.org/10.1016/j.neuroimage.2012.01.024', 'https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/Atlases'] ``` The original ``get_*`` queries are accepted: ```Python >>> api.get_atlases() ['AAL', 'DiFuMo', 'v3', 'CerebrA', 'D99', 'WHS', 'VALiDATe', 'v4', 'Schaefer2018', 'HOCPA', 'HCP', 'CerebA', 'CCFv3', 'CHARM', 'HOCPAL', 'SARM', 'HOSPA'] ```
1 parent 3c1f2cb commit 2227ce4

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

templateflow/api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""TemplateFlow's Python Client."""
2+
import sys
3+
from importlib import import_module
24
from json import loads
35
from pathlib import Path
4-
import sys
56
from bids.layout import Query
67

78
from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD, requires_layout
@@ -249,6 +250,16 @@ def get_citations(template, bibtex=False):
249250
return [_to_bibtex(ref, template, idx).rstrip() for idx, ref in enumerate(refs, 1)]
250251

251252

253+
@requires_layout
254+
def __getattr__(key: str):
255+
key = key.replace("ls_", "get_")
256+
if key.startswith("get_") and key not in ("get_metadata", "get_citations"):
257+
return TF_LAYOUT.__getattr__(key)
258+
259+
# Spit out default message if we get this far
260+
raise AttributeError(f"module '{__name__}' has no attribute '{key}'")
261+
262+
252263
def _datalad_get(filepath):
253264
if not filepath:
254265
return

templateflow/tests/test_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ def test_citations(tmp_path, template, urls, fbib, lbib):
9797
else:
9898
# no citations currently
9999
assert False
100+
101+
102+
def test_pybids_magic_get():
103+
"""Check automatic entity expansion of the layout."""
104+
assert sorted(api.ls_atlases()) == sorted(api.TF_LAYOUT.get_atlases())
105+
assert sorted(api.ls_atlases(template="MNI152NLin6ASym")) == sorted(
106+
api.TF_LAYOUT.get_atlases(template="MNI152NLin6ASym")
107+
)
108+
109+
with pytest.raises(TypeError):
110+
api.ls_atlases("MNI152NLin6ASym")

0 commit comments

Comments
 (0)