Skip to content

Commit 37d39a4

Browse files
committed
refactor: use make_modality_config and exact time
1 parent 6790470 commit 37d39a4

2 files changed

Lines changed: 47 additions & 47 deletions

File tree

tests/create_experiment.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
1-
import copy
21
import shutil
32
from contextlib import contextmanager
43

54
from .create_sequence_data import _generate_sequence_data
65

7-
DEFAULT_CONFIG = {
8-
"device_0": {
9-
"sampling_rate": 1.0,
10-
"chunk_size": 40,
11-
"interpolation": {
12-
"interpolation_mode": "nearest_neighbor",
13-
},
14-
},
15-
"device_1": {
16-
"sampling_rate": 1.0,
17-
"chunk_size": 60,
18-
"interpolation": {
19-
"interpolation_mode": "linear",
20-
},
21-
},
22-
}
23-
24-
25-
def get_default_config():
26-
"""Return a fresh copy of the default modality configuration."""
27-
return copy.deepcopy(DEFAULT_CONFIG)
28-
296

307
@contextmanager
318
def setup_test_experiment(
@@ -48,3 +25,27 @@ def setup_test_experiment(
4825
finally:
4926
if tmp_path.exists():
5027
shutil.rmtree(tmp_path)
28+
29+
30+
def make_modality_config(*device_names, sampling_rates=None, offsets=None):
31+
if sampling_rates is None:
32+
sampling_rates = [10.0] * len(device_names)
33+
elif isinstance(sampling_rates, (int, float)):
34+
sampling_rates = [sampling_rates] * len(device_names)
35+
36+
if offsets is None:
37+
offsets = [0.0] * len(device_names)
38+
elif isinstance(offsets, (int, float)):
39+
offsets = [offsets] * len(device_names)
40+
41+
assert len(device_names) == len(sampling_rates), (
42+
f"sampling_rates length {len(sampling_rates)} does not match device_names length {len(device_names)}"
43+
)
44+
assert len(device_names) == len(offsets), (
45+
f"offsets length {len(offsets)} does not match device_names length {len(device_names)}"
46+
)
47+
48+
return {
49+
name: {"interpolation": {"sampling_rate": sr, "offset": off}}
50+
for name, sr, off in zip(device_names, sampling_rates, offsets, strict=True)
51+
}

tests/test_experiment.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from experanto.interpolators import Interpolator
1212

1313
from .create_experiment import (
14-
get_default_config,
14+
make_modality_config,
1515
setup_test_experiment,
1616
)
1717

@@ -126,7 +126,7 @@ def test_get_valid_range_all_devices(tmp_path, device_name, start_t, end_t):
126126
{"start_time": 5.0, "t_end": 15.0},
127127
],
128128
) as experiment_path:
129-
config = get_default_config()
129+
config = make_modality_config("device_0", "device_1", "device_2")
130130
config["device_2"] = {
131131
"sampling_rate": 1.0,
132132
"chunk_size": 40,
@@ -142,7 +142,8 @@ def test_get_valid_range_all_devices(tmp_path, device_name, start_t, end_t):
142142
def test_get_valid_range_raises_for_invalid_device(tmp_path):
143143
with setup_test_experiment(tmp_path) as experiment_path:
144144
experiment = Experiment(
145-
root_folder=str(experiment_path), modality_config=get_default_config()
145+
root_folder=str(experiment_path),
146+
modality_config=make_modality_config("device_0", "device_1"),
146147
)
147148
with pytest.raises(KeyError):
148149
experiment.get_valid_range("device_does_not_exist")
@@ -157,7 +158,8 @@ def test_experiment_with_non_zero_start_time(tmp_path):
157158
devices_kwargs=[{"t_end": start_offset + duration, "start_time": start_offset}],
158159
) as experiment_path:
159160
experiment = Experiment(
160-
root_folder=str(experiment_path), modality_config=get_default_config()
161+
root_folder=str(experiment_path),
162+
modality_config=make_modality_config("device_0"),
161163
)
162164
res = experiment.interpolate(np.array([start_offset + 1.0]), device="device_0")
163165
assert res is not None
@@ -187,9 +189,11 @@ def test_experiment_numeric_precision_offset(
187189
],
188190
) as experiment_path:
189191
experiment = Experiment(
190-
root_folder=str(experiment_path), modality_config=get_default_config()
192+
root_folder=str(experiment_path),
193+
modality_config=make_modality_config("device_0"),
191194
)
192195
valid_range = experiment.get_valid_range("device_0")
196+
# Keep pytest.approx here as it's dealing with floats generated by Hypothesis
193197
assert valid_range[0] == pytest.approx(start_offset)
194198
assert valid_range[1] == pytest.approx(start_offset + duration)
195199
res = experiment.interpolate(np.array([start_offset]), device="device_0")
@@ -202,7 +206,8 @@ def test_experiment_multi_device_interpolation(tmp_path, return_valid, device):
202206
"""Check data consistency when interpolating across multiple modalities."""
203207
with setup_test_experiment(tmp_path, n_devices=2) as experiment_path:
204208
exp = Experiment(
205-
root_folder=str(experiment_path), modality_config=get_default_config()
209+
root_folder=str(experiment_path),
210+
modality_config=make_modality_config("device_0", "device_1"),
206211
)
207212
times = np.array([1.0, 2.0])
208213
results = exp.interpolate(times, device=device, return_valid=return_valid)
@@ -244,22 +249,18 @@ def test_experiment_start_end_time_reflects_union(
244249
with setup_test_experiment(
245250
tmp_path, n_devices=len(device_ranges), devices_kwargs=devices_kwargs
246251
) as experiment_path:
247-
# Manually build the config dict for however many devices were generated
248-
config = {}
249-
for i in range(len(device_ranges)):
250-
config[f"device_{i}"] = {
251-
"interpolation": {
252-
"sampling_rate": 10.0,
253-
"offset": float(np.random.rand()),
254-
}
255-
}
252+
# Dynamically build the config using make_modality_config
253+
device_names = [f"device_{i}" for i in range(len(device_ranges))]
254+
offsets = [float(np.random.rand()) for _ in device_ranges]
255+
config = make_modality_config(*device_names, offsets=offsets)
256256

257257
experiment = Experiment(
258258
root_folder=str(experiment_path), modality_config=config
259259
)
260260

261-
assert experiment.start_time == pytest.approx(expected_start)
262-
assert experiment.end_time == pytest.approx(expected_end)
261+
# Removed pytest.approx here
262+
assert experiment.start_time == expected_start
263+
assert experiment.end_time == expected_end
263264

264265

265266
@pytest.mark.parametrize("override_meta", INVALID_META_CASES, ids=INVALID_META_IDS)
@@ -275,7 +276,7 @@ def test_experiment_invalid_metadata(tmp_path, override_meta):
275276
with open(meta_file, "w") as f:
276277
yaml.safe_dump(meta, f)
277278

278-
config = {"device_0": {"interpolation": {"sampling_rate": 10.0}}}
279+
config = make_modality_config("device_0")
279280

280281
with pytest.raises(
281282
ValueError, match="Experiment time range could not be determined"
@@ -311,10 +312,7 @@ def test_experiment_skips_invalid_devices(tmp_path, override_meta, caplog):
311312
with open(meta_file, "w") as f:
312313
yaml.safe_dump(meta, f)
313314

314-
config = {
315-
"valid_device": {"interpolation": {"sampling_rate": 10.0}},
316-
"invalid_device": {"interpolation": {"sampling_rate": 10.0}},
317-
}
315+
config = make_modality_config("valid_device", "invalid_device")
318316

319317
with caplog.at_level(logging.WARNING, logger="experanto.experiment"):
320318
experiment = Experiment(
@@ -323,5 +321,6 @@ def test_experiment_skips_invalid_devices(tmp_path, override_meta, caplog):
323321

324322
assert "valid_device" in experiment.devices
325323
assert "invalid_device" not in experiment.devices
326-
assert experiment.start_time == pytest.approx(start_val)
327-
assert experiment.end_time == pytest.approx(end_val)
324+
# Removed pytest.approx here
325+
assert experiment.start_time == start_val
326+
assert experiment.end_time == end_val

0 commit comments

Comments
 (0)