def parse_ranges_highlight(ranges_string): """Process ranges highlight string. Args: ranges_string: (str) A string representing a numerical range of a list of numerical ranges. See the help info of the -r flag of the print_tensor command for more details. Returns: An instance of tensor_format.HighlightOptions, if range_string is a valid representation of a range or a list of ranges. """ ranges = None def ranges_filter(x): r = np.zeros(x.shape, dtype=bool) for range_start, range_end in ranges: r = np.logical_or(r, np.logical_and(x >= range_start, x <= range_end)) return r if ranges_string: ranges = command_parser.parse_ranges(ranges_string) return tensor_format.HighlightOptions(ranges_filter, description=ranges_string) else: return None
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 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) self.assertEqual([ "Tensor \"a\": " "Highlighted(between 0.26 and 0.5): 5 of 24 element(s) (20.83%)", "", "array([[[ 0. , 0.04166667, 0.08333333, 0.125 ],", " [ 0.16666667, 0.20833333, 0.25 , 0.29166667],", " [ 0.33333333, 0.375 , 0.41666667, 0.45833333]],", "", " [[ 0.5 , 0.54166667, 0.58333333, 0.625 ],", " [ 0.66666667, 0.70833333, 0.75 , 0.79166667],", " [ 0.83333333, 0.875 , 0.91666667, 0.95833333]]])", ], out.lines) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. self._checkBeginIndices([0, 0, 0], out.annotations[2]) self._checkBeginIndices([0, 1, 0], out.annotations[3]) self._checkBeginIndices([0, 2, 0], out.annotations[4]) self.assertNotIn(5, out.annotations) self._checkBeginIndices([1, 0, 0], out.annotations[6]) self._checkBeginIndices([1, 1, 0], out.annotations[7]) self._checkBeginIndices([1, 2, 0], out.annotations[8]) # Check font attribute segments for highlighted elements. self.assertNotIn(2, out.font_attr_segs) self.assertEqual([(49, 59, "bold")], out.font_attr_segs[3]) self.assertEqual([(10, 20, "bold"), (23, 28, "bold"), (36, 46, "bold"), (49, 59, "bold")], out.font_attr_segs[4]) self.assertNotIn(5, out.font_attr_segs) self.assertNotIn(6, out.font_attr_segs) self.assertNotIn(7, out.font_attr_segs) self.assertNotIn(8, out.font_attr_segs)
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) self.assertEqual([ "Tensor \"a\": Highlighted: 0 of 24 element(s) (0.00%)", "", "array([[[ 0. , 0.04166667, 0.08333333, 0.125 ],", " [ 0.16666667, 0.20833333, 0.25 , 0.29166667],", " [ 0.33333333, 0.375 , 0.41666667, 0.45833333]],", "", " [[ 0.5 , 0.54166667, 0.58333333, 0.625 ],", " [ 0.66666667, 0.70833333, 0.75 , 0.79166667],", " [ 0.83333333, 0.875 , 0.91666667, 0.95833333]]])" ], out.lines) self._checkTensorMetadata(a, out.annotations) # Check annotations for beginning indices of the lines. self._checkBeginIndices([0, 0, 0], out.annotations[2]) self._checkBeginIndices([0, 1, 0], out.annotations[3]) self._checkBeginIndices([0, 2, 0], out.annotations[4]) self.assertNotIn(5, out.annotations) self._checkBeginIndices([1, 0, 0], out.annotations[6]) self._checkBeginIndices([1, 1, 0], out.annotations[7]) self._checkBeginIndices([1, 2, 0], out.annotations[8]) # Check font attribute segments for highlighted elements. self.assertNotIn(2, out.font_attr_segs) self.assertNotIn(3, out.font_attr_segs) self.assertNotIn(4, out.font_attr_segs) self.assertNotIn(5, out.font_attr_segs) self.assertNotIn(6, out.font_attr_segs) self.assertNotIn(7, out.font_attr_segs) self.assertNotIn(8, out.font_attr_segs)
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)