def test_minimal_inputs(self): """ test minimum input requirement of an array like object """ with self.assertRaises(TypeError): Dataset.from_array() descriptor = Dataset.from_array(np.arange(3)) validate_dataset_properties(self, descriptor, np.arange(3))
def test_minimal_inputs(self): """ test minimum input requirement of an array like object """ with self.assertRaises(TypeError): Dataset.from_array() descriptor = Dataset.from_array(np.arange(3)) self.assertIsInstance(descriptor, Dataset)
def test__equ__(self): values = np.zeros([4, 5]) descriptor1 = Dataset.from_array(values) descriptor2 = Dataset.from_array(values) # TODO: why does direct comparison not work self.assertTrue(descriptor1.__eq__(descriptor2)) self.assertFalse(descriptor1.__eq__(np.arange(4))) descriptor1.set_dimension( 0, Dimension(np.arange(4), 'x', quantity='test', dimension_type='spatial')) self.assertFalse(descriptor1.__eq__(descriptor2)) descriptor2.modality = 'nix' self.assertFalse(descriptor1.__eq__(descriptor2)) descriptor2.data_type = 'image' self.assertFalse(descriptor1.__eq__(descriptor2)) descriptor2.source = 'image' self.assertFalse(descriptor1.__eq__(descriptor2)) descriptor2.quantity = 'image' self.assertFalse(descriptor1.__eq__(descriptor2)) descriptor2.units = 'image' self.assertFalse(descriptor1.__eq__(descriptor2))
def test_fully_configured(self): values = np.arange(5) descriptor = Dataset.from_array(values) for att in generic_attributes: setattr(descriptor, att, 'test') descriptor.metadata = {0: 'test'} actual = '{}'.format(descriptor) out = 'test' da_array = da.from_array(values, chunks='auto', name='generic') expected = 'sidpy.Dataset of type {} with:\n '.format( DataTypes.UNKNOWN.name) expected = expected + '{}'.format(da_array) expected = expected + '\n data contains: {} ({})'.format(out, out) expected = expected + '\n and Dimensions: ' expected = expected + '\n {}: {} ({}) of size {}'.format( 'a', 'generic', 'generic', values.shape) expected = expected + '\n with metadata: {}'.format([0]) """ for exp, act in zip(expected.split('\n'), actual.split('\n')): print('Expected:\t' + exp) print('Actual:\t' + act) print(exp == act) """ self.assertEqual(actual, expected)
def test_minimal_inputs(self): values = np.random.random([4, 5]) dataset = Dataset.from_array(values) descriptor = dataset.copy() self.assertIsInstance(descriptor, Dataset) for expected, actual in zip(dataset, descriptor): self.assertTrue(np.all([x == y for x, y in zip(expected, actual)])) self.assertTrue( np.all([hasattr(descriptor, att) for att in generic_attributes])) self.assertTrue( np.all([ getattr(descriptor, att) == 'generic' for att in generic_attributes ])) self.assertEqual(descriptor.data_type, DataType.UNKNOWN) self.assertEqual(descriptor.metadata, {}) self.assertEqual(descriptor.original_metadata, {}) for dim in range(len(values.shape)): self.assertEqual(getattr(descriptor, string.ascii_lowercase[dim]), getattr(descriptor, 'dim_{}'.format(dim))) self.assertFalse( hasattr(descriptor, 'dim_{}'.format(len(dataset.shape)))) self.assertFalse( hasattr(descriptor, string.ascii_lowercase[len(dataset.shape)]))
def test_minimal_inputs(self): values = np.ones([4, 5]) source_dset = Dataset.from_array(values) values = np.zeros([4, 5]) descriptor = source_dset.like_data(values) self.assertTrue(descriptor.shape == values.shape) self.assertIsInstance(descriptor, Dataset)
def test_std_inputs(self): # verify generic properties, dimensions, etc. values = np.random.random([4, 5, 6]) descriptor = Dataset.from_array(values) for expected, actual in zip(values, descriptor): self.assertTrue(np.all([x == y for x, y in zip(expected, actual)])) self.assertTrue( np.all([hasattr(descriptor, att) for att in generic_attributes])) self.assertTrue( np.all([ getattr(descriptor, att) == 'generic' for att in generic_attributes ])) self.assertEqual(descriptor.data_type, DataTypes.UNKNOWN) self.assertEqual(descriptor.metadata, {}) self.assertEqual(descriptor.original_metadata, {}) for dim in range(len(values.shape)): self.assertEqual(getattr(descriptor, string.ascii_lowercase[dim]), getattr(descriptor, 'dim_{}'.format(dim))) self.assertFalse( hasattr(descriptor, 'dim_{}'.format(len(values.shape)))) self.assertFalse( hasattr(descriptor, string.ascii_lowercase[len(values.shape)]))
def test_unknown_data_type(self): values = np.random.random([4]) descriptor = Dataset.from_array(values) expected = "Supported data_types for plotting are only:" with self.assertRaises(Warning) as context: descriptor.data_type = 'quark' self.assertTrue(expected in str(context.exception))
def test_invalid_main_types(self): """ anything that is not recognized by dask will make an empty dask array but name has to be a string """ # TODO: call validate_dataset_properties instead descriptor = Dataset.from_array(DataTypes.UNKNOWN) self.assertEqual(descriptor.shape, ()) descriptor = Dataset.from_array('test') self.assertEqual(descriptor.shape, ()) descriptor = Dataset.from_array(1) self.assertEqual(descriptor.shape, ()) with self.assertRaises(ValueError): Dataset.from_array(1, 1)
def test_changing_size(self): values = np.ones([4, 5]) source_dset = Dataset.from_array(values) source_dset.a *= 0.5 source_dset.quantity = 'test' values = np.zeros([3, 5]) descriptor = source_dset.like_data(values) self.assertEqual(descriptor.a, np.arange(3) * .5)
def test_invalid_dim_object(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) with self.assertRaises(TypeError): descriptor.set_dimension(3, "New dimension") with self.assertRaises(TypeError): descriptor.set_dimension('2', {'x': np.arange(4)}) with self.assertRaises(TypeError): descriptor.set_dimension(2, np.arange(4))
def test_all_customized_properties(self): values = np.random.random([4, 5]) dataset = Dataset.from_array(values) dataset.rename_dimension(0, 'x') dataset.quantity = 'test' descriptor = dataset.copy() self.assertIsInstance(descriptor, Dataset) self.assertEqual(descriptor.quantity, dataset.quantity) self.assertTrue(hasattr(descriptor, 'a'))
def test_changing_size(self): values = np.ones([4, 5]) source_dset = Dataset.from_array(values) source_dset.a *= 0.5 source_dset.quantity = 'test' values = np.zeros([3, 5]) descriptor = source_dset.like_data(values) # self.assertEqual(descriptor.a.values), np.arange(3)*.5) expected = descriptor.a.values actual = np.arange(3) * .5 self.assertTrue(np.all([x == y for x, y in zip(expected, actual)]))
def test_4d_main_data(self): values = np.random.random([4, 5, 7, 3]) descriptor = Dataset.from_array(values) for expected, actual in zip(values, descriptor): self.assertTrue(np.all([x == y for x, y in zip(expected, actual)])) for dim in range(len(values.shape)): self.assertEqual(getattr(descriptor, string.ascii_lowercase[dim]), getattr(descriptor, 'dim_{}'.format(dim))) self.assertFalse( hasattr(descriptor, 'dim_{}'.format(len(values.shape)))) self.assertFalse( hasattr(descriptor, string.ascii_lowercase[len(values.shape)]))
def test_1d_main_data(self): values = np.ones([10]) descriptor = Dataset.from_array(values) self.assertTrue(np.all([x == y for x, y in zip(values, descriptor)])) # TODO: call validate_dataset_properties instead # Move such validation to validate_dataset_properties for dim in range(len(values.shape)): self.assertEqual(getattr(descriptor, string.ascii_lowercase[dim]), getattr(descriptor, 'dim_{}'.format(dim))) self.assertFalse( hasattr(descriptor, 'dim_{}'.format(len(values.shape)))) self.assertFalse( hasattr(descriptor, string.ascii_lowercase[len(values.shape)]))
def test_user_defined_parms(self): descriptor = Dataset.from_array(np.arange(3), name='test') for att in generic_attributes: setattr(descriptor, att, 'test') self.assertTrue( np.all([ getattr(descriptor, att) == 'test' for att in generic_attributes ])) test_dict = {0: 'test'} descriptor.metadata = test_dict.copy() descriptor.original_metadata = test_dict.copy() self.assertEqual(descriptor.metadata, test_dict) self.assertEqual(descriptor.original_metadata, test_dict)
def test_get_image_dims(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) descriptor.set_dimension( 0, Dimension(np.arange(4), 'x', quantity='test', dimension_type='spatial')) image_dims = descriptor.get_image_dims() self.assertEqual(len(image_dims), 1) self.assertEqual(image_dims[0], 0) descriptor.dim_1.dimension_type = 'spatial' image_dims = descriptor.get_image_dims() self.assertEqual(len(image_dims), 2) self.assertEqual(image_dims[1], 1)
def test_get_extent(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) descriptor.set_dimension( 0, Dimension(np.arange(4), 'x', quantity='test', dimension_type='spatial')) descriptor.dim_1.dimension_type = 'spatial' descriptor.set_dimension( 0, Dimension(np.arange(4), 'x', quantity='test', dimension_type='spatial')) extent = descriptor.get_extent([0, 1]) self.assertEqual(extent[0], -0.5) self.assertEqual(extent[1], 3.5)
def test_all_customized_properties(self): values = np.ones([4, 5]) source_dset = Dataset.from_array(values) for att in generic_attributes: setattr(source_dset, att, 'test') source_dset.metadata = {0: 'test'} values = np.zeros([4, 5]) descriptor = source_dset.like_data(values) self.assertEqual(descriptor.title, 'like test') descriptor.title = 'test' self.assertTrue( np.all([ getattr(descriptor, att) == 'test' for att in generic_attributes ])) self.assertEqual(descriptor.metadata, source_dset.metadata) self.assertEqual(descriptor.original_metadata, source_dset.original_metadata)
def test_user_defined_parms(self): descriptor = Dataset.from_array(np.arange(3), name='test') for att in generic_attributes: setattr(descriptor, att, 'test') test_dict = {0: 'test'} descriptor.metadata = test_dict.copy() descriptor.original_metadata = test_dict.copy() validate_dataset_properties(self, descriptor, np.arange(3), name='test', title='test', quantity='test', units='test', modality='test', source='test', dimension_dict=None, data_type=DataType.UNKNOWN, metadata=test_dict, original_metadata=test_dict)
def test_minimal_inputs(self): values = np.arange(5) descriptor = Dataset.from_array(values) actual = '{}'.format(descriptor) out = 'generic' da_array = da.from_array(values, chunks='auto', name='generic') expected = 'sidpy.Dataset of type {} with:\n '.format( DataTypes.UNKNOWN.name) expected = expected + '{}'.format(da_array) expected = expected + '\n data contains: {} ({})'.format(out, out) expected = expected + '\n and Dimensions: ' expected = expected + '\n {}: {} ({}) of size {}'.format( 'a', out, out, values.shape) """ for exp, act in zip(expected.split('\n'), actual.split('\n')): print('Expected:\t' + exp) print('Actual:\t' + act) print(exp == act) """ self.assertEqual(actual, expected)
def test_all_inputs(self): descriptor = Dataset.from_array(np.arange(3), name='test') self.assertEqual(descriptor.title, 'test')
def test_entered_metadata(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) descriptor.metadata = {0: 'test'}
def test_minimal_inputs(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) # Just the main dataset pass
def test_default_empty_metadata(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values)
def test_valid_index_and_name(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) descriptor.rename_dimension(0, 'v') self.assertEqual(descriptor.v, descriptor.dim_0)
def test_valid_index_and_dim_obj(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) descriptor.set_dimension( 0, Dimension(np.arange(4), 'x', quantity='test', units='test')) self.assertIsInstance(descriptor.x, Dimension)
def test_existing_name(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) with self.assertRaises(ValueError): descriptor.rename_dimension(0, 'b')
def test_invalid_name_object_types(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) with self.assertRaises(TypeError): descriptor.rename_dimension(0, 1)
def test_index_out_of_bounds(self): values = np.zeros([4, 5]) descriptor = Dataset.from_array(values) with self.assertRaises(KeyError): descriptor.rename_dimension(3, 'v')