def test_vector2array(self): class HasVec2Arr(properties.HasProperties): vec2 = properties.Vector2Array('simple vector array') hv2 = HasVec2Arr() hv2.vec2 = np.array([[1., 2.]]) assert isinstance(hv2.vec2, vmath.Vector2Array) hv2.vec2 = ['east', 'south', [1., 1.]] assert np.allclose(hv2.vec2, [[1., 0.], [0., -1.], [1., 1.]]) hv2.vec2 = [1., 2.] assert hv2.vec2.shape == (1, 2) with self.assertRaises(ValueError): hv2.vec2 = 'east' with self.assertRaises(ValueError): hv2.vec2 = ['diagonal'] with self.assertRaises(ValueError): hv2.vec2 = [[1., 2., 3.]] class HasLenVec2Arr(properties.HasProperties): vec2 = properties.Vector2Array('length 5 vector', length=5) hv2 = HasLenVec2Arr() hv2.vec2 = [[0., 1.], [1., 0.]] assert np.allclose(hv2.vec2, [[0., 5.], [5., 0.]]) assert isinstance( properties.Vector2Array.from_json([[5., 6.], [7., 8.]]), vmath.Vector2Array) assert properties.Vector2Array('').equal( vmath.Vector2Array([[5., 6.], [7., 8.]]), vmath.Vector2Array([[5., 6.], [7., 8.]])) assert not properties.Vector2Array('').equal( vmath.Vector2Array([[5., 6.], [7., 8.]]), np.array([[5., 6.], [7., 8.]])) with self.assertRaises(TypeError): properties.Vector2Array('', shape=(2, )) with self.assertRaises(TypeError): properties.Vector2Array('', shape=('*', '*')) class HasShapeVec2Arr(properties.HasProperties): vec2 = properties.Vector2Array('', shape={(2, 2), (3, 2)}) hv2 = HasShapeVec2Arr(vec2=[[1., 2.], [3., 4.]]) hv2.vec2 = [[1., 2.], [3., 4.], [5., 6.]] with self.assertRaises(ValueError): hv2.vec2 = [[1., 2.], [3., 4.], [5., 6.], [7., 8.]]
def from_json(value, **kwargs): return vmath.Vector2Array(value)