From 9800c5215005324cfe54d561060ec6a77cdaa4a1 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Tue, 11 Jul 2023 12:47:38 +0200 Subject: [PATCH] fix: migrate from ``pkg_resources.resource_filename`` to ``importlib_resources`` Resolves: #377. --- sdcflows/utils/misc.py | 5 +++++ sdcflows/workflows/apply/registration.py | 2 +- sdcflows/workflows/fit/pepolar.py | 7 ++++--- sdcflows/workflows/fit/syn.py | 7 +++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/sdcflows/utils/misc.py b/sdcflows/utils/misc.py index b87f37bac4..adcc0a2fa5 100644 --- a/sdcflows/utils/misc.py +++ b/sdcflows/utils/misc.py @@ -24,6 +24,11 @@ import logging +def resource_fname(module: str, path: str): + from importlib.resources import files + return str((files(module) / path).absolute()) + + def front(inlist): """ Pop from a list or tuple, otherwise return untouched. diff --git a/sdcflows/workflows/apply/registration.py b/sdcflows/workflows/apply/registration.py index 0e90efdf84..fe1670b6b9 100644 --- a/sdcflows/workflows/apply/registration.py +++ b/sdcflows/workflows/apply/registration.py @@ -29,10 +29,10 @@ The target EPI is the distorted dataset (or a reference thereof). """ -from pkg_resources import resource_filename as pkgrf from nipype.pipeline import engine as pe from nipype.interfaces import utility as niu from niworkflows.engine.workflows import LiterateWorkflow as Workflow +from sdcflows.utils.misc import resource_fname as pkgrf def init_coeff2epi_wf( diff --git a/sdcflows/workflows/fit/pepolar.py b/sdcflows/workflows/fit/pepolar.py index a50b9d1f51..d4bad4a041 100644 --- a/sdcflows/workflows/fit/pepolar.py +++ b/sdcflows/workflows/fit/pepolar.py @@ -21,11 +21,11 @@ # https://www.nipreps.org/community/licensing/ # """Datasets with multiple phase encoded directions.""" -from pkg_resources import resource_filename as _pkg_fname from nipype.pipeline import engine as pe from nipype.interfaces import utility as niu from niworkflows.engine.workflows import LiterateWorkflow as Workflow +from sdcflows.utils.misc import resource_fname as pkgrf INPUT_FIELDS = ("metadata", "in_data") _PEPOLAR_DESC = """\ @@ -146,9 +146,10 @@ def init_topup_wf( # The core of the implementation # Feed the input images in LAS orientation, so FSL does not run funky reorientations to_las = pe.Node(ReorientImageAndMetadata(target_orientation="LAS"), name="to_las") + topup = pe.Node( TOPUP( - config=_pkg_fname("sdcflows", f"data/flirtsch/b02b0{'_quick' * sloppy}.cnf") + config=pkgrf("sdcflows", f"data/flirtsch/b02b0{'_quick' * sloppy}.cnf") ), name="topup", ) @@ -332,7 +333,7 @@ def init_3dQwarp_wf(omp_nthreads=1, debug=False, name="pepolar_estimate_wf"): align_pes = pe.Node( Registration( - from_file=_pkg_fname("sdcflows", "data/translation_rigid.json"), + from_file=pkgrf("sdcflows", "data/translation_rigid.json"), output_warped_image=True, ), name="align_pes", diff --git a/sdcflows/workflows/fit/syn.py b/sdcflows/workflows/fit/syn.py index b463bcc229..f797f96760 100644 --- a/sdcflows/workflows/fit/syn.py +++ b/sdcflows/workflows/fit/syn.py @@ -34,10 +34,11 @@ """ -from pkg_resources import resource_filename from nipype.pipeline import engine as pe from nipype.interfaces import utility as niu from niworkflows.engine.workflows import LiterateWorkflow as Workflow +from sdcflows.utils.misc import resource_fname as pkgrf + DEFAULT_MEMORY_MIN_GB = 0.01 INPUT_FIELDS = ( @@ -120,7 +121,6 @@ def init_syn_sdc_wf( Short description of the estimation method that was run. """ - from pkg_resources import resource_filename as pkgrf from packaging.version import parse as parseversion, Version from nipype.interfaces.ants import ImageMath from niworkflows.interfaces.fixes import ( @@ -401,7 +401,6 @@ def init_syn_preprocessing_wf( the cost function of SyN. """ - from pkg_resources import resource_filename as pkgrf from niworkflows.interfaces.nibabel import ( IntensityClip, ApplyMask, @@ -496,7 +495,7 @@ def init_syn_preprocessing_wf( ) epi2anat = pe.Node( - Registration(from_file=resource_filename("sdcflows", "data/affine.json")), + Registration(from_file=pkgrf("sdcflows", "data/affine.json")), name="epi2anat", n_procs=omp_nthreads, )