-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest_make_broadcastable.py
265 lines (230 loc) · 7.52 KB
/
test_make_broadcastable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from collections.abc import Callable
from typing import Any, Concatenate
import numpy as np
import pytest
import xarray as xr
from movement.utils.broadcasting import (
KeywordArgs,
ScalarOr1D,
broadcastable_method,
make_broadcastable,
space_broadcastable,
)
def copy_with_collapsed_dimension(
original: xr.DataArray, collapse: str, new_data: np.ndarray
) -> xr.DataArray:
reduced_dims = list(original.dims)
reduced_dims.remove(collapse)
reduced_coords = dict(original.coords)
reduced_coords.pop(collapse, None)
return xr.DataArray(
data=new_data, dims=reduced_dims, coords=reduced_coords
)
def data_in_shape(shape: tuple[int, ...]) -> np.ndarray:
return np.arange(np.prod(shape), dtype=float).reshape(shape)
def mock_shape() -> tuple[int, ...]:
return (10, 2, 3, 4)
@pytest.fixture
def mock_dataset() -> xr.DataArray:
return xr.DataArray(
data=data_in_shape(mock_shape()),
dims=["time", "space", "individuals", "keypoints"],
coords={"space": ["x", "y"]},
)
@pytest.mark.parametrize(
["along_dimension", "expected_output", "mimic_fn", "fn_args", "fn_kwargs"],
[
pytest.param(
"space",
np.zeros(mock_shape()).sum(axis=1),
lambda x: 0.0,
tuple(),
{},
id="Zero everything",
),
pytest.param(
"space",
data_in_shape(mock_shape()).sum(axis=1),
sum,
tuple(),
{},
id="Mimic sum",
),
pytest.param(
"time",
data_in_shape(mock_shape()).prod(axis=0),
np.prod,
tuple(),
{},
id="Mimic prod, on non-space dimensions",
),
pytest.param(
"space",
5.0 * data_in_shape(mock_shape()).sum(axis=1),
lambda x, **kwargs: kwargs.get("multiplier", 1.0) * sum(x),
tuple(),
{"multiplier": 5.0},
id="Preserve kwargs",
),
pytest.param(
"space",
data_in_shape(mock_shape()).sum(axis=1),
lambda x, **kwargs: kwargs.get("multiplier", 1.0) * sum(x),
tuple(),
{},
id="Preserve kwargs [fall back on default]",
),
pytest.param(
"space",
5.0 * data_in_shape(mock_shape()).sum(axis=1),
lambda x, multiplier=1.0: multiplier * sum(x),
(5,),
{},
id="Preserve args",
),
pytest.param(
"space",
data_in_shape(mock_shape()).sum(axis=1),
lambda x, multiplier=1.0: multiplier * sum(x),
tuple(),
{},
id="Preserve args [fall back on default]",
),
],
)
def test_make_broadcastable(
mock_dataset: xr.DataArray,
along_dimension: str,
expected_output: xr.DataArray,
mimic_fn: Callable[Concatenate[Any, KeywordArgs], ScalarOr1D],
fn_args: list[Any],
fn_kwargs: dict[str, Any],
) -> None:
"""Test make_broadcastable decorator, when acting on functions."""
if isinstance(expected_output, np.ndarray):
expected_output = copy_with_collapsed_dimension(
mock_dataset, along_dimension, expected_output
)
decorated_fn = make_broadcastable()(mimic_fn)
decorated_output = decorated_fn(
mock_dataset,
*fn_args,
broadcast_dimension=along_dimension, # type: ignore
**fn_kwargs,
)
assert decorated_output.shape == expected_output.shape
xr.testing.assert_allclose(decorated_output, expected_output)
# Also check the case where we only want to be able to cast over time.
if along_dimension == "space":
decorated_fn_space_only = space_broadcastable()(mimic_fn)
decorated_output_space = decorated_fn_space_only(
mock_dataset, *fn_args, **fn_kwargs
)
assert decorated_output_space.shape == expected_output.shape
xr.testing.assert_allclose(decorated_output_space, expected_output)
@pytest.mark.parametrize(
[
"along_dimension",
"cls_attribute",
"fn_args",
"fn_kwargs",
"expected_output",
],
[
pytest.param(
"space",
1.0,
[1.0],
{},
data_in_shape(mock_shape()).sum(axis=1) + 1.0,
id="In space",
),
pytest.param(
"time",
5.0,
[],
{"c": 2.5},
5.0 * data_in_shape(mock_shape()).sum(axis=0) + 2.5,
id="In time",
),
],
)
def test_make_broadcastable_classmethod(
mock_dataset: xr.DataArray,
along_dimension: str,
cls_attribute: float,
fn_args: list[Any],
fn_kwargs: dict[str, Any],
expected_output: np.ndarray,
) -> None:
"""Test make_broadcastable decorator, when acting on class methods."""
class DummyClass:
mult: float
def __init__(self, multiplier=1.0):
self.mult = multiplier
@broadcastable_method()
def sum_and_mult_plus_c(self, values, c):
return self.mult * sum(values) + c
expected_output = copy_with_collapsed_dimension(
mock_dataset, along_dimension, expected_output
)
d = DummyClass(cls_attribute)
decorated_output = d.sum_and_mult_plus_c(
mock_dataset,
*fn_args,
broadcast_dimension=along_dimension,
**fn_kwargs,
)
assert decorated_output.shape == expected_output.shape
xr.testing.assert_allclose(decorated_output, expected_output)
@pytest.mark.parametrize(
["broadcast_dim", "new_dim_length", "new_dim_name"],
[
pytest.param("space", 3, None, id="(3,), default dim name"),
pytest.param("space", 5, "elephants", id="(5,), custom dim name"),
],
)
def test_vector_outputs(
mock_dataset: xr.DataArray,
broadcast_dim: str,
new_dim_length: int,
new_dim_name: str | None,
) -> None:
"""Test make_broadcastable when 1D vector outputs are provided."""
if not new_dim_name:
# Take on the default value given in the method,
# if not provided
new_dim_name = "result"
@make_broadcastable(
only_broadcastable_along=broadcast_dim, new_dimension_name=new_dim_name
)
def two_to_some(xy_pair) -> np.ndarray:
# A 1D -> 1D function, rather than a function that returns a scalar.
return np.linspace(
xy_pair[0], xy_pair[1], num=new_dim_length, endpoint=True
)
output = two_to_some(mock_dataset)
assert isinstance(output, xr.DataArray)
for d in output.dims:
if d == new_dim_name:
assert len(output[d]) == new_dim_length
else:
assert d in mock_dataset.dims
assert len(output[d]) == len(mock_dataset[d])
def test_retain_underlying_function() -> None:
value_for_arg = 5.0
value_for_kwarg = 7.0
value_for_simple_input = [0.0, 1.0, 2.0]
def simple_function(input_1D, arg, kwarg=3.0):
return arg * sum(input_1D) + kwarg
@make_broadcastable()
def simple_function_broadcastable(input_1D, arg, kwarg=3.0):
return simple_function(input_1D, arg, kwarg=kwarg)
result_from_broadcastable = simple_function_broadcastable(
value_for_simple_input, value_for_arg, kwarg=value_for_kwarg
)
result_from_original = simple_function(
value_for_simple_input, value_for_arg, kwarg=value_for_kwarg
)
assert isinstance(result_from_broadcastable, float)
assert np.isclose(result_from_broadcastable, result_from_original)