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