Exemplo n.º 1
0
 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))
Exemplo n.º 2
0
    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))
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
    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)]))
Exemplo n.º 7
0
 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))
Exemplo n.º 8
0
 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))
Exemplo n.º 9
0
    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))
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
    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))
Exemplo n.º 12
0
 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))
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
 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)
Exemplo n.º 15
0
    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]))
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
 def test_dimension_size_1(self):
     dim = Dimension(1)
     self.assertIsInstance(dim, Dimension)
     assert_array_equal(np.array(dim), [0])
Exemplo n.º 19
0
 def test_single_valued_dimension(self):
     dim = Dimension([1.23])
     self.assertIsInstance(dim, Dimension)
     assert_array_equal(np.array(dim), [1.23])
Exemplo n.º 20
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)
Exemplo n.º 21
0
 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)
Exemplo n.º 22
0
    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))
Exemplo n.º 23
0
 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))
Exemplo n.º 24
0
 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))
Exemplo n.º 25
0
 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))