Skip to content

Commit 958ed8c

Browse files
committed
WIP: Refactor
1 parent 7dbcf77 commit 958ed8c

6 files changed

Lines changed: 771 additions & 352 deletions

File tree

nireports/interfaces/nuisance.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,26 @@ def _run_interface(self, runtime, **kwargs):
260260

261261

262262
class _MotionPlotInputSpec(BaseInterfaceInputSpec):
263-
original_pet = File(
263+
uncorr_file = File(
264264
exists=True,
265265
mandatory=True,
266-
desc="Original (uncorrected) PET series in native PET space",
266+
desc="Original (uncorrected) volume in native space",
267267
)
268-
corrected_pet = File(
268+
corr_file = File(
269269
exists=True,
270270
mandatory=True,
271271
desc=(
272-
"Motion-corrected PET series derived by applying the estimated motion "
273-
"transforms to the original data in native PET space"
272+
"Motion-corrected volume derived by applying the estimated motion "
273+
"transforms to the original data in native space"
274274
),
275275
)
276276
fd_file = File(exists=True, desc="Confounds file containing framewise displacement")
277277
duration = traits.Float(0.2, usedefault=True, desc="Frame duration for the GIF (seconds)")
278+
out_file = traits.Either(None, File, value=None, usedefault=True, desc="Path to save plot")
278279

279280

280281
class _MotionPlotOutputSpec(TraitedSpec):
281-
svg_file = File(exists=True, desc="Animated before/after motion correction SVG")
282+
out_file = File(exists=True, desc="Path to saved plot")
282283

283284

284285
class MotionPlot(SimpleInterface):
@@ -295,25 +296,34 @@ class MotionPlot(SimpleInterface):
295296
output_spec = _MotionPlotOutputSpec
296297

297298
def _run_interface(self, runtime):
298-
runtime.cwd = Path(runtime.cwd)
299-
300-
svg_file = runtime.cwd / "pet_motion_hmc.svg"
301-
svg_file.parent.mkdir(parents=True, exist_ok=True)
302299

303300
fd_values = None
304301
if isdefined(self.inputs.fd_file):
305302
fd_values = load_framewise_displacement(self.inputs.fd_file)
306303

304+
if self.inputs.out_file is None:
305+
self._results["out_file"] = fname_presuffix(
306+
self.inputs.corr_file,
307+
suffix="_hmc.svg",
308+
use_ext=False,
309+
newpath=runtime.cwd,
310+
)
311+
else:
312+
self._results["out_file"] = self.inputs.out_file
313+
307314
# ToDo
308-
# Does not make sense to assign to an input parameter
309-
svg_file = plot_motion(
310-
load_api(self.inputs.original_pet, SpatialImage),
311-
load_api(self.inputs.corrected_pet, SpatialImage),
312-
svg_file,
315+
# Contrary to the other plot functions, IMO it makes most sense that
316+
# we pass the actual data to the plot functions rather than the files.
317+
# Same goes for passing output_fle vs the function returning the fig
318+
svg_content = plot_motion(
319+
load_api(self.inputs.uncorr_file, SpatialImage),
320+
load_api(self.inputs.corr_file, SpatialImage),
313321
self.inputs.duration,
314-
fd_values=fd_values,
322+
fd_values,
315323
)
316324

317-
self._results["svg_file"] = str(svg_file)
325+
out_file = Path(self._results["out_file"])
326+
out_file.write_text(svg_content, encoding="utf-8")
327+
self._results["out_file"] = str(out_file)
318328

319329
return runtime

0 commit comments

Comments
 (0)