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

Handle ops between FDGrid and Python functions #458

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 14 additions & 10 deletions skfda/representation/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterable,
Optional,
Sequence,
Expand Down Expand Up @@ -710,12 +711,11 @@ def __eq__(self, other: object) -> NDArrayBool: # type: ignore[override]

def _get_op_matrix(
self,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> Union[None, float, NDArrayFloat, NDArrayInt]:
if isinstance(other, numbers.Real):
return float(other)
elif isinstance(other, np.ndarray):

if other.shape in {(), (1,)}:
return other
elif other.shape == (self.n_samples,):
Expand All @@ -741,11 +741,15 @@ def _get_op_matrix(
self._check_same_dimensions(other)
return other.data_matrix

elif isinstance(other, Callable):
coordinates = np.array(np.meshgrid(*self.grid_points, indexing='ij')).T.reshape(-1, self.dim_domain)
return np.array([other(x) for x in coordinates]).reshape((1,) + self.data_matrix.shape[1:])

return None

def __add__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

data_matrix = self._get_op_matrix(other)
Expand All @@ -756,14 +760,14 @@ def __add__(

def __radd__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

return self.__add__(other)

def __sub__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

data_matrix = self._get_op_matrix(other)
Expand All @@ -774,7 +778,7 @@ def __sub__(

def __rsub__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

data_matrix = self._get_op_matrix(other)
Expand All @@ -785,7 +789,7 @@ def __rsub__(

def __mul__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

data_matrix = self._get_op_matrix(other)
Expand All @@ -796,14 +800,14 @@ def __mul__(

def __rmul__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

return self.__mul__(other)

def __truediv__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

data_matrix = self._get_op_matrix(other)
Expand All @@ -814,7 +818,7 @@ def __truediv__(

def __rtruediv__(
self: T,
other: Union[T, NDArrayFloat, NDArrayInt, float],
other: Union[T, NDArrayFloat, NDArrayInt, float, Callable[[Union[float, NDArrayFloat]], Union[float, NDArrayFloat]]],
) -> T:

data_matrix = self._get_op_matrix(other)
Expand Down
40 changes: 38 additions & 2 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import numpy as np


class TestFDataGrid(unittest.TestCase):

# def setUp(self): could be defined for set up before any test
Expand Down Expand Up @@ -151,7 +150,22 @@ def test_coordinates(self):
fd.data_matrix)

def test_add(self):
fd1 = FDataGrid([[1, 2, 3, 4], [2, 3, 4, 5]])
fd1 = FDataGrid([[1, 2, 3, 4], [2, 3, 4, 5]], [0, 1, 2, 3])
fd_2d_in = FDataGrid([
[[[0], [0]], [[0], [0]]],
[[[1], [1]], [[1], [1]]]],
[[0, 1], [0, 1]]
)
fd_2d_out = FDataGrid([
[[0, 0], [1, 1]],
[[1, 1], [0, 0]]],
[[0, 1]]
)
fd_2d_in_out = FDataGrid([
[[[0, 0], [0, 1]], [[1, 0], [1, 1]]],
[[[1, 1], [1, 0]], [[0, 1], [0, 0]]]],
[[0, 1], [0, 1]]
)

fd2 = fd1 + fd1
np.testing.assert_array_equal(fd2.data_matrix[..., 0],
Expand All @@ -161,6 +175,28 @@ def test_add(self):
np.testing.assert_array_equal(fd2.data_matrix[..., 0],
[[3, 4, 5, 6], [4, 5, 6, 7]])

fd2 = fd1 + (lambda x: x)
matthieubulte marked this conversation as resolved.
Show resolved Hide resolved
np.testing.assert_array_equal(fd2.data_matrix[..., 0],
[[1, 3, 5, 7], [2, 4, 6, 8]])

fd2 = fd_2d_in + np.max
np.testing.assert_array_equal(fd2.data_matrix[..., 0], [
[[0, 1], [1, 1]],
[[1, 2], [2, 2]]
])

fd2 = fd_2d_out + (lambda x: np.array([x, x]))
np.testing.assert_array_equal(fd2.data_matrix, [
[[0, 0], [2, 2]],
[[1, 1], [1, 1]]
])

fd2 = fd_2d_in_out + (lambda x: x)
np.testing.assert_array_equal(fd2.data_matrix, [
[[[0, 0], [1, 1]], [[1, 1], [2, 2]]],
[[[1, 1], [2, 0]], [[0, 2], [1, 1]]]
])

fd2 = fd1 + np.array(2)
np.testing.assert_array_equal(fd2.data_matrix[..., 0],
[[3, 4, 5, 6], [4, 5, 6, 7]])
Expand Down