def test_validate_depends_on(self, backend): comp = backend.module.Compilation(schema.Compilation({ 'time': schema.Array(dtype='float'), 'voltage': schema.Array(dtype='float', depends_on='time'), }), parent=None, new_params=backend.new_params) comp.time.value = np.array([0, 1, 2], dtype='float') comp.voltage.value = np.array([2, 3, 5], dtype='float') comp.voltage.validate()
def test_validate_fail_dtype(self): node = schema.Array(dtype='int8') with pytest.raises(ValidationError) as err: node.validate(np.array([23., 42]), None) assert err.value.message == 'Invalid dtype.' assert err.value.expected == 'int8' assert err.value.got == 'float'
def test_validate_fail_max_shape(self): node = schema.Array(dtype='int32', max_shape=(3, )) with pytest.raises(ValidationError) as err: node.validate(np.array([1, 2, 3, 4], dtype='int32'), None) assert err.value.message == 'Maximum array shape exceeded.' assert err.value.expected == (3, ) assert err.value.got == (4, )
def test_validate_fail_min_value(self): node = schema.Array(dtype='int32', min_value=23) with pytest.raises(ValidationError) as err: node.validate(np.array([22, 42], dtype='int32'), None) assert err.value.message == 'Minimum array element value undercut.' assert err.value.expected == 23 assert err.value.got == np.array([22])
def test_validate_fail_ndim(self): node = schema.Array(dtype='int32', ndim=1) with pytest.raises(ValidationError) as err: node.validate(np.array([[1, 2], [3, 4]], dtype='int32'), None) assert err.value.message == 'Invalid number of array dimensions.' assert err.value.expected == 1 assert err.value.got == 2
def test_validate_fail_max_value(self): node = schema.Array(dtype='int32', max_value=42) with pytest.raises(ValidationError) as err: node.validate(np.array([23, 43], dtype='int32'), None) assert err.value.message == 'Maximum array element value exceeded.' assert err.value.expected == 42 assert err.value.got == np.array([43])
def test_validate_fail_depends_indep_ndim(self): node = schema.Array(dtype='int32', ndim=1, depends_on=('spam')) spam = np.array([[1, 2], [3, 4]], dtype='int32') with pytest.raises(ValueError) as err: node.validate(np.array([23, 42], dtype='int32'), [spam]) assert err.value.args[0] == ('Independent variable array must be one-' 'dimensional.')
def test_validate_fail_min_shape(self): node = schema.Array(dtype='int32', min_shape=(3, 1)) with pytest.raises(ValidationError) as err: node.validate(np.array([[1, 2], [3, 4]], dtype='int32'), None) assert err.value.message == 'Minimum array shape undercut.' assert err.value.expected == (3, 1) assert err.value.got == (2, 2)
def test_init_defaults(self): node = schema.Array(dtype='int32') assert node.unit == '' assert node.ndim == 1 assert node.max_shape is None assert node.min_shape is None assert node.max_value is None assert node.min_value is None assert node.depends_on is None
def test_validate_fail_depends(self): node = schema.Array(dtype='int32', ndim=2, depends_on=('spam', 'eggs')) spam = np.array([1, 2], dtype='int32') eggs = np.array([0, 1, 2, 3], dtype='int32') with pytest.raises(ValidationError) as err: node.validate(np.array([[1, 2, 3], [4, 5, 6]], dtype='int32'), [spam, eggs]) assert err.value.message == 'Dependent array size mismatch.' assert err.value.expected == 4 assert err.value.got == 3
def test_validate(self): node = schema.Array(dtype='int32', ndim=2, max_shape=(2, 4), min_shape=(1, 3), max_value=8, min_value=1, depends_on=('spam', 'eggs')) spam = np.array([1, 2], dtype='int32') eggs = np.array([0, 1, 2, 3], dtype='int32') node.validate(np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype='int32'), [spam, eggs])
def test_init(self): node = schema.Array(dtype='float', unit='V', max_shape=(3, 2), min_shape=(1, 1), ndim=2, max_value=42, min_value=23, depends_on=('spam', 'eggs')) assert node.dtype == 'float' assert node.unit == 'V' assert node.max_shape == (3, 2) assert node.min_shape == (1, 1) assert node.ndim == 2 assert node.max_value == 42 assert node.min_value == 23 assert node.depends_on == ('spam', 'eggs')
class TestArray(ItemNodeTestBase): class_name = 'Array' schema_node = schema.Array(dtype='int32') valid_data = np.array([23, 42], dtype='int32') def test_getitem(self, data_node): data_node.value = self.valid_data for idx, item in enumerate(self.valid_data): assert data_node[idx] == item assert np.array_equal(data_node[()], self.valid_data) def test_resize(self, data_node): data_node.value = np.array([42]) assert data_node.value.shape == (1, ) data_node.resize((5, )) assert data_node.value.ndim == 1 assert data_node.value.shape == (5, ) def test_setitem(self, data_node): data_node.value = np.array([5, 23, 42]) data_node[0] = 1 assert np.array_equal(data_node.value, np.array([1, 23, 42])) data_node[1:] = np.array([2, 3]) assert np.array_equal(data_node.value, np.array([1, 2, 3])) data_node[()] = np.array([42, 23, 5]) assert np.array_equal(data_node.value, np.array([42, 23, 5])) def test_validate_depends_on(self, backend): comp = backend.module.Compilation(schema.Compilation({ 'time': schema.Array(dtype='float'), 'voltage': schema.Array(dtype='float', depends_on='time'), }), parent=None, new_params=backend.new_params) comp.time.value = np.array([0, 1, 2], dtype='float') comp.voltage.value = np.array([2, 3, 5], dtype='float') comp.voltage.validate()
def test_to_dict(self): node = schema.Array(dtype='int32', unit='V', max_shape=(3, 2), min_shape=(1, 1), max_value=42, min_value=23, depends_on=('spam', 'eggs')) node_dict = node.to_dict() assert 'node_type' in node_dict assert node_dict['node_type'] == 'Array' assert 'config' in node_dict assert node_dict['config'] == { 'dtype': 'int32', 'unit': 'V', 'max_shape': (3, 2), 'min_shape': (1, 1), 'ndim': 2, 'max_value': 42, 'min_value': 23, 'depends_on': ('spam', 'eggs') }
def test_validate_fail_type(self, test_data): node = schema.Array(dtype='int32') with pytest.raises(ValidationError) as err: node.validate(test_data, None) assert err.value.message == 'Invalid type/value.' assert err.value.expected == 'numpy.ndarray'
def test_init_defaults_min_shape(self): node = schema.Array(dtype='int32', min_shape=(2, 1)) assert node.ndim == 2 assert node.min_shape == (2, 1)
def test_init_fail_shapes(self): with pytest.raises(ValueError) as err: schema.Array(dtype='int32', max_shape=(3, 2), min_shape=(2, )) assert err.value.args[0] == ('Shape constraints must have the same ' 'length.')
def test_init_fail_depends_on(self): with pytest.raises(ValueError) as err: schema.Array(dtype='int32', ndim=1, depends_on=('spam', 'eggs')) assert err.value.args[0] == ('Number of independent variables must ' 'be equal to the number of array ' 'dimensions.')
import datetime import json import numpy as np import pytest from dsch import data, schema from dsch.backends import npz @pytest.mark.parametrize('schema_node,valid_data', ( (schema.Array(dtype='int32'), np.array([23, 42], dtype='int32')), (schema.Bool(), True), (schema.Date(), datetime.date.today()), (schema.DateTime(), datetime.datetime.now()), (schema.Scalar(dtype='int32'), np.int32(42)), (schema.String(), 'spam'), (schema.Time(), datetime.datetime.now().time()), )) def test_save_item_node(schema_node, valid_data): data_node = data.data_node_from_schema(schema_node, module_name='dsch.backends.npz', parent=None) data_node.value = valid_data assert np.all(data_node.save() == data_node._storage) class TestCompilation: def test_init_from_storage(self): schema_node = schema.Compilation({'spam': schema.Bool(), 'eggs': schema.Bool()})
import datetime import json import numpy as np import pytest from dsch import schema from dsch.exceptions import ValidationError @pytest.mark.parametrize( 'node', (schema.Array(dtype='int32'), schema.Bool(), schema.Bytes(), schema.Compilation({'spam': schema.Bool()}), schema.Date(), schema.DateTime(), schema.List(schema.Bool()), schema.Scalar(dtype='int32'), schema.String(), schema.Time())) class TestGenericSchemaNode: def test_to_json(self, node): json_str = node.to_json() node_dict = json.loads(json_str) assert isinstance(node_dict, dict) def test_hash(self, node): hash = node.hash() assert len(hash) == 64 @pytest.mark.parametrize('node1, node2', ( (schema.Array(dtype='float'), schema.Array(dtype='float')), (schema.Bool(), schema.Bool()), (schema.Bytes(), schema.Bytes()), (schema.Compilation({'spam': schema.Bool()