Skip to content

Commit 0dc1c63

Browse files
committed
prettify model adapter creation error message
1 parent 6a99519 commit 0dc1c63

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

bioimageio/core/model_adapters/_model_adapter.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,17 @@ def create(
6060
)
6161

6262
weights = model_description.weights
63-
errors: List[str] = []
63+
errors: List[Tuple[str, str]] = []
6464
weight_format_priority_order = (
6565
DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER
6666
if weight_format_priority_order is None
6767
else weight_format_priority_order
6868
)
69+
# limit weight formats to the ones present
70+
weight_format_priority_order = [
71+
w for w in weight_format_priority_order if getattr(weights, w) is not None
72+
]
73+
6974
for wf in weight_format_priority_order:
7075
if wf == "pytorch_state_dict" and weights.pytorch_state_dict is not None:
7176
try:
@@ -77,7 +82,7 @@ def create(
7782
devices=devices,
7883
)
7984
except Exception as e:
80-
errors.append(f"{wf}: {e}")
85+
errors.append((wf, str(e)))
8186
elif (
8287
wf == "tensorflow_saved_model_bundle"
8388
and weights.tensorflow_saved_model_bundle is not None
@@ -89,7 +94,7 @@ def create(
8994
model_description=model_description, devices=devices
9095
)
9196
except Exception as e:
92-
errors.append(f"{wf}: {e}")
97+
errors.append((wf, str(e)))
9398
elif wf == "onnx" and weights.onnx is not None:
9499
try:
95100
from ._onnx_model_adapter import ONNXModelAdapter
@@ -98,7 +103,7 @@ def create(
98103
model_description=model_description, devices=devices
99104
)
100105
except Exception as e:
101-
errors.append(f"{wf}: {e}")
106+
errors.append((wf, str(e)))
102107
elif wf == "torchscript" and weights.torchscript is not None:
103108
try:
104109
from ._torchscript_model_adapter import TorchscriptModelAdapter
@@ -107,7 +112,7 @@ def create(
107112
model_description=model_description, devices=devices
108113
)
109114
except Exception as e:
110-
errors.append(f"{wf}: {e}")
115+
errors.append((wf, str(e)))
111116
elif wf == "keras_hdf5" and weights.keras_hdf5 is not None:
112117
# keras can either be installed as a separate package or used as part of tensorflow
113118
# we try to first import the keras model adapter using the separate package and,
@@ -125,15 +130,24 @@ def create(
125130
model_description=model_description, devices=devices
126131
)
127132
except Exception as e:
128-
errors.append(f"{wf}: {e}")
133+
errors.append((wf, str(e)))
129134

130135
assert errors
131-
error_list = "\n - ".join(errors)
132-
raise ValueError(
133-
"None of the weight format specific model adapters could be created for"
134-
+ f" '{model_description.id or model_description.name}'"
135-
+ f" in this environment. Errors are:\n\n{error_list}.\n\n"
136-
)
136+
if len(weight_format_priority_order) == 1:
137+
assert len(errors) == 1
138+
raise ValueError(
139+
f"The '{weight_format_priority_order[0]}' model adapter could not be created for"
140+
+ f" '{model_description.id or model_description.name}'"
141+
+ f" in this environment:\n{errors[0][1]}.\n\n"
142+
)
143+
144+
else:
145+
error_list = "\n - ".join(f"{wf}: {e}" for wf, e in errors)
146+
raise ValueError(
147+
"None of the weight format specific model adapters could be created for"
148+
+ f" '{model_description.id or model_description.name}'"
149+
+ f" in this environment. Errors are:\n\n{error_list}.\n\n"
150+
)
137151

138152
@final
139153
def load(self, *, devices: Optional[Sequence[str]] = None) -> None:

0 commit comments

Comments
 (0)