Locate a tensor element in formatted text lines, given element indices. Given a RichTextLines object representing a tensor and indices of the sought element, return the row number at which the element is located (if exists). Args: formatted: A RichTextLines object containing formatted te
(formatted, indices)
| 280 | |
| 281 | |
| 282 | def locate_tensor_element(formatted, indices): |
| 283 | """Locate a tensor element in formatted text lines, given element indices. |
| 284 | |
| 285 | Given a RichTextLines object representing a tensor and indices of the sought |
| 286 | element, return the row number at which the element is located (if exists). |
| 287 | |
| 288 | Args: |
| 289 | formatted: A RichTextLines object containing formatted text lines |
| 290 | representing the tensor. |
| 291 | indices: Indices of the sought element, as a list of int or a list of list |
| 292 | of int. The former case is for a single set of indices to look up, |
| 293 | whereas the latter case is for looking up a batch of indices sets at once. |
| 294 | In the latter case, the indices must be in ascending order, or a |
| 295 | ValueError will be raised. |
| 296 | |
| 297 | Returns: |
| 298 | 1) A boolean indicating whether the element falls into an omitted line. |
| 299 | 2) Row index. |
| 300 | 3) Column start index, i.e., the first column in which the representation |
| 301 | of the specified tensor starts, if it can be determined. If it cannot |
| 302 | be determined (e.g., due to ellipsis), None. |
| 303 | 4) Column end index, i.e., the column right after the last column that |
| 304 | represents the specified tensor. Iff it cannot be determined, None. |
| 305 | |
| 306 | For return values described above are based on a single set of indices to |
| 307 | look up. In the case of batch mode (multiple sets of indices), the return |
| 308 | values will be lists of the types described above. |
| 309 | |
| 310 | Raises: |
| 311 | AttributeError: If: |
| 312 | Input argument "formatted" does not have the required annotations. |
| 313 | ValueError: If: |
| 314 | 1) Indices do not match the dimensions of the tensor, or |
| 315 | 2) Indices exceed sizes of the tensor, or |
| 316 | 3) Indices contain negative value(s). |
| 317 | 4) If in batch mode, and if not all sets of indices are in ascending |
| 318 | order. |
| 319 | """ |
| 320 | |
| 321 | if isinstance(indices[0], list): |
| 322 | indices_list = indices |
| 323 | input_batch = True |
| 324 | else: |
| 325 | indices_list = [indices] |
| 326 | input_batch = False |
| 327 | |
| 328 | # Check that tensor_metadata is available. |
| 329 | if "tensor_metadata" not in formatted.annotations: |
| 330 | raise AttributeError("tensor_metadata is not available in annotations.") |
| 331 | |
| 332 | # Sanity check on input argument. |
| 333 | _validate_indices_list(indices_list, formatted) |
| 334 | |
| 335 | dims = formatted.annotations["tensor_metadata"]["shape"] |
| 336 | batch_size = len(indices_list) |
| 337 | lines = formatted.lines |
| 338 | annot = formatted.annotations |
| 339 | prev_r = 0 |
no test coverage detected