-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
479 lines (363 loc) · 14.8 KB
/
Code.py
File metadata and controls
479 lines (363 loc) · 14.8 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env python
# coding: utf-8
# In[1148]:
import copy
from collections import defaultdict
def main():
count=defaultdict(int)
F=defaultdict(list)
k=1
d="data.txt"
p="para.txt"
S=Read_Data(d)
MS,SDC=Read_Para(p)
N=len(S)
M=[] # according to MIS(i)’s stored in MS
for i ,v in sorted(MS.items(),key=lambda kv:kv[1]):
M.append(i)
L,Count_L=Init_Pass(M,S,N,MS) #first pass over S
F[k]=F1gen(L,MS,Count_L,count,N)
printOutput(k, F[k], count)
k=k+1
while (F[k-1]):
Candidates=[]
if k==2:
Candidates=Cand2Gen(L,Count_L,MS,SDC,N)
else:
Candidates=MSCandGen(F[k-1],Count_L,SDC,MS,N) # To be Written
for t,s in S.items():
for cand in Candidates:
isSequence=isSubsequence(cand,s)
if isSequence:
count[str(cand)]+=1
F[k]=GenFk(Candidates,MS,count,N)
printOutput(k, F[k], count)
k+=1
return
# In[1149]:
def Read_Data(file):
with open(file) as f:
line=f.readline()
cnt=0
S=defaultdict(list)
while line:
sequence=[]
cnt+=1
line=line.replace(">",'').replace('<','').replace("}{","_").replace("{","").replace("}","")
for i in line.split("_"):
element=[]
for item in i.split(","):
element.append(int(item.strip(" \n")))
sequence.append(element)
S[cnt]=sequence
line=f.readline()
return S
# In[1150]:
def Read_Para(file):
with open(file) as f:
line=f.readline()
MS=defaultdict()
while line:
if "MIS" in line:
element=line[line.find("(")+1:line.find(")")]
mis=line[line.find("=")+1:].lstrip(" ").lstrip("\n")
MS[int(element)]=float(mis)
else:SDC=float(line[line.find("=")+1:].lstrip(" "))
line=f.readline()
return MS,SDC
# In[1151]:
# Initial pass over transaction and counting the occurence of each item
#this is eliminate any item that is below the lowest MIS
def Init_Pass(M,S,N,MS):
Count=defaultdict(int)
Count_L=defaultdict(int)
L=[]
for i,s in S.items():
for item in M:
if item in sum(s,[]):
Count[item]+=1
for item in M:
if float(Count[item]/N) >= float(MS[item]):
Minimum_MIS,i=float(MS[item]),M.index(item)
break
for index in range(i,len(M)):
if float(Count[M[index]]/N) >= Minimum_MIS:
L.append(M[index])
Count_L[M[index]]=Count[M[index]]
return L,Count_L
# In[1152]:
def F1gen(L,MS,Count,count, N):
F1=[]
for item in L:
#Item satisfying its own MIS
if float(Count[item]/N)>=MS[item]:
itemString= "[" + str(item) +"]"
count[itemString] = Count[item]
# print("item--->{0} count--->{1} ms--->{2}".format(item,Count[item],MS[item]))
F1.append([item])
return F1
# In[1153]:
def Cand2Gen(L,L_count,MS,sdc,n):
def Duplicate_Removal(C2):
#remove candidates with same element like [10,10] and candidates not in lexicographic order like [30,20]
C2[:] = [c for c in C2 if type(c[0]) is int and c[0]<c[1] or type(c[0]) is list]
return C2
if L:
c2 = []
for elemt in L:
if(L_count[elemt] >= MS[elemt]):
for next_elemt in L[L.index(elemt):]:
if L_count[next_elemt] >= MS[elemt] and abs ( float(L_count[next_elemt]/n) - float(L_count[elemt]/n)) <= sdc:
c2.append([elemt, next_elemt])
c2.append([[elemt],[next_elemt]])
for next_elemt in L[:L.index(elemt)]:
if L_count[next_elemt] >= MS[elemt] and abs(float(L_count[next_elemt]/n) - float(L_count[elemt]/n)) <= sdc:
c2.append([elemt, next_elemt])
c2.append([[elemt],[next_elemt]])
C2=Duplicate_Removal(c2)
return C2
# In[1154]:
def isSubsequence(c,s):
#to handle cases like [10,20,30] convert it into [[10,20,30]]
if type(c[0]) is int:
c=[c]
NumElement_C=len(c)
flag=[0]*len(s)
index=-1
for element in c:
for i in range(index+1,len(s)):
if len(element)==len(set(element)):
if set(element).issubset(set(s[i])) and not flag[i]:
index=i
flag[i]=1
break
if sum(flag)==NumElement_C:
return True
else:
return False
# In[1155]:
def GenFk(Candidates,MS,count,N):
F=[]
for cand in Candidates:
if type(cand[0]) is list:
temp=sum(cand,[])
else:temp=copy.deepcopy(cand)
min_mis=float('inf')
for item in temp:
min_mis=min(min_mis,MS[item])
if float(count[str(cand)]/N)>=float(min_mis) :
F.append(cand)
return F
# In[1156]:
def MSCandGen(F,Lcount,SDC,MS,N):
convert=lambda s:sum(s,[]) if type(s[0]) is list else copy.deepcopy(s) # merges lists
candlist=[]
def CheckLastMIS(s2,MS):
convert=lambda s2:sum(s2,[]) if type(s2[0]) is list else copy.deepcopy(s2)
temp=convert(s2)
min_mis=float('inf')
last_item=temp[-1]
if len(set(temp))==1:return False # for s=[[30],[30]]
for item in temp:
if MS[item]<=MS[last_item] and item!=last_item:
return False
return True
def checkIfFirstMISIsSmallest(s, MS):
convert=lambda s2:sum(s2,[]) if type(s2[0]) is list else copy.deepcopy(s2)
temp=convert(s)
min_mis=float('inf')
first_item=temp[0]
if len(set(temp))==1:return False
for item in temp:
if MS[item]<=MS[first_item] and item!=first_item:
return False
return True
def Size(s):
if type(s[0]) is list:
return len(s)
else:
return 1
def length(s):
convert=lambda s:sum(s,[]) if type(s[0]) is list else copy.deepcopy(s)
return len(convert(s))
def Last_MIS_Less(s1,s2):
temp_s1=convert(s1)
temp_s2=convert(s2)
s1first=temp_s1[0]
s2first=temp_s2[0]
MISfirsts1=MS[temp_s1[0]]
MISlasts2=MS[temp_s2[-1]]
firstS1=temp_s1.pop(0) #remove first
if len(temp_s2) == 2 :
secondlastS2=temp_s2.pop(0)
else:
secondlastS2=temp_s2.pop(-2)# remove last but 1
if temp_s1==temp_s2 and MISlasts2<MISfirsts1 and abs(float(Lcount[firstS1]/N)-float((Lcount[secondlastS2]/N)))<=SDC:
if type(s1[0]) is list and len(s1[0])==1:# first is seperate element in s1
if type(s2[0]) is int: # if s2=[10,20] then first element of s1 should be added as list and appended with s2
cand=[]
cand.append(copy.deepcopy(s1[0]))
cand.append(copy.deepcopy(s2))
candlist.append(cand)
else:
cand=[copy.deepcopy(s1[0])]+copy.deepcopy(s2) #if s2=[[10],[20]] add just the first element of s1 as list to s2 .
candlist.append(cand)
if length(s2)==2 and Size(s2)==2 and s1first<s2first: # generate c2
cand=copy.deepcopy(s2)
cand[0].insert(0,s1[0][0])
candlist.append(cand)
elif (length(s2)==2 and Size(s2)==1 and s1first<s2first) or length(s2)>2: # just generate c1
if type(s2[0]) is int:
cand=copy.deepcopy(s2)
cand.insert(0,s1[0])
candlist.append(cand)
else:
first=copy.deepcopy(s1[0])
if isinstance(first,list):item=first[0]
else:item=first
cand=copy.deepcopy(s2)
cand[0].insert(0,item)
candlist.append(cand)
def DefaultJoin(s1,s2):
temp_s1=convert(s1)
temp_s2=convert(s2)
firstS1=temp_s1.pop(0) #remove first
lastS2=temp_s2.pop()#remove last
if temp_s1==temp_s2 and abs(float(Lcount[firstS1]/N)-float((Lcount[lastS2]/N)))<=SDC:
last=copy.deepcopy(s2[-1])
if type(last) is list and len(last)==1:
if type(s1[0]) is int:
cand=[]
cand.append(copy.deepcopy(s1))
cand.append(copy.deepcopy(s2[-1]))
candlist.append(cand)
else:
cand=copy.deepcopy(s1)+[copy.deepcopy(s2[-1])]
candlist.append(cand)
else:
if type(s1[0]) is int: # add the last element at end of list
if isinstance(last,list):
item=last[-1]
else:
item=last
cand=copy.deepcopy(s1)
cand.append(item)
candlist.append(cand)
else:
if isinstance(last,list):item=last[-1]
else:item=last
cand=copy.deepcopy(s1)
cand[-1].append(item)
candlist.append(cand)
def FirstMISList(s1, s2):
temp_s1 = convert(s1)
temp_s2 = convert(s2)
pop_temp_s1 = copy.deepcopy(temp_s1)
pop_temp_s1.pop(1) #remove s1's 2nd element
pop_temp_s2 = temp_s2[:-1] #remove last element from s1
if pop_temp_s1 == pop_temp_s2 and MS[temp_s2[-1]]>= MS[temp_s1[0]] and abs(float(Lcount.get(temp_s2[-1])/N)- float(Lcount.get(temp_s1[1])/N)) <= SDC:#check if s1 without element at 2 and s2 without last element are the same
if isinstance(s2[-1], list) and length(s2[-1]) == 1: #l is added at the end of the last element of s1 to form another candidate sequence c2.
#if last element of s2 is a list and if its the only element in it
#if s1 is a not a list of lists
if isinstance(s1[-1], int):
cand=[]
cand.append(copy.deepcopy(s1))
cand.append(copy.deepcopy(s2[-1]))
candlist.append(cand)
else: #if s2 is a list
cand=copy.deepcopy(s1)+[copy.deepcopy(s2[-1])] #if s2=[[10],[20]] add just the first element of s1 as list to s2 .
candlist.append(cand)
if length(s1) == 2 and Size(s1) == 2 and temp_s2[-1] > temp_s1[-1]:
#check leng and size of s1 and the last item of s2 is greater than the last item of s1
cand=copy.deepcopy(s1)
cand[-1].extend(copy.deepcopy(s2[-1]))
candlist.append(cand)
elif (length(s1) == 2 and Size(s1) == 1 and temp_s2[-1] > temp_s1[-1]) or length(s1) > 2:
if type(s1[-1]) is int:
cand=copy.deepcopy(s1)
cand.append(s2[-1])
candlist.append(cand)
else:
last=copy.deepcopy(s2[-1])
if isinstance(last,list):item=last[-1]
else:item=last
cand=copy.deepcopy(s1)
cand[-1].append(item)
candlist.append(cand)
for s1 in F:
for s2 in F:
if checkIfFirstMISIsSmallest(s1, MS):
FirstMISList(s1, s2)
elif CheckLastMIS(s2,MS):
Last_MIS_Less(s1,s2)
else:
DefaultJoin(s1,s2)
duplicate=[]
for i in range(0, len(candlist)-1):
for j in range(i+1, len(candlist)-2):
if candlist[i] == candlist[j]:
duplicate.append(candlist[i])
for i in duplicate:
candlist.remove(i)
for c in candlist:
if not prune(c,MS,F):
candlist.remove(c)
return candlist
# In[1157]:
import copy
def prune(c,MS,F):
def MinMIS(s, MS):
convert=lambda s2:sum(s2,[]) if type(s2[0]) is list else copy.deepcopy(s2)
temp=convert(s)
Min_MIS=float('inf')
for item in temp:
Min_MIS=min(Min_MIS,MS[item])
return Min_MIS
Min_MIS=MinMIS(c, MS)
if isinstance(c[0],list):
for I in range(len(c)):
for item in range(len(c[I])):
temp=copy.deepcopy(c)
if len(c[I])==1:
temp.remove(c[I])
if len(temp)==1:temp=temp[0]
else:
temp[I].remove(c[I][item])
temp_mis=MinMIS(temp, MS)
if temp_mis==Min_MIS:
if temp not in F:
return False
else:
for i in range(len(c)):
temp=copy.deepcopy(c)
temp.remove(c[i])
temp_mis=MinMIS(temp, MS)
if temp_mis==Min_MIS:
if temp not in F:
return False
return True
# In[1158]:
def printOutput(k, F, count):
file = open("result.txt", "a")
file.write("\nNo of length :{} Frequent sequences: {}\n".format(k, len(F)))
for cand in F:
cand_string = list(str(cand))
if isinstance(cand[-1],list):
cand_string[0] = '<'
cand_string[-1] = '>'
else:
cand_string[0] = '<{'
cand_string[-1] = '}>'
for i in range(0, len(cand_string)):
if(cand_string[i] == '['):
cand_string[i] = '{'
if(cand_string[i] == ']'):
cand_string[i] = '}'
if(cand_string[i] == ','):
if(cand_string[i-1] == '}'):
cand_string[i] = ''
cand_string[i+1] = ''
cand_string = ''.join(cand_string)
file.write("{} count: {}\n".format(cand_string, count[str(cand)]))
# In[1159]:
if __name__ =='__main__':main()