Print a 2D matrix. The function automatically breaks lines when printing a large 2D array. Args: arr: The matrix to print. num_per_line: Number of elements to print per line. -1: print entire row in one line. format: The format to
(arr: T.Union[torch.Tensor, np.ndarray, T.Sequence], num_per_line=-1, format=">6.3f")
| 110 | |
| 111 | |
| 112 | def print2D(arr: T.Union[torch.Tensor, np.ndarray, T.Sequence], num_per_line=-1, format=">6.3f") -> bool: |
| 113 | """Print a 2D matrix. |
| 114 | |
| 115 | The function automatically breaks lines when printing a large 2D array. |
| 116 | |
| 117 | Args: |
| 118 | arr: |
| 119 | The matrix to print. |
| 120 | num_per_line: |
| 121 | Number of elements to print per line. -1: print entire row in one line. |
| 122 | format: |
| 123 | The format to print each element. '>{spacing}.{precision}f' |
| 124 | For example, if want right-aligned, a spacing of 6, and a precision of 3, |
| 125 | set format to be '>6.3f'. |
| 126 | |
| 127 | Returns: |
| 128 | Whether the cursor is at the beginning of a line. |
| 129 | """ |
| 130 | if isinstance(arr, (torch.Tensor, np.ndarray)): |
| 131 | m = arr.shape[0] |
| 132 | else: |
| 133 | m = len(arr) |
| 134 | |
| 135 | new_line = True |
| 136 | for i in range(m): |
| 137 | print(f"[row {i}]") |
| 138 | new_line = print1D(arr[i], num_per_line, format=format) |
| 139 | if new_line is False: |
| 140 | print("") |
| 141 | new_line = True |
| 142 | |
| 143 | return new_line |
| 144 | |
| 145 | |
| 146 | def save_code(src_dir: str, dest_dir: str, excluded_folders=("artifacts", "__MACOSX")): |