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

observables.py: add shape verification #71

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions reservoirpy/observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
from .type import Weights


def _check_arrays(y_true, y_pred):
y_true_array = np.asarray(y_true)
y_pred_array = np.asarray(y_pred)

if not y_true_array.shape == y_pred_array.shape:
raise ValueError(
f"Shape mismatch between y_true and y_pred:
{y_true_array.shape} != {y_pred_array.shape}"
)

return y_true_array, y_pred_array


def spectral_radius(W: Weights, maxiter: int = None) -> float:
"""Compute the spectral radius of a matrix `W`.

Expand Down Expand Up @@ -98,7 +111,8 @@ def mse(y_true: np.ndarray, y_pred: np.ndarray) -> float:
float
Mean squared error.
"""
return float(np.mean((np.asanyarray(y_true) - np.asanyarray(y_pred)) ** 2))
y_true_array, y_pred_array = _check_arrays(y_true, y_pred)
return float(np.mean((y_true_array - y_pred_array) ** 2))


def rmse(y_true: np.ndarray, y_pred: np.ndarray) -> float:
Expand Down Expand Up @@ -157,7 +171,7 @@ def nrmse(
-------
float
Normalized mean squared error.
"""
"""
error = rmse(y_true, y_pred)
if norm_value is not None:
return error / norm_value
Expand All @@ -176,7 +190,7 @@ def nrmse(
f"Available methods are {list(norms.keys())}."
)
else:
return error / norms[norm](np.asarray(y_true))
return error / norms[norm](y_true_array)


def rsquare(y_true: np.ndarray, y_pred: np.ndarray) -> float:
Expand All @@ -201,6 +215,8 @@ def rsquare(y_true: np.ndarray, y_pred: np.ndarray) -> float:
float
Coefficient of determination.
"""
d = (np.asarray(y_true) - np.asarray(y_pred)) ** 2
D = (np.asarray(y_true) - np.asarray(y_true).mean()) ** 2
y_true_array, y_pred_array = _check_arrays(y_true, y_pred)

d = (y_true_array - y_pred_array) ** 2
D = (y_true_array - y_pred_array.mean()) ** 2
return 1 - np.sum(d) / np.sum(D)
4 changes: 4 additions & 0 deletions reservoirpy/tests/test_observables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
(nrmse, [1.0, 2.0, 3.0], [1.5, 2.5, 3.5], {"norm": "foo"}, "raise"),
(nrmse, [1.0, 2.0, 3.0], [1.5, 2.5, 3.5], {"norm_value": 3.0}, None),
(rsquare, [1.0, 2.0, 3.0], [1.5, 2.5, 3.5], {}, None),
(mse, [1.0, 2.0, 3.0, 4.0], [1.5, 2.5, 3.5], {}, "raise"),
(rmse, [[1.0, 2.0, 3.0]], [1.5, 2.5, 3.5], {}, "raise"),
(nrmse, [1.0, 2.0, 3.0], [1.5, 2.5, 3.5, 4.2], {}, "raise"),
(rsquare, [1.0, 2.0, 3.0, 0.0], [1.5, 2.5, 3.5], {}, "raise"),
],
)
def test_observable(obs, ytest, ypred, kwargs, expects):
Expand Down