-
Notifications
You must be signed in to change notification settings - Fork 0
/
chi2_after_cluster.py
executable file
·185 lines (165 loc) · 6.08 KB
/
chi2_after_cluster.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
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import sys
def DTWDistance(s1, s2,w):
DTW={}
w = max(w, abs(len(s1)-len(s2)))
for i in range(-1,len(s1)):
for j in range(-1,len(s2)):
DTW[(i, j)] = float('inf')
DTW[(-1, -1)] = 0
for i in range(len(s1)):
for j in range(max(0, i-w), min(len(s2), i+w)):
dist= (s1[i]-s2[j])**2
DTW[(i, j)] = dist + min(DTW[(i-1, j)],DTW[(i, j-1)], DTW[(i-1, j-1)])
return np.sqrt(DTW[len(s1)-1, len(s2)-1])
def calculateChi(weightedIns, expIns, use_weights = True):
"""
Calculates chis
"""
mixed_term_ = 0.0
square_calc_ = 0.0
Sindex = 0
if use_weights:
weight1 = weightedIns[-1]
weight2 = expIns[-1]
else:
weight1 = 1
weight2 = 1
weightedIns = weightedIns[:-1]
expIns = expIns[:-1]
#for ins in expIns:
# Iobs=ins
# mixed_term_ += Iobs*weightedIns[Sindex]
# square_calc_ += weightedIns[Sindex]*weightedIns[Sindex]
# Sindex+=1
#scale_factor = mixed_term_/square_calc_
scale_factor = 1
chi2_=0.0
square_obs_ = 0.0
Sindex = 0
for ins in expIns:
Iobs=ins
square_obs_ += Iobs*weightedIns[Sindex]
chi2_+=(weight2*Iobs-weight1*weightedIns[Sindex])*(weight2*Iobs-weight1*weightedIns[Sindex])
Sindex+=1
#print chi2_, square_obs_
chi2_=chi2_/square_obs_
return chi2_
def interpolate(results):
rows,cols = np.shape(results)
peak_x = []
x_grid = np.linspace(0,100,100)
fout = open("output_normalized.csv","w")
all_yintensities = []
for i in range(cols-1):
row = results[:,i]
non_zero = len(row[row>0])
max_peak = np.argmax(row)
peak_x.append(float(max_peak)/non_zero)
norm_row = np.linspace(0,100,num=non_zero)
interp_y = np.interp(x_grid, norm_row, row[row>0])
all_yintensities.append(interp_y)
fout.writelines(["%.2f, " % x for x in interp_y])
fout.write("\n")
fout.close()
return all_yintensities, peak_x
def create_heatmap(chi2_array):
plt.imshow(chi2_array, cmap='jet', interpolation='nearest')
plt.savefig("heat_map.png")
plt.show()
def generate_chis(all_yintensities_1, all_yintensities_2, use_weights):
#all_yintensities_1, peak_x_1 = interpolate(data1)
#all_yintensities_2, peak_x_2 = interpolate(data2)
all_yintensities_1 = np.transpose(all_yintensities_1)
all_yintensities_2 = np.transpose(all_yintensities_2)
print("Chi2 calculations")
jindex = 0
rows1 = len(all_yintensities_1)
rows2 = len(all_yintensities_2)
chi2_array = np.zeros((rows1,rows2))
dtw_array = np.zeros((rows1,rows2))
cumulative_chi2 = 0
cumulative_dtw = 0
iindex = 0
for int1 in all_yintensities_1:
jindex=0
for int2 in all_yintensities_2:
dtw = DTWDistance(int2,int1,5)
dtw_array[iindex][jindex] =dtw
chi2 = calculateChi(int1,int2, use_weights)
chi2_array[iindex][jindex] = chi2
#if chi2b<chi2a:
# chi2_array[iindex][jindex] = chi2b
#else:
# chi2_array[iindex][jindex] = chi2a
jindex+=1
iindex+=1
matching_indexes = []
forbiden_indexes = []
for i, row in enumerate(chi2_array):
min_i = (np.argmin(row))
index=0
while (min_i in forbiden_indexes):
min_i = np.argsort(row)[index]
index+=1
matching_indexes.append((i,min_i))
forbiden_indexes.append(min_i)
legend_lines = []
colors = ['red', 'blue', 'green', 'black', 'orange', 'yellow']
for c_i,matches in enumerate(matching_indexes):
i1 = matches[0]
i2 = matches[1]
chi2_fl = round(chi2_array[i1][i2],3)
dtw_fl = round(dtw_array[i1][i2],1)
cumulative_chi2 += chi2_fl
cumulative_dtw += dtw_fl
line1, = plt.plot(all_yintensities_1[i1][:-1],
linestyle='dashed', color=colors[c_i])
line2, = plt.plot(all_yintensities_2[i2][:-1],
label="q = "+str(chi2_fl)+", d = "+str(dtw_fl),
color = colors[c_i])
#legend_lines.append(line1)
legend_lines.append(line2)
plt.legend(handles=legend_lines)
#create_heatmap(chi2_array.transpose())
if use_weights:
print ("Cumulative and max chi2 and dtw (weights)", cumulative_chi2, cumulative_dtw)
else:
print ("Cumulative and max chi2 and dtw", cumulative_chi2, cumulative_dtw)
plt.savefig("chi2_cmp.png", dpi=600)
plt.show()
def generate_chis_pairs(all_yintensities_1, all_yintensities_2, use_weights):
#all_yintensities_1, peak_x_1 = interpolate(data1)
#all_yintensities_2, peak_x_2 = interpolate(data2)
all_yintensities_1 = np.transpose(all_yintensities_1)
all_yintensities_2 = np.transpose(all_yintensities_2)
print("Chi2 pairs calculations")
jindex = 0
rows1 = len(all_yintensities_1)
chi2_array = np.zeros((rows1))
cumulative_chi2 = 0
iindex = 0
for int1 in all_yintensities_1:
chi2 = calculateChi(int1,all_yintensities_2[iindex], use_weights)
chi2_array[iindex] = chi2
cumulative_chi2 += chi2_array[iindex]
iindex+=1
if use_weights:
print ("Cumulative and max chi2 and individual weights", cumulative_chi2, np.argmin(chi2_array))
else:
print ("Cumulative and max chi2 and individual weights", cumulative_chi2, np.argmin(chi2_array))
if __name__ == "__main__":
fin = open(sys.argv[1])
fin1 = open(sys.argv[2])
data1 = np.genfromtxt(sys.argv[1], dtype="float64", delimiter=",")
data2 = np.genfromtxt(sys.argv[2], dtype="float64", delimiter=",")
#print("Comparing with weights "+sys.argv[1]+" with "+sys.argv[2])
#generate_chis(data1,data2, True)
print("Comparing with no weights "+sys.argv[1]+" with "+sys.argv[2])
generate_chis(data1,data2, False)
#print("Comparing "+sys.argv[1]+" with "+sys.argv[1])
#generate_chis(data1,data1)
#print("Comparing "+sys.argv[2]+" with "+sys.argv[2])
#generate_chis(data2,data2)