Skip to content

Commit c8d6556

Browse files
Drew KaulDrew Kaul
authored andcommitted
get scene tests passing
1 parent fc4d18a commit c8d6556

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

nucleus/scene.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from abc import ABC
33
from dataclasses import dataclass, field
4-
from typing import Optional, Union, Any, Dict, List, Set
4+
from typing import Optional, Union, Any, Dict, List
55
from enum import Enum
66
from nucleus.constants import (
77
CAMERA_PARAMS_KEY,
@@ -11,8 +11,6 @@
1111
FX_KEY,
1212
FY_KEY,
1313
HEADING_KEY,
14-
INDEX_KEY,
15-
ITEMS_KEY,
1614
METADATA_KEY,
1715
POSITION_KEY,
1816
REFERENCE_ID_KEY,
@@ -145,13 +143,18 @@ def __post_init__(self):
145143
def add_item(self, item: SceneDatasetItem, sensor_name: str):
146144
self.items[sensor_name] = item
147145

146+
@classmethod
147+
def from_json(cls, payload: dict):
148+
items = {
149+
sensor: SceneDatasetItem.from_json(item)
150+
for sensor, item in payload.items()
151+
}
152+
return cls(items=items)
153+
148154
def to_payload(self) -> dict:
149155
return {
150-
INDEX_KEY: self.index,
151-
ITEMS_KEY: {
152-
sensor: scene_dataset_item.to_payload()
153-
for sensor, scene_dataset_item in self.items.items()
154-
},
156+
sensor: scene_dataset_item.to_payload()
157+
for sensor, scene_dataset_item in self.items.items()
155158
}
156159

157160

@@ -210,7 +213,26 @@ def add_frame(self, frame: Frame, update: bool = False):
210213
):
211214
self.frames_dict[frame.index] = frame
212215

216+
def validate_frames_dict(self):
217+
is_continuous = set(list(range(len(self.frames_dict)))) == set(
218+
self.frames_dict.keys()
219+
)
220+
assert (
221+
is_continuous
222+
), "frames must be 0-indexed and continuous (no missing frames)"
223+
224+
@classmethod
225+
def from_json(cls, payload: dict):
226+
frames_payload = payload.get(FRAMES_KEY, [])
227+
frames = [Frame.from_json(frame) for frame in frames_payload]
228+
return cls(
229+
reference_id=payload[REFERENCE_ID_KEY],
230+
frames=frames,
231+
metadata=payload.get(METADATA_KEY, None),
232+
)
233+
213234
def to_payload(self) -> dict:
235+
self.validate_frames_dict()
214236
ordered_frames = [
215237
frame
216238
for _, frame in sorted(
@@ -244,7 +266,7 @@ def validate(self):
244266
]
245267
)
246268
assert (
247-
len(Set(lidar_sources)) == 1
269+
len(set(lidar_sources)) == 1
248270
), "Each lidar scene must have exactly one lidar source"
249271

250272
for frame in self.frames_dict.values():

tests/helpers.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@
3232
"reference_id": "scene_1",
3333
"frames": [
3434
{
35-
"index": 0,
36-
"items": {
37-
"lidar": {
38-
"url": TEST_POINTCLOUD_URLS[0],
39-
"type": "pointcloud",
40-
"reference_id": "lidar_frame_1",
41-
"metadata": {},
42-
},
35+
"lidar": {
36+
"url": TEST_POINTCLOUD_URLS[0],
37+
"type": "pointcloud",
38+
"reference_id": "lidar_frame_1",
39+
"metadata": {},
4340
},
4441
}
4542
],

tests/test_scene.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from nucleus.constants import SCENES_KEY
1+
from nucleus.constants import SCENES_KEY, UPDATE_KEY
22
import pytest
33

44
from .helpers import (
@@ -10,6 +10,7 @@
1010

1111
from nucleus import (
1212
CuboidAnnotation,
13+
LidarScene,
1314
)
1415

1516

@@ -24,19 +25,29 @@ def dataset(CLIENT):
2425

2526
def test_scene_upload_sync(dataset):
2627
payload = TEST_LIDAR_SCENES
27-
response = dataset.upload_scenes(payload)
28+
scenes = [
29+
LidarScene.from_json(scene_json) for scene_json in payload[SCENES_KEY]
30+
]
31+
update = payload[UPDATE_KEY]
32+
33+
response = dataset.append(scenes, update=update)
2834

2935
assert response["dataset_id"] == dataset.id
30-
assert response["new_scenes"] == len(TEST_LIDAR_SCENES[SCENES_KEY])
36+
assert response["new_scenes"] == len(scenes)
3137

3238

3339
@pytest.mark.integration
3440
def test_scene_and_cuboid_upload_sync(dataset):
3541
payload = TEST_LIDAR_SCENES
36-
response = dataset.upload_scenes(payload)
42+
scenes = [
43+
LidarScene.from_json(scene_json) for scene_json in payload[SCENES_KEY]
44+
]
45+
update = payload[UPDATE_KEY]
46+
47+
response = dataset.append(scenes, update=update)
3748

3849
assert response["dataset_id"] == dataset.id
39-
assert response["new_scenes"] == len(TEST_LIDAR_SCENES[SCENES_KEY])
50+
assert response["new_scenes"] == len(scenes)
4051

4152
TEST_CUBOID_ANNOTATIONS[0]["dataset_item_id"] = dataset.items[0].item_id
4253
annotations = [CuboidAnnotation.from_json(TEST_CUBOID_ANNOTATIONS[0])]

0 commit comments

Comments
 (0)