-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhelpers.py
More file actions
193 lines (162 loc) · 6.26 KB
/
Copy pathhelpers.py
File metadata and controls
193 lines (162 loc) · 6.26 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
from pathlib import Path
import time
from urllib.parse import urlparse
from nucleus import DatasetItem, BoxPrediction
PRESIGN_EXPIRY_SECONDS = 60 * 60 * 24 * 2 # 2 days
TEST_MODEL_NAME = "[PyTest] Test Model"
TEST_MODEL_RUN = "[PyTest] Test Model Run"
TEST_DATASET_NAME = "[PyTest] Test Dataset"
TEST_SLICE_NAME = "[PyTest] Test Slice"
TEST_PROJECT_ID = "60b699d70f139e002dd31bfc"
TEST_IMG_URLS = [
"https://github.com/scaleapi/nucleus-python-client/raw/master/tests/testdata/airplane.jpeg",
"https://github.com/scaleapi/nucleus-python-client/raw/master/tests/testdata/arctichare.jpeg",
"https://github.com/scaleapi/nucleus-python-client/raw/master/tests/testdata/baboon.jpeg",
"https://github.com/scaleapi/nucleus-python-client/raw/master/tests/testdata/barbara.jpeg",
"https://github.com/scaleapi/nucleus-python-client/raw/master/tests/testdata/cat.jpeg",
]
TEST_DATASET_ITEMS = [
DatasetItem(TEST_IMG_URLS[0], "1"),
DatasetItem(TEST_IMG_URLS[1], "2"),
DatasetItem(TEST_IMG_URLS[2], "3"),
DatasetItem(TEST_IMG_URLS[3], "4"),
]
LOCAL_FILENAME = "tests/test_img.jpg"
TEST_PREDS = [
BoxPrediction("[Pytest Box Prediction 1]", 0, 0, 100, 100, "1"),
BoxPrediction("[Pytest Box Prediction 2]", 0, 0, 100, 100, "2"),
BoxPrediction("[Pytest Box Prediction 3]", 0, 0, 100, 100, "3"),
BoxPrediction("[Pytest Box Prediction 4]", 0, 0, 100, 100, "4"),
]
def reference_id_from_url(url):
return Path(url).name
TEST_BOX_ANNOTATIONS = [
{
"label": f"[Pytest] Box Annotation ${i}",
"x": 50 + i * 10,
"y": 60 + i * 10,
"width": 70 + i * 10,
"height": 80 + i * 10,
"reference_id": reference_id_from_url(TEST_IMG_URLS[i]),
"annotation_id": f"[Pytest] Box Annotation Annotation Id{i}",
}
for i in range(len(TEST_IMG_URLS))
]
TEST_POLYGON_ANNOTATIONS = [
{
"label": f"[Pytest] Polygon Annotation ${i}",
"geometry": {
"vertices": [
{
"x": 50 + i * 10 + j,
"y": 60 + i * 10 + j,
}
for j in range(3)
],
},
"reference_id": reference_id_from_url(TEST_IMG_URLS[i]),
"annotation_id": f"[Pytest] Polygon Annotation Annotation Id{i}",
}
for i in range(len(TEST_IMG_URLS))
]
TEST_MASK_URL = "https://raw.githubusercontent.com/scaleapi/nucleus-python-client/master/tests/testdata/000000000285.png"
TEST_SEGMENTATION_ANNOTATIONS = [
{
"reference_id": reference_id_from_url(TEST_IMG_URLS[i]),
"annotation_id": f"[Pytest] Segmentation Annotation Id{i}",
"mask_url": TEST_MASK_URL,
"annotations": [
{"label": "bear", "index": 2},
{"label": "grass-merged", "index": 1},
],
}
for i in range(len(TEST_IMG_URLS))
]
TEST_SEGMENTATION_PREDICTIONS = TEST_SEGMENTATION_ANNOTATIONS
TEST_BOX_MODEL_PDF = {
box_annotation["label"]: 1 / len(TEST_BOX_ANNOTATIONS)
for box_annotation in TEST_BOX_ANNOTATIONS
}
TEST_POLYGON_MODEL_PDF = {
polygon_annotation["label"]: 1 / len(TEST_POLYGON_ANNOTATIONS)
for polygon_annotation in TEST_POLYGON_ANNOTATIONS
}
TEST_BOX_PREDICTIONS = [
{
**TEST_BOX_ANNOTATIONS[i],
"confidence": 0.10 * i,
"class_pdf": TEST_BOX_MODEL_PDF,
}
if i != 0
else {
**TEST_BOX_ANNOTATIONS[i],
"confidence": 0.10 * i,
}
for i in range(len(TEST_BOX_ANNOTATIONS))
]
TEST_POLYGON_PREDICTIONS = [
{
**TEST_POLYGON_ANNOTATIONS[i],
"confidence": 0.10 * i,
"class_pdf": TEST_POLYGON_MODEL_PDF,
}
if i != 0
else {
**TEST_POLYGON_ANNOTATIONS[i],
"confidence": 0.10 * i,
}
for i in range(len(TEST_POLYGON_ANNOTATIONS))
]
TEST_INDEX_EMBEDDINGS_FILE = "https://raw.githubusercontent.com/scaleapi/nucleus-python-client/master/tests/testdata/pytest_embeddings_payload.json"
# Asserts that a box annotation instance matches a dict representing its properties.
# Useful to check annotation uploads/updates match.
def assert_box_annotation_matches_dict(annotation_instance, annotation_dict):
assert annotation_instance.label == annotation_dict["label"]
assert annotation_instance.x == annotation_dict["x"]
assert annotation_instance.y == annotation_dict["y"]
assert annotation_instance.height == annotation_dict["height"]
assert annotation_instance.width == annotation_dict["width"]
assert (
annotation_instance.annotation_id == annotation_dict["annotation_id"]
)
def assert_polygon_annotation_matches_dict(
annotation_instance, annotation_dict
):
assert annotation_instance.label == annotation_dict["label"]
assert (
annotation_instance.annotation_id == annotation_dict["annotation_id"]
)
for instance_pt, dict_pt in zip(
annotation_instance.vertices, annotation_dict["geometry"]["vertices"]
):
assert instance_pt.x == dict_pt["x"]
assert instance_pt.y == dict_pt["y"]
def assert_segmentation_annotation_matches_dict(
annotation_instance, annotation_dict
):
assert annotation_instance.mask_url == annotation_dict["mask_url"]
assert (
annotation_instance.annotation_id == annotation_dict["annotation_id"]
)
# Cannot guarantee segments are in same order
assert len(annotation_instance.annotations) == len(
annotation_dict["annotations"]
)
for instance_segment, dict_segment in zip(
sorted(annotation_instance.annotations, key=lambda i: i.index),
sorted(annotation_dict["annotations"], key=lambda i: i["index"]),
):
assert instance_segment.index == dict_segment["index"]
assert instance_segment.label == dict_segment["label"]
# Asserts that a box prediction instance matches a dict representing its properties.
# Useful to check prediction uploads/updates match.
def assert_box_prediction_matches_dict(prediction_instance, prediction_dict):
assert_box_annotation_matches_dict(prediction_instance, prediction_dict)
assert prediction_instance.confidence == prediction_dict["confidence"]
def assert_polygon_prediction_matches_dict(
prediction_instance, prediction_dict
):
assert_polygon_annotation_matches_dict(
prediction_instance, prediction_dict
)
assert prediction_instance.confidence == prediction_dict["confidence"]