Skip to content
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

RF: Load package data with acres #448

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ sphinx_rtd_theme
sphinxcontrib-apidoc ~= 0.3.0
templateflow
networkx != 2.8.1
acres
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ classifiers = [
license = {file = "LICENSE"}
requires-python = ">=3.10"
dependencies = [
"acres",
"indexed_gzip >= 0.8.8",
"lockfile",
"looseversion",
Expand Down
3 changes: 3 additions & 0 deletions smriprep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

"""

from smriprep.data import load as load_data

from .__about__ import (
__copyright__,
__credits__,
Expand All @@ -19,4 +21,5 @@
'__copyright__',
'__credits__',
'__version__',
'load_data',
]
17 changes: 10 additions & 7 deletions smriprep/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_parser():
SpatialReferences,
)

from ..__about__ import __version__
import smriprep

parser = ArgumentParser(
description='sMRIPrep: Structural MRI PREProcessing workflows',
Expand Down Expand Up @@ -82,7 +82,11 @@ def get_parser():
)

# optional arguments
parser.add_argument('--version', action='version', version=f'smriprep v{__version__}')
parser.add_argument(
'--version',
action='version',
version=f'smriprep v{smriprep.__version__}',
)

g_bids = parser.add_argument_group('Options for filtering BIDS queries')
g_bids.add_argument(
Expand Down Expand Up @@ -470,9 +474,8 @@ def build_workflow(opts, retval):
from nipype import logging
from niworkflows.utils.bids import collect_participants

from ..__about__ import __version__
from ..data import load_resource
from ..workflows.base import init_smriprep_wf
import smriprep
from smriprep.workflows.base import init_smriprep_wf

logger = logging.getLogger('nipype.workflow')

Expand Down Expand Up @@ -596,7 +599,7 @@ def build_workflow(opts, retval):
logger.log(
25,
INIT_MSG(
version=__version__,
version=smriprep.__version__,
bids_dir=bids_dir,
subject_list=subject_list,
uuid=run_uuid,
Expand Down Expand Up @@ -651,7 +654,7 @@ def build_workflow(opts, retval):
boilerplate,
)

boilerplate_bib = load_resource('boilerplate.bib')
boilerplate_bib = smriprep.load_data('boilerplate.bib')

# Generate HTML file resolving citations
cmd = [
Expand Down
4 changes: 2 additions & 2 deletions smriprep/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

from smriprep.data import load_resource
from smriprep.data import load

os.environ['NO_ET'] = '1'

Expand All @@ -12,5 +12,5 @@
def _populate_namespace(doctest_namespace, tmp_path):
doctest_namespace['os'] = os
doctest_namespace['np'] = np
doctest_namespace['load'] = load_resource
doctest_namespace['load'] = load
doctest_namespace['testdir'] = tmp_path
27 changes: 8 additions & 19 deletions smriprep/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import atexit
from contextlib import ExitStack
from pathlib import Path
"""sMRIPRep data files

try:
from functools import cache
except ImportError: # PY38
from functools import lru_cache as cache
.. autofunction:: load

try: # Prefer backport to leave consistency to dependency spec
from importlib_resources import as_file, files
except ImportError:
from importlib.resources import as_file, files
.. automethod:: load.readable

__all__ = ['load_resource']
.. automethod:: load.as_path

exit_stack = ExitStack()
atexit.register(exit_stack.close)
.. automethod:: load.cached
"""

path = files(__package__)
from acres import Loader
mgxd marked this conversation as resolved.
Show resolved Hide resolved


@cache
def load_resource(fname: str) -> Path:
return exit_stack.enter_context(as_file(path.joinpath(fname)))
load = Loader(__package__)
3 changes: 3 additions & 0 deletions smriprep/interfaces/tests/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from acres import Loader

load = Loader(__package__)
16 changes: 5 additions & 11 deletions smriprep/interfaces/tests/test_surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
import numpy as np
from nipype.pipeline import engine as pe

from ...data import load_resource
from smriprep.interfaces.tests.data import load as load_test_data

from ..surf import MakeRibbon


def test_MakeRibbon(tmp_path):
res_template = '{path}/sub-fsaverage_res-4_hemi-{hemi}_desc-cropped_{surf}dist.nii.gz'
res_template = 'sub-fsaverage_res-4_hemi-{hemi}_desc-cropped_{surf}dist.nii.gz'
white, pial = (
[
load_resource(
res_template.format(path='../interfaces/tests/data', hemi=hemi, surf=surf)
)
for hemi in 'LR'
]
[load_test_data(res_template.format(hemi=hemi, surf=surf)) for hemi in 'LR']
for surf in ('wm', 'pial')
)

Expand All @@ -27,9 +23,7 @@ def test_MakeRibbon(tmp_path):
result = make_ribbon.run()

ribbon = nb.load(result.outputs.ribbon)
expected = nb.load(
load_resource('../interfaces/tests/data/sub-fsaverage_res-4_desc-cropped_ribbon.nii.gz')
)
expected = nb.load(load_test_data('sub-fsaverage_res-4_desc-cropped_ribbon.nii.gz'))

assert ribbon.shape == expected.shape
assert np.allclose(ribbon.affine, expected.affine)
Expand Down
8 changes: 4 additions & 4 deletions smriprep/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
from bids.layout import BIDSLayout
from niworkflows.data import load as nwf_load

from ..data import load_resource
import smriprep


def collect_derivatives(derivatives_dir, subject_id, std_spaces, spec=None, patterns=None):
"""Gather existing derivatives and compose a cache."""
if spec is None or patterns is None:
_spec, _patterns = tuple(loads(load_resource('io_spec.json').read_text()).values())
_spec, _patterns = tuple(loads(smriprep.load_data('io_spec.json').read_text()).values())

if spec is None:
spec = _spec
Expand Down Expand Up @@ -96,11 +96,11 @@ def write_derivative_description(bids_dir, deriv_dir):

.. testsetup::

>>> from smriprep.data import load_resource
>>> from smriprep.data import load
>>> from pathlib import Path
>>> from tempfile import TemporaryDirectory
>>> tmpdir = TemporaryDirectory()
>>> bids_dir = load_resource('tests')
>>> bids_dir = load('tests')
>>> deriv_desc = Path(tmpdir.name) / 'dataset_description.json'

.. doctest::
Expand Down
2 changes: 1 addition & 1 deletion smriprep/utils/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from niworkflows.data import Loader
from acres import Loader

load_data = Loader(__package__)

Expand Down
5 changes: 3 additions & 2 deletions smriprep/workflows/anatomical.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
from niworkflows.utils.misc import add_suffix
from niworkflows.utils.spaces import Reference, SpatialReferences

from ..data import load_resource
import smriprep

from ..interfaces import DerivativesDataSink
from ..utils.misc import apply_lut as _apply_bids_lut
from ..utils.misc import fs_isRunning as _fs_isRunning
Expand Down Expand Up @@ -1461,7 +1462,7 @@ def init_anat_template_wf(

if num_files == 1:
get1st = pe.Node(niu.Select(index=[0]), name='get1st')
outputnode.inputs.anat_realign_xfm = [str(load_resource('itkIdentityTransform.txt'))]
outputnode.inputs.anat_realign_xfm = [str(smriprep.load_data('itkIdentityTransform.txt'))]

# fmt:off
workflow.connect([
Expand Down
10 changes: 5 additions & 5 deletions smriprep/workflows/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
MetricResample,
)

import smriprep
from smriprep.interfaces.surf import MakeRibbon
from smriprep.interfaces.workbench import SurfaceResample

from ..data import load_resource
from ..interfaces.freesurfer import MakeMidthickness, ReconAll
from ..interfaces.gifti import MetricMath
from ..interfaces.workbench import CreateSignedDistanceVolume
Expand Down Expand Up @@ -723,7 +723,7 @@ def init_fsLR_reg_wf(*, name='fsLR_reg_wf'):
iterfield=['sphere_in', 'sphere_project_to', 'sphere_unproject_from'],
name='project_unproject',
)
atlases = load_resource('atlases')
atlases = smriprep.load_data('atlases')
project_unproject.inputs.sphere_project_to = [
atlases / 'fs_L' / 'fsaverage.L.sphere.164k_fs_L.surf.gii',
atlases / 'fs_R' / 'fsaverage.R.sphere.164k_fs_R.surf.gii',
Expand Down Expand Up @@ -804,8 +804,8 @@ def init_msm_sulc_wf(*, sloppy: bool = False, name: str = 'msm_sulc_wf'):
# --indata=sub-${SUB}_ses-${SES}_hemi-${HEMI)_sulc.shape.gii \
# --refdata=tpl-fsaverage_hemi-${HEMI}_den-164k_sulc.shape.gii \
# --out=${HEMI}. --verbose
atlases = load_resource('atlases')
msm_conf = load_resource(f'msm/MSMSulcStrain{"Sloppy" if sloppy else "Final"}conf')
atlases = smriprep.load_data('atlases')
msm_conf = smriprep.load_data(f'msm/MSMSulcStrain{"Sloppy" if sloppy else "Final"}conf')
msmsulc = pe.MapNode(
MSM(verbose=True, config_file=msm_conf),
iterfield=['in_mesh', 'reference_mesh', 'in_data', 'reference_data', 'out_base'],
Expand Down Expand Up @@ -1512,7 +1512,7 @@ def init_morph_grayords_wf(
name='outputnode',
)

atlases = load_resource('atlases')
atlases = smriprep.load_data('atlases')
select_surfaces = pe.Node(
KeySelect(
fields=[
Expand Down
7 changes: 3 additions & 4 deletions smriprep/workflows/tests/test_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import pytest
from nipype.pipeline import engine as pe

from ...data import load_resource
from smriprep.interfaces.tests.data import load as load_test_data

from ..surfaces import init_anat_ribbon_wf, init_gifti_surfaces_wf


Expand All @@ -23,9 +24,7 @@ def test_ribbon_workflow(tmp_path: Path):

# Low-res file that includes the fsaverage surfaces in its bounding box
# We will use it both as a template and a comparison.
test_ribbon = load_resource(
'../interfaces/tests/data/sub-fsaverage_res-4_desc-cropped_ribbon.nii.gz'
)
test_ribbon = load_test_data('sub-fsaverage_res-4_desc-cropped_ribbon.nii.gz')

gifti_surfaces_wf = init_gifti_surfaces_wf(surfaces=['white', 'pial'])
anat_ribbon_wf = init_anat_ribbon_wf()
Expand Down