def test_nonposint_values(self): vals = [-1, []] expected = 2*["values should at least be specified as a positive integer"] for v, e in zip(vals, expected): with self.assertRaises(TypeError) as context: _ = Dimension(v, "x") self.assertTrue(e in str(context.exception))
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_conv2arr_values(self): arr = np.arange(5) vals = [5, arr, arr.tolist(), tuple(arr)] vals_expected = arr for v in vals: dim = Dimension(v, "x") self.assertIsInstance(dim, Dimension) assert_array_equal(np.array(dim), vals_expected)
def test_add(self): name = 'Bias' units = 'V' values = np.arange(5) descriptor = Dimension(values, name, units=units) descriptor = descriptor + 3. self.assertIsInstance(descriptor, Dimension)
def test_conv2arr_values(self): arr = np.arange(5) vals = [5, arr, arr.tolist(), tuple(arr)] vals_expected = arr for v in vals: dim = Dimension("x", v) self.assertIsInstance(dim.values, np.ndarray) assert_array_equal(dim.values, vals_expected)
def test_values_as_array(self): name = 'Bias' values = np.random.rand(5) descriptor = Dimension(name, values) for expected, actual in zip([name, values], [descriptor.name, descriptor.values]): self.assertTrue(np.all([x == y for x, y in zip(expected, actual)]))
def test_unknown_dimension_type(self): dim_type = "bad_name" expected_wrn = ["Supported dimension types for plotting are only: [", "Setting DimensionType to UNKNOWN"] with warnings.catch_warnings(record=True) as w: _ = Dimension(5, "x", dimension_type=dim_type) self.assertTrue(expected_wrn[0] in str(w[0].message)) self.assertTrue(expected_wrn[1] in str(w[1].message))
def test_dimensionality(self): vals = np.ones((2, 2)) expected = 'Dimension can only be 1 dimensional' with self.assertRaises(Exception) as context: _ = Dimension( vals, "x", ) self.assertTrue(expected in str(context.exception))
def test_copy(self): name = 'Bias' units = 'V' values = np.arange(5) descriptor = Dimension(values, name, units=units) copy_descriptor = descriptor.copy() for expected, actual in zip([copy_descriptor.name, copy_descriptor.units], [descriptor.name, descriptor.units]): self.assertTrue(np.all([x == y for x, y in zip(expected, actual)])) self.assertTrue(np.allclose(copy_descriptor.values, descriptor.values)) copy_descriptor.units = 'eV' copy_descriptor.name = 'energy' for expected, actual in zip([copy_descriptor.name, copy_descriptor.units], [descriptor.name, descriptor.units]): self.assertTrue(np.all([x != y for x, y in zip(expected, actual)])) copy_descriptor = descriptor +1 self.assertFalse(np.allclose(copy_descriptor.values, descriptor.values))
def test_repr(self): name = 'Bias' values = np.arange(5) descriptor = Dimension(values, name) actual = '{}'.format(descriptor) quantity = 'generic' units = 'generic' expected = '{}: {} ({}) of size {}'.format(name, quantity, units, values.shape) self.assertEqual(actual, expected)
def test_values_as_length(self): name = 'Bias' units = 'V' values = np.arange(5) descriptor = Dimension(name, len(values), units=units) for expected, actual in zip([name, units], [descriptor.name, descriptor.units]): self.assertTrue(np.all([x == y for x, y in zip(expected, actual)])) self.assertTrue(np.allclose(values, descriptor.values))
def test_nonposint_values(self): vals = [-1, .5] expected = [ "values should at least be specified as a positive integer", "values should be array-like" ] for v, e in zip(vals, expected): with self.assertRaises(Exception) as context: _ = Dimension("x", v) self.assertTrue(e in str(context.exception))
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_dimension_type(self): dim_types = ["spatial", "Spatial", "reciprocal", "Reciprocal", "spectral", "Spectral", "temporal", "Temporal", "frame", "Frame", "time", "Time", "stack", "Stack"] dim_vals_expected = [1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4] dim_names_expected = ["SPATIAL", "SPATIAL", "RECIPROCAL", "RECIPROCAL", "SPECTRAL", "SPECTRAL", "TEMPORAL", "TEMPORAL", "TEMPORAL", "TEMPORAL", "TEMPORAL", "TEMPORAL", "TEMPORAL", "TEMPORAL"] for dt, dv, dn in zip(dim_types, dim_vals_expected, dim_names_expected): dim = Dimension(5, "x", dimension_type=dt) self.assertEqual(dim.dimension_type.value, dv) self.assertEqual(dim.dimension_type.name, dn)
def test_inequality_req_inputs(self): name = 'Bias' self.assertNotEqual(Dimension(name, [0, 1, 2, 3]), Dimension(name, [0, 1, 2, 4])) self.assertNotEqual(Dimension('fdfd', [0, 1, 2, 3]), Dimension(name, [0, 1, 2, 3])) self.assertNotEqual(Dimension(name, [0, 1, 2]), Dimension(name, [0, 1, 2, 3]))
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_equality(self): name = 'Bias' dim_1 = Dimension(name, [0, 1, 2, 3, 4]) dim_2 = Dimension(name, np.arange(5, dtype=np.float32)) self.assertEqual(dim_1, dim_2)
def test_dimension_size_1(self): dim = Dimension(1) self.assertIsInstance(dim, Dimension) assert_array_equal(np.array(dim), [0])
def test_single_valued_dimension(self): dim = Dimension([1.23]) self.assertIsInstance(dim, Dimension) assert_array_equal(np.array(dim), [1.23])
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_info(self): expected = "X - Bias (mV): [0. 1. 2. 3. 4.]" dim = Dimension(np.arange(5), "X", "Bias", "mV") self.assertTrue(dim.info, expected)
def test_inequality_req_inputs(self): name = 'X' quantity = "Length" units = 'nm' self.assertTrue(Dimension(5, name) == Dimension(5, name)) self.assertFalse(Dimension(5, 'Y') == Dimension(5, name)) self.assertFalse(Dimension(4, name) == Dimension(5, name)) self.assertTrue( Dimension(5, units=units) == Dimension(5, units=units)) self.assertFalse( Dimension(5, units='pm') == Dimension(5, units=units)) self.assertTrue( Dimension(5, quantity=quantity) == Dimension(5, quantity=quantity)) self.assertFalse( Dimension(5, quantity='Bias') == Dimension(5, quantity=quantity)) self.assertFalse( Dimension(np.arange(5)) == Dimension(np.arange(5) + 1))
def test_empty_array_values(self): with self.assertRaises(TypeError) as context: _ = Dimension([], name="x") self.assertTrue("When specifying values over which a parameter is " "varied, values should not be an empty array" "" in str(context.exception))
def test_dimensionality(self): vals = np.ones((2, 2)) expected = "Values for dimension: x are not 1-dimensional" with self.assertRaises(Exception) as context: _ = Dimension("x", vals) self.assertTrue(expected in str(context.exception))
def test_values_smaller_than_min_size(self): with self.assertRaises(TypeError) as context: _ = Dimension(0, name="x") self.assertTrue("When specifying the size of a Dimension, values " "should at be integers > 1" in str(context.exception))