forked from Networks-Learning/regression-under-assistance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myutil.py
33 lines (25 loc) · 787 Bytes
/
myutil.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
import os
import pickle
import numpy as np
def save(obj, output_file):
with open(output_file + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def write_txt_file(data, file_name):
with open(file_name, 'w') as f:
for line in data:
f.write(" ".join(map(str, line)) + '\n')
def ma(y, window):
avg_mask = np.ones(window) / window
y_ma = np.convolve(y, avg_mask, 'same')
y_ma[0] = y[0]
y_ma[-1] = y[-1]
return y_ma
def load_data(input_file, flag=None):
if flag == 'ifexists':
if not os.path.isfile(input_file + '.pkl'):
# print 'not found', input_file
return {}
# print 'found'
with open(input_file + '.pkl', 'rb') as f:
data = pickle.load(f)
return data