forked from huawei-noah/HEBO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_save.py
56 lines (41 loc) · 1.7 KB
/
utils_save.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import pickle
from typing import Any
import json
import numpy as np
def save_w_pickle(obj: Any, path: str, filename: str) -> None:
""" Save object obj in file exp_path/filename.pkl """
if len(filename) < 4 or filename[-4:] != '.pkl':
filename += '.pkl'
with open(os.path.join(path, filename), 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_w_pickle(path: str, filename: str) -> Any:
""" Load object from file exp_path/filename.pkl """
if len(filename) < 4 or filename[-4:] != '.pkl':
filename += '.pkl'
with open(os.path.join(path, filename), 'rb') as f:
try:
return pickle.load(f)
except EOFError as e:
print(path, filename)
raise
def save_json(obj: Any, path: str, filename: str) -> None:
""" Save object obj in file exp_path/filename.json """
if len(filename) < 5 or filename[-5:] != '.json':
filename += '.json'
with open(os.path.join(path, filename), 'w') as f:
json.dump(obj, f)
def load_json(path: str, filename: str) -> Any:
""" Load object from file exp_path/filename.pkl """
if len(filename) < 5 or filename[-5:] != '.json':
filename += '.json'
with open(os.path.join(path, filename)) as f:
return json.load(f)
def save_np(array: np.ndarray, path: str, filename: str) -> None:
""" Save numpy array in file exp_path/filename.npy """
np.save(os.path.join(path, filename), array)
def load_np(path: str, filename: str) -> np.ndarray:
""" Load numpy array stored in file exp_path/filename.npy """
if len(filename) < 5 or filename[-4:] != '.npy':
filename += '.npy'
return np.load(os.path.join(path, filename))