Skip to content

Commit fa87a92

Browse files
committed
🥅 More specific error handling
1 parent 88de7d6 commit fa87a92

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

CPAC/registration/exceptions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (C) 2022 C-PAC Developers
2+
3+
# This file is part of C-PAC.
4+
5+
# C-PAC is free software: you can redistribute it and/or modify it under
6+
# the terms of the GNU Lesser General Public License as published by the
7+
# Free Software Foundation, either version 3 of the License, or (at your
8+
# option) any later version.
9+
10+
# C-PAC is distributed in the hope that it will be useful, but WITHOUT
11+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
# License for more details.
14+
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
17+
"""Custom registration exceptions"""
18+
class BadRegistrationError(ValueError):
19+
"""Exception for when a QC measure for a registration falls below a
20+
specified threshold"""
21+
def __init__(self, *args, metric=None, value=None, threshold=None,
22+
**kwargs):
23+
"""
24+
Parameters
25+
----------
26+
metric : str
27+
QC metric
28+
29+
value : float
30+
calculated QC value
31+
32+
threshold : float
33+
specified threshold
34+
"""
35+
msg = "Registration failed quality control"
36+
if all(arg is not None for arg in (metric, value, threshold)):
37+
msg += f" ({metric}: {value} < {threshold})"
38+
msg += "."
39+
super().__init__(msg, *args, **kwargs)

CPAC/registration/guardrails.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from CPAC.pipeline.nipype_pipeline_engine import Node, Workflow
2525
# from CPAC.pipeline.nipype_pipeline_engine.utils import connect_from_spec
2626
from CPAC.qc import qc_masks, registration_guardrail_thresholds
27+
from CPAC.registration.exceptions import BadRegistrationError
2728
from CPAC.registration.utils import hardcoded_reg
2829
from CPAC.utils.docs import retry_docstring
2930

@@ -32,30 +33,6 @@
3233
Registration: {'reference': 'reference', 'registered': 'out_file'}}
3334

3435

35-
class BadRegistrationError(ValueError):
36-
"""Exception for when a QC measure for a registration falls below a
37-
specified threshold"""
38-
def __init__(self, *args, metric=None, value=None, threshold=None,
39-
**kwargs):
40-
"""
41-
Parameters
42-
----------
43-
metric : str
44-
QC metric
45-
46-
value : float
47-
calculated QC value
48-
49-
threshold : float
50-
specified threshold
51-
"""
52-
msg = "Registration failed quality control"
53-
if all(arg is not None for arg in (metric, value, threshold)):
54-
msg += f" ({metric}: {value} < {threshold})"
55-
msg += "."
56-
super().__init__(msg, *args, **kwargs)
57-
58-
5936
def guardrail_selection(wf: 'Workflow', node1: 'Node', node2: 'Node',
6037
) -> Node:
6138
"""Generate requisite Nodes for choosing a path through the graph

CPAC/registration/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def hardcoded_reg(moving_brain, reference_brain, moving_skull,
6666
reference_skull, ants_para, moving_mask=None,
6767
reference_mask=None, fixed_image_mask=None, interp=None,
6868
reg_with_skull=0):
69+
import subprocess
70+
from CPAC.registration.exceptions import BadRegistrationError
6971
# TODO: expand transforms to cover all in ANTs para
7072

7173
regcmd = ["antsRegistration"]
@@ -444,10 +446,14 @@ def hardcoded_reg(moving_brain, reference_brain, moving_skull,
444446
f.write(' '.join(regcmd))
445447

446448
try:
447-
retcode = subprocess.check_output(regcmd)
449+
subprocess.check_output(regcmd)
450+
except BadRegistrationError as bad_registration:
451+
raise bad_registration
448452
except Exception as e:
449-
raise Exception('[!] ANTS registration did not complete successfully.'
450-
'\n\nError details:\n{0}\n{1}\n'.format(e, e.output))
453+
msg = '[!] ANTS registration did not complete successfully.'
454+
if hasattr(e, 'output'):
455+
msg += '\n\nError details:\n{e}\n{e.output}\n'
456+
raise Exception(msg) # pylint: disable=raise-missing-from
451457

452458
warp_list = []
453459
warped_image = None

0 commit comments

Comments
 (0)