def test_get_field_normal(self): contours = [np.zeros([4, 2]), np.zeros([4, 2]), np.zeros([4, 2])] polygons = Polygons(contours) num_points = polygons.get_field('num_points') num_points[0] = 1 self.assertEqual(num_points, [1, 4, 4]) self.assertEqual(polygons.get_field('num_points'), [4, 4, 4])
def test_get_field_numpy(self): contours = [np.zeros([4, 2])] polygons = Polygons(contours) np_field = [np.zeros([4])] polygons.add_field('np_field', np_field) np_field_modified = polygons.get_field('np_field') np_field_modified[0][0] = 1 np.testing.assert_equal(np_field_modified, np.array([[1, 0, 0, 0]])) np_field_original = polygons.get_field('np_field') np.testing.assert_equal(np_field_original, np.array([[0, 0, 0, 0]]))
def test_get(self): contours = [ np.array([[2., 1.], [1., 3.], [3., 5.], [4., 2.]]), np.array([[0.5, 0.5], [10.5, 0.5], [10.5, 10.5]]), np.array([[10, 0], [10, 20], [0, 10], [20, 10], [20, 20]]) ] polygons = Polygons(contours) polygons.add_field('training_tag', [True, False, True]) polygon = polygons.get(1) np.testing.assert_equal( polygon['contour'], np.array([[0.5, 0.5], [10.5, 0.5], [10.5, 10.5]])) self.assertEqual(polygon['fields']['training_tag'], False) self.assertEqual(polygon['fields']['num_points'], 3) polygon['fields']['training_tag'] = True polygon['contour'] = np.array([[10, 0], [10, 20], [0, 10], [20, 10], [20, 20]]) np.testing.assert_equal(polygons.get_contours(), [ np.array([[2., 1.], [1., 3.], [3., 5.], [4., 2.]]), np.array([[0.5, 0.5], [10.5, 0.5], [10.5, 10.5]]), np.array([[10, 0], [10, 20], [0, 10], [20, 10], [20, 20]]) ]) self.assertEqual(polygons.get_field('training_tag'), [True, False, True])
def test_fliter_small(self): contours = [ np.array([[2., 1.], [1., 3.], [3., 5.], [4., 2.]]), np.array([[0.5, 0.5], [10.5, 0.5], [10.5, 10.5]]), np.array([[10, 0], [10, 20], [0, 10], [20, 10], [20, 20]]) ] polygons = Polygons(contours) polygons.fliter_small(10) np.testing.assert_equal(polygons.get_field('training_tag'), np.array([False, True, True])) np.testing.assert_equal(polygons.get_contours(), contours)
def test_fliter_out(self): contours = [ np.array([[20, 10], [10, 30], [30, 50], [40, 20]]), np.array([[0, 0], [10, 0], [10, 10]]), np.array([[10, 0], [10, 20], [0, 10], [20, 10], [20, 20]]) ] polygons = Polygons(contours) window = np.array([[0, 0], [20, 20]]) polygons.fliter_out(window) np.testing.assert_equal(polygons.get_field('training_tag'), np.array([False, True, False])) np.testing.assert_equal(polygons.get_contours(), contours)
def test_copy(self): contours = [np.zeros([4, 2]), np.zeros([4, 2]), np.zeros([4, 2])] polygons = Polygons(contours) field = ['s', 's', 's'] polygons.add_field('field', field) p = polygons.copy() np.testing.assert_equal(p.get_contours(), contours) self.assertEqual(p.get_field('num_points'), [4, 4, 4]) self.assertEqual(p.get_field('field'), ['s', 's', 's']) p.set_contours([np.ones([4, 2]), np.ones([4, 2]), np.ones([4, 2])]) p.set_field('field', ['w', 'w', 'w']) np.testing.assert_equal(polygons.get_contours(), contours) self.assertEqual(polygons.get_field('field'), ['s', 's', 's'])