def testFormatTensor3DNoEllipsisWithArgwhereHighlightWithMatches(self): a = np.linspace(0.0, 1.0 - 1.0 / 24.0, 24).reshape([2, 3, 4]) lower_bound = 0.26 upper_bound = 0.5 def highlight_filter(x): return np.logical_and(x > lower_bound, x < upper_bound) highlight_options = tensor_format.HighlightOptions( highlight_filter, description="between 0.26 and 0.5") out = tensor_format.format_tensor( a, "a", highlight_options=highlight_options) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\": " "Highlighted(between 0.26 and 0.5): 5 of 24 element(s) (20.83%)", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. self._checkBeginIndicesAnnotations(out, a) self.assertAllClose( [0.29166667, 0.33333333, 0.375, 0.41666667, 0.45833333], self._extractBoldNumbers(out, 2))
def testLocateTensorElement2DNoEllipsisWithNumericSummary(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a", include_numeric_summary=True) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", "", "Numeric summary:", "| 0 + | total |", "| 1 15 | 16 |", "| min max mean std |"], out.lines[:6]) cli_test_utils.assert_array_lines_close( self, [0.0, 0.9375, 0.46875, 0.28811076429], out.lines[6:7]) cli_test_utils.assert_array_lines_close(self, a, out.lines[8:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testFormatTensor3DNoEllipsisWithArgwhereHighlightWithMatches(self): a = np.linspace(0.0, 1.0 - 1.0 / 24.0, 24).reshape([2, 3, 4]) lower_bound = 0.26 upper_bound = 0.5 def highlight_filter(x): return np.logical_and(x > lower_bound, x < upper_bound) highlight_options = tensor_format.HighlightOptions( highlight_filter, description="between 0.26 and 0.5") out = tensor_format.format_tensor(a, "a", highlight_options=highlight_options) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "Tensor \"a\": " "Highlighted(between 0.26 and 0.5): 5 of 24 element(s) (20.83%)", "" ], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. self._checkBeginIndicesAnnotations(out, a) self.assertAllClose( [0.29166667, 0.33333333, 0.375, 0.41666667, 0.45833333], self._extractBoldNumbers(out, 2))
def testBatchModeWithErrors(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [[0, 0], [0]]) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [[0], [20]]) with self.assertRaisesRegex(ValueError, r"Indices contain negative value\(s\)"): tensor_format.locate_tensor_element(out, [[0], [-1]]) with self.assertRaisesRegex( ValueError, "Input indices sets are not in ascending order"): tensor_format.locate_tensor_element(out, [[5], [0]])
def testLocateTensorElement2DNoEllipsisWithNumericSummary(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a", include_numeric_summary=True) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "Tensor \"a\":", "", "Numeric summary:", "| 0 + | total |", "| 1 15 | 16 |", "| min max mean std |" ], out.lines[:6]) cli_test_utils.assert_array_lines_close( self, [0.0, 0.9375, 0.46875, 0.28811076429], out.lines[6:7]) cli_test_utils.assert_array_lines_close(self, a, out.lines[8:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testFormatTensorWithEllipses(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. actual_row_0_0_0, _ = self._findFirst(out.lines, "1000") self.assertEqual({tensor_format.BEGIN_INDICES_KEY: [0, 0, 0]}, out.annotations[actual_row_0_0_0]) actual_row_0_1_0, _ = self._findFirst(out.lines, "1011") self.assertEqual({tensor_format.BEGIN_INDICES_KEY: [0, 1, 0]}, out.annotations[actual_row_0_1_0]) # Find the first line that is completely omitted. omitted_line = 2 while not out.lines[omitted_line].strip().startswith("..."): omitted_line += 1 self.assertEqual({tensor_format.OMITTED_INDICES_KEY: [0, 2, 0]}, out.annotations[omitted_line]) actual_row_10_10_0, _ = self._findFirst(out.lines, "2320") self.assertEqual({tensor_format.BEGIN_INDICES_KEY: [10, 10, 0]}, out.annotations[actual_row_10_10_0]) # Find the last line that is completely omitted. omitted_line = len(out.lines) - 1 while not out.lines[omitted_line].strip().startswith("..."): omitted_line -= 1 self.assertEqual({tensor_format.OMITTED_INDICES_KEY: [10, 2, 0]}, out.annotations[omitted_line])
def testFormatZeroDimensionTensor(self): a = np.array(42, dtype=np.int32) out = tensor_format.format_tensor(a, "a") cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertTrue(out.lines[2].startswith("array(42")) self._checkTensorMetadata(a, out.annotations)
def testNumericSummaryOnInt(self): x = np.array([-3] * 50 + [3] * 200 + [0], dtype=np.int32) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "| - 0 + | total |", "| 50 1 200 | 251 |", "| min max mean std |" ], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [-3, 3, 1.79282868526, 2.39789673081], out.lines[3:4])
def testNumericSummaryOnInt(self): x = np.array([-3] * 50 + [3] * 200 + [0], dtype=np.int32) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| - 0 + | total |", "| 50 1 200 | 251 |", "| min max mean std |"], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [-3, 3, 1.79282868526, 2.39789673081], out.lines[3:4])
def testNumericSummaryOnFloatFullHouse(self): x = np.array([np.nan, np.nan, -np.inf, np.inf, np.inf, np.inf, -2, -3, -4, 0, 1, 2, 2, 2, 2, 0, 0, 0, np.inf, np.inf, np.inf]) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| nan -inf - 0 + +inf | total |", "| 2 1 3 4 5 6 | 21 |", "| min max mean std |"], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [-4.0, 2.0, 0.0, 1.95789002075], out.lines[3:4])
def testLocateTensorElement1DNoEllipsisBatchMode(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a)
def testFormatTensor3DNoEllipsis(self): a = np.linspace(0.0, 1.0 - 1.0 / 24.0, 24).reshape([2, 3, 4]) out = tensor_format.format_tensor(a, "a") cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) self._checkBeginIndicesAnnotations(out, a)
def testFormatTensorWithMetadata(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a", include_metadata=True) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", " dtype: float64", " shape: (4, 4)", ""], out.lines[:4]) self.assertEqual(repr(a).split("\n"), out.lines[4:]) self._checkTensorMetadata(a, out.annotations) self._checkBeginIndicesAnnotations(out, a)
def testLocateTensorElement1DNoEllipsisBatchMode(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a)
def testLocateTensorElement1DTinyAndNanValues(self): a = np.ones([3, 3]) * 1e-8 a[1, 0] = np.nan a[1, 2] = np.inf out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 100}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a)
def testFormatTensor1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. self._checkBeginIndicesAnnotations(out, a)
def testNumericSummaryOnFloatFullHouse(self): x = np.array([ np.nan, np.nan, -np.inf, np.inf, np.inf, np.inf, -2, -3, -4, 0, 1, 2, 2, 2, 2, 0, 0, 0, np.inf, np.inf, np.inf ]) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "| nan -inf - 0 + +inf | total |", "| 2 1 3 4 5 6 | 21 |", "| min max mean std |" ], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [-4.0, 2.0, 0.0, 1.95789002075], out.lines[3:4])
def testLocateTensorElement1DTinyAndNanValues(self): a = np.ones([3, 3]) * 1e-8 a[1, 0] = np.nan a[1, 2] = np.inf out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 100}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a)
def testLocateTensorElement3DWithEllipsesBatchMode(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor(a, "a", False, np_printoptions={ "threshold": 100, "edgeitems": 2 }) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0]]) self.assertEqual([False], are_omitted) self.assertEqual([actual_row_0_0_0], rows) self.assertEqual([actual_col_0_0_0], start_cols) self.assertEqual([actual_col_0_0_0 + 4], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0, 0, 0], [0, 0, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_0_0_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0, 0, 0], [0, 2, 0]]) self.assertEqual([False, True], are_omitted) self.assertEqual([2, 4], rows) self.assertEqual(2, len(start_cols)) self.assertEqual(2, len(end_cols)) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element( out, [[0, 0, 0], [10, 10, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_10_10_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols)
def testFormatTensor1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. self._checkBeginIndicesAnnotations(out, a)
def testLocateTensorElement3DWithEllipsesBatchMode(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0]]) self.assertEqual([False], are_omitted) self.assertEqual([actual_row_0_0_0], rows) self.assertEqual([actual_col_0_0_0], start_cols) self.assertEqual([actual_col_0_0_0 + 4], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [0, 0, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_0_0_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [0, 2, 0]]) self.assertEqual([False, True], are_omitted) self.assertEqual([2, 4], rows) self.assertEqual(2, len(start_cols)) self.assertEqual(2, len(end_cols)) (are_omitted, rows, start_cols, end_cols) = tensor_format.locate_tensor_element(out, [[0, 0, 0], [10, 10, 10]]) self.assertEqual([False, False], are_omitted) self.assertEqual([actual_row_0_0_0, actual_row_10_10_10], rows) self.assertEqual([actual_col_0_0_0, None], start_cols) self.assertEqual([actual_col_0_0_0 + 4, None], end_cols)
def testFormatTensor2DNoEllipsisWithRowBreak(self): a = np.linspace(0.0, 1.0 - 1.0 / 40.0, 40).reshape([2, 20]) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 50}) self.assertEqual( {"dtype": a.dtype, "shape": a.shape}, out.annotations["tensor_metadata"]) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for the beginning indices of the lines. self._checkBeginIndicesAnnotations(out, a)
def testLocateTensorElement2DNoEllipsis(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a") cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testFormatTensor2DNoEllipsisWithRowBreak(self): a = np.linspace(0.0, 1.0 - 1.0 / 40.0, 40).reshape([2, 20]) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 50}) self.assertEqual({ "dtype": a.dtype, "shape": a.shape }, out.annotations["tensor_metadata"]) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for the beginning indices of the lines. self._checkBeginIndicesAnnotations(out, a)
def testFormatTensorWithEllipses(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor(a, "a", False, np_printoptions={ "threshold": 100, "edgeitems": 2 }) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. actual_row_0_0_0, _ = self._findFirst(out.lines, "1000") self.assertEqual({tensor_format.BEGIN_INDICES_KEY: [0, 0, 0]}, out.annotations[actual_row_0_0_0]) actual_row_0_1_0, _ = self._findFirst(out.lines, "1011") self.assertEqual({tensor_format.BEGIN_INDICES_KEY: [0, 1, 0]}, out.annotations[actual_row_0_1_0]) # Find the first line that is completely omitted. omitted_line = 2 while not out.lines[omitted_line].strip().startswith("..."): omitted_line += 1 self.assertEqual({tensor_format.OMITTED_INDICES_KEY: [0, 2, 0]}, out.annotations[omitted_line]) actual_row_10_10_0, _ = self._findFirst(out.lines, "2320") self.assertEqual({tensor_format.BEGIN_INDICES_KEY: [10, 10, 0]}, out.annotations[actual_row_10_10_0]) # Find the last line that is completely omitted. omitted_line = len(out.lines) - 1 while not out.lines[omitted_line].strip().startswith("..."): omitted_line -= 1 self.assertEqual({tensor_format.OMITTED_INDICES_KEY: [10, 2, 0]}, out.annotations[omitted_line])
def testNumericSummaryOnFloatMissingCategories(self): x = np.array([np.nan, np.nan]) out = tensor_format.numeric_summary(x) self.assertEqual(2, len(out.lines)) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| nan | total |", "| 2 | 2 |"], out.lines[:2]) x = np.array([-np.inf, np.inf, 0, 0, np.inf, np.inf]) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "| -inf 0 +inf | total |", "| 1 2 3 | 6 |", "| min max mean std |" ], out.lines[:3]) cli_test_utils.assert_array_lines_close(self, [0.0, 0.0, 0.0, 0.0], out.lines[3:4]) x = np.array([-120, 120, 130]) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, [ "| - + | total |", "| 1 2 | 3 |", "| min max mean std |" ], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [-120, 130, 43.3333333333, 115.566238822], out.lines[3:4])
def testNumericSummaryOnFloatMissingCategories(self): x = np.array([np.nan, np.nan]) out = tensor_format.numeric_summary(x) self.assertEqual(2, len(out.lines)) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| nan | total |", "| 2 | 2 |"], out.lines[:2]) x = np.array([-np.inf, np.inf, 0, 0, np.inf, np.inf]) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| -inf 0 +inf | total |", "| 1 2 3 | 6 |", "| min max mean std |"], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [0.0, 0.0, 0.0, 0.0], out.lines[3:4]) x = np.array([-120, 120, 130]) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| - + | total |", "| 1 2 | 3 |", "| min max mean std |"], out.lines[:3]) cli_test_utils.assert_array_lines_close( self, [-120, 130, 43.3333333333, 115.566238822], out.lines[3:4])
def testLocateTensorElement2DNoEllipsis(self): a = np.linspace(0.0, 1.0 - 1.0 / 16.0, 16).reshape([4, 4]) out = tensor_format.format_tensor(a, "a") cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [1, 4]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 2]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0])
def testFormatTensor3DNoEllipsisWithArgwhereHighlightWithNoMatches(self): a = np.linspace(0.0, 1.0 - 1.0 / 24.0, 24).reshape([2, 3, 4]) def highlight_filter(x): return x > 10.0 highlight_options = tensor_format.HighlightOptions(highlight_filter) out = tensor_format.format_tensor( a, "a", highlight_options=highlight_options) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\": Highlighted: 0 of 24 element(s) (0.00%)", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) self._checkBeginIndicesAnnotations(out, a) # Check font attribute segments for highlighted elements. for i in range(2, len(out.lines)): self.assertNotIn(i, out.font_attr_segs)
def testLocateTensorElement1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor(a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [20]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0, 0])
def testFormatTensor3DNoEllipsisWithArgwhereHighlightWithNoMatches(self): a = np.linspace(0.0, 1.0 - 1.0 / 24.0, 24).reshape([2, 3, 4]) def highlight_filter(x): return x > 10.0 highlight_options = tensor_format.HighlightOptions(highlight_filter) out = tensor_format.format_tensor(a, "a", highlight_options=highlight_options) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\": Highlighted: 0 of 24 element(s) (0.00%)", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorMetadata(a, out.annotations) self._checkBeginIndicesAnnotations(out, a) # Check font attribute segments for highlighted elements. for i in range(2, len(out.lines)): self.assertNotIn(i, out.font_attr_segs)
def testLocateTensorElement1DNoEllipsis(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) self._checkTensorElementLocations(out, a) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [20]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [0, 0])
def testBatchModeWithErrors(self): a = np.zeros(20) out = tensor_format.format_tensor( a, "a", np_printoptions={"linewidth": 40}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) self.assertEqual(repr(a).split("\n"), out.lines[2:]) with self.assertRaisesRegexp(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [[0, 0], [0]]) with self.assertRaisesRegexp(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [[0], [20]]) with self.assertRaisesRegexp(ValueError, r"Indices contain negative value\(s\)"): tensor_format.locate_tensor_element(out, [[0], [-1]]) with self.assertRaisesRegexp( ValueError, "Input indices sets are not in ascending order"): tensor_format.locate_tensor_element(out, [[5], [0]])
def testNumericSummaryOnBool(self): x = np.array([False, True, True, False], dtype=np.bool_) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| False True | total |", "| 2 2 | 4 |"], out.lines) x = np.array([True] * 10, dtype=np.bool_) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| True | total |", "| 10 | 10 |"], out.lines) x = np.array([False] * 10, dtype=np.bool_) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| False | total |", "| 10 | 10 |"], out.lines) x = np.array([], dtype=np.bool_) out = tensor_format.numeric_summary(x) self.assertEqual(["No numeric summary available due to empty tensor."], out.lines)
def testNumericSummaryOnBool(self): x = np.array([False, True, True, False], dtype=np.bool) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| False True | total |", "| 2 2 | 4 |"], out.lines) x = np.array([True] * 10, dtype=np.bool) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| True | total |", "| 10 | 10 |"], out.lines) x = np.array([False] * 10, dtype=np.bool) out = tensor_format.numeric_summary(x) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["| False | total |", "| 10 | 10 |"], out.lines) x = np.array([], dtype=np.bool) out = tensor_format.numeric_summary(x) self.assertEqual(["No numeric summary available due to empty tensor."], out.lines)
def testLocateTensorElement3DWithEllipses(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor( a, "a", False, np_printoptions={"threshold": 100, "edgeitems": 2}) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_0, row) self.assertEqual(actual_col_0_0_0, start_col) self.assertEqual(actual_col_0_0_0 + 4, end_col) actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_10, row) self.assertIsNone(start_col) # Passes ellipsis. self.assertIsNone(end_col) actual_row_0_1_0, actual_col_0_1_0 = self._findFirst(out.lines, "1011") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 1, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_1_0, row) self.assertEqual(actual_col_0_1_0, start_col) self.assertEqual(actual_col_0_1_0 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 0]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 8, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_0_10_1, actual_col_0_10_1 = self._findFirst(out.lines, "1111") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 10, 1]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_10_1, row) self.assertEqual(actual_col_0_10_1, start_col) self.assertEqual(actual_col_0_10_1 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [5, 1, 1]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [10, 10, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_10_10_10, row) self.assertIsNone(start_col) # Past ellipsis. self.assertIsNone(end_col) with self.assertRaisesRegexp( ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [11, 5, 5]) with self.assertRaisesRegexp( ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 5, 5]) with self.assertRaisesRegexp( ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [5, 5])
def testLocateTensorElement3DWithEllipses(self): a = (np.arange(11 * 11 * 11) + 1000).reshape([11, 11, 11]).astype(np.int32) out = tensor_format.format_tensor(a, "a", False, np_printoptions={ "threshold": 100, "edgeitems": 2 }) cli_test_utils.assert_lines_equal_ignoring_whitespace( self, ["Tensor \"a\":", ""], out.lines[:2]) actual_row_0_0_0, actual_col_0_0_0 = self._findFirst(out.lines, "1000") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_0, row) self.assertEqual(actual_col_0_0_0, start_col) self.assertEqual(actual_col_0_0_0 + 4, end_col) actual_row_0_0_10, _ = self._findFirst(out.lines, "1010") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 0, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_0_10, row) self.assertIsNone(start_col) # Passes ellipsis. self.assertIsNone(end_col) actual_row_0_1_0, actual_col_0_1_0 = self._findFirst(out.lines, "1011") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 1, 0]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_1_0, row) self.assertEqual(actual_col_0_1_0, start_col) self.assertEqual(actual_col_0_1_0 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 0]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 2, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 8, 10]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_0_10_1, actual_col_0_10_1 = self._findFirst( out.lines, "1111") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [0, 10, 1]) self.assertFalse(is_omitted) self.assertEqual(actual_row_0_10_1, row) self.assertEqual(actual_col_0_10_1, start_col) self.assertEqual(actual_col_0_10_1 + 4, end_col) is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [5, 1, 1]) self.assertTrue(is_omitted) # In omitted line. self.assertIsNone(start_col) self.assertIsNone(end_col) actual_row_10_10_10, _ = self._findFirst(out.lines, "2330") is_omitted, row, start_col, end_col = tensor_format.locate_tensor_element( out, [10, 10, 10]) self.assertFalse(is_omitted) self.assertEqual(actual_row_10_10_10, row) self.assertIsNone(start_col) # Past ellipsis. self.assertIsNone(end_col) with self.assertRaisesRegex(ValueError, "Indices exceed tensor dimensions"): tensor_format.locate_tensor_element(out, [11, 5, 5]) with self.assertRaisesRegex(ValueError, "Indices contain negative"): tensor_format.locate_tensor_element(out, [-1, 5, 5]) with self.assertRaisesRegex(ValueError, "Dimensions mismatch"): tensor_format.locate_tensor_element(out, [5, 5])