-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_loader.py
More file actions
371 lines (301 loc) · 11.4 KB
/
test_loader.py
File metadata and controls
371 lines (301 loc) · 11.4 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#
# CEBRA: Consistent EmBeddings of high-dimensional Recordings using Auxiliary variables
# © Mackenzie W. Mathis & Steffen Schneider (v0.4.0+)
# Source code:
# https://github.com/AdaptiveMotorControlLab/CEBRA
#
# Please see LICENSE.md for the full license document:
# https://github.com/AdaptiveMotorControlLab/CEBRA/blob/main/LICENSE.md
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
import torch
import cebra.data
import cebra.io
def parametrize_device(func):
_devices = ("cpu", "cuda") if torch.cuda.is_available() else ("cpu",)
return pytest.mark.parametrize("device", _devices)(func)
class LoadSpeed:
def __init__(self, loader):
self.loader = loader
def __call__(self):
n = 0
for batch in self.loader:
n += 1
assert batch.reference.device.type == self.loader.device
assert n == len(self.loader)
class RandomDataset(cebra.data.SingleSessionDataset):
def __init__(self, N=100, d=5, device="cpu"):
super().__init__(device=device)
self._cindex = torch.randint(0, 5, (N, d), device=device).float()
self._dindex = torch.randint(0, 5, (N,), device=device).long()
self.neural = self._data = torch.randn((N, d), device=device)
@property
def input_dimension(self):
return self._data.shape[1]
def __len__(self):
return len(self._data)
@property
def continuous_index(self):
return self._cindex
@property
def discrete_index(self):
return self._dindex
def __getitem__(self, index):
return self._data[index]
def test_offset():
offset = cebra.data.Offset(5, 4)
assert offset.left == 5
assert offset.right == 4
assert offset.left_slice == slice(0, 5)
assert len(offset) == 5 + 4
offset = cebra.data.Offset(0, 4)
assert offset.left == 0
assert offset.right == 4
assert offset.left_slice == slice(0, 0)
assert len(offset) == 4
offset = cebra.data.Offset(5)
assert offset.left == 5
assert offset.right == 5
assert offset.left_slice == slice(0, 5)
assert offset.right_slice == slice(-5, None)
assert len(offset) == 5 * 2
with pytest.raises(ValueError, match="Invalid.*right"):
offset = cebra.data.Offset(5, 0)
with pytest.raises(ValueError, match="Invalid.*right"):
offset = cebra.data.Offset(0, 0)
with pytest.raises(ValueError, match="Invalid.*number"):
offset = cebra.data.Offset(5, 5, 5)
with pytest.raises(ValueError, match="Invalid.*bounds"):
offset = cebra.data.Offset(-2, 4)
with pytest.raises(ValueError, match="Invalid.*bounds"):
offset = cebra.data.Offset(4, -2)
def _assert_dataset_on_correct_device(loader, device):
assert hasattr(loader, "dataset")
assert hasattr(loader, "device")
assert isinstance(loader.dataset, cebra.io.HasDevice)
assert loader.dataset.neural.device.type == device
def test_demo_data():
if not torch.cuda.is_available():
pytest.skip("Test only possible with CUDA.")
dataset = RandomDataset(N=100, device="cuda")
assert dataset.neural.device.type == "cuda"
dataset.to("cpu")
assert dataset.neural.device.type == "cpu"
def _assert_device(first, second):
def _to_str(val):
if isinstance(val, torch.device):
return val.type
return val
assert _to_str(first) == _to_str(second)
@parametrize_device
@pytest.mark.parametrize(
"data_name, loader_initfunc",
[
("demo-discrete", cebra.data.DiscreteDataLoader),
("demo-continuous", cebra.data.ContinuousDataLoader),
("demo-mixed", cebra.data.MixedDataLoader),
],
)
def test_device(data_name, loader_initfunc, device):
if not torch.cuda.is_available():
pytest.skip("Test only possible with CUDA.")
swap = {"cpu": "cuda", "cuda": "cpu"}
other_device = swap.get(device)
dataset = RandomDataset(N=100, device=other_device)
loader = loader_initfunc(dataset, num_steps=10, batch_size=32)
loader.to(device)
assert loader.dataset == dataset
_assert_device(loader.device, device)
_assert_device(loader.dataset.device, device)
_assert_device(loader.get_indices(10).reference.device, device)
@parametrize_device
@pytest.mark.parametrize("prior", ("uniform", "empirical"))
def test_discrete(prior, device, benchmark):
dataset = RandomDataset(N=100, device=device)
loader = cebra.data.DiscreteDataLoader(
dataset=dataset,
num_steps=10,
batch_size=8,
prior=prior,
)
_assert_dataset_on_correct_device(loader, device)
load_speed = LoadSpeed(loader)
benchmark(load_speed)
@parametrize_device
@pytest.mark.parametrize("conditional", ("time", "time_delta"))
def test_continuous(conditional, device, benchmark):
dataset = RandomDataset(N=100, d=5, device=device)
loader = cebra.data.ContinuousDataLoader(
dataset=dataset,
num_steps=10,
batch_size=8,
conditional=conditional,
)
_assert_dataset_on_correct_device(loader, device)
load_speed = LoadSpeed(loader)
benchmark(load_speed)
@parametrize_device
@pytest.mark.parametrize(
"conditional, positive_sampling, discrete_sampling_prior",
[
("time", "discrete_variable", "empirical"),
("time", "conditional", "empirical"),
("time", "discrete_variable", "uniform"),
("time", "conditional", "uniform"),
("time_delta", "discrete_variable", "empirical"),
("time_delta", "conditional", "empirical"),
("time_delta", "discrete_variable", "uniform"),
("time_delta", "conditional", "uniform"),
],
)
def test_mixed(
conditional, positive_sampling, discrete_sampling_prior, device, benchmark
):
dataset = RandomDataset(N=100, d=5, device=device)
loader = cebra.data.MixedDataLoader(
dataset=dataset,
num_steps=10,
batch_size=8,
conditional=conditional,
positive_sampling=positive_sampling,
discrete_sampling_prior=discrete_sampling_prior,
)
_assert_dataset_on_correct_device(loader, device)
load_speed = LoadSpeed(loader)
benchmark(load_speed)
def _check_attributes(obj, is_list=False):
if is_list:
for obj_ in obj:
_check_attributes(obj_, is_list=False)
elif isinstance(obj, cebra.data.Batch) or isinstance(
obj, cebra.data.BatchIndex):
assert hasattr(obj, "positive")
assert hasattr(obj, "negative")
assert hasattr(obj, "reference")
else:
raise TypeError()
@parametrize_device
@pytest.mark.parametrize(
"data_name, loader_initfunc",
[
("demo-discrete", cebra.data.DiscreteDataLoader),
("demo-continuous", cebra.data.ContinuousDataLoader),
("demo-mixed", cebra.data.MixedDataLoader),
],
)
def test_singlesession_loader(data_name, loader_initfunc, device):
data = cebra.datasets.init(data_name)
data.to(device)
loader = loader_initfunc(data, num_steps=10, batch_size=32)
_assert_dataset_on_correct_device(loader, device)
index = loader.get_indices(100)
_check_attributes(index)
for batch in loader:
_check_attributes(batch)
assert len(batch.positive) == 32
def test_multisession_cont_loader():
data = cebra.datasets.MultiContinuous(nums_neural=[3, 4, 5],
num_behavior=5,
num_timepoints=100)
loader = cebra.data.ContinuousMultiSessionDataLoader(
data,
num_steps=10,
batch_size=32,
)
# Check the sampler
assert hasattr(loader, "sampler")
ref_idx = loader.sampler.sample_prior(1000)
assert len(ref_idx) == 3 # num_sessions
for session in range(3):
assert ref_idx[session].max() < 100
pos_idx, idx, idx_rev = loader.sampler.sample_conditional(ref_idx)
assert pos_idx is not None
assert idx is not None
assert idx_rev is not None
batch = next(iter(loader))
def _mix(array, idx):
shape = array.shape
n, m = shape[:2]
mixed = array.reshape(n * m, -1)[idx]
print(mixed.shape, array.shape, idx.shape)
return mixed.reshape(shape)
def _process(batch, feature_dim=1):
"""Given list_i[(N,d_i)] batch, return (#session, N, feature_dim) tensor"""
return torch.stack(
[b.reference.flatten(1).mean(dim=1, keepdims=True) for b in batch],
dim=0).repeat(1, 1, feature_dim)
assert batch[0].reference.shape == (32, 3, 10)
assert batch[1].reference.shape == (32, 4, 10)
assert batch[2].reference.shape == (32, 5, 10)
dummy_prediction = _process(batch, feature_dim=6)
assert dummy_prediction.shape == (3, 32, 6)
_mix(dummy_prediction, batch[0].index)
def test_multisession_disc_loader():
data = cebra.datasets.MultiDiscrete(nums_neural=[3, 4, 5],
num_timepoints=100)
loader = cebra.data.DiscreteMultiSessionDataLoader(
data,
num_steps=10,
batch_size=32,
)
# Check the sampler
assert hasattr(loader, "sampler")
ref_idx = loader.sampler.sample_prior(1000)
assert len(ref_idx) == 3 # num_sessions
# Check sample points are in session length range
for session in range(3):
assert ref_idx[session].max() < loader.sampler.session_lengths[session]
pos_idx, idx, idx_rev = loader.sampler.sample_conditional(ref_idx)
assert pos_idx is not None
assert idx is not None
assert idx_rev is not None
batch = next(iter(loader))
def _mix(array, idx):
shape = array.shape
n, m = shape[:2]
mixed = array.reshape(n * m, -1)[idx]
print(mixed.shape, array.shape, idx.shape)
return mixed.reshape(shape)
def _process(batch, feature_dim=1):
"""Given list_i[(N,d_i)] batch, return (#session, N, feature_dim) tensor"""
return torch.stack(
[b.reference.flatten(1).mean(dim=1, keepdims=True) for b in batch],
dim=0).repeat(1, 1, feature_dim)
assert batch[0].reference.shape == (32, 3, 10)
assert batch[1].reference.shape == (32, 4, 10)
assert batch[2].reference.shape == (32, 5, 10)
dummy_prediction = _process(batch, feature_dim=6)
assert dummy_prediction.shape == (3, 32, 6)
_mix(dummy_prediction, batch[0].index)
@parametrize_device
@pytest.mark.parametrize(
"data_name, loader_initfunc",
[('demo-discrete-multisession', cebra.data.DiscreteMultiSessionDataLoader),
("demo-continuous-multisession",
cebra.data.ContinuousMultiSessionDataLoader)],
)
def test_multisession_loader(data_name, loader_initfunc, device):
# TODO change number of timepoints across the sessions
data = cebra.datasets.init(data_name)
kwargs = dict(num_steps=10, batch_size=32)
loader = loader_initfunc(data, **kwargs)
index = loader.get_indices(100)
print(index[0])
print(type(index))
_check_attributes(index, is_list=False)
for batch in loader:
_check_attributes(batch, is_list=True)
for session_batch in batch:
assert len(session_batch.positive) == 32