Skip to content

Commit

Permalink
Let metric define the number of quantiles needed
Browse files Browse the repository at this point in the history
Some metrics have a specific number of quantiles needed (e.g.  -m
spreadskill). The driver will now check that the right number of
quantiles are specified.
  • Loading branch information
tnipen committed Dec 22, 2024
1 parent 8e5e817 commit 2cbc83f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions verif/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ def run(argv):
if quantiles is None:
quantiles = data.quantiles
verif.util.warning("Missing '-q <quantiles>'. Automatically setting quantiles: %s" % (','.join(["%g" % q for q in quantiles])))

# Check that we have the right number of quantiles
if m is not None:
if m.min_num_thresholds is not None:
if len(quantiles) < m.min_num_thresholds:
verif.util.error(f"Need to specify at least {m.min_num_thresholds} quantiles (-q)")
if m.max_num_thresholds is not None:
if len(quantiles) > m.max_num_thresholds:
verif.util.error(f"Need to specify at most {m.max_num_thresholds} quantiles (-q)")

# TODO: This is a bit of a hack, using thresholds to hold the
# quantiles. But otherwise, the classes in output need to deal with
# testing if the metric needs thresholds or quantiles
Expand Down
8 changes: 8 additions & 0 deletions verif/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Metric(object):
aggregator = verif.aggregator.Mean()
supports_aggregator = False
type = verif.metric_type.Deterministic()
min_num_thresholds = None
max_num_thresholds = None

def compute(self, data, input_index, axis, interval):
""" Compute the score along an axis
Expand Down Expand Up @@ -1216,6 +1218,8 @@ class QuantileCoverage(Metric):
# default_bin_type = "within"
require_threshold_type = "quantile"
supports_threshold = True
min_num_thresholds = 1
max_num_thresholds = 2

def compute_single(self, data, input_index, axis, axis_index, interval):
var0 = verif.field.Quantile(interval.lower)
Expand Down Expand Up @@ -1336,6 +1340,8 @@ class Spread(Metric):
supports_threshold = True
perfect_score = 0
orientation = -1
min_num_thresholds = 2
max_num_thresholds = 2

def compute_single(self, data, input_index, axis, axis_index, interval):
var0 = verif.field.Quantile(interval.lower)
Expand All @@ -1357,6 +1363,8 @@ class SpreadSkillRatio(Metric):
supports_threshold = True
perfect_score = 1
orientation = 0
min_num_thresholds = 2
max_num_thresholds = 2

def compute_single(self, data, input_index, axis, axis_index, interval):
var0 = verif.field.Quantile(interval.lower)
Expand Down

0 comments on commit 2cbc83f

Please sign in to comment.