|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 3 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 4 | +""" |
| 5 | +Managing statistical maps |
| 6 | +""" |
| 7 | +from __future__ import (print_function, division, unicode_literals, |
| 8 | + absolute_import) |
| 9 | +import os |
| 10 | +import nibabel as nb |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +from ..interfaces.base import ( |
| 14 | + BaseInterfaceInputSpec, TraitedSpec, SimpleInterface, |
| 15 | + traits, InputMultiPath, File |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class ACMInputSpec(BaseInterfaceInputSpec): |
| 20 | + in_files = InputMultiPath(File(exists=True), mandatory=True, |
| 21 | + desc='input file, generally a list of z-stat maps') |
| 22 | + threshold = traits.Float(1.65, usedefault=True, |
| 23 | + desc='binarization threshold. A z-value of 1.65 ' |
| 24 | + 'corresponds to a two-sided test of p<.10') |
| 25 | + |
| 26 | + |
| 27 | +class ACMOutputSpec(TraitedSpec): |
| 28 | + out_file = File(exists=True, desc='output activation count map') |
| 29 | + acm_pos = File(exists=True, desc='positive activation count map') |
| 30 | + acm_neg = File(exists=True, desc='negative activation count map') |
| 31 | + |
| 32 | + |
| 33 | +class ACM(SimpleInterface): |
| 34 | + """ |
| 35 | + Calculate a simple Activation Count Maps |
| 36 | + """ |
| 37 | + input_spec = ACMInputSpec |
| 38 | + output_spec = ACMOutputSpec |
| 39 | + |
| 40 | + def _run_interface(self, runtime): |
| 41 | + allmaps = nb.concat_images( |
| 42 | + [nb.load(f) for f in self.inputs.in_files]) |
| 43 | + acm_pos = np.where(allmaps > self.inputs.threshold).astype( |
| 44 | + float).mean(3) |
| 45 | + acm_neg = np.where(allmaps < -1.0 * self.inputs.threshold).astype( |
| 46 | + float).mean(3) |
| 47 | + |
| 48 | + acm_diff = acm_pos - acm_neg |
| 49 | + |
| 50 | + nii = nb.load(self.inputs.in_files[0]) |
| 51 | + self._results['out_file'] = os.path.join( |
| 52 | + runtime.cwd, 'acm_diff.nii.gz') |
| 53 | + self._results['acm_pos'] = os.path.join( |
| 54 | + runtime.cwd, 'acm_pos.nii.gz') |
| 55 | + self._results['acm_neg'] = os.path.join( |
| 56 | + runtime.cwd, 'acm_neg.nii.gz') |
| 57 | + |
| 58 | + nb.Nifti1Image( |
| 59 | + acm_diff, nii.affine, nii.header).to_filename(self._results['out_file']) |
| 60 | + nb.Nifti1Image( |
| 61 | + acm_pos, nii.affine, nii.header).to_filename(self._results['acm_pos']) |
| 62 | + nb.Nifti1Image( |
| 63 | + acm_neg, nii.affine, nii.header).to_filename(self._results['acm_neg']) |
| 64 | + |
| 65 | + return runtime |
0 commit comments