|
39 | 39 | BaseInterfaceInputSpec, isdefined,
|
40 | 40 | DynamicTraitedSpec, Undefined)
|
41 | 41 | from nipype.utils.filemanip import fname_presuffix, split_filename
|
| 42 | + |
| 43 | +# for backwards compatibility |
| 44 | +from nipype.algorithms.tsnr import TSNR |
| 45 | + |
42 | 46 | iflogger = logging.getLogger('interface')
|
43 | 47 |
|
44 | 48 |
|
@@ -259,93 +263,6 @@ def _list_outputs(self):
|
259 | 263 | outputs['nifti_file'] = self._gen_output_file_name()
|
260 | 264 | return outputs
|
261 | 265 |
|
262 |
| - |
263 |
| -class TSNRInputSpec(BaseInterfaceInputSpec): |
264 |
| - in_file = InputMultiPath(File(exists=True), mandatory=True, |
265 |
| - desc='realigned 4D file or a list of 3D files') |
266 |
| - regress_poly = traits.Range(low=1, desc='Remove polynomials') |
267 |
| - tsnr_file = File('tsnr.nii.gz', usedefault=True, hash_files=False, |
268 |
| - desc='output tSNR file') |
269 |
| - mean_file = File('mean.nii.gz', usedefault=True, hash_files=False, |
270 |
| - desc='output mean file') |
271 |
| - stddev_file = File('stdev.nii.gz', usedefault=True, hash_files=False, |
272 |
| - desc='output tSNR file') |
273 |
| - detrended_file = File('detrend.nii.gz', usedefault=True, hash_files=False, |
274 |
| - desc='input file after detrending') |
275 |
| - |
276 |
| - |
277 |
| -class TSNROutputSpec(TraitedSpec): |
278 |
| - tsnr_file = File(exists=True, desc='tsnr image file') |
279 |
| - mean_file = File(exists=True, desc='mean image file') |
280 |
| - stddev_file = File(exists=True, desc='std dev image file') |
281 |
| - detrended_file = File(desc='detrended input file') |
282 |
| - |
283 |
| - |
284 |
| -class TSNR(BaseInterface): |
285 |
| - """Computes the time-course SNR for a time series |
286 |
| -
|
287 |
| - Typically you want to run this on a realigned time-series. |
288 |
| -
|
289 |
| - Example |
290 |
| - ------- |
291 |
| -
|
292 |
| - >>> tsnr = TSNR() |
293 |
| - >>> tsnr.inputs.in_file = 'functional.nii' |
294 |
| - >>> res = tsnr.run() # doctest: +SKIP |
295 |
| -
|
296 |
| - """ |
297 |
| - input_spec = TSNRInputSpec |
298 |
| - output_spec = TSNROutputSpec |
299 |
| - |
300 |
| - def _run_interface(self, runtime): |
301 |
| - img = nb.load(self.inputs.in_file[0]) |
302 |
| - header = img.header.copy() |
303 |
| - vollist = [nb.load(filename) for filename in self.inputs.in_file] |
304 |
| - data = np.concatenate([vol.get_data().reshape( |
305 |
| - vol.get_shape()[:3] + (-1,)) for vol in vollist], axis=3) |
306 |
| - data = np.nan_to_num(data) |
307 |
| - |
308 |
| - if data.dtype.kind == 'i': |
309 |
| - header.set_data_dtype(np.float32) |
310 |
| - data = data.astype(np.float32) |
311 |
| - |
312 |
| - if isdefined(self.inputs.regress_poly): |
313 |
| - timepoints = img.shape[-1] |
314 |
| - X = np.ones((timepoints, 1)) |
315 |
| - for i in range(self.inputs.regress_poly): |
316 |
| - X = np.hstack((X, legendre( |
317 |
| - i + 1)(np.linspace(-1, 1, timepoints))[:, None])) |
318 |
| - betas = np.dot(np.linalg.pinv(X), np.rollaxis(data, 3, 2)) |
319 |
| - datahat = np.rollaxis(np.dot(X[:, 1:], |
320 |
| - np.rollaxis( |
321 |
| - betas[1:, :, :, :], 0, 3)), |
322 |
| - 0, 4) |
323 |
| - data = data - datahat |
324 |
| - img = nb.Nifti1Image(data, img.get_affine(), header) |
325 |
| - nb.save(img, op.abspath(self.inputs.detrended_file)) |
326 |
| - |
327 |
| - meanimg = np.mean(data, axis=3) |
328 |
| - stddevimg = np.std(data, axis=3) |
329 |
| - tsnr = np.zeros_like(meanimg) |
330 |
| - tsnr[stddevimg > 1.e-3] = meanimg[stddevimg > 1.e-3] / stddevimg[stddevimg > 1.e-3] |
331 |
| - img = nb.Nifti1Image(tsnr, img.get_affine(), header) |
332 |
| - nb.save(img, op.abspath(self.inputs.tsnr_file)) |
333 |
| - img = nb.Nifti1Image(meanimg, img.get_affine(), header) |
334 |
| - nb.save(img, op.abspath(self.inputs.mean_file)) |
335 |
| - img = nb.Nifti1Image(stddevimg, img.get_affine(), header) |
336 |
| - nb.save(img, op.abspath(self.inputs.stddev_file)) |
337 |
| - return runtime |
338 |
| - |
339 |
| - def _list_outputs(self): |
340 |
| - outputs = self._outputs().get() |
341 |
| - for k in ['tsnr_file', 'mean_file', 'stddev_file']: |
342 |
| - outputs[k] = op.abspath(getattr(self.inputs, k)) |
343 |
| - |
344 |
| - if isdefined(self.inputs.regress_poly): |
345 |
| - outputs['detrended_file'] = op.abspath(self.inputs.detrended_file) |
346 |
| - return outputs |
347 |
| - |
348 |
| - |
349 | 266 | class GunzipInputSpec(BaseInterfaceInputSpec):
|
350 | 267 | in_file = File(exists=True, mandatory=True)
|
351 | 268 |
|
|
0 commit comments