|
| 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 | +from ..utils.filemanip import split_filename |
| 18 | + |
| 19 | + |
| 20 | +class ActivationCountInputSpec(BaseInterfaceInputSpec): |
| 21 | + in_files = InputMultiPath(File(exists=True), mandatory=True, |
| 22 | + desc='input file, generally a list of z-stat maps') |
| 23 | + threshold = traits.Float( |
| 24 | + mandatory=True, desc='binarization threshold. E.g. a threshold of 1.65 ' |
| 25 | + 'corresponds to a two-sided Z-test of p<.10') |
| 26 | + |
| 27 | + |
| 28 | +class ActivationCountOutputSpec(TraitedSpec): |
| 29 | + out_file = File(exists=True, desc='output activation count map') |
| 30 | + acm_pos = File(exists=True, desc='positive activation count map') |
| 31 | + acm_neg = File(exists=True, desc='negative activation count map') |
| 32 | + |
| 33 | + |
| 34 | +class ActivationCount(SimpleInterface): |
| 35 | + """ |
| 36 | + Calculate a simple Activation Count Maps |
| 37 | +
|
| 38 | + Adapted from: https://github.com/poldracklab/CNP_task_analysis/\ |
| 39 | + blob/61c27f5992db9d8800884f8ffceb73e6957db8af/CNP_2nd_level_ACM.py |
| 40 | + """ |
| 41 | + input_spec = ActivationCountInputSpec |
| 42 | + output_spec = ActivationCountOutputSpec |
| 43 | + |
| 44 | + def _run_interface(self, runtime): |
| 45 | + allmaps = nb.concat_images(self.inputs.in_files).get_data() |
| 46 | + acm_pos = np.mean(allmaps > self.inputs.threshold, |
| 47 | + axis=3, dtype=np.float32) |
| 48 | + acm_neg = np.mean(allmaps < -1.0 * self.inputs.threshold, |
| 49 | + axis=3, dtype=np.float32) |
| 50 | + acm_diff = acm_pos - acm_neg |
| 51 | + |
| 52 | + template_fname = self.inputs.in_files[0] |
| 53 | + ext = split_filename(template_fname)[2] |
| 54 | + fname_fmt = os.path.join(runtime.cwd, 'acm_{}' + ext).format |
| 55 | + |
| 56 | + self._results['out_file'] = fname_fmt('diff') |
| 57 | + self._results['acm_pos'] = fname_fmt('pos') |
| 58 | + self._results['acm_neg'] = fname_fmt('neg') |
| 59 | + |
| 60 | + img = nb.load(template_fname) |
| 61 | + img.__class__(acm_diff, img.affine, img.header).to_filename( |
| 62 | + self._results['out_file']) |
| 63 | + img.__class__(acm_pos, img.affine, img.header).to_filename( |
| 64 | + self._results['acm_pos']) |
| 65 | + img.__class__(acm_neg, img.affine, img.header).to_filename( |
| 66 | + self._results['acm_neg']) |
| 67 | + |
| 68 | + return runtime |
0 commit comments