def test_text_manager_format(): n_text = 3 text = 'confidence: {confidence:.2f}' classes = np.array(['A', 'B', 'C']) properties = {'class': classes, 'confidence': np.array([0.5, 0.3, 1])} expected_text = np.array( ['confidence: 0.50', 'confidence: 0.30', 'confidence: 1.00'] ) text_manager = TextManager(text=text, n_text=n_text, properties=properties) np.testing.assert_equal(text_manager.values, expected_text) assert text_manager._mode == TextMode.FORMATTED # add new text with properties new_properties = {'class': np.array(['A']), 'confidence': np.array([0.5])} text_manager.add(new_properties, 1) expected_text_2 = np.concatenate([expected_text, ['confidence: 0.50']]) np.testing.assert_equal(text_manager.values, expected_text_2) # test getting the text elements when there are none in view text_view = text_manager.view_text([]) np.testing.assert_equal(text_view, ['']) # test getting the text elements when the first two elements are in view text_view = text_manager.view_text([0, 1]) np.testing.assert_equal(text_view, expected_text_2[0:2]) text_manager.anchor = 'center' coords = np.array([[0, 0], [10, 10], [20, 20]]) text_coords = text_manager.compute_text_coords(coords, ndisplay=3) np.testing.assert_equal(text_coords, (coords, 'center', 'center')) # remove the first text element text_manager.remove({0}) np.testing.assert_equal(text_manager.values, expected_text_2[1::])
def test_view_text_with_constant_text(): features = pd.DataFrame(index=range(3)) text_manager = TextManager(string={'constant': 'A'}, features=features) copied = text_manager._copy([0, 2]) text_manager._paste(**copied) actual = text_manager.view_text([0, 1]) # view_text promises to return an Nx1 array, not just something # broadcastable to an Nx1, so explicitly check the length # because assert_array_equal broadcasts scalars automatically assert len(actual) == 2 np.testing.assert_array_equal(actual, ['A', 'A'])