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