/
parampriors.py
48 lines (40 loc) · 1.51 KB
/
parampriors.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
import os
class ParamBounds(object):
def __init__(self, fileName=None):
self.names = []
self.lower = {}
self.upper = {}
if fileName is not None: self.loadFromFile(fileName)
def loadFromFile(self, fileName):
self.filenameLoadedFrom = os.path.split(fileName)[1]
with open(fileName) as f:
for line in f:
strings = [text.strip() for text in line.split()]
if len(strings) == 3:
self.setRange(strings[0], strings[1:])
def __str__(self):
s = ''
for name in self.names:
valMin = self.getLower(name)
if valMin is not None:
lim1 = "%15.7E" % valMin
else:
lim1 = " N"
valMax = self.getUpper(name)
if valMax is not None:
lim2 = "%15.7E" % valMax
else:
lim2 = " N"
s += "%22s%17s%17s\n" % (name, lim1, lim2)
return s
def saveToFile(self, fileName):
with open(fileName, 'w') as f:
f.write(str(self))
def setRange(self, name, strings):
if strings[0] != 'N' and strings[0] is not None: self.lower[name] = float(strings[0])
if strings[1] != 'N' and strings[1] is not None: self.upper[name] = float(strings[1])
if not name in self.names: self.names.append(name)
def getUpper(self, name):
return self.upper.get(name, None)
def getLower(self, name):
return self.lower.get(name, None)