IMP logo
IMP Reference Guide  develop.714915d50b,2024/12/07
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 dictionary 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 precision(labeled_pdf_fn, output_fn='precision.txt'):
82  """
83  Function that reads in one labeled_pdf from create_DAG and returns the
84  precision, defined as the sum of the squared
85  probability of all trajectories.
86 
87  @param labeled_pdf_fn: string, labeled pdf file name (including the path);
88  labeled_pdf from the total model
89  @param output_fn: string, name of output file
90  (default: 'temporal_precision.txt')
91  @return temporal precision, written to output_fn
92  """
93  # create blank dictionary to store the results
94  prob_list = []
95  # read in labeled pdf file
96  old = open(labeled_pdf_fn, 'r')
97  line = old.readline()
98  # store the path through various nodes, as well as the probability
99  # of that path
100  while line:
101  line_split = line.split()
102  # assumes the first string is the trajectory string, the second
103  # string is the probability
104  if len(line_split) > 1:
105  # use # for comments
106  if line_split[0] == '#':
107  pass
108  else:
109  prob = float(line_split[1])
110  # store in dictionary
111  prob_list.append(prob)
112  line = old.readline()
113  old.close()
114  pure = 0
115  for prob in prob_list:
116  pure += prob * prob
117  with open(output_fn, 'w') as new:
118  new.write('Precision of ' + labeled_pdf_fn + ':\n')
119  new.write(str(pure))
120  print('Precision of ' + labeled_pdf_fn)
121  print(str(pure))
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.
def precision
Function that reads in one labeled_pdf from create_DAG and returns the precision, defined as the sum ...
The general base class for IMP exceptions.
Definition: exception.h:48