Skip to content

Commit

Permalink
enh: finalize writting outputs as a standalone workflow + tests
Browse files Browse the repository at this point in the history
Resolves: #26.
  • Loading branch information
oesteban committed Nov 20, 2020
1 parent b341924 commit 9a02865
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 89 deletions.
54 changes: 27 additions & 27 deletions sdcflows/workflows/fit/tests/test_pepolar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from pathlib import Path
from json import loads
import pytest
from niworkflows.interfaces.bids import DerivativesDataSink
from niworkflows.interfaces.images import IntraModalMerge
from nipype.pipeline import engine as pe

from ..pepolar import Workflow, init_topup_wf
from ..pepolar import init_topup_wf


@pytest.mark.skipif(os.getenv("TRAVIS") == "true", reason="this is TravisCI")
Expand All @@ -29,17 +28,18 @@ def test_topup_wf(tmpdir, datadir, workdir, outdir, epi_path):
epi_path = [datadir / f for f in epi_path]
in_data = [str(f.absolute()) for f in epi_path]

wf = Workflow(
wf = pe.Workflow(
name=f"topup_{epi_path[0].name.replace('.nii.gz', '').replace('-', '_')}"
)

merge = pe.MapNode(IntraModalMerge(hmc=False), name="merge", iterfield=["in_files"])
merge.inputs.in_files = in_data

topup_wf = init_topup_wf(omp_nthreads=2, debug=True)
topup_wf.inputs.inputnode.metadata = [
metadata = [
loads(Path(str(f).replace(".nii.gz", ".json")).read_text()) for f in in_data
]
topup_wf.inputs.inputnode.metadata = metadata

# fmt: off
wf.connect([
Expand All @@ -49,37 +49,37 @@ def test_topup_wf(tmpdir, datadir, workdir, outdir, epi_path):

if outdir:
from nipype.interfaces.afni import Automask
from ....interfaces.reportlets import FieldmapReportlet
from ...outputs import init_fmap_derivatives_wf, init_fmap_reports_wf

pre_mask = pe.Node(Automask(dilate=1, outputtype="NIFTI_GZ"),
name="pre_mask")
merge_corrected = pe.Node(IntraModalMerge(hmc=False), name="merge_corrected")

rep = pe.Node(
FieldmapReportlet(reference_label="EPI Reference"), "simple_report"
fmap_derivatives_wf = init_fmap_derivatives_wf(
output_dir=str(outdir),
write_coeff=True,
custom_entities={"est": "pepolar"},
bids_fmap_id="pepolar_id",
)
rep.interface._always_run = True
ds_report = pe.Node(
DerivativesDataSink(
base_directory=str(outdir),
out_path_base="sdcflows",
datatype="figures",
suffix="fieldmap",
desc="pepolar",
dismiss_entities="fmap",
),
name="ds_report",
fmap_derivatives_wf.inputs.inputnode.source_files = in_data
fmap_derivatives_wf.inputs.inputnode.fmap_meta = metadata

fmap_reports_wf = init_fmap_reports_wf(
output_dir=str(outdir), fmap_type="pepolar",
)
ds_report.inputs.source_file = in_data[0]
fmap_reports_wf.inputs.inputnode.source_files = in_data

pre_mask = pe.Node(Automask(dilate=1, outputtype="NIFTI_GZ"), name="pre_mask")
merge_corrected = pe.Node(IntraModalMerge(hmc=False), name="merge_corrected")

# fmt: off
wf.connect([
(topup_wf, merge_corrected, [("outputnode.fmap_ref", "in_files")]),
(merge_corrected, rep, [("out_avg", "reference")]),
(merge_corrected, pre_mask, [("out_avg", "in_file")]),
(topup_wf, rep, [("outputnode.fmap", "fieldmap")]),
(pre_mask, rep, [("out_file", "mask")]),
(rep, ds_report, [("out_report", "in_file")]),
(merge_corrected, fmap_reports_wf, [("out_avg", "inputnode.fmap_ref")]),
(topup_wf, fmap_reports_wf, [("outputnode.fmap", "inputnode.fieldmap")]),
(pre_mask, fmap_reports_wf, [("out_file", "inputnode.fmap_mask")]),
(topup_wf, fmap_derivatives_wf, [
("outputnode.fmap", "inputnode.fieldmap"),
("outputnode.fmap_ref", "inputnode.fmap_ref"),
("outputnode.coefficients", "inputnode.fmap_coeff"),
]),
])
# fmt: on

Expand Down
63 changes: 25 additions & 38 deletions sdcflows/workflows/fit/tests/test_phdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from json import loads

import pytest
from niworkflows.interfaces.bids import DerivativesDataSink
from nipype.pipeline import engine as pe

from ..fieldmap import init_fmap_wf, Workflow

Expand All @@ -27,58 +25,47 @@ def test_phdiff(tmpdir, datadir, workdir, outdir, fmap_path):
tmpdir.chdir()

fmap_path = [datadir / f for f in fmap_path]
fieldmaps = [
(str(f.absolute()), loads(Path(str(f).replace(".nii.gz", ".json")).read_text()))
for f in fmap_path
]

wf = Workflow(
name=f"phdiff_{fmap_path[0].name.replace('.nii.gz', '').replace('-', '_')}"
)
phdiff_wf = init_fmap_wf(omp_nthreads=2)
phdiff_wf.inputs.inputnode.fieldmap = fieldmaps
phdiff_wf.inputs.inputnode.magnitude = [
str(f.absolute()).replace("diff", "1").replace("phase", "magnitude")
for f in fmap_path
]
phdiff_wf.inputs.inputnode.fieldmap = [
(str(f.absolute()), loads(Path(str(f).replace(".nii.gz", ".json")).read_text()))
for f in fmap_path
f.replace("diff", "1").replace("phase", "magnitude") for f, _ in fieldmaps
]

if outdir:
from ....interfaces.reportlets import FieldmapReportlet

rep = pe.Node(FieldmapReportlet(reference_label="Magnitude"), "simple_report")
rep.interface._always_run = True
from ...outputs import init_fmap_derivatives_wf, init_fmap_reports_wf

ds_report = pe.Node(
DerivativesDataSink(
base_directory=str(outdir),
out_path_base="sdcflows",
datatype="figures",
suffix="fieldmap",
desc="phasediff",
dismiss_entities="fmap",
),
name="ds_report",
fmap_derivatives_wf = init_fmap_derivatives_wf(
output_dir=str(outdir),
custom_entities={"est": "phasediff"},
bids_fmap_id="phasediff_id",
)
ds_report.inputs.source_file = str(fmap_path[0])
fmap_derivatives_wf.inputs.inputnode.source_files = [f for f, _ in fieldmaps]
fmap_derivatives_wf.inputs.inputnode.fmap_meta = [f for _, f in fieldmaps]

dsink_fmap = pe.Node(
DerivativesDataSink(
base_directory=str(outdir),
dismiss_entities="fmap",
desc="phasediff",
suffix="fieldmap",
),
name="dsink_fmap",
fmap_reports_wf = init_fmap_reports_wf(
output_dir=str(outdir),
fmap_type="phasediff" if len(fieldmaps) == 1 else "phases",
)
dsink_fmap.interface.out_path_base = "sdcflows"
dsink_fmap.inputs.source_file = str(fmap_path[0])
fmap_reports_wf.inputs.inputnode.source_files = [f for f, _ in fieldmaps]

# fmt: off
wf.connect([
(phdiff_wf, rep, [("outputnode.fmap", "fieldmap"),
("outputnode.fmap_ref", "reference"),
("outputnode.fmap_mask", "mask")]),
(rep, ds_report, [("out_report", "in_file")]),
(phdiff_wf, dsink_fmap, [("outputnode.fmap", "in_file")]),
(phdiff_wf, fmap_reports_wf, [
("outputnode.fmap", "inputnode.fieldmap"),
("outputnode.fmap_ref", "inputnode.fmap_ref"),
("outputnode.fmap_mask", "inputnode.fmap_mask")]),
(phdiff_wf, fmap_derivatives_wf, [
("outputnode.fmap", "inputnode.fieldmap"),
("outputnode.fmap_ref", "inputnode.fmap_ref"),
]),
])
# fmt: on
else:
Expand Down
Loading

0 comments on commit 9a02865

Please sign in to comment.