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