Skip to content

Commit

Permalink
Merge pull request nipreps#1199 from nipreps/fix/move-to-loader
Browse files Browse the repository at this point in the history
FIX: Move from ``pkg_resources`` to ``niworkflows.data.Loader``
  • Loading branch information
oesteban authored Mar 19, 2024
2 parents 3e26dfa + eca27e4 commit e57973e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions mriqc/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""Utilities: Jinja2 templates."""
from pathlib import Path

from pkg_resources import resource_filename as pkgrf
from niworkflows.data import Loader


class GroupTemplate:
Expand All @@ -37,7 +37,7 @@ class GroupTemplate:
def __init__(self):
import jinja2

self.template_str = pkgrf("mriqc", "data/reports/group.html")
self.template_str = Loader(__package__)("data/reports/group.html")
self.env = jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath="/"),
trim_blocks=True,
Expand Down
7 changes: 4 additions & 3 deletions mriqc/interfaces/common/ensure_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
isdefined,
traits,
)
from pkg_resources import resource_filename as pkgrf
from niworkflows.data import Loader

OUT_FILE_NAME = "{prefix}_resampled{ext}"
OUT_MASK_NAME = "{prefix}_resmask{ext}"
REF_FILE_NAME = "resample_ref.nii.gz"
REF_MASK_NAME = "mask_ref.nii.gz"
load_data = Loader(__package__)


class EnsureSizeInputSpec(BaseInterfaceInputSpec):
Expand Down Expand Up @@ -146,7 +147,7 @@ def _run_interface(self, runtime):
input_image=self.inputs.in_file,
reference_image=REF_FILE_NAME,
interpolation="LanczosWindowedSinc",
transforms=[pkgrf("mriqc", "data/itk_identity.tfm")],
transforms=[load_data("data/itk_identity.tfm")],
output_image=out_file,
).run()

Expand All @@ -167,7 +168,7 @@ def _run_interface(self, runtime):
input_image=self.inputs.in_mask,
reference_image=REF_MASK_NAME,
interpolation="NearestNeighbor",
transforms=[pkgrf("mriqc", "data/itk_identity.tfm")],
transforms=[load_data("data/itk_identity.tfm")],
output_image=out_mask,
).run()

Expand Down
18 changes: 6 additions & 12 deletions mriqc/reports/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def gen_html(csv_file, mod, csv_failed=None, out_file=None):
import datetime
import os.path as op

from pkg_resources import resource_filename as pkgrf
from niworkflows.data import Loader

from .. import __version__ as ver
from mriqc.data.config import GroupTemplate
Expand All @@ -43,6 +43,8 @@ def gen_html(csv_file, mod, csv_failed=None, out_file=None):
else:
from io import BytesIO as TextIO

load_data = Loader(__package__)

QCGROUPS = {
"T1w": [
(["cjv"], None),
Expand Down Expand Up @@ -266,21 +268,13 @@ def gen_html(csv_file, mod, csv_failed=None, out_file=None):
"csv_groups": csv_groups,
"failed": failed,
"boxplots_js": open(
pkgrf(
"mriqc",
op.join("data", "reports", "embed_resources", "boxplots.js"),
)
load_data("data/reports/embed_resources/boxplots.js"),
).read(),
"d3_js": open(
pkgrf(
"mriqc", op.join("data", "reports", "embed_resources", "d3.min.js")
)
load_data("data/reports/embed_resources/d3.min.js"),
).read(),
"boxplots_css": open(
pkgrf(
"mriqc",
op.join("data", "reports", "embed_resources", "boxplots.css"),
)
load_data("data/reports/embed_resources/boxplots.css"),
).read(),
},
out_file,
Expand Down
6 changes: 4 additions & 2 deletions mriqc/reports/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
"""Encapsulates report generation functions."""
from pathlib import Path
from json import loads
from pkg_resources import resource_filename as pkgrf
from nireports.assembler.report import Report
from niworkflows.data import Loader

_load_data = Loader(__package__)


def generate_reports():
Expand Down Expand Up @@ -86,7 +88,7 @@ def _single_report(in_file):
config.execution.output_dir,
config.execution.run_uuid,
reportlets_dir=config.execution.work_dir / "reportlets",
bootstrap_file=pkgrf("mriqc", f"data/bootstrap-{report_type}.yml"),
bootstrap_file=_load_data(f"data/bootstrap-{report_type}.yml"),
metadata={
"dataset": config.execution.dsname,
"about-metadata": {
Expand Down
10 changes: 6 additions & 4 deletions mriqc/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@
from contextlib import contextmanager
from pathlib import Path
from tempfile import mkdtemp

from pkg_resources import resource_filename as pkgrf
from toml import loads

from niworkflows.data import Loader

_load_data = Loader(__package__)


@contextmanager
def mock_config():
"""Create a mock config for documentation and testing purposes."""
from . import config

filename = Path(pkgrf("mriqc", "data/config-example.toml"))
filename = Path(_load_data("data/config-example.toml"))
settings = loads(filename.read_text())
for sectionname, configs in settings.items():
if sectionname != "environment":
Expand All @@ -44,7 +46,7 @@ def mock_config():
config.loggers.init()

config.execution.work_dir = Path(mkdtemp())
config.execution.bids_dir = Path(pkgrf("mriqc", "data/tests/ds000005")).absolute()
config.execution.bids_dir = Path(_load_data("data/tests/ds000005")).absolute()
config.execution.init()

yield

0 comments on commit e57973e

Please sign in to comment.