|
12 | 12 |
|
13 | 13 | from numpy.linalg.linalg import LinAlgError
|
14 | 14 | from nipype.algorithms import confounds as nac
|
15 |
| -from nipype.interfaces.base import File |
16 |
| -from nipype.interfaces.mixins import reporting |
17 | 15 |
|
18 | 16 |
|
19 |
| -class RetryCompCorInputSpecMixin(reporting.ReportCapableInputSpec): |
20 |
| - out_report = File('report.html', usedefault=True, hash_files=False, |
21 |
| - desc='filename for warning HTML snippet') |
| 17 | +class RobustACompCor(nac.ACompCor): |
| 18 | + """ |
| 19 | + Runs aCompCor several times if it suddenly fails with |
| 20 | + https://github.com/poldracklab/fmriprep/issues/776 |
22 | 21 |
|
| 22 | + """ |
23 | 23 |
|
24 |
| -class RetryCompCorMixin(reporting.ReportCapableInterface): |
25 | 24 | def _run_interface(self, runtime):
|
26 |
| - warn = self.inputs.failure_mode == 'NaN' |
27 |
| - |
28 | 25 | failures = 0
|
29 |
| - save_exc = None |
30 | 26 | while True:
|
31 |
| - success = True |
32 |
| - # Identifiy success/failure in both error and NaN mode |
33 | 27 | try:
|
34 |
| - runtime = super()._run_interface(runtime) |
35 |
| - if warn and self._is_allnans(): |
36 |
| - success = False |
37 |
| - except LinAlgError as exc: |
38 |
| - success = False |
39 |
| - save_exc = exc |
40 |
| - |
41 |
| - if success: |
| 28 | + runtime = super(RobustACompCor, self)._run_interface(runtime) |
42 | 29 | break
|
43 |
| - |
44 |
| - failures += 1 |
45 |
| - if failures > 10: |
46 |
| - if warn: |
47 |
| - break |
48 |
| - raise save_exc |
49 |
| - start = (failures - 1) * 10 |
50 |
| - sleep(randint(start + 4, start + 10)) |
| 30 | + except LinAlgError: |
| 31 | + failures += 1 |
| 32 | + if failures > 10: |
| 33 | + raise |
| 34 | + start = (failures - 1) * 10 |
| 35 | + sleep(randint(start + 4, start + 10)) |
51 | 36 |
|
52 | 37 | return runtime
|
53 | 38 |
|
54 |
| - def _is_allnans(self): |
55 |
| - import numpy as np |
56 |
| - outputs = self._list_outputs() |
57 |
| - components = np.loadtxt(outputs['components_file'], skiprows=1) |
58 |
| - return np.isnan(components).all() |
59 |
| - |
60 |
| - def _generate_report(self): |
61 |
| - snippet = '<!-- {} completed without error -->'.format(self._header) |
62 |
| - if self._is_allnans(): |
63 |
| - snippet = '''\ |
64 |
| -<p class="elem-desc"> |
65 |
| - Warning: {} components could not be estimated, due to a linear algebra error. |
66 |
| - While not definitive, this may be an indication of a poor mask. |
67 |
| - Please inspect the {} contours above to ensure that they are located |
68 |
| - in the white matter/CSF. |
69 |
| -</p> |
70 |
| -'''.format(self._header, 'magenta' if self._header[0] == 'a' else 'blue') |
71 |
| - |
72 |
| - with open(self._out_report, 'w') as fobj: |
73 |
| - fobj.write(snippet) |
74 |
| - |
75 |
| - |
76 |
| -class RobustACompCorInputSpec(RetryCompCorInputSpecMixin, nac.CompCorInputSpec): |
77 |
| - pass |
78 |
| - |
79 |
| - |
80 |
| -class RobustACompCorOutputSpec(reporting.ReportCapableOutputSpec, nac.CompCorOutputSpec): |
81 |
| - pass |
82 |
| - |
83 |
| - |
84 |
| -class RobustACompCor(RetryCompCorMixin, nac.ACompCor): |
85 |
| - """ |
86 |
| - Runs aCompCor several times if it suddenly fails with |
87 |
| - https://github.com/poldracklab/fmriprep/issues/776 |
88 |
| -
|
89 |
| - Warns by default, rather than failing, on linear algebra errors. |
90 |
| - https://github.com/poldracklab/fmriprep/issues/1433 |
91 | 39 |
|
| 40 | +class RobustTCompCor(nac.TCompCor): |
92 | 41 | """
|
93 |
| - input_spec = RobustACompCorInputSpec |
94 |
| - output_spec = RobustACompCorOutputSpec |
95 |
| - |
96 |
| - |
97 |
| -class RobustTCompCorInputSpec(RetryCompCorInputSpecMixin, nac.TCompCorInputSpec): |
98 |
| - pass |
99 |
| - |
100 |
| - |
101 |
| -class RobustTCompCorOutputSpec(reporting.ReportCapableOutputSpec, nac.TCompCorOutputSpec): |
102 |
| - pass |
103 |
| - |
| 42 | + Runs tCompCor several times if it suddenly fails with |
| 43 | + https://github.com/poldracklab/fmriprep/issues/940 |
104 | 44 |
|
105 |
| -class RobustTCompCor(RetryCompCorMixin, nac.TCompCor): |
106 | 45 | """
|
107 |
| - Runs tCompCor several times if it suddenly fails with |
108 |
| - https://github.com/poldracklab/fmriprep/issues/776 |
109 | 46 |
|
110 |
| - Warns by default, rather than failing, on linear algebra errors. |
111 |
| - https://github.com/poldracklab/fmriprep/issues/1433 |
| 47 | + def _run_interface(self, runtime): |
| 48 | + failures = 0 |
| 49 | + while True: |
| 50 | + try: |
| 51 | + runtime = super(RobustTCompCor, self)._run_interface(runtime) |
| 52 | + break |
| 53 | + except LinAlgError: |
| 54 | + failures += 1 |
| 55 | + if failures > 10: |
| 56 | + raise |
| 57 | + start = (failures - 1) * 10 |
| 58 | + sleep(randint(start + 4, start + 10)) |
112 | 59 |
|
113 |
| - """ |
114 |
| - input_spec = RobustTCompCorInputSpec |
115 |
| - output_spec = RobustTCompCorOutputSpec |
| 60 | + return runtime |
0 commit comments