Skip to content

Commit

Permalink
Merge pull request #406 from DHI/metric-fun
Browse files Browse the repository at this point in the history
Accept custom metric funs without registering them
  • Loading branch information
jsmariegaard authored Feb 1, 2024
2 parents 0b77cb1 + 1637b6b commit 8843b7e
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 165 deletions.
19 changes: 15 additions & 4 deletions modelskill/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
0.39614855570839064
"""
from __future__ import annotations
import inspect

import sys
import warnings
Expand Down Expand Up @@ -1200,11 +1201,21 @@ def _parse_metric(
elif isinstance(metric, Iterable):
metrics = list(metric)

for metric in metrics:
if not isinstance(metric, str) and not callable(metric):
raise TypeError(f"metric {metric} must be a string or callable")
parsed_metrics = []

for m in metrics:
if isinstance(m, str):
parsed_metrics.append(get_metric(m))
elif callable(m):
if len(inspect.signature(m).parameters) < 2:
raise ValueError(
"Metrics must have at least two arguments (obs, model)"
)
parsed_metrics.append(m)
else:
raise TypeError(f"metric {m} must be a string or callable")

return [get_metric(m) for m in metrics]
return parsed_metrics


# TODO add non-metric functions to __all__
Expand Down
Loading

0 comments on commit 8843b7e

Please sign in to comment.