Skip to content

Commit

Permalink
Wrapping the rest of the metrics in metrics.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewoldag committed Nov 9, 2023
1 parent 68963b3 commit 8f71c38
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 10 deletions.
21 changes: 19 additions & 2 deletions src/qp/metrics/base_metric_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ class BaseMetric(ABC):
metric_input_type = MetricInputType.unknown # The type of input data expected for this metric
metric_output_type = MetricOuputType.unknown # The form of the output data from this metric

Check warning on line 76 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L73-L76

Added lines #L73 - L76 were not covered by tests

def __init__(self, limit:tuple=(0.0, 3.0), dx:float=0.01) -> None:
def __init__(self, limits:tuple=(0.0, 3.0), dx:float=0.01) -> None:

Check warning on line 78 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L78

Added line #L78 was not covered by tests

self._limit = limit
self._limits = limits
self._dx = dx

Check warning on line 81 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L80-L81

Added lines #L80 - L81 were not covered by tests

@classmethod
Expand Down Expand Up @@ -144,7 +144,24 @@ class PointToPointMetric(BaseMetric):

metric_input_type = MetricInputType.point_to_point

Check warning on line 145 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L145

Added line #L145 was not covered by tests

def initialize(self):
raise NotImplementedError()

Check warning on line 148 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L147-L148

Added lines #L147 - L148 were not covered by tests

def evaluate(self, estimate, reference):
raise NotImplementedError()

Check warning on line 151 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L150-L151

Added lines #L150 - L151 were not covered by tests

def finalize(self):
raise NotImplementedError()

Check warning on line 154 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L153-L154

Added lines #L153 - L154 were not covered by tests

class PointToDistMetric(BaseMetric):

Check warning on line 156 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L156

Added line #L156 was not covered by tests

metric_input_type = MetricInputType.point_to_dist

Check warning on line 158 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L158

Added line #L158 was not covered by tests

def initialize(self):
raise NotImplementedError()

Check warning on line 161 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L160-L161

Added lines #L160 - L161 were not covered by tests

def evaluate(self, estimate, reference):
raise NotImplementedError()

Check warning on line 164 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L163-L164

Added lines #L163 - L164 were not covered by tests

def finalize(self):
raise NotImplementedError()

Check warning on line 167 in src/qp/metrics/base_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/base_metric_classes.py#L166-L167

Added lines #L166 - L167 were not covered by tests
218 changes: 210 additions & 8 deletions src/qp/metrics/concrete_metric_classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
from qp.metrics.base_metric_classes import MetricOuputType, DistToDistMetric, SingleEnsembleMetric
from qp.metrics.metrics import calculate_kld, calculate_moment
import numpy as np

Check warning on line 1 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L1

Added line #L1 was not covered by tests

from qp.metrics.base_metric_classes import (

Check warning on line 3 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L3

Added line #L3 was not covered by tests
MetricOuputType,
DistToDistMetric,
DistToPointMetric,
SingleEnsembleMetric,
)
from qp.metrics.metrics import (

Check warning on line 9 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L9

Added line #L9 was not covered by tests
calculate_brier,
calculate_goodness_of_fit,
calculate_kld,
calculate_moment,
calculate_outlier_rate,
calculate_rmse,
calculate_rbpe,
)

class MomentMetric(SingleEnsembleMetric):

Check warning on line 19 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L19

Added line #L19 was not covered by tests
"""Class wrapper around the `calculate_moment` function.
Expand All @@ -8,10 +23,8 @@ class MomentMetric(SingleEnsembleMetric):
metric_name = "moment"

Check warning on line 23 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L23

Added line #L23 was not covered by tests

def __init__(self, moment_order:int=1, limits:tuple=(0.0, 3.0), dx:float=0.01) -> None:
super().__init__()
super().__init__(limits, dx)
self._moment_order = moment_order

Check warning on line 27 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L25-L27

Added lines #L25 - L27 were not covered by tests
self._limits = limits
self._dx = dx

def initialize(self) -> None:
pass

Check warning on line 30 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L29-L30

Added lines #L29 - L30 were not covered by tests
Expand All @@ -31,9 +44,7 @@ class KLDMetric(DistToDistMetric):
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 44 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L43-L44

Added lines #L43 - L44 were not covered by tests

def __init__(self, limits:tuple=(0.0, 3.0), dx:float=0.01) -> None:
super().__init__()
self._limits = limits
self._dx = dx
super().__init__(limits, dx)

Check warning on line 47 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L46-L47

Added lines #L46 - L47 were not covered by tests

def initialize(self) -> None:
pass

Check warning on line 50 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L49-L50

Added lines #L49 - L50 were not covered by tests
Expand All @@ -43,3 +54,194 @@ def evaluate(self, estimate, reference) -> list:

def finalize(self) -> None:
pass

Check warning on line 56 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L55-L56

Added lines #L55 - L56 were not covered by tests


class RMSEMetric(DistToDistMetric):

Check warning on line 59 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L59

Added line #L59 was not covered by tests
"""Class wrapper around the Root Mean Square Error metric
"""

metric_name = "rmse"
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 64 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L63-L64

Added lines #L63 - L64 were not covered by tests

def __init__(self, limits:tuple=(0.0, 3.0), dx:float=0.01) -> None:
super().__init__(limits, dx)

Check warning on line 67 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L66-L67

Added lines #L66 - L67 were not covered by tests

def initialize(self) -> None:
pass

Check warning on line 70 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L69-L70

Added lines #L69 - L70 were not covered by tests

def evaluate(self, estimate, reference) -> list:
return calculate_rmse(estimate, reference, self._limits, self._dx)

Check warning on line 73 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L72-L73

Added lines #L72 - L73 were not covered by tests

def finalize(self) -> None:
pass

Check warning on line 76 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L75-L76

Added lines #L75 - L76 were not covered by tests


class RBPEMetric(SingleEnsembleMetric):

Check warning on line 79 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L79

Added line #L79 was not covered by tests
"""Class wrapper around the Risk Based Point Estimate metric.
"""

metric_name = 'rbpe'
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 84 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L83-L84

Added lines #L83 - L84 were not covered by tests

def __init__(self, limits:tuple=(np.inf, np.inf)) -> None:
super().__init__(limits)

Check warning on line 87 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L86-L87

Added lines #L86 - L87 were not covered by tests

def initialize(self) -> None:
pass

Check warning on line 90 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L89-L90

Added lines #L89 - L90 were not covered by tests

def evaluate(self, estimate) -> list:
return calculate_rbpe(estimate, self._limits)

Check warning on line 93 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L92-L93

Added lines #L92 - L93 were not covered by tests

def finalize(self) -> None:
pass

Check warning on line 96 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L95-L96

Added lines #L95 - L96 were not covered by tests


#! Should this be implemented as `DistToPointMetric` or `DistToDistMetric` ???
class BrierMetric(DistToPointMetric):

Check warning on line 100 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L100

Added line #L100 was not covered by tests
"""Class wrapper around the calculate_brier function. (Which itself is a
wrapper around the `Brier` metric evaluator class).
"""

metric_name = 'brier'
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 106 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L105-L106

Added lines #L105 - L106 were not covered by tests

def __init__(self, limits:tuple=(0.0, 3.0), dx:float=0.01) -> None:
super().__init__(limits, dx)

Check warning on line 109 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L108-L109

Added lines #L108 - L109 were not covered by tests

def initialize(self) -> None:
pass

Check warning on line 112 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L111-L112

Added lines #L111 - L112 were not covered by tests

def evaluate(self, estimate, reference) -> list:
return calculate_brier(estimate, reference, self._limits, self._dx)

Check warning on line 115 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L114-L115

Added lines #L114 - L115 were not covered by tests

def finalize(self) -> None:
pass

Check warning on line 118 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L117-L118

Added lines #L117 - L118 were not covered by tests


class OutlierMetric(SingleEnsembleMetric):

Check warning on line 121 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L121

Added line #L121 was not covered by tests
"""Class wrapper around the outlier calculation metric.
"""

metric_name = 'outlier'
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 126 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L125-L126

Added lines #L125 - L126 were not covered by tests

def __init__(self, cdf_limits:tuple = (0.0001, 0.9999)) -> None:
super().__init__()
self._cdf_limits=cdf_limits

Check warning on line 130 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L128-L130

Added lines #L128 - L130 were not covered by tests

def initialize(self) -> None:
pass

Check warning on line 133 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L132-L133

Added lines #L132 - L133 were not covered by tests

def evaluate(self, estimate) -> list:
return calculate_outlier_rate(estimate, self._cdf_limits[0], self._cdf_limits[1])

Check warning on line 136 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L135-L136

Added lines #L135 - L136 were not covered by tests

def finalize(self) -> None:
pass

Check warning on line 139 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L138-L139

Added lines #L138 - L139 were not covered by tests


class ADMetric(DistToDistMetric):

Check warning on line 142 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L142

Added line #L142 was not covered by tests
"""Class wrapper for Anderson Darling metric.
"""

metric_name = 'ad'
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 147 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L146-L147

Added lines #L146 - L147 were not covered by tests

def __init__(self, num_samples:int=100) -> None:
super().__init__()
self._num_samples = num_samples
self._random_state = None

Check warning on line 152 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L149-L152

Added lines #L149 - L152 were not covered by tests

@property
def _random_state(self):
return self._random_state

Check warning on line 156 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L154-L156

Added lines #L154 - L156 were not covered by tests

@_random_state.setter
def _random_state(self, random_state):
self._random_state=random_state

Check warning on line 160 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L158-L160

Added lines #L158 - L160 were not covered by tests

def initialize(self):
pass

Check warning on line 163 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L162-L163

Added lines #L162 - L163 were not covered by tests

def evaluate(self, estimate, reference) -> list:
return calculate_goodness_of_fit(

Check warning on line 166 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L165-L166

Added lines #L165 - L166 were not covered by tests
estimate,
reference,
fit_metric=self.metric_name,
num_samples=self._num_samples,
_random_state=self._random_state
)

def finalize(self) -> None:
pass

Check warning on line 175 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L174-L175

Added lines #L174 - L175 were not covered by tests


class CvMMetric(DistToDistMetric):

Check warning on line 178 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L178

Added line #L178 was not covered by tests
"""Class wrapper for Cramer von Mises metric.
"""

metric_name = 'cvm'
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 183 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L182-L183

Added lines #L182 - L183 were not covered by tests

def __init__(self, num_samples:int=100) -> None:
super().__init__()
self._num_samples = num_samples
self._random_state = None

Check warning on line 188 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L185-L188

Added lines #L185 - L188 were not covered by tests

@property
def _random_state(self):
return self._random_state

Check warning on line 192 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L190-L192

Added lines #L190 - L192 were not covered by tests

@_random_state.setter
def _random_state(self, random_state):
self._random_state=random_state

Check warning on line 196 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L194-L196

Added lines #L194 - L196 were not covered by tests

def initialize(self):
pass

Check warning on line 199 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L198-L199

Added lines #L198 - L199 were not covered by tests

def evaluate(self, estimate, reference) -> list:
return calculate_goodness_of_fit(

Check warning on line 202 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L201-L202

Added lines #L201 - L202 were not covered by tests
estimate,
reference,
fit_metric=self.metric_name,
num_samples=self._num_samples,
_random_state=self._random_state
)

def finalize(self) -> None:
pass

Check warning on line 211 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L210-L211

Added lines #L210 - L211 were not covered by tests


class KSMetric(DistToDistMetric):

Check warning on line 214 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L214

Added line #L214 was not covered by tests
"""Class wrapper for Kolmogorov Smirnov metric.
"""

metric_name = 'ks'
metric_output_type = MetricOuputType.one_value_per_distribution

Check warning on line 219 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L218-L219

Added lines #L218 - L219 were not covered by tests

def __init__(self, num_samples:int=100) -> None:
super().__init__()
self._num_samples = num_samples
self._random_state = None

Check warning on line 224 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L221-L224

Added lines #L221 - L224 were not covered by tests

@property
def _random_state(self):
return self._random_state

Check warning on line 228 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L226-L228

Added lines #L226 - L228 were not covered by tests

@_random_state.setter
def _random_state(self, random_state):
self._random_state=random_state

Check warning on line 232 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L230-L232

Added lines #L230 - L232 were not covered by tests

def initialize(self):
pass

Check warning on line 235 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L234-L235

Added lines #L234 - L235 were not covered by tests

def evaluate(self, estimate, reference) -> list:
return calculate_goodness_of_fit(

Check warning on line 238 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L237-L238

Added lines #L237 - L238 were not covered by tests
estimate,
reference,
fit_metric=self.metric_name,
num_samples=self._num_samples,
_random_state=self._random_state
)

def finalize(self) -> None:
pass

Check warning on line 247 in src/qp/metrics/concrete_metric_classes.py

View check run for this annotation

Codecov / codecov/patch

src/qp/metrics/concrete_metric_classes.py#L246-L247

Added lines #L246 - L247 were not covered by tests

0 comments on commit 8f71c38

Please sign in to comment.