Skip to content

Commit

Permalink
RF: Drop pkg_resources, use loaders where feasible
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jul 10, 2023
1 parent 8a591b6 commit 6786973
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 109 deletions.
6 changes: 2 additions & 4 deletions niworkflows/anat/ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# general purpose
from collections import OrderedDict
from multiprocessing import cpu_count
from pkg_resources import resource_filename as pkgr_fn
from warnings import warn

# nipype
Expand All @@ -40,6 +39,7 @@
ThresholdImage,
)

from ..data import load as load_data
from ..utils.misc import get_template_specs
from ..utils.connections import pop_file as _pop

Expand Down Expand Up @@ -302,9 +302,7 @@ def init_brain_extraction_wf(
else "antsBrainExtractionNoLaplacian_%s.json"
)
norm = pe.Node(
Registration(
from_file=pkgr_fn("niworkflows.data", settings_file % normalization_quality)
),
Registration(from_file=load_data(settings_file % normalization_quality)),
name="norm",
n_procs=omp_nthreads,
mem_gb=mem_gb,
Expand Down
6 changes: 4 additions & 2 deletions niworkflows/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import pytest
import tempfile

from . import load_resource

try:
import importlib_resources
except ImportError:
Expand All @@ -40,7 +42,7 @@


def find_resource_or_skip(resource):
pathlike = importlib_resources.files("niworkflows") / resource
pathlike = load_resource(resource)
if not pathlike.exists():
pytest.skip(f"Missing resource {resource}; run this test from a source repository")
return pathlike
Expand All @@ -63,7 +65,7 @@ def add_np(doctest_namespace):
doctest_namespace["datadir"] = data_dir
doctest_namespace["data_dir_canary"] = data_dir_canary
doctest_namespace["bids_collect_data"] = collect_data
doctest_namespace["test_data"] = importlib_resources.files("niworkflows") / "tests" / "data"
doctest_namespace["test_data"] = load_resource('tests/data')

tmpdir = tempfile.TemporaryDirectory()

Expand Down
6 changes: 2 additions & 4 deletions niworkflows/func/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
#
"""Utility workflows."""
from packaging.version import parse as parseversion, Version
from pkg_resources import resource_filename as pkgr_fn

from nipype.pipeline import engine as pe
from nipype.interfaces import utility as niu, fsl, afni

from templateflow.api import get as get_template

from .. import data
from ..engine.workflows import LiterateWorkflow as Workflow
from ..interfaces.fixes import (
FixHeaderRegistration as Registration,
Expand Down Expand Up @@ -452,9 +452,7 @@ def init_enhance_and_skullstrip_bold_wf(

# Set up spatial normalization
norm = pe.Node(
Registration(
from_file=pkgr_fn("niworkflows.data", "epi_atlasbased_brainmask.json")
),
Registration(from_file=data.load("epi_atlasbased_brainmask.json")),
name="norm",
n_procs=omp_nthreads,
)
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from pathlib import Path
import shutil
import os
from pkg_resources import resource_filename as _pkgres
import re

import nibabel as nb
Expand All @@ -50,12 +49,13 @@
)
from nipype.interfaces.io import add_traits
import templateflow as tf
from .. import data
from ..utils.bids import _init_layout, relative_to_root
from ..utils.images import set_consumables, unsafe_write_nifti_header_and_data
from ..utils.misc import _copy_any, unlink

regz = re.compile(r"\.gz$")
_pybids_spec = loads(Path(_pkgres("niworkflows", "data/nipreps.json")).read_text())
_pybids_spec = loads(data.load.readable("nipreps.json").read_text())
BIDS_DERIV_ENTITIES = _pybids_spec["entities"]
BIDS_DERIV_PATTERNS = tuple(_pybids_spec["default_path_patterns"])

Expand Down
11 changes: 4 additions & 7 deletions niworkflows/interfaces/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from os import path as op

from multiprocessing import cpu_count
import pkg_resources as pkgr
from packaging.version import Version
import numpy as np

Expand All @@ -40,6 +39,7 @@

from templateflow.api import get as get_template
from .. import NIWORKFLOWS_LOG, __version__
from ..data import load as load_data
from .fixes import FixHeaderRegistration as Registration


Expand Down Expand Up @@ -166,16 +166,13 @@ def _get_settings(self):
self.inputs.moving.lower(), self.inputs.flavor
)

data_dir = load_data()
# Get a list of settings files that match the flavor.
filenames = [
i
for i in pkgr.resource_listdir("niworkflows", "data")
if i.startswith(filestart) and i.endswith(".json")
i for i in data_dir.iterdir() if i.startswith(filestart) and i.endswith(".json")
]
# Return the settings files.
return [
pkgr.resource_filename("niworkflows.data", f) for f in sorted(filenames)
]
return [str(data_dir / f) for f in sorted(filenames)]

def _run_interface(self, runtime):
# Get a list of settings files.
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/interfaces/tests/test_itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#
import pytest
from ..itk import _applytfms
from ... import data
from nipype.interfaces.ants.base import Info


Expand All @@ -32,14 +33,13 @@
def test_applytfms(tmpdir, ext, copy_dtype, in_dtype):
import numpy as np
import nibabel as nb
from pkg_resources import resource_filename as pkgr_fn

in_file = str(tmpdir / ("src" + ext))
nii = nb.Nifti1Image(np.zeros((5, 5, 5), dtype=np.float32), np.eye(4))
nii.set_data_dtype(in_dtype)
nii.to_filename(in_file)

in_xform = pkgr_fn("niworkflows", "data/itkIdentityTransform.txt")
in_xform = data.load("itkIdentityTransform.txt")

ifargs = {"copy_dtype": copy_dtype, "reference_image": in_file}
args = (in_file, in_xform, ifargs, 0, str(tmpdir))
Expand Down
21 changes: 9 additions & 12 deletions niworkflows/interfaces/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
from niworkflows import viz
from niworkflows.utils.timeseries import _cifti_timeseries, _nifti_timeseries
from niworkflows.interfaces.plotting import _get_tr
from niworkflows.tests.conftest import datadir
from niworkflows.tests.data import load_test_data


def test_cifti_carpetplot():
"""Exercise extraction of timeseries from CIFTI2."""
save_artifacts = os.getenv("SAVE_CIRCLE_ARTIFACTS", False)

cifti_file = os.path.join(
datadir,
"sub-01_task-mixedgamblestask_run-02_space-fsLR_den-91k_bold.dtseries.nii",
cifti_file = load_test_data(
"sub-01_task-mixedgamblestask_run-02_space-fsLR_den-91k_bold.dtseries.nii"
)
data, segments = _cifti_timeseries(cifti_file)
data, segments = _cifti_timeseries(str(cifti_file))
viz.plot_carpet(
data,
segments,
Expand All @@ -56,15 +55,13 @@ def test_nifti_carpetplot():
"""Exercise extraction of timeseries from CIFTI2."""
save_artifacts = os.getenv("SAVE_CIRCLE_ARTIFACTS", False)

nifti_file = os.path.join(
datadir,
"sub-ds205s03_task-functionallocalizer_run-01_bold_volreg.nii.gz",
nifti_file = load_test_data(
"sub-ds205s03_task-functionallocalizer_run-01_bold_volreg.nii.gz"
)
seg_file = os.path.join(
datadir,
"sub-ds205s03_task-functionallocalizer_run-01_bold_parc.nii.gz",
seg_file = load_test_data(
"sub-ds205s03_task-functionallocalizer_run-01_bold_parc.nii.gz"
)
data, segments = _nifti_timeseries(nifti_file, seg_file)
data, segments = _nifti_timeseries(str(nifti_file), str(seg_file))
viz.plot_carpet(
data,
segments,
Expand Down
53 changes: 14 additions & 39 deletions niworkflows/reports/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
import re
from itertools import compress
from collections import defaultdict
from pkg_resources import resource_filename as pkgrf
from bids.layout import BIDSLayout, add_config_paths
import jinja2
from nipype.utils.filemanip import copyfile

from .. import data, load_resource

# Add a new figures spec
try:
add_config_paths(figures=pkgrf("niworkflows", "data/nipreps.json"))
add_config_paths(figures=data.load('nipreps.json'))
except ValueError as e:
if "Configuration 'figures' already exists" != str(e):
raise
Expand Down Expand Up @@ -100,17 +101,13 @@ class Reportlet(Element):
.. testsetup::
>>> cwd = os.getcwd()
>>> os.chdir(tmpdir)
>>> from shutil import copytree
>>> from bids.layout import BIDSLayout
>>> test_data_path = find_resource_or_skip('data/tests/work')
>>> source = find_resource_or_skip('data/tests/work')
>>> testdir = Path(tmpdir)
>>> with importlib_resources.as_file(test_data_path) as source:
... data_dir = copytree(source, str(testdir / 'work'))
>>> _ = copytree(source, testdir / 'work')
>>> out_figs = testdir / 'out' / 'fmriprep'
>>> bl = BIDSLayout(str(testdir / 'work' / 'reportlets'),
>>> bl = BIDSLayout(testdir / 'work' / 'reportlets',
... config='figures', validate=False)
>>> bl.get(subject='01', desc='reconall') # doctest: +ELLIPSIS
Expand Down Expand Up @@ -165,10 +162,6 @@ class Reportlet(Element):
>>> r.is_empty()
True
.. testcleanup::
>>> os.chdir(cwd)
"""

def __init__(self, layout, out_dir, config=None):
Expand Down Expand Up @@ -249,15 +242,11 @@ class Report:
.. testsetup::
>>> cwd = os.getcwd()
>>> os.chdir(tmpdir)
>>> from shutil import copytree
>>> from bids.layout import BIDSLayout
>>> test_data_path = find_resource_or_skip('data/tests/work')
>>> source = find_resource_or_skip('data/tests/work')
>>> testdir = Path(tmpdir)
>>> with importlib_resources.as_file(test_data_path) as source:
... data_dir = copytree(source, str(testdir / 'work'))
>>> _ = copytree(source, str(testdir / 'work'))
>>> out_figs = testdir / 'out' / 'fmriprep'
>>> robj = Report(testdir / 'out', 'madeoutuuid', subject_id='01', packagename='fmriprep',
Expand All @@ -270,10 +259,6 @@ class Report:
>>> len((testdir / 'out' / 'fmriprep' / 'sub-01.html').read_text())
36713
.. testcleanup::
>>> os.chdir(cwd)
"""

def __init__(
Expand Down Expand Up @@ -303,8 +288,8 @@ def __init__(
self.out_filename = f"sub-{self.subject_id}.html"

# Default template from niworkflows
self.template_path = Path(pkgrf("niworkflows", "reports/report.tpl"))
self._load_config(Path(config or pkgrf("niworkflows", "reports/default.yml")))
self.template_path = load_resource('reports') / 'report.tpl'
self._load_config(Path(config or load_resource('reports') / 'default.yml'))
assert self.template_path.exists()

def _load_config(self, config):
Expand Down Expand Up @@ -424,14 +409,12 @@ def generate_report(self):
.findall((logs_path / "CITATION.tex").read_text())[0]
.strip()
)
bib = data.Loader(self.packagename).readable("data/boilerplate.bib")
boilerplate.append(
(
boiler_idx,
"LaTeX",
f"""<pre>{text}</pre>
<h3>Bibliography</h3>
<pre>{Path(pkgrf(self.packagename, 'data/boilerplate.bib')).read_text()}</pre>
""",
f"<pre>{text}</pre>\n<h3>Bibliography</h3>\n<pre>{bib.read_text()}</pre>\n",
)
)
boiler_idx += 1
Expand Down Expand Up @@ -519,24 +502,16 @@ def run_reports(
.. testsetup::
>>> cwd = os.getcwd()
>>> os.chdir(tmpdir)
>>> from shutil import copytree
>>> test_data_path = find_resource_or_skip('data/tests/work')
>>> source = find_resource_or_skip('data/tests/work')
>>> testdir = Path(tmpdir)
>>> with importlib_resources.as_file(test_data_path) as source:
... data_dir = copytree(source, str(testdir / 'work'))
>>> _ = copytree(source, testdir / 'work')
>>> (testdir / 'fmriprep').mkdir(parents=True, exist_ok=True)
>>> run_reports(testdir / 'out', '01', 'madeoutuuid', packagename='fmriprep',
... reportlets_dir=testdir / 'work' / 'reportlets')
0
.. testcleanup::
>>> os.chdir(cwd)
"""
return Report(
out_dir,
Expand Down
Loading

0 comments on commit 6786973

Please sign in to comment.