IMP  2.2.1
The Integrative Modeling Platform
Analysis.py
1 #!/usr/bin/env python
2 
3 import matplotlib.pyplot
4 from numpy import *
5 from IMP.isd.History import History
6 
7 
8 class Analysis:
9 
10  """Class that produces analysis-related output, and is able to parse the
11  output of a file produced by the Statistics class.
12  """
13 
14  def create_entry(self, h, lineno, el):
15  """adds a new entry to the hierarchy by parsing a title entry"""
16  if lineno == len(self.correspondences):
17  self.correspondences.append([])
18  entryno, cat, name = el.split(':')
19  entryno = int(entryno) - 1
20  if len(self.correspondences[lineno]) == entryno:
21  self.correspondences[lineno].append((cat, name))
22  else:
23  self.correspondences[lineno][entryno] = (cat, name)
24  h.create_entry(cat, name)
25 
26  def add_data(self, h, lineno, colno, data):
27  """adds data point to hierarchy"""
28  cat, name = self.correspondences[lineno][colno]
29  if data.isdigit():
30  data = int(data)
31  else:
32  try:
33  data = float(data)
34  except ValueError:
35  pass
36  h.add_data(cat, name, data)
37 
38  def read_variables(self, statfile):
39  """reads a *_stats.txt file and returns variables present in it"""
40  h = History(statfile)
41  # read title and setup history
42  self.correspondences = []
43  for line in open(statfile):
44  if line.startswith('*'):
45  continue
46  tokens = line.split()
47  if not tokens[0][1].isdigit():
48  continue
49  lineno = int(tokens[0][1:]) - 1
50  if line.startswith('#'):
51  for el in tokens[1:]:
52  self.create_entry(h, lineno, el)
53  continue
54  break
55  return self.correspondences
56 
57  def read_AMBER_variables(self, statfile):
58  """reads an AMBER mden file and returns variables present in it"""
59  h = History(statfile)
60  # read title and setup history
61  self.correspondences = []
62  oldnum = -1
63  for line in open(statfile):
64  tokens = line.split()
65  lineno = int(tokens[0][1:])
66  if lineno < oldnum:
67  break
68  oldnum = lineno
69  for i, el in enumerate(tokens[1:]):
70  self.create_entry(h, lineno, '%d:global:%s' % (i + 1, el))
71  return self.correspondences
72 
73  def read_AMBER_stats(self, statfile):
74  """reads an AMBER mden file and returns a History instance"""
75  h = History(statfile)
76  # read title and setup history
77  read_title = True
78  oldnum = -1
79  self.correspondences = []
80  for line in open(statfile):
81  tokens = line.split()
82  lineno = int(tokens[0][1:])
83  if lineno < oldnum and read_title:
84  read_title = False
85  oldnum = lineno
86  if read_title:
87  for i, el in enumerate(tokens[1:]):
88  self.create_entry(h, lineno, '%d:global:%s' % (i + 1, el))
89  continue
90  # from here on, the line contains data
91  for i, el in enumerate(tokens[1:]):
92  self.add_data(h, lineno, i, el)
93  # h.sanity_check()
94  return h
95 
96  def read_stats(self, statfile):
97  """reads a *_stats.txt file and returns a History instance"""
98  h = History(statfile)
99  # read title and setup history
100  read_title = True
101  self.correspondences = []
102  for line in open(statfile):
103  if line.startswith('*'):
104  continue
105  tokens = line.split()
106  if not tokens[0][1].isdigit():
107  continue
108  lineno = int(tokens[0][1:]) - 1
109  if line.startswith('#'):
110  if read_title:
111  for el in tokens[1:]:
112  self.create_entry(h, lineno, el)
113  continue
114  elif read_title:
115  read_title = False
116  # from here on, the line starts with 'L'
117  for i, el in enumerate(tokens[1:]):
118  self.add_data(h, lineno, i, el)
119  # h.sanity_check()
120  return h
121 
122  def plot(self, h, *datums, **kwargs):
123  """plots datum (cat,name) from hierarchy h, optionnally specifying a
124  range. To plot multiple data at the same time, add them sequentially.
125  Takes x axis from the 'step' entry of the first datum. TODO.
126  """
127  data = [array(h.get_data(cat, name), dtype=float)
128  for (cat, name) in datums]
129  x = h.get_data(datums[0][0], 'step')
130  toplot = []
131  for i in range(len(data)):
132  toplot.extend([x, data[i]])
133  matplotlib.pyplot.plot(*data, **kwargs)
134  matplotlib.pyplot.grid(True)
135  matplotlib.pyplot.legend()
136  matplotlib.pyplot.show()
137 
138  def histogram(self, h, *datums, **kwargs):
139  """plots histogram of datum (cat,name) from hierarchy h, optionnally
140  specifying a range. To plot multiple data at the same time, add them
141  sequentially.
142  """
143  data = [array(h.get_data(*dat), dtype=float)
144  for dat in datums]
145  matplotlib.pyplot.hist(*data, **kwargs)
146  matplotlib.pyplot.grid(True)
147  matplotlib.pyplot.legend()
148  matplotlib.pyplot.show()
149 
150  def dump(self, h, dest, *args):
151  """"dump float data from history h to file dest.
152  args can be either strings corresponding to categories, or tuples
153  corresponding to entries of a certain category. Only one counter will be
154  output for the whole dump, it corresponds to the counter of the first
155  entry's category. You can always specify additional counters if needed.
156  """
157  # parse args
158  cats = []
159  names = []
160  for arg in args:
161  if isinstance(arg, str):
162  # get rid of counter
163  ent = h.get_entries(arg)[1:]
164  names.extend(ent)
165  cats.extend([arg] * len(ent))
166  else:
167  # argument should be (cat, entry)
168  names.append(arg[1])
169  cats.append(arg[0])
170  # write data
171  steps = h.get_data(cats[0], 'step')
172  fl = open(dest, 'w')
173  fl.write("# %s:step\t" % cats[0])
174  fl.write('\t'.join(['%s:%s' % (i, j) for (i, j) in zip(cats, names)]))
175  fl.write('\n')
176  data = [h.get_data(i, j) for (i, j) in zip(cats, names)]
177  for i, st in enumerate(steps):
178  fl.write("%10d\t" % st)
179  for j in data:
180  fl.write('%10f\t' % j[i])
181  fl.write('\n')
182  fl.close()
183 
184 
185 if __name__ == '__main__':
186 
187  import sys
188  a = Analysis()
189  h = a.read_stats(sys.argv[1])
190  h.toc()
191  matplotlib.pyplot.ion() # interactive
def add_data
adds data point to hierarchy
Definition: Analysis.py:26
def read_stats
reads a *_stats.txt file and returns a History instance
Definition: Analysis.py:96
def dump
"dump float data from history h to file dest.
Definition: Analysis.py:150
def read_AMBER_stats
reads an AMBER mden file and returns a History instance
Definition: Analysis.py:73
def create_entry
adds a new entry to the hierarchy by parsing a title entry
Definition: Analysis.py:14
Classes to store output from replicas.
Definition: History.py:1
def read_variables
reads a *_stats.txt file and returns variables present in it
Definition: Analysis.py:38
def plot
plots datum (cat,name) from hierarchy h, optionnally specifying a range.
Definition: Analysis.py:122
Class that produces analysis-related output, and is able to parse the output of a file produced by th...
Definition: Analysis.py:8
def read_AMBER_variables
reads an AMBER mden file and returns variables present in it
Definition: Analysis.py:57
def histogram
plots histogram of datum (cat,name) from hierarchy h, optionnally specifying a range.
Definition: Analysis.py:138