Save data into disk.
(data, path, fmt)
| 48 | |
| 49 | |
| 50 | def save_data(data, path, fmt): |
| 51 | """Save data into disk.""" |
| 52 | # Make sure the directory exists. |
| 53 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 54 | |
| 55 | if fmt not in ["numpy", "torch"]: |
| 56 | raise RuntimeError(f"Unsupported format: {fmt}") |
| 57 | |
| 58 | # Perform necessary conversion. |
| 59 | if fmt == "numpy" and isinstance(data, torch.Tensor): |
| 60 | data = data.cpu().numpy() |
| 61 | elif fmt == "torch" and isinstance(data, np.ndarray): |
| 62 | data = torch.from_numpy(data).cpu() |
| 63 | |
| 64 | # Save the data. |
| 65 | if fmt == "numpy": |
| 66 | if not data.flags["C_CONTIGUOUS"]: |
| 67 | Warning( |
| 68 | "The ndarray saved to disk is not contiguous, " |
| 69 | "so it will be copied to contiguous memory." |
| 70 | ) |
| 71 | data = np.ascontiguousarray(data) |
| 72 | numpy_save_aligned(path, data) |
| 73 | elif fmt == "torch": |
| 74 | if not data.is_contiguous(): |
| 75 | Warning( |
| 76 | "The tensor saved to disk is not contiguous, " |
| 77 | "so it will be copied to contiguous memory." |
| 78 | ) |
| 79 | data = data.contiguous() |
| 80 | torch.save(data, path) |
| 81 | |
| 82 | |
| 83 | def get_npy_dim(npy_path): |
no test coverage detected