|
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 |
15 | 17 |
|
16 | 18 |
|
17 |
| -class RobustACompCor(nac.ACompCor): |
18 |
| - """ |
19 |
| - Runs aCompCor several times if it suddenly fails with |
20 |
| - https://github.com/poldracklab/fmriprep/issues/776 |
| 19 | +class RetryCompCorInputSpecMixin(reporting.ReportCapableInputSpec): |
| 20 | + out_report = File('report.html', usedefault=True, hash_files=False, |
| 21 | + desc='filename for warning HTML snippet') |
21 | 22 |
|
22 |
| - """ |
23 | 23 |
|
| 24 | +class RetryCompCorMixin(reporting.ReportCapableInterface): |
24 | 25 | def _run_interface(self, runtime):
|
| 26 | + warn = self.inputs.failure_mode == 'NaN' |
| 27 | + |
25 | 28 | failures = 0
|
| 29 | + save_exc = None |
26 | 30 | while True:
|
| 31 | + success = True |
| 32 | + # Identifiy success/failure in both error and NaN mode |
27 | 33 | try:
|
28 |
| - runtime = super(RobustACompCor, self)._run_interface(runtime) |
| 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: |
29 | 42 | break
|
30 |
| - except LinAlgError: |
31 |
| - failures += 1 |
32 |
| - if failures > 10: |
33 |
| - raise |
34 |
| - start = (failures - 1) * 10 |
35 |
| - sleep(randint(start + 4, start + 10)) |
| 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)) |
36 | 51 |
|
37 | 52 | return runtime
|
38 | 53 |
|
| 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) |
39 | 74 |
|
40 |
| -class RobustTCompCor(nac.TCompCor): |
| 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): |
41 | 85 | """
|
42 |
| - Runs tCompCor several times if it suddenly fails with |
43 |
| - https://github.com/poldracklab/fmriprep/issues/940 |
| 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 |
44 | 91 |
|
45 | 92 | """
|
| 93 | + input_spec = RobustACompCorInputSpec |
| 94 | + output_spec = RobustACompCorOutputSpec |
46 | 95 |
|
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)) |
59 | 96 |
|
60 |
| - return runtime |
| 97 | +class RobustTCompCorInputSpec(RetryCompCorInputSpecMixin, nac.TCompCorInputSpec): |
| 98 | + pass |
| 99 | + |
| 100 | + |
| 101 | +class RobustTCompCorOutputSpec(reporting.ReportCapableOutputSpec, nac.TCompCorOutputSpec): |
| 102 | + pass |
| 103 | + |
| 104 | + |
| 105 | +class RobustTCompCor(RetryCompCorMixin, nac.TCompCor): |
| 106 | + """ |
| 107 | + Runs tCompCor several times if it suddenly fails with |
| 108 | + https://github.com/poldracklab/fmriprep/issues/776 |
| 109 | +
|
| 110 | + Warns by default, rather than failing, on linear algebra errors. |
| 111 | + https://github.com/poldracklab/fmriprep/issues/1433 |
| 112 | +
|
| 113 | + """ |
| 114 | + input_spec = RobustTCompCorInputSpec |
| 115 | + output_spec = RobustTCompCorOutputSpec |
0 commit comments