IMP logo
IMP Reference Guide  2.21.0
The Integrative Modeling Platform
spatiotemporal/Analysis.py
1 """@namespace IMP.spatiotemporal.analysis
2  Functions to analyze spatiotemporal models.
3 """
4 import numpy as np
5 
6 
7 def temporal_precision(labeled_pdf1_fn, labeled_pdf2_fn,
8  output_fn='temporal_precision.txt'):
9  """
10  Function that reads in two labeled_pdfs from create_DAG and returns the
11  temporal_precision, defined as the probability overlap between two
12  pathway models.
13 
14  @param labeled_pdf1_fn: string, labeled pdf file name (including the path);
15  labeled_pdf from one independent sampling
16  @param labeled_pdf2_fn: string, labeled pdf file name (including the path);
17  labeled_pdf from another independent sampling
18  @param output_fn: string, name of output file
19  (default: 'temporal_precision.txt')
20  @return temporal precision, written to output_fn
21  """
22  pdf_files = [labeled_pdf1_fn, labeled_pdf2_fn]
23  dict_list = []
24  for pdf_file in pdf_files:
25  # create blank dictonary to store the results
26  prob_dict = {}
27  # read in labeled pdf file
28  old = open(pdf_file, 'r')
29  line = old.readline()
30  # store the path through various nodes, as well as the probability
31  # of that path
32  while line:
33  line_split = line.split()
34  # assumes the first string is the trajectory string, the second
35  # string is the probability
36  if len(line_split) > 1:
37  # use # for comments
38  if line_split[0] == '#':
39  pass
40  else:
41  trj = line_split[0]
42  prob = float(line_split[1])
43  # store in dictionary
44  prob_dict[trj] = prob
45  line = old.readline()
46  old.close()
47  # append dictionary to dict_list
48  dict_list.append(prob_dict)
49  # calculate
50  key_list = dict_list[0].keys()
51  key_list2 = dict_list[1].keys()
52  # print error if keys not found
53  if len(key_list) == 0 or len(key_list2) == 0:
54  raise Exception('Error reading labeled_pdf!!! Keys not found')
55  # precision starts at 1
56  precision = 1
57  for key in key_list2:
58  if key in key_list:
59  # reduce by 1/2 the Manhattan distance between probabilities
60  precision -= 0.5 * np.abs(dict_list[0][key] - dict_list[1][key])
61  else:
62  # states in key_list2, but not key_list inherently contribute
63  # to the temporal precision
64  precision -= 0.5 * np.abs(dict_list[1][key])
65  for key in key_list:
66  if key in key_list2:
67  pass
68  # states in key_list, but not key_list2 inherently contribute to
69  # the temporal precision
70  else:
71  precision -= 0.5 * np.abs(dict_list[0][key])
72  with open(output_fn, 'w') as new:
73  new.write('Temporal precision between ' + labeled_pdf1_fn + ' and '
74  + labeled_pdf2_fn + ':\n')
75  new.write(str(precision))
76  print('Temporal precision between ' + labeled_pdf1_fn + ' and '
77  + labeled_pdf2_fn + ':')
78  print(precision)
79 
80 
81 def purity(labeled_pdf_fn, output_fn='purity.txt'):
82  """
83  Function that reads in one labeled_pdf from create_DAG and returns the
84  purity, defined as the sum of the squared probability of all trajectories.
85 
86  @param labeled_pdf_fn: string, labeled pdf file name (including the path);
87  labeled_pdf from the total model
88  @param output_fn: string, name of output file
89  (default: 'temporal_precision.txt')
90  @return temporal purity, written to output_fn
91  """
92  # create blank dictonary to store the results
93  prob_list = []
94  # read in labeled pdf file
95  old = open(labeled_pdf_fn, 'r')
96  line = old.readline()
97  # store the path through various nodes, as well as the probability
98  # of that path
99  while line:
100  line_split = line.split()
101  # assumes the first string is the trajectory string, the second
102  # string is the probability
103  if len(line_split) > 1:
104  # use # for comments
105  if line_split[0] == '#':
106  pass
107  else:
108  prob = float(line_split[1])
109  # store in dictionary
110  prob_list.append(prob)
111  line = old.readline()
112  old.close()
113  pure = 0
114  for prob in prob_list:
115  pure += prob * prob
116  with open(output_fn, 'w') as new:
117  new.write('Purity of ' + labeled_pdf_fn + ':\n')
118  new.write(str(pure))
119  print('Purity of ' + labeled_pdf_fn)
120  print(str(pure))
def purity
Function that reads in one labeled_pdf from create_DAG and returns the purity, defined as the sum of ...
def temporal_precision
Function that reads in two labeled_pdfs from create_DAG and returns the temporal_precision, defined as the probability overlap between two pathway models.
The general base class for IMP exceptions.
Definition: exception.h:48