-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpectrumGenerator.py
More file actions
109 lines (92 loc) · 4.08 KB
/
Copy pathSpectrumGenerator.py
File metadata and controls
109 lines (92 loc) · 4.08 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
#!/usr/bin/env python3
import csv
import os
import numpy as np
import random
import pickle
class ODIN():
def __init__(self):
pass
def genSpectrum(self,C,B,T,gamma,delta,freq_buggy,freq_nonbuggy):
assert(C > 0 and B > 0 and T > 0)
assert(C >= B)
assert(gamma >= 0.0 and gamma <= 1.0)
assert(type(delta) == list)
assert(len(delta) == B)
# creating activity matric, error vector and spectrum
Activity_Mat = np.zeros((T,C),dtype="int")
ErrorVector = np.zeros((T),dtype="int")
#considering B number of components randomly as buggy components.
buggy_comps = random.sample(range(C),B)
#will help to decrease search time.
buggy_comps_dict = {}
for c in buggy_comps:
buggy_comps_dict[c] = None
# generating activity matrix
b_index = 0
nb_index = 0
for c in range(C):
if c in buggy_comps_dict:
#rho = freq_buggy[b_index]
Activity_Mat[:,c] = np.random.binomial(1,freq_buggy[b_index],T)
b_index += 1
else:
#rho = freq_nonbuggy[nb_index]
Activity_Mat[:,c] = np.random.binomial(1,freq_nonbuggy[nb_index],T)
nb_index += 1
# generating Error vector.
for i in range(T):
#check if any buggy component is activated
if np.sum(Activity_Mat[i,buggy_comps]) != 0:
#compute btp probability
product =1
for j, delta_j in zip(buggy_comps,delta):
e_j = Activity_Mat[i,j]
product *= (1 - delta_j)**e_j
testfailed_prob = 1- product
k = np.random.binomial(1,testfailed_prob);
if k ==1:
ErrorVector[i] = 1
else:
if np.random.binomial(1,gamma) == 1:
ErrorVector[i] =1
else :
ErrorVector[i] =0
#form spectrum using activity matrix and error-vector
Spectrum = np.concatenate((Activity_Mat,ErrorVector.reshape(T,1)),axis=1)
return Activity_Mat, ErrorVector, buggy_comps, Spectrum
def sample(self, N=1, C=100, B=1, T=100, gamma=0.0, delta=[1.0], outputpath="generated_spectrums/", return_spectrums=False):
# if output folder not exist then create folder
if return_spectrums !=True:
if not os.path.exists(outputpath):
os.makedirs(outputpath)
#variable used to store spectrums if return_spectrums = True
SpectrumsList=[];
BugList=[]
act_buggy = np.clip(np.random.normal(0.3, 0.01, B), a_min = 0.1, a_max = 1)
act_nonbuggy = np.random.uniform(0.1,1, C-B)
# generate N test cases
for i in range(N):
A,E,buggy_comps,spectrum=self.genSpectrum(
C=C,B=B, T=T,
gamma = gamma, delta=delta,freq_buggy = act_buggy,freq_nonbuggy = act_nonbuggy)
if return_spectrums==True:
SpectrumsList.append(spectrum)
BugList.append(buggy_comps)
else:
with open(outputpath+str(np.round(gamma,2))+"_"+str(delta)+"_Spectrum_"+str(i+1)+".txt","w") as File:
writer = csv.writer(File)
writer.writerows(spectrum)
with open(outputpath+str(gamma)+"_"+str(delta)+"_buggy_"+str(i+1)+".txt","w") as File:
writer = csv.writer(File)
writer.writerow(buggy_comps)
if return_spectrums==True:
return SpectrumsList,BugList
# #-----Testing function
# sampler = ODIN()
# spectrums, bugs = sampler.sample(N=1000, C=10, B=4, T=100,gamma=0.0,delta=[0.1]*4,outputpath="test/",return_spectrums=True)
# failed = np.zeros(len(spectrums))
# for index, spec in enumerate(spectrums):
# failed[index] = np.sum(spec[:,-1]) / spec.shape[0]
# print(failed)
# print(np.mean(failed), np.median(failed))