Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Visualization of defacing workflow #28

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions petdeface/mideface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from nipype.interfaces.base import File
from nipype.interfaces.base import TraitedSpec
from nipype.interfaces.base import traits
from nipype.interfaces.base import isdefined
import os



class MidefaceInputSpec(CommandLineInputSpec):
Expand Down Expand Up @@ -145,6 +148,8 @@ class MidefaceInputSpec(CommandLineInputSpec):
class MidefaceOutputSpec(TraitedSpec):
out_file = File(desc="Defaced input", exists=True)
out_facemask = File(desc="Facemask", exists=True)
out_before_pic = File(desc="before pic", exists=True)
out_after_pic = File(desc="after pic", exists=True)


class Mideface(CommandLine):
Expand All @@ -153,6 +158,24 @@ class Mideface(CommandLine):
output_spec = MidefaceOutputSpec


def _list_outputs(self):
metadata = dict(name_source=lambda t: t is not None)
traits = self.inputs.traits(**metadata)
if traits:
outputs = self.output_spec().trait_get()
for name, trait_spec in list(traits.items()):
out_name = name
if trait_spec.output_name is not None:
out_name = trait_spec.output_name
fname = self._filename_from_source(name)
if isdefined(fname):
outputs[out_name] = os.path.abspath(fname)
if self.inputs.pics is True and self.inputs.odir is not None and self.inputs.code is not None:
outputs["out_before_pic"] = os.path.abspath(f"{self.inputs.odir}/{self.inputs.code}.face-before.png")
outputs["out_after_pic"] = os.path.abspath(f"{self.inputs.odir}/{self.inputs.code}.face-after.png")
return outputs


class ApplyMidefaceInputSpec(CommandLineInputSpec):
in_file = File(
desc="Volume to deface",
Expand Down
9 changes: 7 additions & 2 deletions petdeface/petdeface.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ def init_single_subject_wf(
name="sink",
)

datasink.inputs.substitutions = [(".face-after", "_desc-faceafter_T1w"),
('.face-before', '_desc-facebefore_T1w')]

# deface t1w(s)
# an MRI might get matched with multiple PET scans, but we need to run
# deface only once per MRI. This MRI file is the value for each entry in the output of
Expand All @@ -274,7 +277,7 @@ def init_single_subject_wf(

t1w_wf = Workflow(name=workflow_name)
deface_t1w = Node(
Mideface(in_file=pathlib.Path(t1w_file)),
Mideface(in_file=pathlib.Path(t1w_file), pics=True, odir=".", code=f"{anat_string}"),
name=f"deface_t1w_{anat_string}",
)
t1w_wf.connect(
Expand All @@ -288,6 +291,8 @@ def init_single_subject_wf(
"out_facemask",
f"{anat_string.replace('_', '.')}.anat.@defacemask",
),
("out_before_pic", f"{anat_string.replace('_', '.')}.anat.@before"),
("out_after_pic", f"{anat_string.replace('_', '.')}.anat.@after"),
],
),
]
Expand Down Expand Up @@ -472,7 +477,7 @@ def wrap_up_defacing(
# we also want to carry over the defacing masks and registration files
masks_and_reg = list(
set(
layout.get(extension=["mgz", "lta"])
layout.get(extension=["mgz", "lta", "png"])
+ layout.get(suffix="defacemask", extension=["nii.gz", "nii", "mgz"])
)
)
Expand Down