forked from GambitBSM/pippi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pippi_utils.py
278 lines (241 loc) · 8.69 KB
/
pippi_utils.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
#############################################################
# pippi: parse it, plot it
# ------------------------
# Utilities module for pippi.
#
# Author: Pat Scott ([email protected])
# Originally developed: March 2012
#############################################################
from __future__ import print_function
import sys
import os.path
import re
import numpy as np
from pippi_colours import *
def mapToRefLike(otherlike,array):
#Map from one permitted likelihood form to another
return [permittedLikes[otherlike](member) for member in array]
def safe_dir(dirname):
#Check that dirname exists, if not then make it
if not os.path.exists(dirname):
os.makedirs(os.path.abspath(dirname))
def try_append(indices, cols, x):
try:
indices.append(cols.index(x))
except:
sys.exit("ERROR: hdf5 file does not contain a field titled \""+x+"\".")
def castable_to_int(x):
try:
int(x)
return True
except ValueError:
return False
def usage():
#Print pippi usage information
print()
print('You must be new here (or have fat fingers). You can use pippi to')
print()
print(' merge two or more chains:')
print(' pippi merge <chain1> <chain2> ... <chainx>')
print()
print(' post-process a chain:')
print(' pippi pare <chain> <name_of_python_module> <name_of_function_in_module>')
print()
print(' parse a chain using options in iniFile.pip:')
print(' pippi parse iniFile.pip')
print()
print(' write plotting scipts for a chain using options in iniFile.pip:')
print(' pippi script iniFile.pip')
print()
print(' run plotting scipts for a chain using options in iniFile.pip:')
print(' pippi plot iniFile.pip')
print()
print(' print an hdf5 file\'s computed column indices, using options in iniFile.pip:')
print(' pippi probe iniFile.pip')
print()
print(' parse, script and plot in one go:')
print(' pippi iniFile.pip')
print()
def safe_open(filename):
#Try to open input file
try:
infile = open(filename,"r")
return infile
except IOError:
#Crash if file does not exist
sys.exit('\n Sorry, file '+filename+' not found or read-protected. Quitting...\n')
def smart_open(filename,mode):
#Try to open file for output
try:
outfile = open(filename,mode)
return outfile
except IOError:
#Crash if file cannot be opened the way requested
sys.exit('\n Sorry, file '+filename+' cannot be opened for writing.\nCheck disk space and folder permissions. Quitting...\n')
def is_functional_assignment(assignment):
return len(re.findall("\$", assignment)) != len(re.findall("[\\\]\$", assignment))
def parse_functional_assignment(assignment, replacement):
replacement_parts = replacement.split('$')
return re.sub("\$([0-9]*)", replacement_parts[0]+'\\1'+replacement_parts[1], assignment)
class dataObject:
#Class for pip file entries, containing their values, keys and expected data types
pipFileKey = value = dataType = ''
def __init__(self,pipEntry,dataType):
#Save key and expected data type for new pip file field
self.pipFileKey = pipEntry
self.conversion = dataType
def seek(self,string):
#Look for field's key in string
if self.pipFileKey in string: return True
return False
def convert(self,string):
#Convert string to appropriate data format for pip file field
string = string.strip()
try:
if string == '':
self.value = None
else:
self.value = self.conversion(string)
except:
print("Failed to convert string:")
print(string)
sys.exit('Error: invalid data format in field '+self.pipFileKey+'. Quitting...\n')
#Conversion functions for parsing pip file entries
def integer(x):
x = re.sub(r"[':;,]", '', x)
if len(x.split()) != 1: raise Exception
return int(x)
def floater(x):
x = re.sub(r"[':;,]", '', x)
if len(x.split()) != 1: raise Exception
return float(x)
def string(x):
if len(re.findall("'", x)) != 2: raise Exception
return re.sub(r"^.*?'|'.*?$", '', x)
def safe_string(x):
if len(re.findall("'", x)) != 2 or ';' in x: raise Exception
return re.sub(r"^.*?'|'.*?$", '', x)
def boolean(x):
x = re.sub(r"[':;,]", '', x)
if len(x.split()) != 1: raise Exception
if x.lower().strip() in ("yes", "true", "t", "1"):
return True
elif x.lower().strip() in ("no", "false", "f", "0"):
return False
else: raise Exception
def int_list(x): return single_list(x,integer)
def float_list(x): return single_list(x,floater)
def single_list(x,singleType):
x = re.sub(r"[':;,]", ' ', x)
returnVal = []
for entry in x.split():
returnVal.append(singleType(entry))
return returnVal
def intuple_list(x): return tuple_list(x,integer)
def floatuple_list(x): return tuple_list(x,floater)
def tuple_list(x,tupleType):
if len(re.findall("\{", x)) != len(re.findall("\}", x)): raise Exception
x = re.findall("\{.+?\}", x)
for i, pair in enumerate(x):
pair = re.sub(r"[':;,\}\{]", ' ', pair).split()
if len(pair) < 2: raise Exception
x[i] = [tupleType(j) for j in pair]
return x
def string_list(x):
returnVal = []
if len(re.findall("'", x))%2 != 0: raise Exception
x = re.findall("'.+?'", x)
for i, entry in enumerate(x):
returnVal.append(string(entry))
return returnVal
def int_dictionary(x):
returnVal = {}
x = re.findall(".+?:.+?[\s,;]+?|.+?:.+?$", x)
for i, pair in enumerate(x):
pair = re.sub("[\s,;]+$", '', pair).split(':')
pair = [integer(pair[0]), integer(pair[1])]
returnVal[pair[0]] = pair[1]
return returnVal
def float_dictionary(x):
returnVal = {}
x = re.findall(".+?:.+?[\s,;]+?|.+?:.+?$", x)
for i, pair in enumerate(x):
pair = re.sub("[\s,;]+$", '', pair).split(':')
pair = [integer(pair[0]), floater(pair[1])]
returnVal[pair[0]] = pair[1]
return returnVal
def floatuple_dictionary(x):
returnVal = {}
x = re.findall(".+?:{.+?}[\s,;]+?|.+?:{.+?}$", x)
for i, pair in enumerate(x):
pair = re.sub("[\s,;]+$", '', pair).split(':')
pair = [integer(pair[0]), floatuple_list(pair[1])[0]]
returnVal[pair[0]] = pair[1]
return returnVal
def string_dictionary(x):
returnVal = {}
if len(re.findall("'", x))%2 != 0: raise Exception
x = re.findall("(.+?\s*:\s*'.+?'[\s,;$]*?|'.+?'\s*:\s*.+?[\s,;]+?|'.+?'\s*:\s*.+?[\s,;]*?$)", x)
for i, pair in enumerate(x):
capture = re.findall("'.+?'", pair)
if len(capture) > 1: raise Exception
pair = re.sub("'.+?'", '__temp__', pair)
pair = re.sub("[\s,;]+$", '', pair).split(':')
for j, single in enumerate(pair):
single.strip()
if '__temp__' in single:
pair[j] = string(capture[0].strip())
else:
pair[j] = integer(single)
returnVal[pair[0]] = pair[1]
returnVal[pair[1]] = pair[0]
return returnVal
def int_pair_string_dictionary(x):
returnVal = {}
if len(re.findall("'", x))%2 != 0: raise Exception
x = re.findall(".+?:'.+?'[\s,;]*", x)
for i, pair in enumerate(x):
pair = re.sub("[\s,;]+$", '', pair).split(':')
for j, single in enumerate(pair):
if single[0] == '\'':
pair[j] = string(single)
else:
pair[j] = intuple_list(single)[0]
if pair[0][0] in returnVal:
returnVal[pair[0][0]][pair[0][1]] = pair[1]
else:
returnVal[pair[0][0]] = {pair[0][1]:pair[1]}
return returnVal
def internal(x):
return permittedInternals[re.sub(r"[':;,]", ' ', x).lower()]
#Global constants and simple one-liners
pippiVersion = 'v2.2'
def times1(x): return x
def half(x): return x*0.5
def negative(x): return -x
def negativehalf(x): return -x*0.5
def negln(x): return -np.log(x)
def ts(x): return x*0.25
permittedLikes = {'-lnlike':times1,'lnlike':negative,'-2lnlike':half,'chi2':half,'2lnlike':negativehalf,'like':negln, 'TS':ts}
permittedLikes_samesign = ['-lnlike', '-2lnlike', 'TS']
refLike = '-lnlike'
permittedMults = ['mult','mult.','multiplicity','multiplic.','mtpcty','Posterior']
refMult = permittedMults[0]
permittedPriors = ['prior','priors','pri.']
refPrior = permittedPriors[0]
mcmc = 'mcmc'
multinest = 'multinest'
other = 'other'
permittedInternals = {'mcmc':mcmc, 'multinest':multinest, 'other':other}
if permittedSchemes is not None: permittedInternals.update(permittedSchemes)
allowedIntMethods = ['bilinear', 'spline']
mainChain = dataObject('main_chain',safe_string)
secChain = dataObject('comparison_chain',safe_string)
doPosterior = dataObject('do_posterior_pdf',boolean)
doProfile = dataObject('do_profile_like',boolean)
oneDplots = dataObject('oneD_plot_quantities',int_list)
twoDplots = dataObject('twoD_plot_quantities',intuple_list)
contours1D = dataObject('oneD_contour_levels',float_list)
contours2D = dataObject('twoD_contour_levels',float_list)
obsPlots = dataObject('plot_observables',int_list)
keys = [mainChain,secChain,doPosterior,doProfile,oneDplots,twoDplots,contours1D,contours2D,obsPlots]