-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTar3.py
283 lines (239 loc) · 8.61 KB
/
Tar3.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 19:22:26 2020
@author: Sara Baradaran, Mahdi Heidari, Ali Kamali
"""
import subprocess,re,itertools
import numpy as np
def runTAR3(I,V,VV,VVV,inode,smooth=False,vv=None):
NODE_PATH='./.node-{0}.txt'.format(inode)
RUN_DIR='./analysis/tar3/sample/'
lines=[]
num_vars=len(V[0])
if num_vars == 0:
return None
for item in V:
if item in VV:
item.append('good')
elif item in VVV:
item.append('bad')
lines.append(list(map(str,item)))
with open('./analysis/tar3/sample/data.data','w+') as handler:
for line in lines:
handler.write(','.join(line) + '\n')
cp=subprocess.run(["../source/tar3/tar3", "data"],universal_newlines=True, stdout=subprocess.PIPE, cwd=RUN_DIR)
lines=cp.stdout.split('\n')
res=[]
for x in lines:
if re.search('^\d worth=.*',x.lstrip()) is not None:
res.append(x.strip())
if len(res) > 0:
with open(NODE_PATH,'w+') as handler:
for item in res:
handler.write(item + '\n')
if smooth:
var_names=_seperateValues(res.copy(),only_names=True)
vals=_seperateValues(res.copy())
c_VV,c_vv=_correctInputs(vals, VV, vv)
final_res=[]
for i in var_names:
idx=int(i.split('_')[1]) -1
xdata=(np.array(c_VV)[:,idx]).copy()
ydata=(np.array(c_vv)[:,idx]).copy()
points_data=np.array(_sortPoints(xdata,ydata))
islip=isLipschitz(points_data, 20)
if islip:
r=tuple(('{}'.format(i),points_data[:,0],points_data[:,1]))
final_res.append(r)
else:
r=tuple(('{}'.format(i),None,None))
final_res.append(r)
return final_res
elif len(res) == 0:
with open(NODE_PATH,'w+') as handler:
error='Error: no cdf value found!'
if error in lines:
handler.write("No CDF.")
else:
goodPart=None
for line in lines:
c=line.strip()
if re.search('good:.*',c) is not None:
goodPart=c
if goodPart is None:
handler.write('No Value')
else:
goodPart=goodPart.split('[')[1]
goodPart=goodPart.replace(']','').split('-')[1]
goodPart=goodPart.replace('%','')
goodPart=int(goodPart)
if goodPart > 70:
handler.write("R")
else :
handler.write('No Value')
if smooth:
final_res=[]
for i in range(num_vars):
xdata=(np.array(VV)[:,i]).copy()
ydata=(np.array(vv)[:,i]).copy()
points_data=np.array(_sortPoints(xdata,ydata))
islip=isLipschitz(points_data, 20)
if islip:
r=tuple(('var_{}'.format(i+1),points_data[:,0],points_data[:,1]))
final_res.append(r)
else:
r=tuple(('var_{}'.format(i+1),None,None))
final_res.append(r)
return final_res
def _correctInputs(vals,system_in,unit_in):
sinputs=[]
uinputs=[]
for val in vals:
indx=[]
bounds=[]
for var_name,bnd in val:
indx.append(int(var_name.split('_')[1])-1)
bounds.append((float(bnd[0]),float(bnd[1])))
for inp_indx in range(len(system_in)):
sys_inp=system_in[inp_indx]
unit_inp=unit_in[inp_indx]
flag=False
for i in range(len(indx)):
if sys_inp[indx[i]] >= bounds[i][0] and sys_inp[indx[i]] <= bounds[i][1]:
flag=True
else:
flag=False
break
if flag == True:
sinputs.append(sys_inp)
uinputs.append(unit_inp)
return (sinputs,uinputs)
def _sortPoints(xdata,ydata):
data=[]
for i in range(0,len(xdata)):
data.append([xdata[i],ydata[i]])
_data=[]
for i in data:
if i not in _data:
_data.append(i.copy())
del(data)
_data.sort(key=lambda t: t[0])
return _data
def isLipschitz(datas,k):
dx = np.diff(np.array(datas)[:,0])
#extra condition bcz any does not act correctly
if (np.any(dx)<=0) or ( 0 in dx):
return False
points=list(itertools.combinations(datas, 2))
max=None
for point1,point2 in points:
x1,y1=point1
x2,y2=point2
if x1==x2 and y1==y2:
continue
s=abs(x1-x2)
r=abs(y1-y2)
if r == 0:
return False
t=s/r
if max is None:
max=t
if t > max:
max=t
return max<k
def _seperateValues(res,only_names=False):
var=[]
if len(res) > 0:
for t in res:
t=t.replace(')','')
t=t.split('\t')[1]
t=t.replace('[','')
items=t.split(' ')
list_var=[]
for item in items:
name=item.split('=')[0]
item=item.split('=')[1]
item=item.replace(']', '')
if only_names:
if name not in var:
var.append(name)
else:
bnds=item.split('..')
bnds[0]=bnds[0].strip()
bnds[1]=bnds[1].strip()
list_var.append((name,bnds))
if only_names == False:
var.append(list_var)
return var
def getPosOnConfigFile(self,config_file='./analysis/tar3/sample/data.names'):
handler=open(config_file,'r+')
lines=handler.readlines()
handler.close()
change_idx=0;now_idx=0;attr_idx=0;indx=0;now_visitted=False;change_visitted=False
for line in lines:
if 'NOW' in line:
now_visitted=True
attr_idx=indx-1
while True:
if lines[attr_idx-1 ] != '\n':
break
attr_idx=attr_idx-1
indx=indx+1
continue
if now_visitted == True and line == '\n':
now_idx=indx
indx=indx+1
now_visitted=False
continue
if 'CHANGES' in line:
change_visitted=True
indx=indx+1
continue
if change_visitted and line =='\n':
change_idx=indx
break
indx = indx + 1
return (attr_idx,now_idx,change_idx)
def generateTar3ConfingFile(arg_status,interest_indexes,config_file='./analysis/tar3/sample/data.names'):
'''
this function build tar3 data.names file based on unit arg status and intersting index on char stars in unit
'''
wrlist=[]
wrlist.extend(['|Class:\n','bad,good\n', '\n', '|Attributes : 4\n'])
nowlist=[ 'NOW\n']
changelist=[ 'CHANGES\n']
current_indx=1
maps={}
for indx,tp in arg_status.items():
if tp != 'charPointer':
var_name='var_{}'.format(current_indx)
wrlist.append(var_name + ':continuous\n')
nowlist.append(var_name + ':true\n')
changelist.append(var_name + ':true\n')
current_indx=current_indx+1
else:
for var_ind,pos in interest_indexes:
if int(var_ind.split('_')[1]) == indx:
var_name='var_{}'.format(current_indx)
wrlist.append(var_name + ':continuous\n')
nowlist.append(var_name + ':[32;126]\n')
changelist.append(var_name + ':true\n')
current_indx=current_indx+1
if indx in maps.keys():
maps[indx].append((var_name,pos))
else:
maps[indx]=[(var_name,pos)]
scorelist=[ 'SCORE\n', '4,8\n']
wrlist.append('\n')
nowlist.append('\n')
changelist.append('\n')
wrlist.extend(nowlist)
wrlist.extend(changelist)
wrlist.extend(scorelist)
file=open(config_file,'w+')
file.writelines(wrlist)
file.close()
if len(nowlist) == 2:
return None
return maps