3 import matplotlib.pyplot
8 """Class that produces analysis-related output, and is able to parse the
9 output of a file produced by the Statistics class.
13 """adds a new entry to the hierarchy by parsing a title entry"""
14 if lineno == len(self.correspondences):
15 self.correspondences.append([])
16 entryno, cat, name = el.split(
':')
17 entryno=int(entryno)-1
18 if len(self.correspondences[lineno]) == entryno:
19 self.correspondences[lineno].append((cat,name))
21 self.correspondences[lineno][entryno]=(cat,name)
22 h.create_entry(cat,name)
24 def add_data(self, h, lineno, colno, data):
25 """adds data point to hierarchy"""
26 cat,name = self.correspondences[lineno][colno]
34 h.add_data(cat,name,data)
37 """reads a *_stats.txt file and returns variables present in it"""
40 self.correspondences=[]
41 for line
in open(statfile):
42 if line.startswith(
'*'):
45 if not tokens[0][1].isdigit():
47 lineno = int(tokens[0][1:])-1
48 if line.startswith(
'#'):
53 return self.correspondences
56 """reads an AMBER mden file and returns variables present in it"""
59 self.correspondences=[]
61 for line
in open(statfile):
63 lineno = int(tokens[0][1:])
67 for i,el
in enumerate(tokens[1:]):
69 return self.correspondences
72 """reads an AMBER mden file and returns a History instance"""
77 self.correspondences=[]
78 for line
in open(statfile):
80 lineno = int(tokens[0][1:])
81 if lineno < oldnum
and read_title:
85 for i,el
in enumerate(tokens[1:]):
89 for i, el
in enumerate(tokens[1:]):
95 """reads a *_stats.txt file and returns a History instance"""
99 self.correspondences=[]
100 for line
in open(statfile):
101 if line.startswith(
'*'):
104 if not tokens[0][1].isdigit():
106 lineno = int(tokens[0][1:])-1
107 if line.startswith(
'#'):
109 for el
in tokens[1:]:
115 for i, el
in enumerate(tokens[1:]):
120 def plot(self, h, *datums, **kwargs):
121 """plots datum (cat,name) from hierarchy h, optionnally specifying a
122 range. To plot multiple data at the same time, add them sequentially.
123 Takes x axis from the 'step' entry of the first datum. TODO.
125 data=[array(h.get_data(cat,name), dtype=float) \
126 for (cat, name)
in datums]
127 x = h.get_data(datums[0][0],
'step')
129 for i
in range(len(data)):
130 toplot.extend([x,data[i]])
131 matplotlib.pyplot.plot(*data, **kwargs)
132 matplotlib.pyplot.grid(
True)
133 matplotlib.pyplot.legend()
134 matplotlib.pyplot.show()
136 def histogram(self, h, *datums, **kwargs):
137 """plots histogram of datum (cat,name) from hierarchy h, optionnally
138 specifying a range. To plot multiple data at the same time, add them
141 data=[array(h.get_data(*dat), dtype=float) \
143 matplotlib.pyplot.hist(*data, **kwargs)
144 matplotlib.pyplot.grid(
True)
145 matplotlib.pyplot.legend()
146 matplotlib.pyplot.show()
148 def dump(self, h, dest, *args):
149 """"dump float data from history h to file dest.
150 args can be either strings corresponding to categories, or tuples
151 corresponding to entries of a certain category. Only one counter will be
152 output for the whole dump, it corresponds to the counter of the first
153 entry's category. You can always specify additional counters if needed.
161 ent=h.get_entries(arg)[1:]
163 cats.extend([arg]*len(ent))
169 steps=h.get_data(cats[0],
'step')
171 fl.write(
"# %s:step\t" % cats[0])
172 fl.write(
'\t'.join([
'%s:%s' % (i,j)
for (i,j)
in zip(cats,names)]))
174 data=[h.get_data(i,j)
for (i,j)
in zip(cats,names)]
175 for i,st
in enumerate(steps):
176 fl.write(
"%10d\t" % st)
178 fl.write(
'%10f\t' % j[i])
186 if __name__ ==
'__main__':
190 h=a.read_stats(sys.argv[1])
192 matplotlib.pyplot.ion()