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

Implement scores for FDatairregular objects as described in #609 #610

Merged
merged 9 commits into from
Jul 5, 2024
1 change: 1 addition & 0 deletions skfda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FData as FData,
FDataBasis as FDataBasis,
FDataGrid as FDataGrid,
FDataIrregular as FDataIrregular,
concatenate as concatenate,
)

Expand Down
148 changes: 131 additions & 17 deletions skfda/misc/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing_extensions import Literal, Protocol

from .._utils import nquad_vec
from ..representation import FData, FDataBasis, FDataGrid
from ..representation import FData, FDataBasis, FDataGrid, FDataIrregular
from ..representation._functional_data import EvalPointsType
from ..typing._numpy import NDArrayFloat

Expand Down Expand Up @@ -125,6 +125,37 @@
return float(np.mean(score.integrate()[0]) / _domain_measure(score))


def _integral_average_fdatairregular(
score: FDataIrregular,
squared: bool = True,
weights: NDArrayFloat | None = None,
) -> float:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
DAR201 Missing "Returns" in Docstring: - return

"""Calculate the weighted average of the normalized integrals of the score.

The integral of the score is normalized because each integral is divided by
the length of the curve's domain.

If the score is vector-valued, then the mean of each codimension integral
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what we want? Is what we do for the other types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the question is regarding whether to divide by the length of the curve's domain or by the length of the FDataIrregular object's domain. This is the only difference that there is between the results of FDataGrid scores and the FDataIrregular that I implemented. As I said in #609, I think that dividing by each curve's domain length is more accurate, as the integral of that curve is being made only taking into account its particular domain.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant the treatment of vector-valued functions, but you also raised an interesting point that I didn't notice, and maybe we should discuss in the meeting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answering the initial question, then: yes, for other types we also take the mean of each codimension integral in the case of vector-valued functions. I think it is a reasonable design decision.

is calculated for every functional observation.

Args:
score: Score of the functions.
squared: If False, the square root is taken.
vnmabus marked this conversation as resolved.
Show resolved Hide resolved
weights: Weights for the mean.
"""
if score.dim_domain != 1:
raise ValueError(

Check warning on line 147 in skfda/misc/scoring.py

View check run for this annotation

Codecov / codecov/patch

skfda/misc/scoring.py#L147

Added line #L147 was not covered by tests
"Only univariate FDataIrregular objects are supported",
)
if not squared:
score = np.sqrt(score)

Check warning on line 151 in skfda/misc/scoring.py

View check run for this annotation

Codecov / codecov/patch

skfda/misc/scoring.py#L151

Added line #L151 was not covered by tests
vnmabus marked this conversation as resolved.
Show resolved Hide resolved

integrals = np.mean(score.integrate(), axis=1)
lebesgue_measures = np.diff(score.sample_range, axis=-1).reshape(-1)
normalized_integrals = integrals / lebesgue_measures
return np.average(normalized_integrals, weights=weights)


@overload
def explained_variance_score(
y_true: DataType,
Expand Down Expand Up @@ -361,8 +392,9 @@
where :math:`D` is the function domain and :math:`V` the volume of that
domain.

For :class:`~skfda.representation.FDataBasis` only
'uniform_average' is available.
For :class:`~skfda.representation.FDataBasis` and
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is
available.

If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function
is called.
Expand All @@ -378,8 +410,10 @@
Mean absolute error.

If multioutput = 'uniform_average' or
:math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataBasis` objects, float is returned.
:math:`y\_pred` and :math:`y\_true` are both
:class:`~skfda.representation.FDataBasis` or both
:class:`~skfda.representation.FDataIrregular` objects, float is
returned.

If both :math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataGrid`
Expand Down Expand Up @@ -412,6 +446,20 @@
return _multioutput_score_grid(error, multioutput)


@mean_absolute_error.register # type: ignore[attr-defined, misc]
def _mean_absolute_error_fdatairregular(
y_true: FDataIrregular,
y_pred: FDataIrregular,
*,
sample_weight: Optional[NDArrayFloat] = None,
vnmabus marked this conversation as resolved.
Show resolved Hide resolved
multioutput: MultiOutputType = 'uniform_average',
) -> float:
return _integral_average_fdatairregular(
np.abs(y_true - y_pred),
weights=sample_weight,
)


@mean_absolute_error.register # type: ignore[attr-defined, misc]
def _mean_absolute_error_fdatabasis(
y_true: FDataBasis,
Expand Down Expand Up @@ -491,8 +539,9 @@
where :math:`D` is the function domain and :math:`V` the volume of that
domain.

For :class:`~skfda.representation.FDataBasis` only
'uniform_average' is available.
For :class:`~skfda.representation.FDataBasis` and
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is
available.

If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function
is called.
Expand All @@ -511,8 +560,10 @@
Mean absolute percentage error.

If multioutput = 'uniform_average' or
:math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataBasis` objects, float is returned.
:math:`y\_pred` and :math:`y\_true` are both
:class:`~skfda.representation.FDataBasis` or both
:class:`~skfda.representation.FDataIrregular` objects, float is
returned.

If both :math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataGrid`
Expand Down Expand Up @@ -554,6 +605,23 @@
return _multioutput_score_grid(error, multioutput)


@mean_absolute_percentage_error.register # type: ignore[attr-defined, misc]
def _mean_absolute_percentage_error_fdatairregular(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
WPS118 Found too long name: _mean_absolute_percentage_error_fdatairregular > 45

y_true: FDataIrregular,
y_pred: FDataIrregular,
*,
sample_weight: Optional[NDArrayFloat] = None,
multioutput: MultiOutputType = 'uniform_average',
) -> float:
epsilon = np.finfo(np.float64).eps

if np.any(np.abs(y_true.values) < epsilon):
warnings.warn('Zero denominator', RuntimeWarning)

Check warning on line 619 in skfda/misc/scoring.py

View check run for this annotation

Codecov / codecov/patch

skfda/misc/scoring.py#L619

Added line #L619 was not covered by tests
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pep8] reported by reviewdog 🐶
B028 No explicit stacklevel argument found. The warn method from the warnings module uses a stacklevel of 1 by default. This will only show a stack trace for the line on which the warn method is called. It is therefore recommended to use a stacklevel of 2 or greater to provide more information to the user.


mape = np.abs(y_pred - y_true) / np.maximum(np.abs(y_true), epsilon)
return _integral_average_fdatairregular(mape, weights=sample_weight)


@mean_absolute_percentage_error.register # type: ignore[attr-defined, misc]
def _mean_absolute_percentage_error_fdatabasis(
y_true: FDataBasis,
Expand Down Expand Up @@ -644,8 +712,9 @@
where :math:`D` is the function domain and :math:`V` the volume of that
domain.

For :class:`~skfda.representation.FDataBasis` only
'uniform_average' is available.
For :class:`~skfda.representation.FDataBasis` and
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is
available.

If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function
is called.
Expand All @@ -662,8 +731,10 @@
Mean squared error.

If multioutput = 'uniform_average' or
:math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataBasis` objects, float is returned.
:math:`y\_pred` and :math:`y\_true` are both
:class:`~skfda.representation.FDataBasis` or both
:class:`~skfda.representation.FDataIrregular` objects, float is
returned.

If both :math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataGrid`
Expand Down Expand Up @@ -702,6 +773,22 @@
return _multioutput_score_grid(error, multioutput, squared=squared)


@mean_squared_error.register # type: ignore[attr-defined, misc]
def _mean_squared_error_fdatairregular(
y_true: FDataIrregular,
y_pred: FDataIrregular,
*,
sample_weight: Optional[NDArrayFloat] = None,
multioutput: MultiOutputType = 'uniform_average',
squared: bool = True,
) -> float:
return _integral_average_fdatairregular(
np.power(y_true - y_pred, 2),
weights=sample_weight,
squared=squared,
)


@mean_squared_error.register # type: ignore[attr-defined, misc]
def _mean_squared_error_fdatabasis(
y_true: FDataBasis,
Expand Down Expand Up @@ -791,8 +878,9 @@
where :math:`D` is the function domain and :math:`V` the volume of that
domain.

For :class:`~skfda.representation.FDataBasis` only
'uniform_average' is available.
For :class:`~skfda.representation.FDataBasis` and
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is
available.

If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function
is called.
Expand All @@ -812,8 +900,10 @@
Mean squared log error.

If multioutput = 'uniform_average' or
:math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataBasis` objects, float is returned.
:math:`y\_pred` and :math:`y\_true` are both
:class:`~skfda.representation.FDataBasis` or both
:class:`~skfda.representation.FDataIrregular` objects, float is
returned.

If both :math:`y\_pred` and :math:`y\_true` are
:class:`~skfda.representation.FDataGrid`
Expand Down Expand Up @@ -860,6 +950,30 @@
)


@mean_squared_log_error.register # type: ignore[attr-defined, misc]
def _mean_squared_log_error_fdatairregular(
y_true: FDataIrregular,
y_pred: FDataIrregular,
*,
sample_weight: Optional[NDArrayFloat] = None,
multioutput: MultiOutputType = 'uniform_average',
squared: bool = True,
) -> float:
if np.any(y_true.values < 0) or np.any(y_pred.values < 0):
raise ValueError(

Check warning on line 963 in skfda/misc/scoring.py

View check run for this annotation

Codecov / codecov/patch

skfda/misc/scoring.py#L963

Added line #L963 was not covered by tests
"Mean Squared Logarithmic Error cannot be used when "
"targets functions have negative values.",
)

return mean_squared_error(
np.log1p(y_true),
np.log1p(y_pred),
sample_weight=sample_weight,
multioutput=multioutput,
squared=squared,
)


@mean_squared_log_error.register # type: ignore[attr-defined, misc]
def _mean_squared_log_error_fdatabasis(
y_true: FDataBasis,
Expand Down
2 changes: 1 addition & 1 deletion skfda/representation/irregular.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _reduceat(
dtype=None,
out=None,
*,
value_empty
value_empty,
):
"""
Wrapped `np.ufunc.reduceat` to manage some edge cases.
Expand Down
108 changes: 107 additions & 1 deletion skfda/tests/test_scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from typing import Any, Optional, Sequence, Tuple

import numpy as np
import pytest
import sklearn.metrics

from skfda import FDataBasis, FDataGrid
from skfda import FDataBasis, FDataGrid, FDataIrregular
from skfda.misc.scoring import (
ScoreFunction,
explained_variance_score,
Expand All @@ -32,6 +33,13 @@
r2_score,
)

irregular_score_functions: Sequence[ScoreFunction] = (
mean_absolute_error,
mean_absolute_percentage_error,
mean_squared_error,
mean_squared_log_error,
)


def _create_data_basis() -> Tuple[FDataBasis, FDataBasis]:
coef_true = [[1, 2, 3], [4, 5, 6]]
Expand Down Expand Up @@ -461,3 +469,101 @@
y_true_grid,
y_pred_grid,
)


# ------------------ Test irregular data scoring ------------------


@pytest.fixture(params=irregular_score_functions)
def irregular_score_function(request) -> ScoreFunction:
"""Fixture to test score functions with irregular data."""
return request.param


_y_true_grid, _y_pred_grid = _create_data_grid()
vnmabus marked this conversation as resolved.
Show resolved Hide resolved
_y_true_irregular = FDataIrregular.from_fdatagrid(_y_true_grid)
_y_pred_irregular = FDataIrregular.from_fdatagrid(_y_pred_grid)


@pytest.fixture
def y_true_grid() -> FDataGrid:
"""Fixture with FDataGrid true representation."""
return _y_true_grid


@pytest.fixture
def y_pred_grid() -> FDataGrid:
"""Fixture with FDataGrid prediction representation."""
return _y_pred_grid


@pytest.fixture
def y_true_irregular() -> FDataIrregular:
"""Fixture with FDataIrregular true representation.

Same data as y_true_grid.
"""
return _y_true_irregular


@pytest.fixture
def y_pred_irregular() -> FDataIrregular:
"""Fixture with FDataIrregular true representation.

Same data as y_pred_grid.
"""
return _y_pred_irregular


def _cmp_score_functions(
y_true_grid: FDataGrid,
y_pred_grid: FDataGrid,
y_true_irregular: FDataIrregular,
y_pred_irregular: FDataIrregular,
irregular_score_function: ScoreFunction,
**kwargs: Any,
) -> None:
score_grid = irregular_score_function(
y_true_grid,
y_pred_grid,
**kwargs,
)
score_irregular = irregular_score_function(
y_true_irregular,
y_pred_irregular,
**kwargs,
)
np.testing.assert_allclose(
score_grid, score_irregular,
)


def test_score_functions_irregular(
y_true_grid: FDataGrid,
y_pred_grid: FDataGrid,
y_true_irregular: FDataIrregular,
y_pred_irregular: FDataIrregular,
irregular_score_function: ScoreFunction,
) -> None:
"""Test score functions with irregular data."""
weight = np.array([3, 1])

try:
_cmp_score_functions(
y_true_grid,
y_pred_grid,
y_true_irregular,
y_pred_irregular,
irregular_score_function,
sample_weight=weight,
)
except TypeError:
vnmabus marked this conversation as resolved.
Show resolved Hide resolved
pass

Check warning on line 561 in skfda/tests/test_scoring.py

View check run for this annotation

Codecov / codecov/patch

skfda/tests/test_scoring.py#L560-L561

Added lines #L560 - L561 were not covered by tests

_cmp_score_functions(
pcuestas marked this conversation as resolved.
Show resolved Hide resolved
y_true_grid,
y_pred_grid,
y_true_irregular,
y_pred_irregular,
irregular_score_function,
)
Loading