-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhypothesisAnalyse.py
215 lines (147 loc) · 7.46 KB
/
hypothesisAnalyse.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
import sys
import os
import matplotlib
import datetime
matplotlib.use('Agg')
import numpy as np
from numpy import exp, log
import matplotlib.pyplot as plt
import sys, os
import json
import pymultinest
modelSetB = [['singleGaussian', 'singleGaussian', 2, 2, ['mu', 'sigma', 'mu', 'sigma']],
['singleGaussian', 'twoGaussian', 2, 5, ['mu', 'sigma', 'mu1', 'mu2', 'sigma1', 'sigma2', 'alpha']],
['singleGaussian', 'uniform', 2, 2, ['mu', 'sigma', 'mMin', 'mMax']],
['twoGaussian', 'singleGaussian', 5, 2, ['mu1', 'mu2', 'sigma1', 'sigma2', 'alpha', 'mu', 'sigma']],
['twoGaussian', 'twoGaussian', 5, 5, ['mu1', 'mu2', 'sigma1', 'sigma2', 'alpha', 'mu1', 'mu2', 'sigma1', 'sigma2', 'alpha']],
['twoGaussian', 'uniform', 5, 2, ['mu1', 'mu2', 'sigma1', 'sigma2', 'alpha', 'mMin', 'mMax']],
['uniform', 'singleGaussian', 2, 2, ['mMin', 'mMax', 'mu', 'sigma']],
['uniform', 'twoGaussian', 2, 5, ['mMin', 'mMax', 'mu1', 'mu2', 'sigma1', 'sigma2', 'alpha']],
['uniform', 'uniform', 2, 2, ['mMin', 'mMax', 'mMin', 'mMax']]]
modelSetA = [['singleGaussian', 2, ['mu', 'sigma']],
['twoGaussian', 5, ['mu1', 'mu2', 'sigma1', 'sigma2', 'alpha']],
['uniform', 2, ['mMin', 'mMax']]]
def roundNumber(number):
return round(number, 4)
def cleanRound(number, dec=3):
newNum = str(round(number, 3))
print(newNum)
missing = dec - len(newNum.split('.')[1])
return newNum + '0' * missing
def readStats(statsFile):
globalEvidenceLine = statsFile.readline()
nestedGlobalEvidenceLine = statsFile.readline()
globalEvidenceLine
print(globalEvidenceLine.split())
globalEvidence = float(globalEvidenceLine.split()[-3])
print(globalEvidenceLine.split()[-1])
evidenceStd = float(globalEvidenceLine.split()[-1])
print(globalEvidence, evidenceStd)
return globalEvidence, evidenceStd
def fractionalEvidence(resultList, resultDirectory, saveName="fracResults.txt"):
with open(resultDirectory + saveName,"w+") as z:
totalEv = 0
for result in resultList:
totalEv += result[-2]
### Hypo B
if len(resultList[0]) == 4:
for result1 in resultList:
tb = ' & '
tableString = ' ' + result1[0][:4] + '-' + result1[1][:4] + tb + str(result1[2]/totalEv) + "\\" * 2
z.write(tableString + '\n')
else:
for result1 in resultList:
tb = ' & '
tableString = ' ' + result1[0][:4] + tb + str(result1[1]/totalEv) + "\\" * 2
z.write(tableString + '\n')
return
def makeTableB(resultList, resultDirectory, saveName="tableResults.txt"):
with open(resultDirectory + saveName,"w+") as z:
reverseNameTitle = ' ' * 4 + ' & '.join([result[0][:4] + '-' + result[1][:4] for result in resultList[::-1]]) + "\\" * 2
z.write(reverseNameTitle + '\n')
for index, result1 in enumerate(resultList):
print(index, result1)
evidence1 = result1[2]
lineBFs = []
#for result2 in resultList[:-index - 1]:
for result2 in resultList[:index:-1]:
evidence2 = result2[2]
bayesFactor = evidence1/evidence2
lineBFs.append(cleanRound(bayesFactor))
tb = ' & '
tableString = ' ' + result1[0][:4] + '-' + result1[1][:4] + tb + tb.join(lineBFs) + "\\" * 2
z.write(tableString + '\n')
return
def makeTableA(resultList, resultDirectory, saveName="tableResults.txt"):
with open(resultDirectory + saveName,"w+") as z:
reverseNameTitle = ' ' * 3 + ' & '.join([result[0][:4] for result in resultList[::-1]]) + "\\" * 2
z.write(reverseNameTitle + '\n')
for index, result1 in enumerate(resultList):
print(index, result1)
evidence1 = result1[1]
lineBFs = []
#for result2 in resultList[:-index - 1]:
for result2 in resultList[:index:-1]:
evidence2 = result2[1]
bayesFactor = evidence1/evidence2
lineBFs.append(cleanRound(bayesFactor))
tb = ' & '
tableString = ' ' + result1[0][:4] + tb + tb.join(lineBFs) + "\\" * 2
z.write(tableString + '\n')
return
def analyseMainB(resultDirectory):
with open(resultDirectory + "collectedResults.txt","w+") as g:
g.write("Model1 ------ Model2 ------ Evidence ------ LogEvidence std\n")
resultsList = []
for modelName1, modelName2, ndim1, ndim2, paramNames in modelSetB:
ndim = ndim1 + ndim2
print(modelName1, modelName2)
prefix = resultDirectory + modelName1[:4] + "/" + modelName2[:4] + "/"
with open(prefix + "stats.dat","r") as statsFile:
logevidence, evidenceStd = readStats(statsFile)
evidence = np.exp(logevidence)
print("{}, {}: {} +- {}\n".format(modelName1, modelName2, evidence, evidenceStd))
g.write("{}, {}: {} +- {}\n".format(modelName1, modelName2, evidence, evidenceStd))
resultsList.append([modelName1, modelName2, evidence, evidenceStd])
evidenceSum = 0
for i in range(len(resultsList)):
evidenceSum += float(resultsList[i][2])
g.write("Total evidence: {}".format(evidenceSum))
#makeTableB(resultsList, resultDirectory)
sortedResults = sorted(resultsList, key = lambda x: x[2], reverse=True)
makeTableB(sortedResults, resultDirectory, "sortedResultsB.txt")
fractionalEvidence(sortedResults, resultDirectory, "fracResultsB.txt")
return
def analyseMainA(resultDirectory):
with open(resultDirectory + "collectedResults.txt","w+") as g:
g.write("Model------ Evidence ------ LogEvidence std\n")
resultsList = []
for modelName, ndim, paramNames in modelSetA:
print(modelName)
prefix = resultDirectory + modelName[:4] + "/"
with open(prefix + "stats.dat","r") as statsFile:
logevidence, evidenceStd = readStats(statsFile)
evidence = np.exp(logevidence)
print("{}: {} +- {}\n".format(modelName, evidence, evidenceStd))
g.write("{}: {} +- {}\n".format(modelName, evidence, evidenceStd))
resultsList.append([modelName, evidence, evidenceStd])
evidenceSum = 0
for i in range(len(resultsList)):
evidenceSum += float(resultsList[i][1])
g.write("Total evidence: {}".format(evidenceSum))
sortedResults = sorted(resultsList, key = lambda x: x[1], reverse=True)
makeTableA(sortedResults, resultDirectory, "sortedResultsA.txt")
fractionalEvidence(sortedResults, resultDirectory, "fracResultsA.txt")
return
### Get results directory
if len(sys.argv) != 3:
print("ERROR")
print("Usage: hypothesisAnalyse.py [-A, -B] /out_directory/")
else:
outputfileName = sys.argv[2]
if sys.argv[1] == 'A':
analyseMainA(outputfileName)
elif sys.argv[1] == 'B':
analyseMainB(outputfileName)
else:
print("Argument Error")