@@ -253,21 +253,53 @@ def test_read_write_annot():
253
253
254
254
255
255
def test_write_annot_fill_ctab ():
256
-
257
256
nvertices = 10
258
257
nlabels = 3
259
258
names = ['label {}' .format (l ) for l in range (1 , nlabels + 1 )]
260
259
labels = list (range (nlabels )) + \
261
260
list (np .random .randint (0 , nlabels , nvertices - nlabels ))
262
261
labels = np .array (labels , dtype = np .int32 )
263
262
np .random .shuffle (labels )
264
- rgbal = np .array (np .random .randint (0 , 255 , (nlabels , 4 )), dtype = np .int32 )
263
+ rgba = np .array (np .random .randint (0 , 255 , (nlabels , 4 )), dtype = np .int32 )
265
264
annot_path = 'c.annot'
266
265
267
266
with InTemporaryDirectory ():
268
- write_annot (annot_path , labels , rgbal , names , fill_ctab = True )
267
+ write_annot (annot_path , labels , rgba , names , fill_ctab = True )
268
+ labels2 , rgbal2 , names2 = read_annot (annot_path )
269
+ assert np .all (np .isclose (rgbal2 [:, :4 ], rgba ))
270
+ assert np .all (np .isclose (labels2 , labels ))
271
+ assert names2 == names
272
+
273
+ # make sure a warning is emitted if fill_ctab is False, and the
274
+ # annotation values are wrong. Use orig_ids=True so we get those bad
275
+ # values back.
276
+ badannot = (10 * np .arange (nlabels , dtype = np .int32 )).reshape (- 1 , 1 )
277
+ rgbal = np .hstack ((rgba , badannot ))
278
+ print (labels )
279
+ with clear_and_catch_warnings () as w :
280
+ write_annot (annot_path , labels , rgbal , names , fill_ctab = False )
281
+ assert_true (
282
+ any ('Annotation values in {} will be incorrect' .format (
283
+ annot_path ) == str (ww .message ) for ww in w ))
284
+ labels2 , rgbal2 , names2 = read_annot (annot_path , orig_ids = True )
285
+ assert np .all (np .isclose (rgbal2 [:, :4 ], rgba ))
286
+ assert np .all (np .isclose (labels2 , badannot [labels ].squeeze ()))
287
+ assert names2 == names
288
+
289
+ # make sure a warning is *not* emitted if fill_ctab is False, but the
290
+ # annotation values are correct.
291
+ rgbal = np .hstack ((rgba , np .zeros ((nlabels , 1 ), dtype = np .int32 )))
292
+ rgbal [:, 4 ] = (rgbal [:, 0 ] +
293
+ rgbal [:, 1 ] * (2 ** 8 ) +
294
+ rgbal [:, 2 ] * (2 ** 16 ) +
295
+ rgbal [:, 3 ] * (2 ** 24 ))
296
+ with clear_and_catch_warnings () as w :
297
+ write_annot (annot_path , labels , rgbal , names , fill_ctab = False )
298
+ assert_true (
299
+ not any ('Annotation values in {} will be incorrect' .format (
300
+ annot_path ) == str (ww .message ) for ww in w ))
269
301
labels2 , rgbal2 , names2 = read_annot (annot_path )
270
- assert np .all (np .isclose (rgbal2 [:, :4 ], rgbal ))
302
+ assert np .all (np .isclose (rgbal2 [:, :4 ], rgba ))
271
303
assert np .all (np .isclose (labels2 , labels ))
272
304
assert names2 == names
273
305
0 commit comments