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