1
+ import pytest
2
+
3
+ from helpers import (
4
+ TEST_DATASET_NAME ,
5
+ TEST_MODEL_NAME ,
6
+ TEST_MODEL_REFERENCE ,
7
+ TEST_IMG_URLS ,
8
+ TEST_BOX_PREDICTIONS ,
9
+ TEST_POLYGON_PREDICTIONS ,
10
+ reference_id_from_url ,
11
+ assert_box_prediction_matches_dict ,
12
+ assert_polygon_prediction_matches_dict ,
13
+ )
14
+
15
+ from nucleus import BoxPrediction , PolygonPrediction , DatasetItem
16
+ from nucleus .constants import ERROR_PAYLOAD
17
+
18
+ @pytest .fixture ()
19
+ def dataset (CLIENT ):
20
+ ds = CLIENT .create_dataset (TEST_DATASET_NAME )
21
+ ds_items = []
22
+ for url in TEST_IMG_URLS :
23
+ ds_items .append (DatasetItem (
24
+ image_location = url ,
25
+ reference_id = reference_id_from_url (url ),
26
+ ))
27
+
28
+ response = ds .append (ds_items )
29
+ assert ERROR_PAYLOAD not in response .json ()
30
+
31
+ model = CLIENT .add_model (
32
+ name = TEST_MODEL_NAME ,
33
+ reference_id = TEST_MODEL_NAME
34
+ )
35
+
36
+
37
+ yield ds
38
+
39
+ response = CLIENT .delete_dataset (ds .id )
40
+ assert response == {}
41
+
42
+
43
+ def test_box_pred_upload (dataset ):
44
+ prediction = BoxPrediction (** TEST_BOX_PREDICTIONS [0 ])
45
+ response = dataset .annotate (predictions = [prediction ])
46
+
47
+ assert response ['dataset_id' ] == dataset .id
48
+ assert response ['predictions_processed' ] == 1
49
+
50
+ response = dataset .refloc (prediction .reference_id )['predictions' ]
51
+ assert len (response ) == 1
52
+ response_prediction = response [0 ]
53
+ assert_box_prediction_matches_dict (response_prediction , TEST_BOX_PREDICTIONS [0 ])
54
+
55
+
56
+ def test_polygon_pred_upload (dataset ):
57
+ prediction = PolygonPrediction (** TEST_POLYGON_PREDICTIONS [0 ])
58
+ response = dataset .annotate (predictions = [prediction ])
59
+
60
+ assert response ['dataset_id' ] == dataset .id
61
+ assert response ['predictions_processed' ] == 1
62
+
63
+ response = dataset .refloc (prediction .reference_id )['predictions' ]
64
+ assert len (response ) == 1
65
+ response_prediction = response [0 ]
66
+ print (response_prediction )
67
+ assert_polygon_prediction_matches_dict (response_prediction , TEST_POLYGON_PREDICTIONS [0 ])
68
+
69
+
70
+ def test_box_pred_upload_update (dataset ):
71
+ prediction = BoxPrediction (** TEST_BOX_PREDICTIONS [0 ])
72
+ response = dataset .annotate (predictions = [prediction ])
73
+
74
+ assert response ['predictions_processed' ] == 1
75
+
76
+ # Copy so we don't modify the original.
77
+ prediction_update_params = dict (TEST_BOX_PREDICTIONS [1 ])
78
+ prediction_update_params ['annotation_id' ] = TEST_BOX_PREDICTIONS [0 ]['annotation_id' ]
79
+ prediction_update_params ['reference_id' ] = TEST_BOX_PREDICTIONS [0 ]['reference_id' ]
80
+
81
+ prediction_update = BoxPrediction (** prediction_update_params )
82
+ response = dataset .annotate (predictions = [prediction_update ], update = True )
83
+
84
+ assert response ['predictions_processed' ] == 1
85
+
86
+ response = dataset .refloc (prediction .reference_id )['predictions' ]
87
+ assert len (response ) == 1
88
+ response_prediction = response [0 ]
89
+ assert_box_prediction_matches_dict (response_prediction , prediction_update_params )
90
+
91
+
92
+ def test_box_pred_upload_ignore (dataset ):
93
+ prediction = BoxPrediction (** TEST_BOX_PREDICTIONS [0 ])
94
+ response = dataset .annotate (predictions = [prediction ])
95
+
96
+ assert response ['predictions_processed' ] == 1
97
+
98
+ # Copy so we don't modify the original.
99
+ prediction_update_params = dict (TEST_BOX_PREDICTIONS [1 ])
100
+ prediction_update_params ['annotation_id' ] = TEST_BOX_PREDICTIONS [0 ]['annotation_id' ]
101
+ prediction_update_params ['reference_id' ] = TEST_BOX_PREDICTIONS [0 ]['reference_id' ]
102
+ prediction_update = BoxPrediction (** prediction_update_params )
103
+ # Default behavior is ignore.
104
+ response = dataset .annotate (predictions = [prediction_update ])
105
+
106
+ assert response ['predictions_processed' ] == 1
107
+
108
+ response = dataset .refloc (prediction .reference_id )['predictions' ]
109
+ assert len (response ) == 1
110
+ response_prediction = response [0 ]
111
+ assert_box_prediction_matches_dict (response_prediction , TEST_BOX_PREDICTIONS [0 ])
112
+
113
+
114
+ def test_polygon_pred_upload_update (dataset ):
115
+ prediction = PolygonPrediction (** TEST_POLYGON_PREDICTIONS [0 ])
116
+ response = dataset .annotate (predictions = [prediction ])
117
+
118
+ assert response ['predictions_processed' ] == 1
119
+
120
+ # Copy so we don't modify the original.
121
+ prediction_update_params = dict (TEST_POLYGON_PREDICTIONS [1 ])
122
+ prediction_update_params ['annotation_id' ] = TEST_POLYGON_PREDICTIONS [0 ]['annotation_id' ]
123
+ prediction_update_params ['reference_id' ] = TEST_POLYGON_PREDICTIONS [0 ]['reference_id' ]
124
+
125
+ prediction_update = PolygonPrediction (** prediction_update_params )
126
+ response = dataset .annotate (predictions = [prediction_update ], update = True )
127
+
128
+ assert response ['predictions_processed' ] == 1
129
+
130
+ response = dataset .refloc (prediction .reference_id )['predictions' ]
131
+ assert len (response ) == 1
132
+ response_prediction = response [0 ]
133
+ assert_polygon_prediction_matches_dict (response_prediction , prediction_update_params )
134
+
135
+
136
+ def test_polygon_pred_upload_ignore (dataset ):
137
+ prediction = PolygonPrediction (** TEST_POLYGON_PREDICTIONS [0 ])
138
+ response = dataset .annotate (predictions = [prediction ])
139
+
140
+ assert response ['predictions_processed' ] == 1
141
+
142
+ # Copy so we don't modify the original.
143
+ prediction_update_params = dict (TEST_POLYGON_PREDICTIONS [1 ])
144
+ prediction_update_params ['annotation_id' ] = TEST_POLYGON_PREDICTIONS [0 ]['annotation_id' ]
145
+ prediction_update_params ['reference_id' ] = TEST_POLYGON_PREDICTIONS [0 ]['reference_id' ]
146
+
147
+ prediction_update = PolygonPrediction (** prediction_update_params )
148
+ # Default behavior is ignore.
149
+ response = dataset .annotate (predictions = [prediction_update ])
150
+
151
+ assert response ['predictions_processed' ] == 1
152
+
153
+ response = dataset .refloc (prediction .reference_id )['predictions' ]
154
+ assert len (response ) == 1
155
+ response_prediction = response [0 ]
156
+ assert_polygon_prediction_matches_dict (response_prediction , TEST_POLYGON_PREDICTIONS [0 ])
0 commit comments