Skip to content

Commit

Permalink
FIX: Bertrand's review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
man-shu committed Feb 23, 2021
1 parent d1fd620 commit 9378e0d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 136 deletions.
67 changes: 34 additions & 33 deletions pypreprocess/nipype_preproc_spm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
_do_subject_smooth as _pp_do_subject_smooth)

from nilearn.plotting.html_document import HTMLDocument
from .reporting.nilearn_reporting import *
from .reporting.nilearn_reporting import (embed_in_HTML,
initialize_report, add_component, finalize_report,
generate_realignment_report, generate_registration_report,
generate_corregistration_report, generate_segmentation_report,
generate_normalization_report, generate_tsdiffana_report,
generate_preproc_steps_docstring)

# configure SPM
EPI_TEMPLATE = SPM_DIR = SPM_T1_TEMPLATE = T1_TEMPLATE = None
Expand Down Expand Up @@ -242,8 +247,8 @@ def _do_subject_slice_timing(subject_data, TR, TA=None, spm_dir=None,
def _do_subject_realign(subject_data, reslice=False, register_to_mean=False,
caching=True, report=True, software="spm",
spm_dir=None, matlab_exec=None, spm_mcr=None,
hardlink_output=True,report_path=None,log_path=None
, **kwargs):
hardlink_output=True, report_path=None,
log_path=None, **kwargs):
"""
Wrapper for running spm.Realign with optional reporting.
Expand Down Expand Up @@ -383,23 +388,22 @@ def _do_subject_realign(subject_data, reslice=False, register_to_mean=False,
if report:
subject_data.generate_realignment_thumbnails()

if report_path not in [False,None]:
if report_path not in [False, None]:
rp_plot, rp_log = generate_realignment_report(
subject_data=subject_data,
estimated_motion=subject_data.realignment_parameters,
output_dir=subject_data.output_dir,
report_path=report_path
)
add_component(rp_plot,report_path,rp_log,log_path)
report_path=report_path)
add_component(rp_plot, report_path, rp_log,log_path)

return subject_data.sanitize()


def _do_subject_coregister(subject_data, reslice=False, spm_dir=None,
matlab_exec=None, spm_mcr=None,
coreg_anat_to_func=False, caching=True,
report=True, software="spm", hardlink_output=True
,report_path=None,log_path=None,**kwargs):
report=True, software="spm", hardlink_output=True,
report_path=None, log_path=None, **kwargs):
"""Wrapper for running spm.Coregister with optional reporting.
If subject_data has a `results_gallery` attribute, then QA thumbnails will
Expand Down Expand Up @@ -466,7 +470,7 @@ def _do_subject_coregister(subject_data, reslice=False, spm_dir=None,
return _pp_do_subject_coregister(
subject_data, reslice=reslice,
coreg_func_to_anat=not coreg_anat_to_func, report=report,
caching=caching,)
caching=caching)

# sanitize software choice
if not software in ["spm", "fsl"]:
Expand Down Expand Up @@ -566,20 +570,20 @@ def _do_subject_coregister(subject_data, reslice=False, spm_dir=None,
if report:
subject_data.generate_coregistration_thumbnails()

if report_path not in [False,None]:
if report_path not in [False, None]:
correg_plot, correg_log = generate_corregistration_report(
subject_data=subject_data,
output_dir=subject_data.output_dir,
report_path=report_path)
add_component(correg_plot,report_path,correg_log,log_path)
add_component(correg_plot, report_path, correg_log, log_path)

return subject_data.sanitize()


def _do_subject_segment(subject_data, output_modulated_tpms=True, spm_dir=None,
matlab_exec=None, spm_mcr=None, normalize=False,
caching=True, report=True, software="spm",
hardlink_output=True,report_path=None,log_path=None):
hardlink_output=True, report_path=None, log_path=None):
"""
Wrapper for running spm.Segment with optional reporting.
Expand Down Expand Up @@ -713,15 +717,15 @@ def _do_subject_segment(subject_data, output_modulated_tpms=True, spm_dir=None,
if report:
subject_data.generate_segmentation_thumbnails()

if report_path not in [False,None]:
if report_path not in [False, None]:
seg_plot, seg_log = generate_segmentation_report(
subject_data=subject_data,
output_dir=subject_data.output_dir,
subject_gm_file=getattr(subject_data, 'gm', None),
subject_wm_file=getattr(subject_data, 'wm', None),
subject_csf_file=getattr(subject_data, 'csf', None),
only_native=True,report_path=report_path)
add_component(seg_plot,report_path,seg_log,log_path)
only_native=True, report_path=report_path)
add_component(seg_plot, report_path, seg_log, log_path)

return subject_data.sanitize()

Expand All @@ -732,9 +736,8 @@ def _do_subject_normalize(subject_data, fwhm=0., anat_fwhm=0., caching=True,
anat_write_voxel_sizes=[1, 1, 1], report=True,
software="spm", hardlink_output=True,
smooth_software="spm", epi_template=None,
t1_template=None, coregister=True
,report_path=None,log_path=None
, **kwargs):
t1_template=None, coregister=True,
report_path=None, log_path=None, **kwargs):
"""
Wrapper for running spm.Segment with optional reporting.
Expand Down Expand Up @@ -958,12 +961,12 @@ def _do_subject_normalize(subject_data, fwhm=0., anat_fwhm=0., caching=True,
if report:
subject_data.generate_normalization_thumbnails()

if report_path not in [False,None]:
if report_path not in [False, None]:
norm_plot, norm_log = generate_normalization_report(
subject_data=subject_data,
output_dir=subject_data.output_dir,
report_path=report_path)
add_component(norm_plot,report_path,norm_log,log_path)
add_component(norm_plot, report_path, norm_log, log_path)

# explicit smoothing
if np.sum(fwhm) + np.sum(anat_fwhm) > 0:
Expand All @@ -979,8 +982,7 @@ def _do_subject_normalize(subject_data, fwhm=0., anat_fwhm=0., caching=True,

def _do_subject_smooth(subject_data, fwhm, anat_fwhm=None, spm_dir=None,
matlab_exec=None, spm_mcr=None, caching=True,
software="spm", report=True, hardlink_output=True,
nilearn_report=None):
software="spm", report=True, hardlink_output=True):
"""
Wrapper for running spm.Smooth with optional reporting.
Expand Down Expand Up @@ -1402,7 +1404,7 @@ def do_subject_preproc(
tsdiffana=tsdiffana)

if nilearn_report:
report_path,log_path = initialize_report(subject_data.output_dir,
report_path, log_path = initialize_report(subject_data.output_dir,
subject_name=subject_data.subject_id,
dcm2nii=subject_data.isdicom,
deleteorient=deleteorient,
Expand All @@ -1415,10 +1417,9 @@ def do_subject_preproc(
dartel=dartel,
coreg_func_to_anat=not coreg_anat_to_func,
prepreproc_undergone=prepreproc_undergone,
has_func=subject_data.func
)
has_func=subject_data.func)
else:
report_path,log_path = None, None
report_path, log_path = None, None

#############################
# Slice-Timing Correction
Expand Down Expand Up @@ -1448,7 +1449,7 @@ def do_subject_preproc(
report=report,
hardlink_output=hardlink_output,
software=realign_software,
report_path=report_path,log_path=log_path,
report_path=report_path, log_path=log_path,
**kwargs.get("interface_realign", {}))

# handle failed node
Expand All @@ -1467,7 +1468,7 @@ def do_subject_preproc(
report=report,
hardlink_output=hardlink_output,
software=coregister_software,
report_path=report_path,log_path=log_path,
report_path=report_path, log_path=log_path,
**kwargs.get("interface_coregister", {}))

# handle failed node
Expand All @@ -1481,7 +1482,7 @@ def do_subject_preproc(
if segment:
subject_data = _do_subject_segment(
subject_data, caching=caching, normalize=normalize, report=report,
hardlink_output=hardlink_output,report_path=report_path,
hardlink_output=hardlink_output, report_path=report_path,
log_path=log_path)


Expand Down Expand Up @@ -1541,9 +1542,9 @@ def do_subject_preproc(

if tsdiffana:
tdsdiffana_plot = generate_tsdiffana_report(
subject_data.func, subject_data.session_ids
, subject_data.subject_id
, output_dir=subject_data.output_dir)
subject_data.func, subject_data.session_ids,
subject_data.subject_id,
output_dir=subject_data.output_dir)

add_component(tdsdiffana_plot,report_path)

Expand Down
25 changes: 10 additions & 15 deletions pypreprocess/reporting/check_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
import base64
import urllib.parse

def _plot_to_svg(fig,dpi=300):
def _plot_to_svg(fig, dpi=300):

with io.BytesIO() as io_buffer:
fig.savefig(
io_buffer, format="svg", facecolor="white", edgecolor="white",
dpi=dpi
)
io_buffer, format="svg", facecolor="white",
edgecolor="white", dpi=dpi)
return urllib.parse.quote(io_buffer.getvalue().decode("utf-8"))


def plot_spm_motion_parameters(parameter_file, lengths
, title=None, output_filename=None, close=False
, report_path=None):
def plot_spm_motion_parameters(parameter_file, lengths,
title=None, output_filename=None,
close=False, report_path=None):
""" Plot motion parameters obtained with SPM software
Parameters
Expand Down Expand Up @@ -68,9 +67,8 @@ def plot_spm_motion_parameters(parameter_file, lengths
plt.xlabel('time(scans)')
plt.ylabel('Estimated motion (mm/degrees)')

if report_path not in [False,None]:
if report_path not in [False, None]:
fig = plt.gcf()
# fig.set_rasterized(True)
svg_plot = _plot_to_svg(fig)
else:
svg_plot = None
Expand Down Expand Up @@ -147,9 +145,8 @@ def plot_registration(reference_img, coregistered_img,
# misc
_slicer.title(title, size=12, color='w', alpha=0)

if report_path not in [False,None]:
if report_path not in [False, None]:
fig = plt.gcf()
# fig.set_rasterized(True)
svg_plot = _plot_to_svg(fig)
else:
svg_plot = None
Expand Down Expand Up @@ -215,17 +212,15 @@ def plot_segmentation(
# misc
_slicer.title(title, size=12, color='w', alpha=0)

if report_path not in [False,None]:
if report_path not in [False, None]:
fig = plt.gcf()
# fig.set_rasterized(True)
svg_plot = _plot_to_svg(fig)
else:
svg_plot = None

if not output_filename is None:
plt.savefig(output_filename, bbox_inches='tight', dpi=200,
facecolor="k",
edgecolor="k")
facecolor="k", edgecolor="k")
if close:
plt.close()

Expand Down
Loading

0 comments on commit 9378e0d

Please sign in to comment.