From 279f79cfd499c762f24b390da0c10d9cfe25b287 Mon Sep 17 00:00:00 2001 From: oesteban Date: Wed, 7 Sep 2016 17:41:48 -0700 Subject: [PATCH 1/8] [ENH] Add a FramewiseDisplacement interface Calculates the FD following the implementation of fsl_motion_outliers, that theoretically follows that of Power et al. 2012. If ```save_plot``` is ```True```, then it will save an FD plot using seaborn (but this is optional). Includes a test that assess that this implementation is equivalent to the one in fsl_motion_outliers --- nipype/algorithms/misc.py | 97 +++++ nipype/algorithms/tests/test_fd.py | 18 + nipype/testing/data/fsl_mcflirt_movpar.txt | 365 ++++++++++++++++++ .../testing/data/fsl_motion_outliers_fd.txt | 364 +++++++++++++++++ 4 files changed, 844 insertions(+) create mode 100644 nipype/algorithms/tests/test_fd.py create mode 100644 nipype/testing/data/fsl_mcflirt_movpar.txt create mode 100644 nipype/testing/data/fsl_motion_outliers_fd.txt diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index edd4ef83f1..ae99c88921 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -38,6 +38,69 @@ iflogger = logging.getLogger('interface') +class FramewiseDisplacementInputSpec(BaseInterfaceInputSpec): + in_plots = File(exists=True, desc='motion parameters as written by FSL MCFLIRT') + radius = traits.Float(50, usedefault=True, + desc='radius in mm to calculate angular FDs, 50mm is the ' + 'default since it is used in Power et al. 2012') + out_file = File('fd_power_2012.txt', usedefault=True, desc='output file name') + out_figure = File('fd_power_2012.pdf', usedefault=True, desc='output figure name') + series_tr = traits.Float(desc='repetition time in sec.') + save_plot = traits.Bool(False, usedefault=True, desc='write FD plot') + normalize = traits.Bool(False, usedefault=True, desc='calculate FD in mm/s', + requires=['series_tr']) + figdpi = traits.Int(100, usedefault=True, desc='output dpi for the FD plot') + figsize = traits.Tuple(traits.Float(11.7), traits.Float(2.3), usedefault=True, + desc='output figure size') + +class FramewiseDisplacementOutputSpec(TraitedSpec): + out_file = File(desc='calculated FD per timestep') + out_figure = File(desc='output image file') + fd_average = traits.Float(desc='average FD') + +class FramewiseDisplacement(BaseInterface): + """ + Calculate the :abbr:`FD (framewise displacement)` as in [Power2012]_. + This implementation reproduces the calculation in fsl_motion_outliers + + .. [Power2012] Power et al., Spurious but systematic correlations in functional + connectivity MRI networks arise from subject motion, NeuroImage 59(3), + 2012. doi:`10.1016/j.neuroimage.2011.10.018 + `_. + + + """ + + input_spec = FramewiseDisplacementInputSpec + output_spec = FramewiseDisplacementOutputSpec + + def _run_interface(self, runtime): + mpars = np.loadtxt(self.inputs.in_plots) # mpars is N_t x 6 + diff = mpars[:-1, :] - mpars[1:, :] + diff[:, :3] *= self.inputs.radius + fd_res = np.abs(diff).sum(axis=1) + + self._results = { + 'out_file': op.abspath(self.inputs.out_file), + 'fd_average': float(fd_res.mean()) + } + np.savetxt(self.inputs.out_file, fd_res) + + if self.inputs.save_plot: + self._results['out_figure'] = op.abspath(self.inputs.out_figure) + tr = None + if self.inputs.normalize: + tr = self.inputs.series_tr + fig = plot_fd(fd_res, self.inputs.figsize, series_tr=tr) + fig.savefig(self._results['out_figure'], dpi=float(self.inputs.figdpi), + format=self.inputs.out_figure[-3:], + bbox_inches='tight') + return runtime + + def _list_outputs(self): + return self._results + + class PickAtlasInputSpec(BaseInterfaceInputSpec): atlas = File(exists=True, desc="Location of the atlas that will be used.", mandatory=True) @@ -1460,6 +1523,40 @@ def merge_rois(in_files, in_idxs, in_ref, return out_file +def plot_fd(fd_values, figsize, series_tr=None): + """ + A helper function to plot the framewise displacement + """ + import matplotlib + matplotlib.use('Agg') + import matplotlib.pyplot as plt + from matplotlib.gridspec import GridSpec + from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas + import seaborn as sns + + fig = plt.Figure(figsize=figsize) + FigureCanvas(fig) + grid = GridSpec(1, 2, width_ratios=[3, 1], wspace=0.025) + grid.update(hspace=1.0, right=0.95, left=0.1, bottom=0.2) + + ax = fig.add_subplot(grid[0, :-1]) + ax.plot(fd_values) + ax.set_xlim((0, len(fd_values))) + ax.set_ylabel('FD [{}]'.format('mm/s' if series_tr is not None else 'mm')) + + xlabel = 'Frame #' + if series_tr is not None: + xlabel = 'Frame # ({} sec TR)'.format(series_tr) + ax.set_xlabel(xlabel) + ylim = ax.get_ylim() + + ax = fig.add_subplot(grid[0, -1]) + sns.distplot(fd_values, vertical=True, ax=ax) + ax.set_xlabel('Frames') + ax.set_ylim(ylim) + ax.set_yticklabels([]) + return fig + # Deprecated interfaces ------------------------------------------------------ class Distance(nam.Distance): diff --git a/nipype/algorithms/tests/test_fd.py b/nipype/algorithms/tests/test_fd.py new file mode 100644 index 0000000000..2fa6e29624 --- /dev/null +++ b/nipype/algorithms/tests/test_fd.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from nipype.testing import (assert_equal, example_data) +from nipype.algorithms.misc import FramewiseDisplacement +import numpy as np +from tempfile import mkdtemp +from shutil import rmtree + +def test_fd(): + tempdir = mkdtemp() + ground_truth = np.loadtxt(example_data('fsl_motion_outliers_fd.txt')) + fd = FramewiseDisplacement(in_plots=example_data('fsl_mcflirt_movpar.txt'), + out_file=tempdir + '/fd.txt') + res = fd.run() + yield assert_equal, np.allclose(ground_truth, np.loadtxt(res.outputs.out_file)), True + yield assert_equal, np.abs(ground_truth.mean() - res.outputs.fd_average) < 1e-4, True + rmtree(tempdir) diff --git a/nipype/testing/data/fsl_mcflirt_movpar.txt b/nipype/testing/data/fsl_mcflirt_movpar.txt new file mode 100644 index 0000000000..cfbfbfc499 --- /dev/null +++ b/nipype/testing/data/fsl_mcflirt_movpar.txt @@ -0,0 +1,365 @@ +-0.00848102 0.00369798 0.003424 0.31043 -0.751705 0.619666 +-0.00786305 0.00338866 0.0031168 0.305984 -0.736865 0.60846 +-0.0078758 0.00327434 0.00305205 0.310853 -0.712291 0.60703 +-0.00697324 0.00321863 0.00290525 0.312876 -0.690708 0.574235 +-0.00493536 0.00253503 0.00278449 0.306737 -0.640998 0.497959 +-0.0050205 0.00287942 0.00278449 0.302929 -0.611536 0.504744 +-0.00515396 0.0028174 0.00278449 0.306624 -0.626585 0.495511 +-0.00502531 0.00303599 0.00238171 0.304389 -0.615857 0.476798 +-0.0051203 0.00275721 0.00238171 0.293279 -0.609103 0.479662 +-0.0051486 0.00302039 0.00238171 0.3024 -0.590623 0.504029 +-0.00480493 0.00280246 0.00254305 0.299592 -0.607553 0.496145 +-0.00483143 0.0029749 0.00238171 0.296309 -0.610005 0.49058 +-0.00500805 0.00294036 0.00238171 0.289551 -0.586523 0.475725 +-0.00507794 0.00290279 0.00251728 0.291829 -0.581586 0.4659 +-0.00505649 0.00287046 0.00238171 0.285892 -0.558162 0.486484 +-0.00486141 0.0029014 0.00238171 0.281708 -0.576438 0.500581 +-0.00451471 0.00325153 0.00238171 0.260234 -0.581576 0.48697 +-0.00435536 0.00291181 0.00205411 0.243392 -0.57584 0.482712 +-0.00372446 0.00344849 0.00215792 0.243957 -0.532488 0.454305 +-0.00426827 0.00306549 0.00211817 0.247016 -0.524634 0.443058 +-0.00430498 0.00292419 0.00204627 0.250965 -0.520665 0.447152 +-0.00416968 0.00284674 0.0015551 0.227943 -0.517509 0.427806 +-0.00412455 0.00257037 0.0015551 0.220707 -0.541823 0.446838 +-0.00375444 0.00283961 0.0015551 0.240189 -0.523312 0.445508 +-0.004049 0.00295457 0.00171111 0.247864 -0.542133 0.441171 +-0.00341088 0.00353936 0.00196785 0.241637 -0.530257 0.458271 +-0.00385554 0.00307726 0.00192797 0.235657 -0.549361 0.462623 +-0.00381038 0.00290875 0.0019887 0.227608 -0.520289 0.446221 +-0.00404867 0.00272854 0.0015551 0.228016 -0.504383 0.428189 +-0.00407163 0.0026387 0.0015551 0.228074 -0.504025 0.412914 +-0.00364706 0.00253998 0.0015551 0.228137 -0.475896 0.384941 +-0.0037216 0.00258769 0.00129908 0.219247 -0.475888 0.39801 +-0.00354442 0.0026195 0.00161976 0.223251 -0.494004 0.413261 +-0.00365106 0.00266975 0.00140795 0.214972 -0.475915 0.395145 +-0.00345051 0.0026654 0.00143458 0.217735 -0.499591 0.395244 +-0.00386278 0.0032511 0.00148858 0.227885 -0.4916 0.410386 +-0.00370985 0.0028687 0.00155317 0.22808 -0.454425 0.401618 +-0.00361409 0.00281021 0.00114015 0.210271 -0.474531 0.395154 +-0.00336899 0.0028391 0.00122588 0.206562 -0.48207 0.395278 +-0.00322115 0.0025016 0.000971286 0.19819 -0.488312 0.401101 +-0.00332647 0.00214777 0.000951943 0.19833 -0.441523 0.362933 +-0.00379846 0.0025001 0.0011168 0.200561 -0.41288 0.375803 +-0.00385488 0.00269027 0.00159237 0.197731 -0.456883 0.408944 +-0.00316266 0.00254457 0.00153409 0.23235 -0.46996 0.41613 +-0.00287148 0.00279855 0.00140178 0.221113 -0.452827 0.399155 +-0.00330114 0.00308189 0.00119473 0.217267 -0.437719 0.400279 +-0.00272255 0.00296461 0.00121548 0.205994 -0.422254 0.38321 +-0.00225855 0.0027351 0.001041 0.210033 -0.427521 0.374364 +-0.00217991 0.00264376 0.00113029 0.214503 -0.411771 0.383445 +-0.00214648 0.00214777 0.00105354 0.212427 -0.426881 0.370372 +-0.00161787 0.00260412 0.000998276 0.207648 -0.430173 0.362644 +-0.00180066 0.00214777 0.0010713 0.20834 -0.444568 0.365418 +-0.00180454 0.0024648 0.000798108 0.202802 -0.403147 0.356207 +-0.0020506 0.00214777 0.000798108 0.197762 -0.399779 0.348588 +-0.00190313 0.0024758 0.000883538 0.194956 -0.400015 0.358683 +-0.00146259 0.00214777 0.000798108 0.197762 -0.40225 0.370598 +-0.00174289 0.00245124 0.00116596 0.197472 -0.39386 0.376956 +-0.00155825 0.00276825 0.00122167 0.197315 -0.412931 0.383866 +-0.00185648 0.00259941 0.00105382 0.197469 -0.395061 0.383594 +-0.00182118 0.00267729 0.00107721 0.197449 -0.391875 0.365938 +-0.000973134 0.00301533 0.00115758 0.197249 -0.394952 0.350869 +-0.00167533 0.00247387 0.000798107 0.187618 -0.392903 0.365637 +-0.00138586 0.00266239 0.00114166 0.197418 -0.434129 0.378076 +-0.00150404 0.00262249 0.000978026 0.197533 -0.381574 0.37689 +-0.00159637 0.00238195 0.000798108 0.197691 -0.399326 0.371102 +-0.0015072 0.00243395 0.000798107 0.203798 -0.40491 0.383813 +-0.00148289 0.00240714 0.00113101 0.199821 -0.37287 0.375321 +-0.00125133 0.00251404 0.00106359 0.194161 -0.376139 0.375408 +-0.0012191 0.00263648 0.00122436 0.188593 -0.377702 0.366024 +-0.00113731 0.00240082 0.0010032 0.178868 -0.378858 0.358996 +-0.00148008 0.00232081 0.00101022 0.180199 -0.372738 0.383817 +-0.0012126 0.00250957 0.000798107 0.175212 -0.34567 0.37173 +-0.00111795 0.00241347 0.000798107 0.170299 -0.353606 0.358821 +-0.00095758 0.00213035 0.000397963 0.161141 -0.357446 0.365647 +-0.00109816 0.00210888 0.000620097 0.16592 -0.367498 0.356733 +-0.0025239 0.00241204 0.000875664 0.174841 -0.318649 0.365096 +-0.00194576 0.00235136 0.000791062 0.173719 -0.34921 0.365346 +-0.00136463 0.00219537 0.000472743 0.183497 -0.349016 0.347253 +-0.00105549 0.00221794 0.000524981 0.172247 -0.356581 0.336894 +-0.000934616 0.00225553 0.000472743 0.19016 -0.348219 0.341451 +-0.000892303 0.00210585 0.000472743 0.190212 -0.354825 0.346641 +-0.000674097 0.00207306 0.000472743 0.187654 -0.347504 0.36574 +-0.000731368 0.00224711 0.000472743 0.162845 -0.311492 0.347469 +-0.000851133 0.00219081 0.00031125 0.166713 -0.330998 0.327355 +-0.000823373 0.00271374 0.000472743 0.161522 -0.300522 0.336238 +-0.00186156 0.0026479 0.000527825 0.174187 -0.311056 0.384604 +-0.00181456 0.00293178 0.000472743 0.17862 -0.31363 0.365878 +-0.00145165 0.00292302 0.000472743 0.174693 -0.323792 0.366044 +-0.000280547 0.00302176 0.000613008 0.171158 -0.307377 0.354648 +-0.00102857 0.00241798 0.000472743 0.169783 -0.33105 0.366085 +-0.000910833 0.00233255 0.000472743 0.172468 -0.326596 0.370393 +-0.00259817 0.00305771 0.000318212 0.172369 -0.233163 0.328424 +-0.00274121 0.002114 0.000472743 0.15706 -0.378932 0.346467 +-0.000925877 0.002114 0.000472743 0.16657 -0.331152 0.323551 +-0.000908101 0.00225225 0.000472744 0.157014 -0.303804 0.331662 +-0.000989428 0.00211401 0.000209262 0.161837 -0.277138 0.335176 +-0.000944042 0.00230984 0.000119085 0.176478 -0.291953 0.337145 +-0.000735466 0.00232536 0.000472743 0.176324 -0.326139 0.320604 +-0.000891236 0.00211401 0.000472744 0.175609 -0.304903 0.337474 +-0.000458968 0.00211401 0.000472744 0.164809 -0.30954 0.312743 +-0.000668192 0.00211401 0.000363271 0.160076 -0.29724 0.336205 +-0.00148407 0.00223152 0.000472744 0.171184 -0.275324 0.366291 +-0.00201892 0.00250118 0.000472744 0.168691 -0.286058 0.348595 +-0.00146059 0.00236547 0.000472744 0.161138 -0.278151 0.326023 +-0.00135461 0.00222972 0.000472744 0.161689 -0.260247 0.318822 +-0.00092949 0.00222308 0.000643927 0.161278 -0.272452 0.316306 +-0.000847415 0.00234605 0.000618961 0.162992 -0.256975 0.315654 +-0.00101742 0.00232242 0.000472744 0.153625 -0.227373 0.297289 +-0.000361782 0.00189599 0.000252999 0.142833 -0.261867 0.277084 +-0.000479091 0.0015063 0.000472744 0.150675 -0.239074 0.281411 +-0.000360777 0.00165293 0.000472744 0.159345 -0.239806 0.294284 +-3.19434e-05 0.00168715 0.000406089 0.159362 -0.232677 0.319091 +-0.000533827 0.00173204 0.00036781 0.159385 -0.233485 0.308725 +-0.000668182 0.00188107 0.000369061 0.159356 -0.207783 0.275831 +-0.000355443 0.00176457 0.000365783 0.159394 -0.220577 0.276127 +-0.00060883 0.00189395 0.000472744 0.15932 -0.22128 0.287449 +-0.000746941 0.0018624 0.000616126 0.15924 -0.225162 0.291474 +-0.000564671 0.0017201 0.000472744 0.153814 -0.226566 0.287388 +-0.00145663 0.00258813 0.00116296 0.185202 -0.244621 0.335017 +-0.00219006 0.00245987 0.000787457 0.169001 -0.208462 0.354626 +-0.00191619 0.00231926 0.000472743 0.166009 -0.216173 0.338666 +-0.00172903 0.00216372 0.000690806 0.170092 -0.219892 0.310842 +-0.00123709 0.00180974 0.000419105 0.173846 -0.20502 0.317748 +-0.00137921 0.00199518 0.000472743 0.159294 -0.202677 0.329726 +-0.00144674 0.00173962 0.000472743 0.15938 -0.217726 0.325648 +-0.00118526 0.00170087 0.000472743 0.159396 -0.209523 0.310477 +-0.00118249 0.00167393 0.000555762 0.176098 -0.218246 0.310515 +-0.00151082 0.00179082 0.000472743 0.17037 -0.196022 0.316673 +-0.00162813 0.00180919 0.000472743 0.153439 -0.182404 0.310498 +-0.00139222 0.00185995 0.000472743 0.149298 -0.185631 0.30168 +-0.00139271 0.0017295 0.000472743 0.147365 -0.176281 0.299172 +-0.00180321 0.00144362 0.000322926 0.143595 -0.183191 0.298361 +-0.00214857 0.00144269 0.000472743 0.142475 -0.192361 0.293597 +-0.00154472 0.00146742 0.000472743 0.141103 -0.192287 0.281476 +-0.000568332 0.00143017 0.000472743 0.130095 -0.188297 0.269273 +-0.00113892 0.00137752 4.43659e-07 0.130224 -0.175748 0.269201 +-0.00103425 0.00142273 0.000185835 0.13561 -0.183987 0.269193 +-0.000774376 0.0015795 0.000472743 0.138689 -0.165624 0.257333 +-0.00173929 0.00179112 0.000254479 0.130085 -0.184029 0.274216 +-0.000135947 0.00195957 2.76676e-05 0.130163 -0.144255 0.240247 +-0.00103911 0.0016387 0.000115201 0.130179 -0.167486 0.246782 +-0.00110093 0.00171344 0.000115603 0.123061 -0.149876 0.239903 +-0.00109477 0.00165112 0.000265259 0.123906 -0.165456 0.247424 +-0.000802759 0.00159631 0.000288558 0.130126 -0.17102 0.262394 +-0.000862495 0.00135147 -6.8268e-05 0.130255 -0.144222 0.259091 +-0.00297174 0.00174883 0.000260285 0.167241 -0.0379135 0.188088 +-0.00105132 0.00108635 -0.000173276 0.0878331 -0.204579 0.207703 +-0.0013336 -0.000126747 -0.000484435 0.0254196 -0.11665 0.195031 +-0.0010814 -0.000294802 -0.000484435 0.0140919 -0.0978324 0.126513 +-0.000603341 0.000302712 2.64698e-23 0.0405334 -0.104296 0.0975108 +-0.000825199 0.000493607 -8.17347e-05 0.0263388 -0.0460845 0.0975304 +-0.000750846 0.00028972 -0.000410709 0.0187101 -0.0634676 0.0974913 +-0.00077172 0.000374374 0 0.0186823 -0.06348 0.097471 +-0.000700404 0.000438789 -5.29396e-23 0.0186822 -0.0825618 0.0773451 +-0.000380307 0.00023343 0.00029862 0.0326818 -0.0864233 0.0530209 +-0.000104716 0.00023343 0.00011753 0.0297457 -0.0664206 0.0363457 +-0.000368444 0.00023343 1.32349e-23 0.0245133 -0.0499621 0.0113951 +0.000291951 0.00023343 0 0.0186943 -0.0499781 0.0114422 +-2.62807e-05 0.00023343 0 0.0186943 -0.0546394 0.0114204 +0.000255729 0.00023343 -0.000164307 0.0186982 -0.0395877 0.0078155 +0.000194914 0.00023343 0 0.0213834 -0.0626574 0.020654 +0.000193759 0.00023343 0 0.0186943 -0.0329949 0.0114375 +-3.70427e-05 0.00023343 0 0.0186943 -0.0279062 0.0114589 +-0.000323262 0.00023343 0 0.0186943 -0.0282253 0.0321371 +0.00084391 0.000638024 2.11758e-22 0.0186785 -0.0278405 0.0113712 +-7.69024e-06 0.000109617 -0.000147268 0.0220757 -0.032972 0.0230633 +-0.000129427 0.000109617 0 0.0321617 -0.0329855 0.0230997 +0.000226123 0.000305439 -6.66191e-05 0.0186917 -0.032967 0.0420957 +0.000226123 0.000109617 0 0.0187093 -0.0329701 0.0444471 +0.00042491 0.000109617 0 0.0187093 -0.0351983 0.0208685 +0.000143792 0.000539639 0 0.0186927 0.00303243 0.0145763 +-4.10923e-05 0.000539639 -8.43282e-05 0.0278386 -0.00870932 0.0453143 +-0.000184071 0.000539639 -0.000101156 0.0255131 -0.0137627 0.0395709 +0.00132307 -5.3134e-05 -0.000449938 0.0206134 0.00314807 0.0205795 +0.000250579 0.000391543 0 0.018478 -0.0139536 0.0283306 +0.000377446 -0 0 0.0151655 0.00295563 0.0203817 +0.000586776 -0 0 0.0175544 -0.00204988 0.0203686 +0.000276676 -0 -0.000255649 -2.83769e-07 -0.00704635 0.0387269 +-0.000371102 0.000301187 0 -1.3095e-05 0.0271561 0.0227428 +0.000115231 0.000164171 -0.000236649 -1.08038e-05 0.000926837 0.00844783 +-0.000636739 -0.00048414 -0.000178152 6.10722e-05 -0.0295802 0.0422578 +0.000574861 -1.32349e-23 -0.00017029 4.76745e-06 -0.0220311 0.0160948 +0 -0 0 0 0 0 +5.49884e-05 -0 0 0 9.64137e-08 -2.30886e-06 +-0.00122678 -0.00040948 -0.000406403 9.19199e-05 -0.000129593 0.0283654 +0.00105998 0.000520764 0 -6.85096e-06 0.0200838 -0.0147987 +0.000226882 -4.6423e-05 0 0.00537123 0.0200853 -0.00548566 +0.000513422 -4.6423e-05 -5.29275e-05 -2.36415e-06 0.00468766 -0.0254726 +6.55836e-05 -0.000154116 0.000252928 -0.00237985 0.02008 -0.039783 +-0.000235384 -4.6423e-05 0 -0.0238541 0.0316494 -0.0486997 +-0.000576924 -0.000140285 -0.000262576 -0.0151304 0.0112579 -0.015439 +-1.99358e-06 -4.6423e-05 -0.000205989 -0.023605 0.0200991 -0.0367281 +-0.000119211 4.71597e-05 0 -0.020794 0.0375814 -0.0414349 +-6.89438e-05 0.00032748 -7.78778e-05 -0.020814 0.0372644 -0.0311736 +-0.000180973 0.000239954 -0.000271366 -0.0276414 0.0372721 -0.0413698 +-0.000289225 0.00017937 -0.000261918 -0.0208393 0.03727 -0.0612577 +0.000378139 9.3084e-06 -0.000162182 -0.0207426 0.0372873 -0.0528533 +0.000690299 6.51783e-05 0 -0.0210435 0.0233806 -0.029622 +0.000410376 9.3084e-06 0 -0.0043979 0.0549634 -0.0295984 +-0.0001865 9.3084e-06 0 -0.0043979 0.0549764 -0.0502441 +0.00154853 0.000360969 -0.00027623 -0.0204531 0.0701428 -0.0709676 +9.6498e-05 0.00015804 0 -0.0253298 0.0581827 -0.0539739 +0.000215858 -1.9591e-05 0 -0.0253359 0.06282 -0.0816875 +0.000574688 -0.000126265 -0.000429956 -0.0253925 0.0587834 -0.0900856 +-0.000105844 0.000136093 -0.000501494 -0.0225436 0.0815906 -0.0952095 +-0.00197246 0.000393695 -0.000502585 -0.0253649 0.073506 -0.0625592 +8.61726e-05 0.000146047 -4.33949e-05 -0.00716072 0.0272798 -0.08714 +0.000434765 -1.9591e-05 -0.00031543 -0.00729227 0.081828 -0.101178 +0.000762957 -0.000460443 -0.000365245 -0.0213107 0.0818694 -0.102253 +0.000665589 -1.9591e-05 -2.92773e-05 -0.029813 0.0818495 -0.0820078 +0.000376118 -1.9591e-05 -0.000212159 -0.0212457 0.081845 -0.0819125 +-0.000979862 -1.9591e-05 -0.000365245 -0.0372373 0.108408 -0.0817308 +0.000264963 -1.9591e-05 -0.000266924 -0.032113 0.051199 -0.0988675 +0.00146606 0.000195412 -0.00029391 -0.0321146 0.0800139 -0.109011 +0.000291661 -0.000127204 -0.000365245 -0.0321555 0.0522606 -0.103778 +0.000291661 -0.000183757 -0.00030397 -0.0321532 0.0614865 -0.0985986 +0.000195473 -0.000127204 -0.000365245 -0.019861 0.0614905 -0.0805213 +-2.14393e-05 -0.000127204 -0.000365245 -0.0181936 0.0692439 -0.0870888 +0.0003353 -0.000203575 -0.000365245 -0.0211638 0.081888 -0.103809 +0.000776189 -0.000127204 -0.000365245 -0.0125477 0.0614844 -0.0771749 +0.000176355 -0.000127204 -0.000365245 -0.0233034 0.0878089 -0.0878032 +0.000176355 -0.000127204 -0.000365245 -0.0231802 0.0791931 -0.110647 +-3.83874e-06 0.00038859 -2.73452e-05 0.0125002 0.125464 -0.142273 +0.000868848 -0.000393939 -9.96098e-05 -0.0155239 0.0397744 -0.155987 +0.00091589 -0.000562014 -0.00028095 -0.0190541 0.0824586 -0.135075 +0.00087502 -0.000450016 -0.000365245 -0.0193506 0.104995 -0.133548 +0.000800525 -0.000377505 -0.00031291 -0.0303528 0.114086 -0.136783 +0.00146988 -2.37356e-05 -0.000436639 -0.0323861 0.114029 -0.146069 +0.000719755 -0.000299638 -0.000365245 -0.023389 0.114132 -0.142513 +0.00110753 -0.000377505 -0.000365244 -0.0303642 0.0892388 -0.155885 +0.00136252 -0.000377505 -0.000365244 -0.0303642 0.113575 -0.147853 +0.00231361 -0.000377505 -0.000365244 -0.0303642 0.139888 -0.193263 +0.00110108 -0.000607891 -0.000365244 -0.0304006 0.121659 -0.18148 +0.000927566 -0.000377505 -0.000691199 -0.0281333 0.105962 -0.17862 +0.000751022 -0.000476087 -0.000789899 -0.0334775 0.121691 -0.194406 +0.00112335 -0.000695008 -0.000973046 -0.030542 0.151978 -0.237643 +0.000893459 -0.000377505 -0.000973046 -0.03048 0.137543 -0.242902 +0.00114371 -0.000843107 -0.000973046 -0.0305719 0.121522 -0.237287 +0.00134174 -0.000660276 -0.000973046 -0.0358186 0.128653 -0.219912 +0.00155944 -0.000377505 -0.000793386 -0.0304513 0.145991 -0.222683 +0.00108988 -0.000767429 -0.000720323 -0.0433613 0.122593 -0.215994 +0.00146938 -0.000721923 -0.000762238 -0.0304939 0.122184 -0.198866 +0.00110931 -0.000377505 -0.000688982 -0.0483037 0.143174 -0.19271 +0.0010997 -0.000377505 -0.0007618 -0.0479196 0.143158 -0.208667 +0.000985933 -0.000377505 -0.000789877 -0.0479144 0.136532 -0.198784 +0.000511305 -0.000897433 -0.000789877 -0.0479746 0.125719 -0.183577 +0.00194047 -0.00077943 -0.000895627 -0.0479619 0.122494 -0.212341 +0.00145614 -0.000390931 -0.000794261 -0.0477741 0.138857 -0.194832 +0.00108945 -0.000302473 -0.000413053 -0.0476644 0.168143 -0.20273 +0.000620222 7.65737e-05 -0.000712959 -0.0432873 0.165084 -0.16488 +0.000986368 -0.000118732 -0.000724724 -0.0729611 0.129411 -0.189903 +0.00102258 7.65737e-05 -0.000608053 -0.0812101 0.123499 -0.189655 +0.00127968 -0.000109986 -0.000824954 -0.0882434 0.141524 -0.187677 +0.00151888 -0.000301247 -0.000807071 -0.0795715 0.139571 -0.18773 +0.000598165 -0.000123989 -0.00121758 -0.073005 0.154065 -0.198756 +0.000996279 -0.000331064 -0.000828674 -0.072995 0.134982 -0.201029 +0.000650365 -0.000325216 -0.000828674 -0.0729717 0.126871 -0.187524 +0.00163804 -0.000443304 -0.000657716 -0.0729441 0.137021 -0.187792 +0.00133349 -0.000648157 -0.000828674 -0.0850728 0.153436 -0.209151 +0.00123139 -0.000325216 -0.000828674 -0.0893936 0.16363 -0.220149 +0.00104134 -0.000386943 -0.00102111 -0.0915095 0.173769 -0.220081 +0.000990241 -0.000381413 -0.000828674 -0.0894022 0.173802 -0.220013 +0.00113917 -0.000325216 -0.000828674 -0.0627174 0.128688 -0.24724 +0.00201342 -0.00112228 -0.00123176 -0.0828882 0.191247 -0.245811 +0.00192589 -0.000582862 -0.000983975 -0.111296 0.151928 -0.236204 +0.00180842 -0.000715002 -0.00106754 -0.10782 0.178834 -0.224875 +0.00131595 -0.00040685 -0.000743985 -0.104328 0.181422 -0.220757 +0.0013881 -0.00082289 -0.00115783 -0.104466 0.181384 -0.227374 +0.00151408 -0.000675727 -0.00115783 -0.109676 0.165188 -0.22427 +0.00165711 -0.000666016 -0.00103012 -0.104391 0.146302 -0.212609 +0.00163797 -0.000451953 -0.000952173 -0.107087 0.193623 -0.217631 +0.00109362 -0.000201175 -0.00082053 -0.104261 0.189356 -0.220543 +0.00197396 -0.000229021 -0.000838134 -0.120553 0.17773 -0.235394 +0.0012392 -0.000660943 -0.000944107 -0.109186 0.16886 -0.213007 +0.00053133 0.0001527 -0.00115783 -0.115681 0.175217 -0.207241 +0.00152932 -0.000606794 -0.000995529 -0.11225 0.185208 -0.238669 +0.00123335 -0.000606794 -0.00115783 -0.117824 0.195997 -0.238557 +0.000908555 -0.000606794 -0.00101416 -0.121231 0.190725 -0.238396 +0.00162981 -0.000606794 -0.000844137 -0.121191 0.205108 -0.243566 +0.0015124 -0.000606794 -0.000872531 -0.128413 0.18357 -0.225067 +0.00190692 -0.000606794 -0.000794863 -0.121178 0.188853 -0.225206 +0.00183851 -0.000606794 -0.000854444 -0.130781 0.1979 -0.225175 +0.00164302 -0.000606794 -0.00127114 -0.128815 0.19232 -0.249187 +0.00163785 -0.000606794 -0.00119892 -0.130764 0.193826 -0.240729 +0.00131593 -0.000606794 -0.000906967 -0.131006 0.205435 -0.249097 +0.00200212 -0.000771996 -0.000854444 -0.137492 0.190545 -0.240773 +0.00117287 -0.000698498 -0.000733931 -0.118064 0.201684 -0.225944 +0.00182313 -0.000752464 -0.000854444 -0.134366 0.19632 -0.237962 +0.00180647 -0.000402503 -0.000854444 -0.119718 0.260103 -0.27449 +0.00144831 -0.000793027 -0.000854444 -0.127216 0.190046 -0.2569 +0.00217583 -0.000827675 -0.00129585 -0.12717 0.225556 -0.259739 +0.00201476 -0.000780337 -0.00129585 -0.143892 0.185635 -0.249578 +0.00159089 -0.000794244 -0.00129585 -0.147577 0.1836 -0.241166 +0.00160264 -0.000794244 -0.00129585 -0.149757 0.21402 -0.252527 +0.00233683 -0.000770757 -0.00156229 -0.147641 0.208383 -0.262793 +0.00195913 -0.000720721 -0.00170284 -0.15704 0.199682 -0.285747 +0.00226092 -0.000720721 -0.00160804 -0.156816 0.199696 -0.271042 +0.00212442 -0.000921885 -0.00157298 -0.156921 0.199745 -0.25961 +0.00305442 -0.000720721 -0.00129585 -0.163956 0.212381 -0.245986 +0.00330534 -0.000373221 -0.00179279 -0.186692 0.194904 -0.309214 +0.00346353 -0.000720721 -0.00195238 -0.194758 0.241267 -0.320427 +0.00380009 -0.000720721 -0.00174551 -0.191207 0.222927 -0.322773 +0.0039433 -0.00100031 -0.00175103 -0.195371 0.226483 -0.320548 +0.00446154 -0.00124138 -0.00195885 -0.205667 0.243396 -0.311808 +0.00482548 -0.0009442 -0.00186616 -0.205525 0.222744 -0.309492 +0.00440585 -0.00117045 -0.00196728 -0.205623 0.237897 -0.32044 +0.00414956 -0.000458838 -0.00205432 -0.21865 0.204871 -0.485582 +0.00508424 -0.000610207 -0.00243206 -0.224415 0.162328 -0.430429 +0.00363123 -0.000668638 -0.00243206 -0.252762 0.237689 -0.348448 +0.00387692 -0.00103499 -0.00243206 -0.253835 0.232035 -0.355247 +0.00375814 -0.00104958 -0.00263932 -0.255201 0.242367 -0.37341 +0.00391373 -0.000941601 -0.00243206 -0.263516 0.216384 -0.366453 +0.00373123 -0.00110162 -0.00243206 -0.258176 0.228623 -0.375837 +0.00350662 -0.00104958 -0.00243206 -0.26355 0.262808 -0.366282 +0.00384451 -0.00111889 -0.00263034 -0.263635 0.241215 -0.346647 +0.00381224 -0.00104958 -0.00243206 -0.26355 0.240246 -0.359769 +0.00433029 -0.00104958 -0.00243206 -0.26355 0.240051 -0.373859 +0.00414349 -0.00104958 -0.00243206 -0.26355 0.257642 -0.373794 +0.00346039 -0.00123575 -0.00243206 -0.263612 0.257872 -0.39108 +0.00367153 -0.00104958 -0.0026819 -0.263654 0.249964 -0.396576 +0.0037025 -0.00104958 -0.00292171 -0.26377 0.260542 -0.385052 +0.00361749 -0.00146812 -0.00277011 -0.263837 0.268398 -0.391177 +0.00377836 -0.00145107 -0.00260942 -0.273741 0.277082 -0.391254 +0.00379373 -0.00150051 -0.00243206 -0.255325 0.277121 -0.413741 +0.00280526 -0.00104958 -0.00193346 -0.202101 0.245354 -0.390949 +0.00371669 -0.00104958 -0.00234173 -0.237025 0.260223 -0.391367 +0.00365351 -0.00121915 -0.00267775 -0.260016 0.274336 -0.416525 +0.00356198 -0.00126632 -0.00292571 -0.272055 0.300707 -0.407601 +0.00432012 -0.00120794 -0.00284687 -0.278776 0.245382 -0.390087 +0.00415636 -0.00106094 -0.00267775 -0.273668 0.270464 -0.402974 +0.0047789 -0.00105474 -0.00312553 -0.282745 0.27839 -0.426055 +0.00445082 -0.00120537 -0.00312553 -0.295592 0.3062 -0.425905 +0.00526023 -0.00117712 -0.00312553 -0.278908 0.323336 -0.4457 +0.00516529 -0.00105474 -0.00312553 -0.298321 0.249594 -0.40922 +0.00369585 -0.00105474 -0.00292349 -0.278809 0.250177 -0.411018 +0.00393004 -0.000963189 -0.00292447 -0.278774 0.279156 -0.423023 +0.00394811 -0.00105474 -0.00312553 -0.293691 0.27907 -0.423031 +0.00401 -0.00122407 -0.00312553 -0.292801 0.279061 -0.437143 +0.0042117 -0.00125451 -0.00306221 -0.291453 0.271675 -0.422949 +0.00433882 -0.00122886 -0.00312553 -0.307194 0.266258 -0.427415 +0.0043577 -0.000958762 -0.00312553 -0.310699 0.273065 -0.436539 +0.00445181 -0.000877902 -0.00312553 -0.298478 0.278914 -0.437571 +0.00438267 -0.000890411 -0.00312553 -0.298475 0.278951 -0.448015 +0.0036134 -0.00101293 -0.00344615 -0.298568 0.326858 -0.447108 +0.00458554 -0.00118916 -0.0032966 -0.298609 0.267921 -0.437643 +0.00499018 -0.0011511 -0.00346207 -0.317683 0.276572 -0.437796 +0.00423056 -0.000918149 -0.00343336 -0.317588 0.276881 -0.437575 +0.00418439 -0.000981184 -0.00331384 -0.317534 0.327044 -0.437738 +0.00490767 -0.00111059 -0.00356384 -0.31462 0.298181 -0.454389 +0.0048099 -0.00112715 -0.00356384 -0.317706 0.309028 -0.454346 +0.00550176 -0.0012964 -0.0032231 -0.31762 0.30011 -0.454599 +0.00443756 -0.00118411 -0.00356384 -0.322466 0.350907 -0.448268 +0.00481129 -0.00103471 -0.00356384 -0.32692 0.288698 -0.451768 +0.00548097 -0.00108202 -0.00356384 -0.317694 0.292463 -0.439131 +0.00532322 -0.0013465 -0.00320294 -0.317634 0.299715 -0.4304 +0.00522822 -0.0012716 -0.00324059 -0.332011 0.316303 -0.447072 +0.00508726 -0.00124569 -0.00338776 -0.325842 0.311571 -0.455335 +0.00515591 -0.00113512 -0.00356384 -0.317712 0.319284 -0.445405 +0.00519494 -0.00119238 -0.00356384 -0.321919 0.319276 -0.455369 +0.00480415 -0.00113038 -0.00356384 -0.324561 0.317617 -0.455227 +0.00591115 -0.00134397 -0.00356384 -0.336967 0.307161 -0.455684 +0.00540187 -0.00134084 -0.00356384 -0.336978 0.321724 -0.462179 +0.00495632 -0.00122923 -0.00356384 -0.336943 0.338448 -0.465876 +0.00498272 -0.0012813 -0.00356384 -0.319189 0.338466 -0.465136 +0.00548257 -0.00133825 -0.00385146 -0.335071 0.296729 -0.468606 diff --git a/nipype/testing/data/fsl_motion_outliers_fd.txt b/nipype/testing/data/fsl_motion_outliers_fd.txt new file mode 100644 index 0000000000..3bdee53eaf --- /dev/null +++ b/nipype/testing/data/fsl_motion_outliers_fd.txt @@ -0,0 +1,364 @@ +0.0922165 +0.040464 +0.111654 +0.274237 +0.0615315 +0.037751 +0.069177 +0.0394165 +0.066542 +0.0637689 +0.029314 +0.055653 +0.0291915 +0.0594125 +0.0478581 +0.0750645 +0.0681695 +0.135893 +0.070488 +0.0245075 +0.08072 +0.0666571 +0.0712906 +0.0591095 +0.109185 +0.076768 +0.067243 +0.076951 +0.021331 +0.0823295 +0.0408805 +0.0638545 +0.062919 +0.0381145 +0.0858815 +0.076134 +0.0727425 +0.029358 +0.0574337 +0.109022 +0.0932028 +0.116082 +0.099692 +0.0792185 +0.0660805 +0.079638 +0.0615515 +0.0422645 +0.0605675 +0.0678102 +0.0534692 +0.0858751 +0.0441815 +0.0411835 +0.059656 +0.0626191 +0.054006 +0.050042 +0.0276905 +0.0816688 +0.106604 +0.104542 +0.0699417 +0.0493374 +0.0314606 +0.0637101 +0.02931 +0.032287 +0.0448395 +0.053762 +0.0775596 +0.0352955 +0.0620057 +0.0429542 +0.165356 +0.0681041 +0.080837 +0.0483714 +0.0413671 +0.0214476 +0.0415278 +0.090658 +0.0603659 +0.0801592 +0.12952 +0.0450311 +0.0328385 +0.101851 +0.111088 +0.0216054 +0.263852 +0.241185 +0.170973 +0.0528163 +0.0591555 +0.0479947 +0.0797687 +0.057177 +0.0617814 +0.0564299 +0.115253 +0.0711485 +0.072734 +0.0377425 +0.0452792 +0.0293435 +0.0743266 +0.130582 +0.0712992 +0.0355222 +0.0534384 +0.0404496 +0.0728568 +0.0347538 +0.0365854 +0.0236392 +0.0343136 +0.219582 +0.133829 +0.0631227 +0.0636641 +0.0814131 +0.0479329 +0.0353675 +0.0384015 +0.0310994 +0.060522 +0.043508 +0.0305195 +0.020338 +0.0538009 +0.0398593 +0.044996 +0.0778829 +0.0675269 +0.0303966 +0.0684796 +0.113632 +0.173751 +0.0953603 +0.0384551 +0.0348528 +0.04526 +0.0633001 +0.356055 +0.416511 +0.253341 +0.119676 +0.139908 +0.0971501 +0.0554116 +0.0258724 +0.0459943 +0.0833891 +0.0624481 +0.0657044 +0.0389019 +0.0205947 +0.0409763 +0.0498495 +0.0416258 +0.0166502 +0.0353083 +0.0997548 +0.0965846 +0.0235861 +0.0633841 +0.0154942 +0.0357461 +0.0800965 +0.0650863 +0.0211126 +0.163237 +0.125343 +0.0540911 +0.017874 +0.0691969 +0.11043 +0.0835264 +0.137328 +0.118949 +0.0753882 +0.00275183 +0.133472 +0.244647 +0.0847069 +0.0577316 +0.0751496 +0.0750398 +0.0972748 +0.0748739 +0.0458396 +0.0310216 +0.0366835 +0.0356063 +0.0553765 +0.0639495 +0.0650416 +0.0505025 +0.170091 +0.13039 +0.0472066 +0.0572643 +0.0815014 +0.149822 +0.227285 +0.108031 +0.0560778 +0.0724768 +0.0322847 +0.11819 +0.146627 +0.111114 +0.111445 +0.020299 +0.0410743 +0.0268339 +0.05399 +0.0815168 +0.0777002 +0.0315828 +0.165272 +0.213802 +0.0869493 +0.036218 +0.0332953 +0.068719 +0.0675272 +0.0685225 +0.0451177 +0.119277 +0.102194 +0.0573171 +0.0555505 +0.115179 +0.0471257 +0.0575205 +0.0487958 +0.0594829 +0.0896243 +0.0537505 +0.083843 +0.0204785 +0.0236064 +0.075808 +0.114648 +0.0827696 +0.0791115 +0.102695 +0.119031 +0.0318184 +0.0600644 +0.0330951 +0.107511 +0.0710708 +0.0392274 +0.0742816 +0.0839207 +0.0467648 +0.0345335 +0.0146616 +0.109282 +0.187879 +0.12107 +0.0583698 +0.0664069 +0.0518948 +0.0381672 +0.0498546 +0.0705965 +0.0563436 +0.0890585 +0.106257 +0.10538 +0.140839 +0.0393886 +0.0322632 +0.0641569 +0.0545492 +0.0362664 +0.0250806 +0.0621673 +0.0157825 +0.0509126 +0.0748957 +0.0965591 +0.0749209 +0.13329 +0.132579 +0.0985737 +0.0772244 +0.0360209 +0.0445485 +0.0692248 +0.0694683 +0.0347725 +0.0302222 +0.10371 +0.158209 +0.098906 +0.0514085 +0.0313609 +0.0843055 +0.0608005 +0.063549 +0.263942 +0.17665 +0.261261 +0.0441281 +0.0468925 +0.0647964 +0.044089 +0.0629465 +0.071587 +0.029169 +0.0401875 +0.026996 +0.0610415 +0.0458035 +0.035757 +0.0468055 +0.0355955 +0.0530505 +0.204683 +0.116196 +0.0907005 +0.066667 +0.124328 +0.067071 +0.09391 +0.0647426 +0.095498 +0.140501 +0.105467 +0.0573551 +0.0305451 +0.026572 +0.037701 +0.0364285 +0.0338849 +0.0278505 +0.0145665 +0.109527 +0.133339 +0.0582865 +0.0516891 +0.0618162 +0.103562 +0.0196925 +0.0693495 +0.137835 +0.0963195 +0.0614775 +0.0551995 +0.0580145 +0.034866 +0.0435379 +0.0189935 +0.0270825 +0.0893485 +0.0466895 +0.048314 +0.0224355 +0.10331 From de2b36f52fd1d7427fdee540b400b2721d245f36 Mon Sep 17 00:00:00 2001 From: oesteban Date: Wed, 7 Sep 2016 17:44:16 -0700 Subject: [PATCH 2/8] update CHANGES --- CHANGES | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES b/CHANGES index d581872b38..325ee1b26f 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,7 @@ Upcoming release 0.13 ===================== +* ENH: Add a Framewise Displacement calculation interface (https://github.com/nipy/nipype/pull/1604) * FIX: Use builtins open and unicode literals for py3 compatibility (https://github.com/nipy/nipype/pull/1572) * TST: reduce the size of docker images & use tags for images (https://github.com/nipy/nipype/pull/1564) * ENH: Implement missing inputs/outputs in FSL AvScale (https://github.com/nipy/nipype/pull/1563) From 54a6acb94774935e217a099054917d68c5fa817d Mon Sep 17 00:00:00 2001 From: oesteban Date: Wed, 7 Sep 2016 18:29:12 -0700 Subject: [PATCH 3/8] fix plotting with speeds/tr --- nipype/algorithms/misc.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index ae99c88921..241f59de8a 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -47,8 +47,7 @@ class FramewiseDisplacementInputSpec(BaseInterfaceInputSpec): out_figure = File('fd_power_2012.pdf', usedefault=True, desc='output figure name') series_tr = traits.Float(desc='repetition time in sec.') save_plot = traits.Bool(False, usedefault=True, desc='write FD plot') - normalize = traits.Bool(False, usedefault=True, desc='calculate FD in mm/s', - requires=['series_tr']) + normalize = traits.Bool(False, usedefault=True, desc='calculate FD in mm/s') figdpi = traits.Int(100, usedefault=True, desc='output dpi for the FD plot') figsize = traits.Tuple(traits.Float(11.7), traits.Float(2.3), usedefault=True, desc='output figure size') @@ -86,12 +85,18 @@ def _run_interface(self, runtime): } np.savetxt(self.inputs.out_file, fd_res) + if self.inputs.save_plot: - self._results['out_figure'] = op.abspath(self.inputs.out_figure) tr = None - if self.inputs.normalize: + if isdefined(self.inputs.series_tr): tr = self.inputs.series_tr - fig = plot_fd(fd_res, self.inputs.figsize, series_tr=tr) + + if self.inputs.normalize and tr is None: + iflogger.warn('FD plot cannot be normalized if TR is not set') + + self._results['out_figure'] = op.abspath(self.inputs.out_figure) + fig = plot_fd(fd_res, self.inputs.figsize, series_tr=tr, + normalize=self.inputs.normalize) fig.savefig(self._results['out_figure'], dpi=float(self.inputs.figdpi), format=self.inputs.out_figure[-3:], bbox_inches='tight') @@ -1523,7 +1528,7 @@ def merge_rois(in_files, in_idxs, in_ref, return out_file -def plot_fd(fd_values, figsize, series_tr=None): +def plot_fd(fd_values, figsize, series_tr=None, normalize=False): """ A helper function to plot the framewise displacement """ @@ -1540,9 +1545,12 @@ def plot_fd(fd_values, figsize, series_tr=None): grid.update(hspace=1.0, right=0.95, left=0.1, bottom=0.2) ax = fig.add_subplot(grid[0, :-1]) + if normalize and series_tr is not None: + fd_values /= series_tr + ax.plot(fd_values) ax.set_xlim((0, len(fd_values))) - ax.set_ylabel('FD [{}]'.format('mm/s' if series_tr is not None else 'mm')) + ax.set_ylabel('FD {}'.format('speed [mm/s]' if normalize else '[mm]')) xlabel = 'Frame #' if series_tr is not None: From 4c666224cdcb7e2dee6605e192244c1ebd578a70 Mon Sep 17 00:00:00 2001 From: oesteban Date: Wed, 7 Sep 2016 22:47:35 -0700 Subject: [PATCH 4/8] add duecredit reference --- nipype/algorithms/misc.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index 241f59de8a..561cca6a4c 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -29,6 +29,7 @@ import warnings from .. import logging +from ..external.due import due, Doi, BibTeX from . import metrics as nam from ..interfaces.base import (BaseInterface, traits, TraitedSpec, File, InputMultiPath, OutputMultiPath, @@ -73,6 +74,25 @@ class FramewiseDisplacement(BaseInterface): input_spec = FramewiseDisplacementInputSpec output_spec = FramewiseDisplacementOutputSpec + references_ = [{ + 'entry': BibTex("""\ +@article{power_spurious_2012, + title = {Spurious but systematic correlations in functional connectivity {MRI} networks \ +arise from subject motion}, + volume = {59}, + doi = {10.1016/j.neuroimage.2011.10.018}, + number = {3}, + urldate = {2016-08-16}, + journal = {NeuroImage}, + author = {Power, Jonathan D. and Barnes, Kelly A. and Snyder, Abraham Z. and Schlaggar, \ +Bradley L. and Petersen, Steven E.}, + year = {2012}, + pages = {2142--2154}, +} +"""), + 'tags': ['method'] + }] + def _run_interface(self, runtime): mpars = np.loadtxt(self.inputs.in_plots) # mpars is N_t x 6 diff = mpars[:-1, :] - mpars[1:, :] @@ -1225,6 +1245,10 @@ class SplitROIs(BaseInterface): """ Splits a 3D image in small chunks to enable parallel processing. ROIs keep time series structure in 4D images. + + Example + ------- + >>> from nipype.algorithms import misc >>> rois = misc.SplitROIs() >>> rois.inputs.in_file = 'diffusion.nii' From 98b522ca355a294a78befe903d25222cf5f0d5c6 Mon Sep 17 00:00:00 2001 From: oesteban Date: Thu, 8 Sep 2016 00:12:35 -0700 Subject: [PATCH 5/8] fix error, add auto spec --- nipype/algorithms/misc.py | 2 +- .../tests/test_auto_FramewiseDisplacement.py | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 nipype/algorithms/tests/test_auto_FramewiseDisplacement.py diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index 561cca6a4c..cad41c7db6 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -75,7 +75,7 @@ class FramewiseDisplacement(BaseInterface): output_spec = FramewiseDisplacementOutputSpec references_ = [{ - 'entry': BibTex("""\ + 'entry': BibTeX("""\ @article{power_spurious_2012, title = {Spurious but systematic correlations in functional connectivity {MRI} networks \ arise from subject motion}, diff --git a/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py b/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py new file mode 100644 index 0000000000..d16f46bf5e --- /dev/null +++ b/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py @@ -0,0 +1,43 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from ...testing import assert_equal +from ..misc import FramewiseDisplacement + + +def test_FramewiseDisplacement_inputs(): + input_map = dict(figdpi=dict(usedefault=True, + ), + figsize=dict(usedefault=True, + ), + ignore_exception=dict(nohash=True, + usedefault=True, + ), + in_plots=dict(), + normalize=dict(usedefault=True, + ), + out_figure=dict(usedefault=True, + ), + out_file=dict(usedefault=True, + ), + radius=dict(usedefault=True, + ), + save_plot=dict(usedefault=True, + ), + series_tr=dict(), + ) + inputs = FramewiseDisplacement.input_spec() + + for key, metadata in list(input_map.items()): + for metakey, value in list(metadata.items()): + yield assert_equal, getattr(inputs.traits()[key], metakey), value + + +def test_FramewiseDisplacement_outputs(): + output_map = dict(fd_average=dict(), + out_figure=dict(), + out_file=dict(), + ) + outputs = FramewiseDisplacement.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + yield assert_equal, getattr(outputs.traits()[key], metakey), value From 938e252b491cddd64a3cce64bbdd5e424804b93e Mon Sep 17 00:00:00 2001 From: oesteban Date: Thu, 8 Sep 2016 09:57:41 -0700 Subject: [PATCH 6/8] mv FD computation to new confounds module --- nipype/algorithms/confounds.py | 154 +++++++++++++++++++++++++++++++++ nipype/algorithms/misc.py | 125 -------------------------- 2 files changed, 154 insertions(+), 125 deletions(-) create mode 100644 nipype/algorithms/confounds.py diff --git a/nipype/algorithms/confounds.py b/nipype/algorithms/confounds.py new file mode 100644 index 0000000000..76f5d4aa71 --- /dev/null +++ b/nipype/algorithms/confounds.py @@ -0,0 +1,154 @@ +# -*- 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: +''' +Algorithms to compute confounds in :abbr:`fMRI (functional MRI)` + + Change directory to provide relative paths for doctests + >>> import os + >>> filepath = os.path.dirname(os.path.realpath(__file__)) + >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data')) + >>> os.chdir(datadir) + +''' +from __future__ import print_function, division, unicode_literals, absolute_import +from builtins import str, zip, range, open + +import os.path as op +import numpy as np + +from .. import logging +from ..external.due import due, BibTeX +from ..interfaces.base import (traits, TraitedSpec, BaseInterface, + BaseInterfaceInputSpec, File, isdefined) +IFLOG = logging.getLogger('interface') + + +class FramewiseDisplacementInputSpec(BaseInterfaceInputSpec): + in_plots = File(exists=True, desc='motion parameters as written by FSL MCFLIRT') + radius = traits.Float(50, usedefault=True, + desc='radius in mm to calculate angular FDs, 50mm is the ' + 'default since it is used in Power et al. 2012') + out_file = File('fd_power_2012.txt', usedefault=True, desc='output file name') + out_figure = File('fd_power_2012.pdf', usedefault=True, desc='output figure name') + series_tr = traits.Float(desc='repetition time in sec.') + save_plot = traits.Bool(False, usedefault=True, desc='write FD plot') + normalize = traits.Bool(False, usedefault=True, desc='calculate FD in mm/s') + figdpi = traits.Int(100, usedefault=True, desc='output dpi for the FD plot') + figsize = traits.Tuple(traits.Float(11.7), traits.Float(2.3), usedefault=True, + desc='output figure size') + +class FramewiseDisplacementOutputSpec(TraitedSpec): + out_file = File(desc='calculated FD per timestep') + out_figure = File(desc='output image file') + fd_average = traits.Float(desc='average FD') + +class FramewiseDisplacement(BaseInterface): + """ + Calculate the :abbr:`FD (framewise displacement)` as in [Power2012]_. + This implementation reproduces the calculation in fsl_motion_outliers + + .. [Power2012] Power et al., Spurious but systematic correlations in functional + connectivity MRI networks arise from subject motion, NeuroImage 59(3), + 2012. doi:`10.1016/j.neuroimage.2011.10.018 + `_. + + + """ + + input_spec = FramewiseDisplacementInputSpec + output_spec = FramewiseDisplacementOutputSpec + + references_ = [{ + 'entry': BibTeX("""\ +@article{power_spurious_2012, + title = {Spurious but systematic correlations in functional connectivity {MRI} networks \ +arise from subject motion}, + volume = {59}, + doi = {10.1016/j.neuroimage.2011.10.018}, + number = {3}, + urldate = {2016-08-16}, + journal = {NeuroImage}, + author = {Power, Jonathan D. and Barnes, Kelly A. and Snyder, Abraham Z. and Schlaggar, \ +Bradley L. and Petersen, Steven E.}, + year = {2012}, + pages = {2142--2154}, +} +"""), + 'tags': ['method'] + }] + + def _run_interface(self, runtime): + mpars = np.loadtxt(self.inputs.in_plots) # mpars is N_t x 6 + diff = mpars[:-1, :] - mpars[1:, :] + diff[:, :3] *= self.inputs.radius + fd_res = np.abs(diff).sum(axis=1) + + self._results = { + 'out_file': op.abspath(self.inputs.out_file), + 'fd_average': float(fd_res.mean()) + } + np.savetxt(self.inputs.out_file, fd_res) + + + if self.inputs.save_plot: + tr = None + if isdefined(self.inputs.series_tr): + tr = self.inputs.series_tr + + if self.inputs.normalize and tr is None: + IFLOG.warn('FD plot cannot be normalized if TR is not set') + + self._results['out_figure'] = op.abspath(self.inputs.out_figure) + fig = plot_confound(fd_res, self.inputs.figsize, 'FD', units='mm', + series_tr=tr, normalize=self.inputs.normalize) + fig.savefig(self._results['out_figure'], dpi=float(self.inputs.figdpi), + format=self.inputs.out_figure[-3:], + bbox_inches='tight') + fig.clf() + return runtime + + def _list_outputs(self): + return self._results + + +def plot_confound(tseries, figsize, name, units=None, + series_tr=None, normalize=False): + """ + A helper function to plot :abbr:`fMRI (functional MRI)` confounds. + """ + import matplotlib + matplotlib.use('Agg') + import matplotlib.pyplot as plt + from matplotlib.gridspec import GridSpec + from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas + import seaborn as sns + + fig = plt.Figure(figsize=figsize) + FigureCanvas(fig) + grid = GridSpec(1, 2, width_ratios=[3, 1], wspace=0.025) + grid.update(hspace=1.0, right=0.95, left=0.1, bottom=0.2) + + ax = fig.add_subplot(grid[0, :-1]) + if normalize and series_tr is not None: + tseries /= series_tr + + ax.plot(tseries) + ax.set_xlim((0, len(tseries))) + ylabel = name + if units is not None: + ylabel += (' speed [{}/s]' if normalize else ' [{}]').format(units) + ax.set_ylabel(ylabel) + + xlabel = 'Frame #' + if series_tr is not None: + xlabel = 'Frame # ({} sec TR)'.format(series_tr) + ax.set_xlabel(xlabel) + ylim = ax.get_ylim() + + ax = fig.add_subplot(grid[0, -1]) + sns.distplot(tseries, vertical=True, ax=ax) + ax.set_xlabel('Frames') + ax.set_ylim(ylim) + ax.set_yticklabels([]) + return fig diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index cad41c7db6..02be67f926 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -29,7 +29,6 @@ import warnings from .. import logging -from ..external.due import due, Doi, BibTeX from . import metrics as nam from ..interfaces.base import (BaseInterface, traits, TraitedSpec, File, InputMultiPath, OutputMultiPath, @@ -39,93 +38,6 @@ iflogger = logging.getLogger('interface') -class FramewiseDisplacementInputSpec(BaseInterfaceInputSpec): - in_plots = File(exists=True, desc='motion parameters as written by FSL MCFLIRT') - radius = traits.Float(50, usedefault=True, - desc='radius in mm to calculate angular FDs, 50mm is the ' - 'default since it is used in Power et al. 2012') - out_file = File('fd_power_2012.txt', usedefault=True, desc='output file name') - out_figure = File('fd_power_2012.pdf', usedefault=True, desc='output figure name') - series_tr = traits.Float(desc='repetition time in sec.') - save_plot = traits.Bool(False, usedefault=True, desc='write FD plot') - normalize = traits.Bool(False, usedefault=True, desc='calculate FD in mm/s') - figdpi = traits.Int(100, usedefault=True, desc='output dpi for the FD plot') - figsize = traits.Tuple(traits.Float(11.7), traits.Float(2.3), usedefault=True, - desc='output figure size') - -class FramewiseDisplacementOutputSpec(TraitedSpec): - out_file = File(desc='calculated FD per timestep') - out_figure = File(desc='output image file') - fd_average = traits.Float(desc='average FD') - -class FramewiseDisplacement(BaseInterface): - """ - Calculate the :abbr:`FD (framewise displacement)` as in [Power2012]_. - This implementation reproduces the calculation in fsl_motion_outliers - - .. [Power2012] Power et al., Spurious but systematic correlations in functional - connectivity MRI networks arise from subject motion, NeuroImage 59(3), - 2012. doi:`10.1016/j.neuroimage.2011.10.018 - `_. - - - """ - - input_spec = FramewiseDisplacementInputSpec - output_spec = FramewiseDisplacementOutputSpec - - references_ = [{ - 'entry': BibTeX("""\ -@article{power_spurious_2012, - title = {Spurious but systematic correlations in functional connectivity {MRI} networks \ -arise from subject motion}, - volume = {59}, - doi = {10.1016/j.neuroimage.2011.10.018}, - number = {3}, - urldate = {2016-08-16}, - journal = {NeuroImage}, - author = {Power, Jonathan D. and Barnes, Kelly A. and Snyder, Abraham Z. and Schlaggar, \ -Bradley L. and Petersen, Steven E.}, - year = {2012}, - pages = {2142--2154}, -} -"""), - 'tags': ['method'] - }] - - def _run_interface(self, runtime): - mpars = np.loadtxt(self.inputs.in_plots) # mpars is N_t x 6 - diff = mpars[:-1, :] - mpars[1:, :] - diff[:, :3] *= self.inputs.radius - fd_res = np.abs(diff).sum(axis=1) - - self._results = { - 'out_file': op.abspath(self.inputs.out_file), - 'fd_average': float(fd_res.mean()) - } - np.savetxt(self.inputs.out_file, fd_res) - - - if self.inputs.save_plot: - tr = None - if isdefined(self.inputs.series_tr): - tr = self.inputs.series_tr - - if self.inputs.normalize and tr is None: - iflogger.warn('FD plot cannot be normalized if TR is not set') - - self._results['out_figure'] = op.abspath(self.inputs.out_figure) - fig = plot_fd(fd_res, self.inputs.figsize, series_tr=tr, - normalize=self.inputs.normalize) - fig.savefig(self._results['out_figure'], dpi=float(self.inputs.figdpi), - format=self.inputs.out_figure[-3:], - bbox_inches='tight') - return runtime - - def _list_outputs(self): - return self._results - - class PickAtlasInputSpec(BaseInterfaceInputSpec): atlas = File(exists=True, desc="Location of the atlas that will be used.", mandatory=True) @@ -1552,43 +1464,6 @@ def merge_rois(in_files, in_idxs, in_ref, return out_file -def plot_fd(fd_values, figsize, series_tr=None, normalize=False): - """ - A helper function to plot the framewise displacement - """ - import matplotlib - matplotlib.use('Agg') - import matplotlib.pyplot as plt - from matplotlib.gridspec import GridSpec - from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas - import seaborn as sns - - fig = plt.Figure(figsize=figsize) - FigureCanvas(fig) - grid = GridSpec(1, 2, width_ratios=[3, 1], wspace=0.025) - grid.update(hspace=1.0, right=0.95, left=0.1, bottom=0.2) - - ax = fig.add_subplot(grid[0, :-1]) - if normalize and series_tr is not None: - fd_values /= series_tr - - ax.plot(fd_values) - ax.set_xlim((0, len(fd_values))) - ax.set_ylabel('FD {}'.format('speed [mm/s]' if normalize else '[mm]')) - - xlabel = 'Frame #' - if series_tr is not None: - xlabel = 'Frame # ({} sec TR)'.format(series_tr) - ax.set_xlabel(xlabel) - ylim = ax.get_ylim() - - ax = fig.add_subplot(grid[0, -1]) - sns.distplot(fd_values, vertical=True, ax=ax) - ax.set_xlabel('Frames') - ax.set_ylim(ylim) - ax.set_yticklabels([]) - return fig - # Deprecated interfaces ------------------------------------------------------ class Distance(nam.Distance): From 5f2cdb5536366ed046c13e569acdef592fc1c762 Mon Sep 17 00:00:00 2001 From: oesteban Date: Thu, 8 Sep 2016 09:58:00 -0700 Subject: [PATCH 7/8] update specs --- nipype/algorithms/tests/test_auto_FramewiseDisplacement.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py b/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py index d16f46bf5e..ce12c6f4fa 100644 --- a/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py +++ b/nipype/algorithms/tests/test_auto_FramewiseDisplacement.py @@ -1,6 +1,6 @@ # AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT from ...testing import assert_equal -from ..misc import FramewiseDisplacement +from ..confounds import FramewiseDisplacement def test_FramewiseDisplacement_inputs(): From ee3676cff3e8f094aeb4ddf9d5392430420b90dd Mon Sep 17 00:00:00 2001 From: oesteban Date: Thu, 8 Sep 2016 10:26:22 -0700 Subject: [PATCH 8/8] update regression test --- nipype/algorithms/tests/{test_fd.py => test_confounds.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename nipype/algorithms/tests/{test_fd.py => test_confounds.py} (91%) diff --git a/nipype/algorithms/tests/test_fd.py b/nipype/algorithms/tests/test_confounds.py similarity index 91% rename from nipype/algorithms/tests/test_fd.py rename to nipype/algorithms/tests/test_confounds.py index 2fa6e29624..1a785591fd 100644 --- a/nipype/algorithms/tests/test_fd.py +++ b/nipype/algorithms/tests/test_confounds.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- from nipype.testing import (assert_equal, example_data) -from nipype.algorithms.misc import FramewiseDisplacement +from nipype.algorithms.confounds import FramewiseDisplacement import numpy as np from tempfile import mkdtemp from shutil import rmtree