-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNALSM_GEN_SUPPORT.py
247 lines (160 loc) · 5.85 KB
/
NALSM_GEN_SUPPORT.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import pickle as pk
import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
import sys
def check_create_save_dir(save_dir):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
else:
print(
'SAVE DIRECTORY ALREADY EXISTS, TERMINATE PROGRAM IMMEDIATELY TO PREVENT LOSS OF EXISTING DATA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
def unpack_file(filename, dataPath):
data_fn = os.path.abspath(os.path.join(dataPath, filename))
names = []
data = []
f = open(data_fn, 'rb')
read = True
while read == True:
dat_temp = pk.load(f)
if dat_temp == 'end':
read = False
else:
# print(isinstance(dat_temp, str))
if isinstance(dat_temp, str):
names.append(dat_temp)
data.append(pk.load(f))
# print(data)
f.close()
return names, data
def save_tf_data(names, data, filename, savePath):
check_create_save_dir(savePath)
data_fn = os.path.abspath(os.path.join(savePath, filename))
f = open(data_fn, 'wb')
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(0,len(names)):
pk.dump(names[i],f)
pk.dump(sess.run(data[i]), f)
pk.dump('end',f)
sess.close()
f.close()
print('File__'+str(filename)+'__saved to__'+data_fn)
def save_tf_nontf_data(names, data, names_nontf, data_nontf, filename, savePath):
check_create_save_dir(savePath)
data_fn = os.path.abspath(os.path.join(savePath, filename))
f = open(data_fn, 'wb')
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(0,len(names)):
pk.dump(names[i],f)
pk.dump(sess.run(data[i]), f)
sess.close()
for i in range(0,len(names_nontf)):
pk.dump(names_nontf[i],f)
pk.dump(data_nontf[i], f)
pk.dump('end',f)
f.close()
print('File__'+str(filename)+'__saved to__'+data_fn)
def save_non_tf_data(names, data, filename, savePath):
check_create_save_dir(savePath)
data_fn = os.path.abspath(os.path.join(savePath, filename))
f = open(data_fn, 'wb')
for i in range(0,len(names)):
pk.dump(names[i],f)
pk.dump(data[i], f)
pk.dump('end',f)
f.close()
print('File__'+str(filename)+'__saved to__'+data_fn)
def save_log_file_of_parameters(root_savename,savepath,parameter_names,parameters_values):
log_fn = os.path.abspath(os.path.join(savepath, root_savename+'.txt'))
save_non_tf_data(names=parameter_names, data=parameters_values, filename=root_savename+'.params', savePath=savepath)
with open(log_fn, 'w') as f:
for ppp in range(0,len(parameter_names)):
f.write(str(parameter_names[ppp])+': '+str(parameters_values[ppp])+'\n')
print('LOG SAVED.')
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
def str_splitter_into_2_intervals(str):
idx_split = str.find('_')
int1 = int(str[0:idx_split])
int2 = int(str[idx_split+1:len(str)])
return [int1,int2]
def func_exp(x, a, b, c,d):
return a * np.exp(-b * (x-c)) + d
def func_exp_inv(y,a,b,c,d):
return np.divide(np.log(np.divide(y-d,a)),-b) + c
def func_linear(x,a,b,c):
return a*(x-c) + b
def func_linear_inv(x,a,b,c):
return ((x - b)/a)+c
def func_quad(x,a,b,c):
return a*((x-b)**2) + c
def func_3deg(x,a,b,c,d,e):
return a * ((x - b) ** 3) + c * ((x - d) ** 2)+ e
def r_square_for_curver_fit(y_data,y_model):
residuals = y_data - y_model
ss_res = np.sum(residuals ** 2)
ss_tot = np.sum((y_data - np.mean(y_data)) ** 2)
return 1 - (ss_res / ss_tot)
def residual_sum_of_sqrs(y_data,y_model):
residuals = y_data - y_model
ss_res = np.sum(residuals ** 2)
return ss_res
def remove_files_op(files_found_l):
rem_idx_raw = input('List idx to remove sep by commas')
rem_idx = str(rem_idx_raw)
fls_l = []
idx_l = []
search_on = True
while search_on:
split_idx = rem_idx.find(',')
print(split_idx)
if split_idx == -1:
idx_l.append(int(rem_idx))
fls_l.append(files_found_l[int(rem_idx)])
search_on = False
break
idx_to_remove = int(rem_idx[0:split_idx])
rem_idx = rem_idx[split_idx + 1:len(rem_idx)]
idx_l.append(idx_to_remove)
fls_l.append(files_found_l[idx_to_remove])
print('Will remove these:')
print(idx_l)
proceed = input('Proceed [y/n]')
if proceed == 'n':
sys.exit(2)
for kk in range(0, len(fls_l)):
files_found_l.remove(fls_l[kk])
return files_found_l,proceed
def select_files_op(files_found_l):
sel_idx_raw = input('List idx to select sep by commas')
sel_idx = str(sel_idx_raw)
fls_l = []
idx_l = []
search_on = True
while search_on:
split_idx = sel_idx.find(',')
print(split_idx)
if split_idx == -1:
idx_l.append(int(sel_idx))
fls_l.append(files_found_l[int(sel_idx)])
search_on = False
break
idx_to_select = int(sel_idx[0:split_idx])
sel_idx = sel_idx[split_idx + 1:len(sel_idx)]
idx_l.append(idx_to_select)
fls_l.append(files_found_l[idx_to_select])
print('Will process these:')
print(idx_l)
proceed = input('Proceed [y/n]')
if proceed == 'n':
sys.exit(2)
process_l = []
for kk in range(0, len(fls_l)):
process_l.append(fls_l[kk])
return process_l,proceed
# def compute_f_statistic(complex_model_rss, simple_model_rss,num_samples,)