IMP logo
IMP Reference Guide  2.5.0
The Integrative Modeling Platform
History.py
1 #!/usr/bin/env python
2 
3 """@namespace IMP.isd.History
4  Classes to store output from replicas.
5 """
6 
7 from __future__ import print_function
8 import numpy
9 import sys
10 
11 
12 class History:
13 
14  """Class that contains the output of one replica, used by the
15  Analysis class.
16  """
17 
18  def __init__(self, filename):
19  self.filename = filename
20  self.data = {}
21 
22  def create_category(self, name):
23  self.data[name] = {}
24 
25  def create_entry(self, cat, name):
26  if not cat in self.data:
27  self.create_category(cat)
28  self.data[cat][name] = []
29 
30  def add_data(self, cat, name, data):
31  self.data[cat][name].append(data)
32 
33  def sanity_check(self):
34  """checks if all entries have same length and are of constant type"""
35  goodlen = None
36  had_warnings = False
37  for cat in self.data:
38  for name in self.data[cat]:
39  if goodlen is None:
40  goodlen = len(self.data[cat][name])
41  if len(self.data[cat][name]) != goodlen:
42  print("category %s entry %s : length %s expected, got %s"\
43  % (cat, name, goodlen, len(self.data[cat][name])))
44  break
45  goodtype = type(self.data[cat][name][0])
46  for ent in self.data[cat][name]:
47  if not isinstance(ent, goodtype):
48  print("category %s entry %s : %s expected, got %s"\
49  % (cat, name, goodtype, type(ent)))
50  break
51 
52  def get_data(self, cat, name):
53  return self.data[cat][name]
54 
55  def get_categories(self):
56  i = sorted(self.data.keys())
57  if 'step' in i:
58  i.remove('step')
59  i = ['step'] + i
60  return i
61 
62  def get_entries(self, cat):
63  i = sorted(self.data[cat].keys())
64  if 'step' in i:
65  i.remove('step')
66  i = ['step'] + i
67  return i
68 
69  def remove_NAs(self):
70  for cat in self.data:
71  for name in self.data[cat]:
72  self.data[cat][name] = [i for i in self.data[cat][name]
73  if i != 'N/A']
74 
75  def toc(self, out=sys.stdout):
76  """print the "table of contents" of this History
77  tendency is a comparison of the last 200 frames, and whether it goes up
78  or down.
79  mean100 is the average over the last 100 frames
80  """
81  if not isinstance(out, file):
82  out = open(out, 'w')
83  out.write(
84  "category\tkey_name\tn_frames\ttype\taverage\tstd\tmean100\ttendency\n")
85  for cat in self.data:
86  out.write(cat + '\n')
87  for name in self.data[cat]:
88  ent = self.data[cat][name]
89  tp = type(ent[-1])
90  if tp == float:
91  tp = 'float'
92  elif tp == int:
93  tp = 'int'
94  if tp == 'float':
95  try:
96  ent = numpy.array(ent)
97  avg = numpy.mean(ent)
98  avg100 = numpy.mean(ent[-100:])
99  avg200 = numpy.mean(ent[-200:-100])
100  st = numpy.std(ent)
101  st200 = numpy.std(ent[-200])
102  if st200 == 0 or abs(avg200 - avg100) / st200 > 3:
103  if avg200 > avg100:
104  tend = '\\'
105  else:
106  tend = '/'
107  else:
108  tend = '--'
109  out.write(
110  "\t%20s\t%12d\t%20s\t%10f\t%10f\t%10f\t%5s\n" %
111  (name, len(ent), tp, avg, st, avg100, tend))
112  continue
113  except:
114  pass
115  out.write("\t%20s\t%12d\t%20s\n" % (name, len(ent), tp))
def sanity_check
checks if all entries have same length and are of constant type
Definition: History.py:33
def toc
print the "table of contents" of this History tendency is a comparison of the last 200 frames...
Definition: History.py:75
Class that contains the output of one replica, used by the Analysis class.
Definition: History.py:12