Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression metrics patch #382

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion fuse/eval/metrics/regression/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Union
from typing import List, Optional, Union, Sequence
from fuse.eval.metrics.libs.stat import Stat
from fuse.eval.metrics.metrics_common import MetricDefault
import numpy as np
Expand Down Expand Up @@ -41,6 +41,7 @@ def __init__(
self,
pred: str,
target: str,
mask: Optional[str] = None,
**kwargs: dict,
) -> None:
"""
Expand All @@ -53,6 +54,7 @@ def __init__(
super().__init__(
pred=pred,
target=target,
mask=mask,
metric_func=self.mae,
**kwargs,
)
Expand All @@ -61,8 +63,31 @@ def mae(
self,
pred: Union[List, np.ndarray],
target: Union[List, np.ndarray],
mask: Optional[np.ndarray] = None,
**kwargs: dict,
) -> float:

if mask is not None:

if isinstance(pred, Sequence):
if np.isscalar(pred[0]):
pred = np.array(pred)
else:
pred = np.concatenate(pred)
if isinstance(target, Sequence):
if np.isscalar(target[0]):
target = np.array(target)
else:
target = np.concatenate(target)
if isinstance(mask, Sequence):
if np.isscalar(mask[0]):
mask = np.array(mask).astype("bool")
else:
mask = np.concatenate(mask).astype("bool")

target = target[mask]
pred = pred[mask]

return mean_absolute_error(y_true=target, y_pred=pred)


Expand All @@ -71,6 +96,7 @@ def __init__(
self,
pred: str,
target: str,
mask: Optional[str] = None,
**kwargs: dict,
) -> None:
"""
Expand All @@ -84,6 +110,7 @@ def __init__(
super().__init__(
pred=pred,
target=target,
mask=mask,
metric_func=self.mse,
**kwargs,
)
Expand All @@ -92,8 +119,31 @@ def mse(
self,
pred: Union[List, np.ndarray],
target: Union[List, np.ndarray],
mask: Optional[np.ndarray] = None,
**kwargs: dict,
) -> float:

if mask is not None:

if isinstance(pred, Sequence):
if np.isscalar(pred[0]):
pred = np.array(pred)
else:
pred = np.concatenate(pred)
if isinstance(target, Sequence):
if np.isscalar(target[0]):
target = np.array(target)
else:
target = np.concatenate(target)
if isinstance(mask, Sequence):
if np.isscalar(mask[0]):
mask = np.array(mask).astype("bool")
else:
mask = np.concatenate(mask).astype("bool")

target = target[mask]
pred = pred[mask]

return mean_squared_error(y_true=target, y_pred=pred)


Expand All @@ -102,6 +152,7 @@ def __init__(
self,
pred: str,
target: str,
mask: Optional[str] = None,
**kwargs: dict,
) -> None:
"""
Expand All @@ -112,6 +163,7 @@ def __init__(
super().__init__(
pred=pred,
target=target,
mask=mask,
metric_func=self.rmse,
**kwargs,
)
Expand All @@ -120,10 +172,34 @@ def rmse(
self,
pred: Union[List, np.ndarray],
target: Union[List, np.ndarray],
mask: Optional[np.ndarray] = None,
**kwargs: dict,
) -> float:

if mask is not None:

if isinstance(pred, Sequence):
if np.isscalar(pred[0]):
pred = np.array(pred)
else:
pred = np.concatenate(pred)
if isinstance(target, Sequence):
if np.isscalar(target[0]):
target = np.array(target)
else:
target = np.concatenate(target)
if isinstance(mask, Sequence):
if np.isscalar(mask[0]):
mask = np.array(mask).astype("bool")
else:
mask = np.concatenate(mask).astype("bool")

target = target[mask]
pred = pred[mask]

pred = np.array(pred).flatten()
target = np.array(target).flatten()

assert len(pred) == len(
target
), f"Expected pred and target to have the dimensions but found: {len(pred)} elements in pred and {len(target)} in target"
Expand Down