Spaces:
Sleeping
Sleeping
Upload file_utils.py
Browse files- file_utils.py +71 -0
file_utils.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import os.path as osp
|
3 |
+
import pickle
|
4 |
+
import subprocess
|
5 |
+
from typing import Any
|
6 |
+
|
7 |
+
import h5py
|
8 |
+
import numpy as np
|
9 |
+
import torch
|
10 |
+
|
11 |
+
num_channels, num_frames, height, width = None, None, None, None
|
12 |
+
|
13 |
+
|
14 |
+
def create_dir(dir_name: str):
|
15 |
+
"""Create a directory if it does not exist yet."""
|
16 |
+
if not osp.exists(dir_name):
|
17 |
+
os.makedirs(dir_name)
|
18 |
+
|
19 |
+
|
20 |
+
def move_files(source_path: str, destpath: str):
|
21 |
+
"""Move files from `source_path` to `dest_path`."""
|
22 |
+
subprocess.call(["mv", source_path, destpath])
|
23 |
+
|
24 |
+
|
25 |
+
def load_pickle(pickle_path: str) -> Any:
|
26 |
+
"""Load a pickle file."""
|
27 |
+
with open(pickle_path, "rb") as f:
|
28 |
+
data = pickle.load(f)
|
29 |
+
return data
|
30 |
+
|
31 |
+
|
32 |
+
def load_hdf5(hdf5_path: str) -> Any:
|
33 |
+
with h5py.File(hdf5_path, "r") as h5file:
|
34 |
+
data = {key: np.array(value) for key, value in h5file.items()}
|
35 |
+
return data
|
36 |
+
|
37 |
+
|
38 |
+
def save_hdf5(data: Any, hdf5_path: str):
|
39 |
+
with h5py.File(hdf5_path, "w") as h5file:
|
40 |
+
for key, value in data.items():
|
41 |
+
h5file.create_dataset(key, data=value)
|
42 |
+
|
43 |
+
|
44 |
+
def save_pickle(data: Any, pickle_path: str):
|
45 |
+
"""Save data in a pickle file."""
|
46 |
+
with open(pickle_path, "wb") as f:
|
47 |
+
pickle.dump(data, f, protocol=4)
|
48 |
+
|
49 |
+
|
50 |
+
def load_txt(txt_path: str):
|
51 |
+
"""Load a txt file."""
|
52 |
+
with open(txt_path, "r") as f:
|
53 |
+
data = f.read()
|
54 |
+
return data
|
55 |
+
|
56 |
+
|
57 |
+
def save_txt(data: str, txt_path: str):
|
58 |
+
"""Save data in a txt file."""
|
59 |
+
with open(txt_path, "w") as f:
|
60 |
+
f.write(data)
|
61 |
+
|
62 |
+
|
63 |
+
def load_pth(pth_path: str) -> Any:
|
64 |
+
"""Load a pth (PyTorch) file."""
|
65 |
+
data = torch.load(pth_path)
|
66 |
+
return data
|
67 |
+
|
68 |
+
|
69 |
+
def save_pth(data: Any, pth_path: str):
|
70 |
+
"""Save data in a pth (PyTorch) file."""
|
71 |
+
torch.save(data, pth_path)
|