Generate annotations for line-by-line begin indices of tensor text. Parse the numpy-generated text representation of a numpy ndarray to determine the indices of the first element of each text line (if any element is present in the line). For example, given the following multi-line ndarray
(
array_lines, tensor, np_printoptions=None, offset=0)
| 200 | |
| 201 | |
| 202 | def _annotate_ndarray_lines( |
| 203 | array_lines, tensor, np_printoptions=None, offset=0): |
| 204 | """Generate annotations for line-by-line begin indices of tensor text. |
| 205 | |
| 206 | Parse the numpy-generated text representation of a numpy ndarray to |
| 207 | determine the indices of the first element of each text line (if any |
| 208 | element is present in the line). |
| 209 | |
| 210 | For example, given the following multi-line ndarray text representation: |
| 211 | ["array([[ 0. , 0.0625, 0.125 , 0.1875],", |
| 212 | " [ 0.25 , 0.3125, 0.375 , 0.4375],", |
| 213 | " [ 0.5 , 0.5625, 0.625 , 0.6875],", |
| 214 | " [ 0.75 , 0.8125, 0.875 , 0.9375]])"] |
| 215 | the generate annotation will be: |
| 216 | {0: {BEGIN_INDICES_KEY: [0, 0]}, |
| 217 | 1: {BEGIN_INDICES_KEY: [1, 0]}, |
| 218 | 2: {BEGIN_INDICES_KEY: [2, 0]}, |
| 219 | 3: {BEGIN_INDICES_KEY: [3, 0]}} |
| 220 | |
| 221 | Args: |
| 222 | array_lines: Text lines representing the tensor, as a list of str. |
| 223 | tensor: The tensor being formatted as string. |
| 224 | np_printoptions: A dictionary of keyword arguments that are passed to a |
| 225 | call of np.set_printoptions(). |
| 226 | offset: Line number offset applied to the line indices in the returned |
| 227 | annotation. |
| 228 | |
| 229 | Returns: |
| 230 | An annotation as a dict. |
| 231 | """ |
| 232 | |
| 233 | if np_printoptions and "edgeitems" in np_printoptions: |
| 234 | edge_items = np_printoptions["edgeitems"] |
| 235 | else: |
| 236 | edge_items = _NUMPY_DEFAULT_EDGE_ITEMS |
| 237 | |
| 238 | annotations = {} |
| 239 | |
| 240 | # Put metadata about the tensor in the annotations["tensor_metadata"]. |
| 241 | annotations["tensor_metadata"] = { |
| 242 | "dtype": tensor.dtype, "shape": tensor.shape} |
| 243 | |
| 244 | dims = np.shape(tensor) |
| 245 | ndims = len(dims) |
| 246 | if ndims == 0: |
| 247 | # No indices for a 0D tensor. |
| 248 | return annotations |
| 249 | |
| 250 | curr_indices = [0] * len(dims) |
| 251 | curr_dim = 0 |
| 252 | for i, raw_line in enumerate(array_lines): |
| 253 | line = raw_line.strip() |
| 254 | |
| 255 | if not line: |
| 256 | # Skip empty lines, which can appear for >= 3D arrays. |
| 257 | continue |
| 258 | |
| 259 | if line == _NUMPY_OMISSION: |
no test coverage detected