Skip to content

Commit

Permalink
Draft medic workflow test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Dec 12, 2024
1 parent e205348 commit 8f54fad
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sdcflows/interfaces/fmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class _MEDICInputSpec(CommandLineInputSpec):
class _MEDICOutputSpec(TraitedSpec):
native_field_map = File(
exists=True,
desc="4D ative (distorted) space field map in Hertz",
desc="4D native (distorted) space field map in Hertz",
)
displacement_map = File(
exists=True,
Expand Down
6 changes: 3 additions & 3 deletions sdcflows/workflows/fit/medic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def init_medic_wf(name="medic_wf"):
Outputs
-------
fieldmap : :obj:`str`
fmap : :obj:`str`
The path of the estimated fieldmap time series file. Units are Hertz.
displacement : :obj:`list` of :obj:`str`
Path to the displacement time series files. Units are mm.
Expand All @@ -85,7 +85,7 @@ def init_medic_wf(name="medic_wf"):

inputnode = pe.Node(niu.IdentityInterface(fields=INPUT_FIELDS), name="inputnode")
outputnode = pe.Node(

Check warning on line 87 in sdcflows/workflows/fit/medic.py

View check run for this annotation

Codecov / codecov/patch

sdcflows/workflows/fit/medic.py#L86-L87

Added lines #L86 - L87 were not covered by tests
niu.IdentityInterface(fields=["fieldmap", "displacement", "method"]),
niu.IdentityInterface(fields=["fmap", "displacement", "method"]),
name="outputnode",
)
outputnode.inputs.method = "MEDIC"

Check warning on line 91 in sdcflows/workflows/fit/medic.py

View check run for this annotation

Codecov / codecov/patch

sdcflows/workflows/fit/medic.py#L91

Added line #L91 was not covered by tests
Expand Down Expand Up @@ -119,7 +119,7 @@ def init_medic_wf(name="medic_wf"):
(write_metadata, medic, [("out_file", "metadata")]),
(phase2rad, medic, [("out_file", "phase_files")]),
(medic, outputnode, [
("native_field_map", "fieldmap"),
("native_field_map", "fmap"),
("displacement_map", "displacement"),
]),
]) # fmt:skip
Expand Down
82 changes: 82 additions & 0 deletions sdcflows/workflows/fit/tests/test_medic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2021 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
"""Test phase-difference type of fieldmaps."""
from pathlib import Path
from json import loads

import pytest

from ..medic import init_medic_wf, Workflow


@pytest.mark.slow
def test_medic(tmpdir, datadir, workdir, outdir):
"""Test creation of the workflow."""
tmpdir.chdir()

pattern = 'ds005250/sub-04/ses-2/func/*_part-mag_bold.nii.gz'
magnitude_files = sorted(datadir.glob(pattern))
phase_files = [f.with_name(f.name.replace("part-mag", "part-phase")) for f in magnitude_files]
metadata_dicts = [
loads(Path(f.name.replace('.nii.gz', '.json')).read_text()) for f in magnitude_files
]

wf = Workflow(name=f"medic_{magnitude_files[0].name.replace('.nii.gz', '').replace('-', '_')}")
medic_wf = init_medic_wf()
medic_wf.inputs.inputnode.magnitude = magnitude_files
medic_wf.inputs.inputnode.phase = phase_files
medic_wf.inputs.inputnode.metadata = metadata_dicts

if outdir:
from ...outputs import init_fmap_derivatives_wf, init_fmap_reports_wf

outdir = outdir / "unittests" / magnitude_files[0].split("/")[0]
fmap_derivatives_wf = init_fmap_derivatives_wf(
output_dir=str(outdir),
write_coeff=True,
bids_fmap_id="phasediff_id",
)
fmap_derivatives_wf.inputs.inputnode.source_files = [str(f) for f in magnitude_files]
fmap_derivatives_wf.inputs.inputnode.fmap_meta = metadata_dicts

fmap_reports_wf = init_fmap_reports_wf(
output_dir=str(outdir),
fmap_type='medic',
)
fmap_reports_wf.inputs.inputnode.source_files = [str(f) for f in magnitude_files]

wf.connect([
(medic_wf, fmap_reports_wf, [
("outputnode.fmap", "inputnode.fieldmap"),
]),
(medic_wf, fmap_derivatives_wf, [
("outputnode.fmap", "inputnode.fieldmap"),
]),
]) # fmt:skip
else:
wf.add_nodes([medic_wf])

if workdir:
wf.base_dir = str(workdir)

wf.run(plugin="Linear")

0 comments on commit 8f54fad

Please sign in to comment.