<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Interfaces to generate reportlets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


"""
from __future__ import print_function, division, absolute_import, unicode_literals

import os
from niworkflows.nipype.interfaces.base import (
    traits, TraitedSpec, BaseInterfaceInputSpec,
    File, Directory, InputMultiPath, isdefined)
from niworkflows.interfaces.base import SimpleInterface

from niworkflows.nipype.interfaces import freesurfer as fs

ANATOMICAL_TEMPLATE = """\t\t<h3 class="elem-title">Summary</h3>
\t\t<ul class="elem-desc">
\t\t\t<li>Structural images: {n_t1s:d}</li>
\t\t\t<li>FreeSurfer reconstruction: {freesurfer_status}</li>
\t\t\t<li>Output spaces: {output_spaces}</li>
\t\t</ul>
"""


class SummaryOutputSpec(TraitedSpec):
    out_report = File(exists=True, desc='HTML segment containing summary')


class SummaryInterface(SimpleInterface):
    output_spec = SummaryOutputSpec

    def _run_interface(self, runtime):
        segment = self._generate_segment()
        fname = os.path.abspath('report.html')
        with open(fname, 'w') as fobj:
            fobj.write(segment)

        self._results['out_report'] = fname

        return runtime


class AnatomicalSummaryInputSpec(BaseInterfaceInputSpec):
    t1w = InputMultiPath(File(exists=True), desc='T1w structural images')
    subjects_dir = Directory(desc='FreeSurfer subjects directory')
    subject_id = traits.Str(desc='FreeSurfer subject ID')
    output_spaces = traits.List(desc='Target spaces')


class AnatomicalSummary(SummaryInterface):
    input_spec = AnatomicalSummaryInputSpec

    def _generate_segment(self):
        if not isdefined(self.inputs.subjects_dir):
            freesurfer_status = 'Not run'
        else:
            recon = fs.ReconAll(subjects_dir=self.inputs.subjects_dir,
                                subject_id=self.inputs.subject_id,
                                T1_files=self.inputs.t1w,
                                flags='-noskullstrip')
            if recon.cmdline.startswith('echo'):
                freesurfer_status = 'Pre-existing directory'
            else:
                freesurfer_status = 'Run by FMRIPREP'

        return ANATOMICAL_TEMPLATE.format(n_t1s=len(self.inputs.t1w),
                                          freesurfer_status=freesurfer_status,
                                          output_spaces=', '.join(self.inputs.output_spaces))<commit_msg>REPORT: Add FunctionalSummary interface<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Interfaces to generate reportlets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


"""
from __future__ import print_function, division, absolute_import, unicode_literals

import os
from niworkflows.nipype.interfaces.base import (
    traits, TraitedSpec, BaseInterfaceInputSpec,
    File, Directory, InputMultiPath, isdefined)
from niworkflows.interfaces.base import SimpleInterface

from niworkflows.nipype.interfaces import freesurfer as fs

ANATOMICAL_TEMPLATE = """\t\t<h3 class="elem-title">Summary</h3>
\t\t<ul class="elem-desc">
\t\t\t<li>Structural images: {n_t1s:d}</li>
\t\t\t<li>FreeSurfer reconstruction: {freesurfer_status}</li>
\t\t\t<li>Output spaces: {output_spaces}</li>
\t\t</ul>
"""


FUNCTIONAL_TEMPLATE = """\t\t<h3 class="elem-title">Summary</h3>
\t\t<ul class="elem-desc">
\t\t\t<li>Slice timing correction: {stc}</li>
\t\t\t<li>Susceptibility distortion correction: {sdc}</li>
\t\t\t<li>Registration: {registration}</li>
\t\t\t<li>Functional series resampled to spaces: {output_spaces}</li>
\t\t\t<li>Confounds collected: {confounds}</li>
\t\t</ul>
"""


class SummaryOutputSpec(TraitedSpec):
    out_report = File(exists=True, desc='HTML segment containing summary')


class SummaryInterface(SimpleInterface):
    output_spec = SummaryOutputSpec

    def _run_interface(self, runtime):
        segment = self._generate_segment()
        fname = os.path.abspath('report.html')
        with open(fname, 'w') as fobj:
            fobj.write(segment)

        self._results['out_report'] = fname

        return runtime


class AnatomicalSummaryInputSpec(BaseInterfaceInputSpec):
    t1w = InputMultiPath(File(exists=True), desc='T1w structural images')
    subjects_dir = Directory(desc='FreeSurfer subjects directory')
    subject_id = traits.Str(desc='FreeSurfer subject ID')
    output_spaces = traits.List(desc='Target spaces')


class AnatomicalSummary(SummaryInterface):
    input_spec = AnatomicalSummaryInputSpec

    def _generate_segment(self):
        if not isdefined(self.inputs.subjects_dir):
            freesurfer_status = 'Not run'
        else:
            recon = fs.ReconAll(subjects_dir=self.inputs.subjects_dir,
                                subject_id=self.inputs.subject_id,
                                T1_files=self.inputs.t1w,
                                flags='-noskullstrip')
            if recon.cmdline.startswith('echo'):
                freesurfer_status = 'Pre-existing directory'
            else:
                freesurfer_status = 'Run by FMRIPREP'

        return ANATOMICAL_TEMPLATE.format(n_t1s=len(self.inputs.t1w),
                                          freesurfer_status=freesurfer_status,
                                          output_spaces=', '.join(self.inputs.output_spaces))


class FunctionalSummaryInputSpec(BaseInterfaceInputSpec):
    slice_timing = traits.Bool(False, usedefault=True, desc='Slice timing correction used')
    distortion_correction = traits.Enum('epi', 'fieldmap', 'phasediff', 'SyN', 'None',
                                        desc='Susceptibility distortion correction method')
    registration = traits.Enum('FLIRT', 'bbregister',
                               desc='Functional/anatomical registration method')
    output_spaces = traits.List(desc='Target spaces')
    confounds = traits.List(desc='Confounds collected')


class FunctionalSummary(SummaryInterface):
    input_spec = FunctionalSummaryInputSpec

    def _generate_segment(self):
        stc = "Applied" if self.inputs.slice_timing else "Not applied"
        sdc = {'epi': 'Phase-encoding polarity (pepolar)',
               'fieldmap': 'Direct fieldmapping',
               'phasediff': 'Phase difference',
               'SyN': 'Symmetric normalization (SyN) - no fieldmaps',
               'None': 'None'}[self.inputs.distortion_correction]
        reg = {'FLIRT': 'FLIRT with boundary-based registration (BBR) metric',
               'bbregister': 'FreeSurfer boundary-based registration (bbregister)'
               }[self.inputs.registration]
        return FUNCTIONAL_TEMPLATE.format(stc=stc, sdc=sdc, registration=reg,
                                          output_spaces=', '.join(self.inputs.output_spaces),
                                          confounds=', '.join(self.inputs.confounds))
{'fraction_non_alphanumeric': 0.12530120481927712, 'fraction_numerical': 0.004252303330970942, 'mean_word_length': 2.975211267605634, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4601168', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2088, 'n_words': 404}