Skip to content

Commit

Permalink
Add SubjectSummary.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Aug 19, 2024
1 parent ee3ec0b commit 5dba5e6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
19 changes: 19 additions & 0 deletions qsirecon/interfaces/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ def _run_interface(self, runtime):
return runtime


class _GetUniqueInputSpec(BaseInterfaceInputSpec):
inlist = traits.List(mandatory=True, desc="list of things")


class _GetUniqueOutputSpec(TraitedSpec):
outlist = traits.List()


class GetUnique(SimpleInterface):
input_spec = _GetUniqueInputSpec
output_spec = _GetUniqueOutputSpec

def _run_interface(self, runtime):
in_list = self.inputs.inlist
in_list = [x for x in in_list if isdefined(x)]
self._results["outlist"] = sorted(list(set(in_list)))
return runtime


def _resample_atlas(input_atlas, output_atlas, transform, ref_image):
xform = ants.ApplyTransforms(
transforms=[transform],
Expand Down
60 changes: 48 additions & 12 deletions qsirecon/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from dipy import __version__ as dipy_ver
from nilearn import __version__ as nilearn_ver
from nipype import __version__ as nipype_ver
from nipype.interfaces import utility as niu
from nipype.utils.filemanip import split_filename
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.utils.misc import fix_multi_T1w_source_name
Expand Down Expand Up @@ -87,7 +88,8 @@ def init_single_subject_recon_wf(subject_id):
recon_workflow_anatomical_input_fields,
recon_workflow_input_fields,
)
from ..interfaces.reports import AboutSummary
from ..interfaces.reports import AboutSummary, SubjectSummary
from ..interfaces.utils import GetUnique
from .recon.anatomical import (
init_dwi_recon_anatomical_workflow,
init_highres_recon_anatomical_wf,
Expand Down Expand Up @@ -142,14 +144,19 @@ def init_single_subject_recon_wf(subject_id):
"Anatomical (T1w) available for recon: %s", available_anatomical_data
)

aggregate_anatomical_nodes = pe.Node(
niu.Merge(len(dwi_recon_inputs)),
name="aggregate_anatomical_nodes",
)

# create a processing pipeline for the dwis in each session
dwi_recon_wfs = {}
dwi_individual_anatomical_wfs = {}
recon_full_inputs = {}
dwi_ingress_nodes = {}
anat_ingress_nodes = {}
print(dwi_recon_inputs)
for dwi_input in dwi_recon_inputs:
for i_run, dwi_input in enumerate(dwi_recon_inputs):
dwi_file = dwi_input["bids_dwi_file"]
wf_name = _get_wf_name(dwi_file)

Expand All @@ -159,6 +166,7 @@ def init_single_subject_recon_wf(subject_id):
QsiReconDWIIngress(dwi_file=dwi_file),
name=f"{wf_name}_ingressed_dwi_data",
)
anat_ingress_nodes[dwi_file] = anat_ingress_node

elif config.workflow.recon_input_pipeline == "ukb":
dwi_ingress_nodes[dwi_file] = pe.Node(
Expand All @@ -176,6 +184,13 @@ def init_single_subject_recon_wf(subject_id):
)
)

# Aggregate the anatomical data from all the dwi files
workflow.connect([
(anat_ingress_nodes[dwi_file], aggregate_anatomical_nodes, [
("outputnode.t1_preproc", f"in{i_run + 1}")
]),
]) # fmt:skip

# Create scan-specific anatomical data (mask, atlas configs, odf ROIs for reports)
print(available_anatomical_data)
dwi_individual_anatomical_wfs[dwi_file], dwi_available_anatomical_data = (
Expand Down Expand Up @@ -203,12 +218,6 @@ def init_single_subject_recon_wf(subject_id):
name=f"{wf_name}_recon_wf",
)

file_anat_ingress_node = (
anat_ingress_node
if config.workflow.recon_input_pipeline == "qsiprep"
else anat_ingress_nodes[dwi_file]
)

# Connect the collected diffusion data (gradients, etc) to the inputnode
workflow.connect([
# The dwi data
Expand All @@ -232,7 +241,7 @@ def init_single_subject_recon_wf(subject_id):
(trait, f"inputnode.{trait}") for trait in recon_workflow_input_fields
]),

(file_anat_ingress_node, dwi_individual_anatomical_wfs[dwi_file], [
(anat_ingress_nodes[dwi_file], dwi_individual_anatomical_wfs[dwi_file], [
(f"outputnode.{trait}", f"inputnode.{trait}")
for trait in anatomical_workflow_outputs
]),
Expand All @@ -258,9 +267,36 @@ def init_single_subject_recon_wf(subject_id):
name="ds_report_about",
run_without_submitting=True,
)
workflow.connect([
(about, ds_report_about, [('out_report', 'in_file')]),
]) # fmt:skip
workflow.connect([(about, ds_report_about, [("out_report", "in_file")])])

reduce_t1_preproc = pe.Node(
GetUnique(),
name="reduce_t1_preproc",
)
workflow.connect([(aggregate_anatomical_nodes, reduce_t1_preproc, [("out", "inlist")])])
summary = pe.Node(
SubjectSummary(
subject_id=subject_id,
subjects_dir=config.execution.fs_subjects_dir,
std_spaces=["MNIInfant" if config.workflow.infant else "MNI152NLin2009cAsym"],
nstd_spaces=[],
dwi=dwi_recon_inputs,
),
name="summary",
run_without_submitting=True,
)
workflow.connect([(reduce_t1_preproc, summary, [("out", "t1w")])])
ds_report_summary = pe.Node(
DerivativesDataSink(
source_file=dwi_basename,
base_directory=config.execution.output_dir,
datatype="figures",
suffix="summary",
),
name="ds_report_summary",
run_without_submitting=True,
)
workflow.connect([(summary, ds_report_summary, [("out_report", "in_file")])])

# Fill-in datasinks of reportlets seen so far
for node in workflow.list_node_names():
Expand Down

0 comments on commit 5dba5e6

Please sign in to comment.