-
Notifications
You must be signed in to change notification settings - Fork 4
/
saved_data_io.py
58 lines (50 loc) · 1.87 KB
/
saved_data_io.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
57
58
import pickle
import numpy as np
data_path_dict = {
'Q_locs': '../saved/newData_Q_locs_1000_7000_peakdist100.pkl',
'Codes': '../saved/newData_Codes_1000_7000_peakdist100.pkl',
'dataset_train_idx': '../saved/dataset_train_idx.pkl',
'dataset_test_idx': '../saved/dataset_test_idx.pkl',
'Data_labels_train': '../saved/Data_labels_train_stratified.npy',
'Data_labels_test': '../saved/Data_labels_test_stratified.npy',
'Signals_train': '../saved/Signals_train_stratified.npy',
'Signals_test': '../saved/Signals_test_stratified.npy' ,
'data_imgs_dataset1' :'../saved/data_imgs2.pkl',
'data_imgs_dataset2' :'../saved/data_imgs_dataset2.pkl',
'data_imgs_dataset2_2' :'../saved/data_imgs_dataset2_2.pkl',
'data_imgs_dataset2_new' :'../saved/data_imgs_dataset2_new.pkl',
}
def read_file(name, default_saved_path=False, verbose=False):
res = None
saved_path = None
if name[-4:] in ['.npy', '.pkl']:
saved_path = name
else:
if not default_saved_path:
saved_path = data_path_dict[name]
else:
saved_path = 'saved/{}.pkl'.format(name)
if saved_path[-4:] == '.npy':
res = np.load(saved_path)
else:
with open(saved_path, 'rb') as saved_file:
res = pickle.load(saved_file)
if verbose:
print('read from', saved_path)
return res
def write_file(name, obj, default_saved_path=False, verbose=False):
saved_path = None
if name[-4:] in ['.npy', '.pkl']:
saved_path = name
else:
if not default_saved_path:
saved_path = data_path_dict[name]
else:
saved_path = 'saved/{}.pkl'.format(name)
if saved_path[-4:] == '.npy':
np.save(saved_path, obj)
else:
with open(saved_path, 'wb') as saved_file:
pickle.dump(obj, saved_file)
if verbose:
print('saved at', saved_path)