def test_invalid_dim_array(self): try: makes_duplicate = copy.copy(self.makes) makes_duplicate[1] = makes_bad[0] friendly = fm.ndarray(self.price_array, ['Make', 'Color', 'Size', 'Year'], makes_duplicate, self.colors, self.sizes, self.years) self.assertTrue(False) except: self.assertTrue(True) try: makes_wrong_size = copy.copy(self.makes) makes_wrong_size.append('C, Inc.') friendly = fm.ndarray(self.price_array, ['Make', 'Color', 'Size', 'Year'], makes_wrong_size, self.colors, self.sizes, self.years) self.assertTrue(False) except: self.assertTrue(True) try: # Extra dim array friendly = fm.ndarray(self.price_array, ['Make', 'Color', 'Size', 'Year'], self.makes, self.colors, self.sizes, self.years, []) self.assertTrue(False) except: self.assertTrue(True)
def setUp(self): self.x1 = ['0', '1'] self.y1 = ['0', '1'] self.array1 = np.array([[0, 1], [2, 3]]) self.friendly1 = fm.ndarray(self.array1, ['x', 'y'], self.x1, self.y1) self.x2 = ['2', '3'] self.y2 = ['2', '3'] self.array2 = np.array([[0.0, 0.1], [0.2, 0.3]]) self.friendly2 = fm.ndarray(self.array2, ['x', 'y'], self.x2, self.y2)
def test_invalid_array(self): try: price_array_wrong_size = self.price_array[1:] friendly = fm.ndarray(price_array_wrong_size, ['Make', 'Color', 'Size', 'Year'], self.makes, self.colors, self.sizes, self.years) self.assertTrue(False) except: self.assertTrue(True) try: price_array_wrong_ndim = self.price_array[0] friendly = fm.ndarray(price_array_wrong_ndim, ['Make', 'Color', 'Size', 'Year'], self.makes, self.colors, self.sizes, self.years) self.assertTrue(False) except: self.assertTrue(True)
def test_valid(self): friendly = fm.ndarray(self.price_array, ['Make', 'Color', 'Size', 'Year'], self.makes, self.colors, self.sizes, self.years) self.assertEqual(friendly.shape, (2, 3, 2, 2)) self.assertEqual(friendly.ndim, 4) self.assertEqual(friendly.dim_names, ['Make', 'Color', 'Size', 'Year']) self.assertEqual(friendly.dim_arrays[0], self.makes) self.assertEqual(friendly.dim_arrays[3], self.years) self.assertTrue(np.all(friendly.array == self.price_array))
def setUp(self): self.makes = ['A, Inc.', 'B, Inc.'] self.colors = ['red', 'blue', 'green'] self.sizes = ['small', 'big'] self.years = [2000, 2020] self.golden_array = np.array( [[[[0.0, 17.0], [0.0, 19.0]], [[0.0, 17.0], [0.0, 19.0]], [[0.0, 17.0], [0.0, 19.0]]], [[[9.0, 17.0], [11.0, 19.0]], [[0.0, 0.0], [0.0, 0.0]], [[10.800000190734863, 18.0], [12.600000381469727, 19.799999237060547]]]]) self.golden_friendly = fm.ndarray(self.golden_array, ['Make', 'Color', 'Size', 'Year'], self.makes, self.colors, self.sizes, self.years)
def setUp(self): self.makes = ['A, Inc.', 'B, Inc.'] self.colors = ['red', 'blue', 'green'] self.sizes = ['small', 'big'] self.years = [2000, 2020] self.array = np.array([[[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]]], [[[12, 13], [14, 15]], [[16, 17], [18, 19]], [[20, 21], [22, 23]]]]) # Axis names are lowercase to support kwargs indexing self.friendly = fm.ndarray(self.array, ['make', 'color', 'size', 'year'], self.makes, self.colors, self.sizes, self.years) # Example formatted string does not contain dim names self.friendly_formatted = open(os.path.join('tests', 'friendly_formatted.txt'), encoding='utf-8').read()
def test_aggregate(self): result = self.friendly1.mean(axis=0) friendly_expected = fm.ndarray(self.array1.mean(axis=0), ['y'], self.y1)
def test_flip(self): result = fm.flip(self.friendly1) friendly_expected = fm.ndarray(np.flip(self.array1), ['x', 'y'], list(reversed(self.x1)), list(reversed(self.y1))) self.assertEqual(result, friendly_expected)
def test_moveaxis(self): result = fm.moveaxis(self.friendly1, 'x', 'y') friendly_expected = fm.ndarray(np.moveaxis(self.array1, 0, 1), ['y', 'x'], self.y1, self.x1) self.assertEqual(result, friendly_expected)