def testParseSingleRange(self): self.assertAllClose([[-0.1, 0.2]], command_parser.parse_ranges("[-0.1, 0.2]")) self.assertAllClose([[-0.1, self.INF_VALUE]], command_parser.parse_ranges("[-0.1, inf]")) self.assertAllClose([[-self.INF_VALUE, self.INF_VALUE]], command_parser.parse_ranges("[-inf, inf]"))
def testParseSingleListOfRanges(self): self.assertAllClose( [[-0.1, 0.2], [10.0, 12.0]], command_parser.parse_ranges("[[-0.1, 0.2], [10, 12]]")) self.assertAllClose( [[-self.INF_VALUE, -1.0], [1.0, self.INF_VALUE]], command_parser.parse_ranges("[[-inf, -1.0],[1.0, inf]]"))
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 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 testParseInvalidRangeString(self): with self.assertRaises(SyntaxError): command_parser.parse_ranges("[[1,2]") with self.assertRaisesRegexp(ValueError, "Incorrect number of elements in range"): command_parser.parse_ranges("[1,2,3]") with self.assertRaisesRegexp(ValueError, "Incorrect number of elements in range"): command_parser.parse_ranges("[inf]") with self.assertRaisesRegexp(ValueError, "Incorrect type in the 1st element of range"): command_parser.parse_ranges("[1j, 1]") with self.assertRaisesRegexp(ValueError, "Incorrect type in the 2nd element of range"): command_parser.parse_ranges("[1, 1j]")
def testParseInvalidRangeString(self): with self.assertRaises(SyntaxError): command_parser.parse_ranges("[[1,2]") with self.assertRaisesRegexp(ValueError, "Incorrect number of elements in range"): command_parser.parse_ranges("[1,2,3]") with self.assertRaisesRegexp(ValueError, "Incorrect number of elements in range"): command_parser.parse_ranges("[inf]") with self.assertRaisesRegexp( ValueError, "Incorrect type in the 1st element of range"): command_parser.parse_ranges("[1j, 1]") with self.assertRaisesRegexp( ValueError, "Incorrect type in the 2nd element of range"): command_parser.parse_ranges("[1, 1j]")
def testParseSingleListOfRanges(self): self.assertAllClose([[-0.1, 0.2], [10.0, 12.0]], command_parser.parse_ranges("[[-0.1, 0.2], [10, 12]]")) self.assertAllClose( [[-self.INF_VALUE, -1.0], [1.0, self.INF_VALUE]], command_parser.parse_ranges("[[-inf, -1.0],[1.0, inf]]"))
def testParseEmptyRangeString(self): self.assertEqual([], command_parser.parse_ranges("")) self.assertEqual([], command_parser.parse_ranges(" "))