def array_view(array, slicing=None, mapping=None): """View a slice or the entirety of an ndarray. Args: array: The input array, as an numpy.ndarray. slicing: Optional slicing string, e.g., "[:, 1:3, :]". mapping: Optional mapping string. Supported mappings: `None` or case-insensitive `'None'`: Unmapped nested list. `'image/png'`: Image encoding of a 2D sliced array or 3D sliced array with 3 as the last dimension. If the sliced array is not 2D or 3D with 3 as the last dimension, a `ValueError` will be thrown. `health-pill`: A succinct summary of the numeric values of a tensor. See documentation in [`health_pill_calc.py`] for more details. Returns: 1. dtype as a `str`. 2. shape of the sliced array, as a tuple of `int`s. 3. the potentially sliced values, as a nested `list`. """ dtype = str(array.dtype) # String-type TensorFlow Tensors are represented as object-type arrays in # numpy. We map the type name back to 'string' for clarity. if dtype == 'object': dtype = 'string' sliced_array = (array[command_parser._parse_slices(slicing)] if slicing else array) if np.isscalar(sliced_array) and str(dtype) == 'string': # When a string Tensor (for which dtype is 'object') is sliced down to only # one element, it becomes a string, instead of an numpy array. # We preserve the dimensionality of original array in the returned shape # and slice. ndims = len(array.shape) slice_shape = [] for _ in range(ndims): sliced_array = [sliced_array] slice_shape.append(1) return dtype, tuple(slice_shape), sliced_array else: shape = sliced_array.shape if mapping == "image/png": if len(sliced_array.shape) == 2: return dtype, shape, array_to_base64_png(sliced_array) elif len(sliced_array.shape) == 3: raise NotImplementedError( "image/png mapping for 3D array has not been implemented") else: raise ValueError("Invalid rank for image/png mapping: %d" % len(sliced_array.shape)) elif mapping == 'health-pill': health_pill = health_pill_calc.calc_health_pill(array) return dtype, shape, health_pill elif mapping is None or mapping == '' or mapping.lower() == 'none': return dtype, shape, sliced_array.tolist() else: raise ValueError("Invalid mapping: %s" % mapping)
def array_view(array, slicing=None, mapping=None): """View a slice or the entirety of an ndarray. Args: array: The input array, as an numpy.ndarray. slicing: Optional slicing string, e.g., "[:, 1:3, :]". mapping: Optional mapping string. Supported mappings: `None` or case-insensitive `'None'`: Unmapped nested list. `'image/png'`: Image encoding of a 2D sliced array or 3D sliced array with 3 as the last dimension. If the sliced array is not 2D or 3D with 3 as the last dimension, a `ValueError` will be thrown. `health-pill`: A succinct summary of the numeric values of a tensor. See documentation in [`health_pill_calc.py`] for more details. Returns: 1. dtype as a `str`. 2. shape of the sliced array, as a tuple of `int`s. 3. the potentially sliced values, as a nested `list`. """ dtype = translate_dtype(array.dtype) sliced_array = (array[command_parser._parse_slices(slicing)] if slicing else array) if np.isscalar(sliced_array) and str(dtype) == 'string': # When a string Tensor (for which dtype is 'object') is sliced down to only # one element, it becomes a string, instead of an numpy array. # We preserve the dimensionality of original array in the returned shape # and slice. ndims = len(array.shape) slice_shape = [] for _ in range(ndims): sliced_array = [sliced_array] slice_shape.append(1) return dtype, tuple(slice_shape), sliced_array else: shape = sliced_array.shape if mapping == "image/png": if len(sliced_array.shape) == 2: return dtype, shape, array_to_base64_png(sliced_array) elif len(sliced_array.shape) == 3: raise NotImplementedError( "image/png mapping for 3D array has not been implemented") else: raise ValueError("Invalid rank for image/png mapping: %d" % len(sliced_array.shape)) elif mapping == 'health-pill': health_pill = health_pill_calc.calc_health_pill(array) return dtype, shape, health_pill elif mapping is None or mapping == '' or mapping.lower() == 'none': return dtype, shape, sliced_array.tolist() else: raise ValueError("Invalid mapping: %s" % mapping)
def testScalar(self): x = np.array(-1337.0) health_pill = health_pill_calc.calc_health_pill(x) self.assertEqual(14, len(health_pill)) self.assertEqual(1, health_pill[0]) # Is initialized. self.assertEqual(1, health_pill[1]) # numel. self.assertEqual(0, health_pill[2]) # NaN count. self.assertEqual(0, health_pill[3]) # -Infinity count. self.assertEqual(1, health_pill[4]) # Finite negative count. self.assertEqual(0, health_pill[5]) # Zero count. self.assertEqual(0, health_pill[6]) # Finite positive count. self.assertEqual(0, health_pill[7]) # +Infinity count. self.assertEqual(-1337.0, health_pill[8]) self.assertEqual(-1337.0, health_pill[9]) self.assertEqual(-1337.0, health_pill[10]) self.assertEqual(0, health_pill[11]) self.assertEqual(0, health_pill[13]) # Number of dimensions.
def testScalar(self): x = np.array(-1337.0) health_pill = health_pill_calc.calc_health_pill(x) self.assertEqual(14, len(health_pill)) self.assertEqual(1, health_pill[0]) # Is initialized. self.assertEqual(1, health_pill[1]) # numel. self.assertEqual(0, health_pill[2]) # NaN count. self.assertEqual(0, health_pill[3]) # -Infinity count. self.assertEqual(1, health_pill[4]) # Finite negative count. self.assertEqual(0, health_pill[5]) # Zero count. self.assertEqual(0, health_pill[6]) # Finite positive count. self.assertEqual(0, health_pill[7]) # +Infinity count. self.assertEqual(-1337.0, health_pill[8]) self.assertEqual(-1337.0, health_pill[9]) self.assertEqual(-1337.0, health_pill[10]) self.assertEqual(0, health_pill[11]) self.assertEqual(0, health_pill[13]) # Number of dimensions.
def testInfAndNanOnlyArray(self): x = np.array([np.inf, -np.inf, np.nan]) health_pill = health_pill_calc.calc_health_pill(x) self.assertEqual(15, len(health_pill)) self.assertEqual(1, health_pill[0]) # Is initialized. self.assertEqual(3, health_pill[1]) # numel. self.assertEqual(1, health_pill[2]) # NaN count. self.assertEqual(1, health_pill[3]) # -Infinity count. self.assertEqual(0, health_pill[4]) # Finite negative count. self.assertEqual(0, health_pill[5]) # Zero count. self.assertEqual(0, health_pill[6]) # Finite positive count. self.assertEqual(1, health_pill[7]) # +Infinity count. self.assertEqual(np.inf, health_pill[8]) self.assertEqual(-np.inf, health_pill[9]) self.assertTrue(np.isnan(health_pill[10])) self.assertTrue(np.isnan(health_pill[11])) self.assertEqual(1, health_pill[13]) # Number of dimensions. self.assertEqual(3, health_pill[14]) # Size is (3,).
def testInfAndNanOnlyArray(self): x = np.array([np.inf, -np.inf, np.nan]) health_pill = health_pill_calc.calc_health_pill(x) self.assertEqual(15, len(health_pill)) self.assertEqual(1, health_pill[0]) # Is initialized. self.assertEqual(3, health_pill[1]) # numel. self.assertEqual(1, health_pill[2]) # NaN count. self.assertEqual(1, health_pill[3]) # -Infinity count. self.assertEqual(0, health_pill[4]) # Finite negative count. self.assertEqual(0, health_pill[5]) # Zero count. self.assertEqual(0, health_pill[6]) # Finite positive count. self.assertEqual(1, health_pill[7]) # +Infinity count. self.assertEqual(np.inf, health_pill[8]) self.assertEqual(-np.inf, health_pill[9]) self.assertTrue(np.isnan(health_pill[10])) self.assertTrue(np.isnan(health_pill[11])) self.assertEqual(1, health_pill[13]) # Number of dimensions. self.assertEqual(3, health_pill[14]) # Size is (3,).
def array_view(array, slicing=None, mapping=None): """View a slice or the entirety of an ndarray. Args: array: The input array, as an numpy.ndarray. slicing: Optional slicing string, e.g., "[:, 1:3, :]". mapping: Optional mapping string. Supported mappings: `None` or case-insensitive `'None'`: Unmapped nested list. `'image/png'`: Image encoding of a 2D sliced array or 3D sliced array with 3 as the last dimension. If the sliced array is not 2D or 3D with 3 as the last dimension, a `ValueError` will be thrown. `health-pill`: A succinct summary of the numeric values of a tensor. See documentation in [`health_pill_calc.py`] for more details. Returns: 1. dtype as a `str`. 2. shape of the sliced array, as a tuple of `int`s. 3. the potentially sliced values, as a nested `list`. """ dtype = str(array.dtype) sliced_array = (array[command_parser._parse_slices(slicing)] if slicing else array) shape = sliced_array.shape if mapping == "image/png": if len(sliced_array.shape) == 2: return dtype, shape, array_to_base64_png(sliced_array) elif len(sliced_array.shape) == 3: raise NotImplementedError( "image/png mapping for 3D array has not been implemented") else: raise ValueError("Invalid rank for image/png mapping: %d" % len(sliced_array.shape)) elif mapping == 'health-pill': health_pill = health_pill_calc.calc_health_pill(array) return dtype, shape, health_pill elif mapping is None or mapping == '' or mapping.lower() == 'none': return dtype, shape, sliced_array.tolist() else: raise ValueError("Invalid mapping: %s" % mapping)