|
1 | | -from pathlib import Path |
2 | | -from typing import Union |
3 | | - |
4 | 1 | import numpy as np |
5 | 2 |
|
6 | 3 | from vidata.registry import register_loader, register_writer |
7 | 4 |
|
8 | 5 |
|
9 | 6 | @register_loader("image", ".npy", backend="numpy") |
10 | 7 | @register_loader("mask", ".npy", backend="numpy") |
11 | | -def load_npy(path: str) -> np.ndarray: |
| 8 | +def load_npy(file: str) -> np.ndarray: |
12 | 9 | """Load a NumPy array from a .npy file. |
13 | 10 |
|
14 | 11 | Args: |
15 | | - path (str): Path to the .npy file. |
| 12 | + file (str): Path to the .npy file. |
16 | 13 |
|
17 | 14 | Returns: |
18 | 15 | np.ndarray: Loaded NumPy array. |
19 | 16 | """ |
20 | | - return np.load(path, allow_pickle=False), {} |
| 17 | + return np.load(file, allow_pickle=False), {} |
21 | 18 |
|
22 | 19 |
|
23 | 20 | @register_writer("image", ".npy", backend="numpy") |
24 | 21 | @register_writer("mask", ".npy", backend="numpy") |
25 | | -def save_npy(array: np.ndarray, path: Union[str, Path], *args, **kwargs) -> None: |
| 22 | +def save_npy(array: np.ndarray, file: str, *args, **kwargs) -> list[str]: |
26 | 23 | """Save a NumPy array to a .npy file. |
27 | 24 |
|
28 | 25 | Args: |
29 | 26 | array (np.ndarray): NumPy array to save. |
30 | | - path (str): Output file path. |
| 27 | + file (str): Output file file. |
31 | 28 | """ |
32 | | - np.save(path, array) |
| 29 | + np.save(file, array) |
| 30 | + return [file] |
33 | 31 |
|
34 | 32 |
|
35 | 33 | @register_loader("image", ".npz", backend="numpy") |
36 | 34 | @register_loader("mask", ".npz", backend="numpy") |
37 | | -def load_npz(path: str) -> tuple[dict[str, np.ndarray], dict]: |
| 35 | +def load_npz(file: str) -> tuple[dict[str, np.ndarray], dict]: |
38 | 36 | """Load multiple arrays from a .npz file into a dictionary. |
39 | 37 |
|
40 | 38 | Args: |
41 | | - path (str): Path to the .npz file. |
| 39 | + file (str): Path to the .npz file. |
42 | 40 |
|
43 | 41 | Returns: |
44 | 42 | dict[str, np.ndarray]: dictionary mapping keys to arrays. |
45 | 43 | """ |
46 | | - with np.load(path) as data: |
| 44 | + with np.load(file) as data: |
47 | 45 | return {key: data[key] for key in data.files}, {} |
48 | 46 |
|
49 | 47 |
|
50 | 48 | @register_writer("image", ".npz", backend="numpy") |
51 | 49 | @register_writer("mask", ".npz", backend="numpy") |
52 | 50 | def save_npz( |
53 | | - data_dict: dict[str, np.ndarray], path: str, compress: bool = True, *args, **kwargs |
54 | | -) -> None: |
| 51 | + data_dict: dict[str, np.ndarray], file: str, compress: bool = True, *args, **kwargs |
| 52 | +) -> list[str]: |
55 | 53 | """Save multiple NumPy arrays to a .npz file. |
56 | 54 |
|
57 | 55 | Args: |
58 | 56 | data_dict (dict[str, np.ndarray]): dictionary of arrays to save. |
59 | | - path (str): Output file path. |
| 57 | + file (str): Output file file. |
60 | 58 | compress (bool, optional): Whether to use compressed format. Defaults to True. |
61 | 59 | """ |
62 | 60 | if compress: |
63 | 61 | if isinstance(data_dict, dict): |
64 | | - np.savez_compressed(path, **data_dict) |
| 62 | + np.savez_compressed(file, **data_dict) |
65 | 63 | else: |
66 | | - np.savez_compressed(path, data_dict) |
| 64 | + np.savez_compressed(file, data_dict) |
67 | 65 | else: |
68 | 66 | if isinstance(data_dict, dict): |
69 | | - np.savez(path, **data_dict) |
| 67 | + np.savez(file, **data_dict) |
70 | 68 | else: |
71 | | - np.savez(path, data_dict) |
| 69 | + np.savez(file, data_dict) |
| 70 | + return [file] |
0 commit comments