-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_rlds_loader.py
More file actions
547 lines (433 loc) · 19.5 KB
/
Copy pathtest_rlds_loader.py
File metadata and controls
547 lines (433 loc) · 19.5 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
"""Tests for the RLDS loader."""
from unittest.mock import MagicMock, Mock, patch
import numpy as np
import pytest
from robodm.loader.rlds import RLDSLoader
@pytest.fixture
def mock_tensorflow():
"""Mock TensorFlow modules."""
with patch.dict("sys.modules", {
"tensorflow": Mock(),
"tensorflow_datasets": Mock()
}):
yield
@pytest.fixture
def mock_tfds_builder():
"""Mock TensorFlow Datasets builder."""
mock_builder = Mock()
mock_dataset = Mock()
mock_builder.as_dataset.return_value = mock_dataset
# Mock dataset length
mock_dataset.__len__ = Mock(return_value=100)
# Mock dataset methods
mock_dataset.repeat.return_value = mock_dataset
mock_dataset.shuffle.return_value = mock_dataset
mock_dataset.take.return_value = mock_dataset
mock_dataset.skip.return_value = mock_dataset
return mock_builder
@pytest.fixture
def sample_trajectory_data():
"""Sample trajectory data structure."""
return {
"steps": [
{
"observation": {
"image": np.random.rand(64, 64, 3),
"state": np.array([0.1, 0.2, 0.3]),
},
"action": np.array([1.0, -1.0]),
"reward": np.array([0.5]),
"is_terminal": np.array([False]),
},
{
"observation": {
"image": np.random.rand(64, 64, 3),
"state": np.array([0.2, 0.3, 0.4]),
},
"action": np.array([0.5, -0.5]),
"reward": np.array([1.0]),
"is_terminal": np.array([True]),
},
]
}
class TestRLDSLoader:
"""Test RLDSLoader class."""
def test_init_without_tensorflow(self):
"""Test initialization when TensorFlow is not available."""
with patch.dict("sys.modules", {"tensorflow": None}):
with pytest.raises(
ImportError,
match="Please install tensorflow and tensorflow_datasets"):
RLDSLoader("/path/to/dataset")
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_init_basic(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test basic initialization."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset",
split="train",
batch_size=4,
shuffling=False)
assert loader.path == "/path/to/dataset"
assert loader.batch_size == 4
assert loader.split == "train"
assert loader.length == 100
assert loader.shuffling is False
assert loader.index == 0
mock_tfds.builder_from_directory.assert_called_once_with(
"/path/to/dataset")
mock_tfds_builder.as_dataset.assert_called_once_with("train")
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_init_with_shuffling(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test initialization with shuffling enabled."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset",
shuffling=True,
shuffle_buffer=20)
assert loader.shuffling is True
# Verify shuffle and repeat were called
mock_tfds_builder.as_dataset.return_value.repeat.assert_called_once()
mock_tfds_builder.as_dataset.return_value.shuffle.assert_called_once_with(
20)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_len(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test __len__ method."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset")
assert len(loader) == 100
def test_len_without_tensorflow(self):
"""Test __len__ when TensorFlow is not available."""
# Create a mock loader without proper TensorFlow setup
loader = object.__new__(RLDSLoader)
loader.length = 50
with patch.dict("sys.modules", {"tensorflow": None}):
with pytest.raises(ImportError, match="Please install tensorflow"):
len(loader)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_iter(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test __iter__ method."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset")
assert iter(loader) is loader
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_get_batch(self, mock_tf, mock_tfds, mock_tfds_builder,
sample_trajectory_data):
"""Test get_batch method."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
# Mock the batch data
mock_batch = [sample_trajectory_data, sample_trajectory_data]
mock_tfds_builder.as_dataset.return_value.take.return_value = mock_batch
loader = RLDSLoader("/path/to/dataset", batch_size=2, shuffling=False)
with patch.object(
loader,
"_convert_traj_to_numpy",
side_effect=lambda x: f"converted_{id(x)}") as mock_convert:
batch = loader.get_batch()
assert len(batch) == 2
assert loader.index == 2
assert mock_convert.call_count == 2
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_get_batch_stop_iteration(self, mock_tf, mock_tfds,
mock_tfds_builder):
"""Test get_batch raises StopIteration when no shuffling and at end."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset", batch_size=10, shuffling=False)
loader.index = 95 # Near the end
mock_batch = [{}] * 10
mock_tfds_builder.as_dataset.return_value.take.return_value = mock_batch
with patch.object(loader,
"_convert_traj_to_numpy",
return_value="converted"):
batch = loader.get_batch()
# After this batch, index will be 105 > length (100)
assert loader.index == 105
# Next call should raise StopIteration
with pytest.raises(StopIteration):
loader.get_batch()
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_next(self, mock_tf, mock_tfds, mock_tfds_builder,
sample_trajectory_data):
"""Test __next__ method."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
# Mock the iterator
mock_iterator = Mock()
mock_iterator.__next__ = Mock(return_value=sample_trajectory_data)
loader = RLDSLoader("/path/to/dataset", shuffling=False)
loader.iterator = mock_iterator
with patch.object(loader,
"_convert_traj_to_numpy",
return_value="converted_traj") as mock_convert:
result = next(loader)
assert result == ["converted_traj"]
assert loader.index == 1
mock_convert.assert_called_once_with(sample_trajectory_data)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_next_stop_iteration(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test __next__ raises StopIteration at end."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset", shuffling=False)
loader.index = 99 # At the end
mock_iterator = Mock()
mock_iterator.__next__ = Mock(return_value={})
loader.iterator = mock_iterator
with patch.object(loader,
"_convert_traj_to_numpy",
return_value="converted"):
result = next(loader)
assert loader.index == 100
# Next call should raise StopIteration
with pytest.raises(StopIteration):
next(loader)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_getitem(self, mock_tf, mock_tfds, mock_tfds_builder,
sample_trajectory_data):
"""Test __getitem__ method."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
# Mock the dataset skip/take operations
mock_dataset = mock_tfds_builder.as_dataset.return_value
mock_skip_take = Mock()
mock_skip_take.__iter__ = Mock(
return_value=iter([sample_trajectory_data]))
mock_dataset.skip.return_value.take.return_value = mock_skip_take
loader = RLDSLoader("/path/to/dataset")
with patch.object(loader,
"_convert_traj_to_numpy",
return_value="converted_item") as mock_convert:
result = loader[5]
assert result == "converted_item"
mock_dataset.skip.assert_called_once_with(5)
mock_dataset.skip.return_value.take.assert_called_once_with(1)
mock_convert.assert_called_once_with(sample_trajectory_data)
def test_convert_traj_to_numpy_simple(self, sample_trajectory_data):
"""Test _convert_traj_to_numpy with simple data."""
loader = object.__new__(RLDSLoader) # Create without __init__
with patch("robodm.loader.rlds.tf"):
result = loader._convert_traj_to_numpy(sample_trajectory_data)
assert isinstance(result, list)
assert len(result) == 2 # Two steps
# Check first step
step1 = result[0]
assert "observation" in step1
assert "action" in step1
assert "reward" in step1
assert "is_terminal" in step1
# Check that observation is a dict with numpy arrays
assert isinstance(step1["observation"], dict)
assert "image" in step1["observation"]
assert "state" in step1["observation"]
assert isinstance(step1["observation"]["image"], np.ndarray)
assert isinstance(step1["observation"]["state"], np.ndarray)
# Check other fields are numpy arrays
assert isinstance(step1["action"], np.ndarray)
assert isinstance(step1["reward"], np.ndarray)
assert isinstance(step1["is_terminal"], np.ndarray)
def test_convert_traj_to_numpy_flat_structure(self):
"""Test _convert_traj_to_numpy with flat structure."""
flat_traj = {
"steps": [{
"action": np.array([1.0, 2.0]),
"reward": np.array([0.5])
}]
}
loader = object.__new__(RLDSLoader)
with patch("robodm.loader.rlds.tf"):
result = loader._convert_traj_to_numpy(flat_traj)
assert len(result) == 1
step = result[0]
assert "action" in step
assert "reward" in step
assert isinstance(step["action"], np.ndarray)
assert isinstance(step["reward"], np.ndarray)
def test_convert_traj_to_numpy_nested_dict(self):
"""Test _convert_traj_to_numpy with deeply nested dictionaries."""
nested_traj = {
"steps": [{
"observation": {
"sensors": {
"camera": np.array([1, 2, 3]),
"lidar": np.array([4, 5, 6]),
},
"proprioception": {
"joint_pos": np.array([0.1, 0.2]),
"joint_vel": np.array([1.0, 2.0]),
},
},
"action": np.array([0.5]),
}]
}
loader = object.__new__(RLDSLoader)
with patch("robodm.loader.rlds.tf"):
result = loader._convert_traj_to_numpy(nested_traj)
step = result[0]
# Check nested structure is preserved
assert "observation" in step
obs = step["observation"]
assert "sensors" in obs
assert "proprioception" in obs
# Check sensors
sensors = obs["sensors"]
assert "camera" in sensors
assert "lidar" in sensors
assert isinstance(sensors["camera"], np.ndarray)
assert isinstance(sensors["lidar"], np.ndarray)
# Check proprioception
proprio = obs["proprioception"]
assert "joint_pos" in proprio
assert "joint_vel" in proprio
assert isinstance(proprio["joint_pos"], np.ndarray)
assert isinstance(proprio["joint_vel"], np.ndarray)
class TestRLDSLoaderEdgeCases:
"""Test edge cases for RLDS loader."""
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_empty_trajectory(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test handling of empty trajectory."""
empty_traj = {"steps": []}
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset")
with patch("robodm.loader.rlds.tf"):
result = loader._convert_traj_to_numpy(empty_traj)
assert result == []
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_zero_batch_size(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test with zero batch size."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset", batch_size=0)
assert loader.batch_size == 0
# Mock empty batch
mock_tfds_builder.as_dataset.return_value.take.return_value = []
batch = loader.get_batch()
assert batch == []
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_different_splits(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test with different dataset splits."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
# Test different splits
for split in ["train", "test", "validation"]:
loader = RLDSLoader("/path/to/dataset", split=split)
assert loader.split == split
mock_tfds_builder.as_dataset.assert_called_with(split)
def test_convert_traj_to_numpy_mixed_types(self):
"""Test _convert_traj_to_numpy with mixed data types."""
mixed_traj = {
"steps": [{
"string_field": "text_data",
"int_field": 42,
"float_field": 3.14,
"array_field": np.array([1, 2, 3]),
"nested": {
"inner_string": "inner_text",
"inner_array": np.array([4, 5, 6]),
},
}]
}
loader = object.__new__(RLDSLoader)
with patch("robodm.loader.rlds.tf"):
result = loader._convert_traj_to_numpy(mixed_traj)
step = result[0]
# All fields should be converted to numpy arrays or dict of numpy arrays
assert isinstance(step["string_field"], np.ndarray)
assert isinstance(step["int_field"], np.ndarray)
assert isinstance(step["float_field"], np.ndarray)
assert isinstance(step["array_field"], np.ndarray)
# Nested dict should preserve structure
assert isinstance(step["nested"], dict)
assert isinstance(step["nested"]["inner_string"], np.ndarray)
assert isinstance(step["nested"]["inner_array"], np.ndarray)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_large_shuffle_buffer(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test with large shuffle buffer."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset",
shuffle_buffer=10000,
shuffling=True)
# Verify shuffle was called with large buffer
mock_tfds_builder.as_dataset.return_value.shuffle.assert_called_once_with(
10000)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_index_tracking_with_shuffling(self, mock_tf, mock_tfds,
mock_tfds_builder):
"""Test index tracking with shuffling enabled."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset", shuffling=True)
# With shuffling, should not raise StopIteration based on index
loader.index = 150 # Beyond original length
mock_iterator = Mock()
mock_iterator.__next__ = Mock(return_value={"steps": []})
loader.iterator = mock_iterator
with patch.object(loader,
"_convert_traj_to_numpy",
return_value="converted"):
# Should not raise StopIteration because shuffling=True
result = next(loader)
assert result == ["converted"]
class TestRLDSLoaderIntegration:
"""Test integration scenarios for RLDS loader."""
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_full_iteration_cycle(self, mock_tf, mock_tfds, mock_tfds_builder):
"""Test full iteration cycle without shuffling."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
# Create loader with small dataset
mock_tfds_builder.as_dataset.return_value.__len__ = Mock(
return_value=3)
loader = RLDSLoader("/path/to/dataset", shuffling=False)
loader.length = 3
# Mock iterator
sample_data = {"steps": [{"action": np.array([1.0])}]}
mock_iterator = Mock()
mock_iterator.__next__ = Mock(
side_effect=[sample_data, sample_data, sample_data, StopIteration])
loader.iterator = mock_iterator
with patch.object(loader,
"_convert_traj_to_numpy",
return_value=["converted"]):
# Should be able to iterate through all items
items = []
try:
while True:
items.append(next(loader))
except StopIteration:
pass
assert len(items) == 3
assert all(item == ["converted"] for item in items)
@patch("robodm.loader.rlds.tfds")
@patch("robodm.loader.rlds.tf")
def test_batch_and_single_item_consistency(self, mock_tf, mock_tfds,
mock_tfds_builder,
sample_trajectory_data):
"""Test that batch and single item access return consistent data."""
mock_tfds.builder_from_directory.return_value = mock_tfds_builder
loader = RLDSLoader("/path/to/dataset", batch_size=1)
# Mock single item access
mock_dataset = mock_tfds_builder.as_dataset.return_value
mock_skip_take = Mock()
mock_skip_take.__iter__ = Mock(
return_value=iter([sample_trajectory_data]))
mock_dataset.skip.return_value.take.return_value = mock_skip_take
# Mock batch access
mock_dataset.take.return_value = [sample_trajectory_data]
with patch.object(
loader,
"_convert_traj_to_numpy",
side_effect=lambda x: f"converted_{id(x)}") as mock_convert:
# Get single item
single_item = loader[0]
# Get batch
batch = loader.get_batch()
# Both should have called convert function
assert mock_convert.call_count == 2
# Batch should contain one item (since batch_size=1)
assert len(batch) == 1