@@ -253,21 +253,53 @@ def test_read_write_annot():
253253
254254
255255def test_write_annot_fill_ctab ():
256-
257256 nvertices = 10
258257 nlabels = 3
259258 names = ['label {}' .format (l ) for l in range (1 , nlabels + 1 )]
260259 labels = list (range (nlabels )) + \
261260 list (np .random .randint (0 , nlabels , nvertices - nlabels ))
262261 labels = np .array (labels , dtype = np .int32 )
263262 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 )
265264 annot_path = 'c.annot'
266265
267266 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 ))
269301 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 ))
271303 assert np .all (np .isclose (labels2 , labels ))
272304 assert names2 == names
273305
0 commit comments