IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
/process_output.py
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 import argparse
5 import difflib
6 
7 
8 p = argparse.ArgumentParser(
9  description="Process output data file saved as dictionaries. "
10  "It has two modalities: print selected fields for all "
11  "lines or print a particular line where a field has a "
12  "given value. Example of usage: process_output.py "
13  "--soft -s To E S -f log.3.native-2-no-red. "
14  "process_output.py --soft --search_field EV0 "
15  "--search_value 5.67750116023 -f log.3.native-2-no-red")
16 p.add_argument('-f', action="store", dest="filename",
17  help="file name to process")
18 p.add_argument('-s', dest="fields", nargs="+",
19  help="Specify all fields to be printed. Multiple flags "
20  "will append a list of fields to be printed")
21 p.add_argument('-t', dest="single_column_field",
22  help="Specify a single column field to be printed. It "
23  "will be printed as a column. If the field name is "
24  "not complete, it will print all fields whose name "
25  "contain the queried string.")
26 p.add_argument('-p', action="store_true", dest="print_fields",
27  default=False, help="print the fields contained in the file")
28 p.add_argument('--head', action="store_true", dest="print_header",
29  default=False,
30  help="print the fields contained in the file (only stat2)")
31 p.add_argument('-n', action="store", dest="print_raw_number",
32  help="print the selected raw")
33 p.add_argument('--soft', action="store_true", dest="soft_match", default=False,
34  help="Soft match. Closest matching field will be printed, "
35  "e.g. S will give Step_Number, En will give energy, etc. ")
36 p.add_argument('--search_field', dest="search_field",
37  help="Search a line from the file. Specify the field to "
38  "be searched for. ")
39 p.add_argument('--search_value', dest="search_value",
40  help="Search a line from the file. Specify the value to "
41  "be searched for. ")
42 p.add_argument('--nframe', action="store_true", dest="nframe", default=False,
43  help="Print the frame number as initial column")
44 
45 result = p.parse_args()
46 
47 isstat1 = False
48 isstat2 = False
49 
50 # open the file
51 if not result.filename is None:
52  f = open(result.filename, "r")
53 else:
54  raise ValueError("No file name provided. Use -h for help")
55 
56 # get the keys from the first line
57 for line in f.readlines():
58  d = eval(line)
59  klist = list(d.keys())
60  # check if it is a stat2 file
61  if "STAT2HEADER" in klist:
62  import operator
63  isstat2 = True
64  for k in klist:
65  if "STAT2HEADER" in str(k):
66  if result.print_header:
67  print(k, d[k])
68  del d[k]
69  stat2_dict = d
70  # get the list of keys sorted by value
71  kkeys = [k[0]
72  for k in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
73  klist = [k[1]
74  for k in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
75  invstat2_dict = {}
76  for k in kkeys:
77  invstat2_dict.update({stat2_dict[k]: k})
78  else:
79  isstat1 = True
80  klist.sort()
81 
82  break
83 f.close()
84 
85 # print the keys
86 if result.print_fields:
87  for key in klist:
88  if len(key) <= 100:
89  print(key)
90  else:
91  print(key[0:100], "... omitting the rest of the string (>100 characters)")
92 
93 
94 # the field string matching is by default strict, i.e., the input string
95 # must be the same as the one in the file
96 match_strictness = 1.0
97 if result.soft_match:
98  match_strictness = 0.1
99 
100 # print the queried fields
101 if not result.fields is None:
102  field_list = []
103  # check whether the fields exist and convert them to best maching existing
104  # field names
105  for field in result.fields:
106  found_entries = difflib.get_close_matches(
107  field,
108  klist,
109  1,
110  match_strictness)
111  if len(found_entries) == 0:
112  raise ValueError("field " + field + " non found")
113  else:
114  field_list.append(found_entries[0])
115 
116  # print comment line
117  s0 = ' '.join(["%20s" % (field) for field in field_list])
118  print("# " + s0)
119 
120  # print fields values
121  f = open(result.filename, "r")
122  line_number = 0
123  for line in f.readlines():
124  line_number += 1
125  try:
126  d = eval(line)
127  except:
128  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
129  continue
130  if isstat1:
131  s0 = ' '.join(["%20s" % (str(d[field])) for field in field_list])
132  elif isstat2:
133  if line_number == 1:
134  continue
135  s0 = ' '.join(["%20s" % (str(d[invstat2_dict[field]]))
136  for field in field_list])
137  if not result.nframe:
138  print("> " + s0)
139  else:
140  print(str(line_number)+ " > " + s0)
141  f.close()
142 
143 
144 if not result.single_column_field is None:
145  field_list = []
146  for k in klist:
147  if result.single_column_field in k:
148  field_list.append(k)
149 
150  f = open(result.filename, "r")
151  line_number = 0
152  for line in f.readlines():
153  line_number += 1
154  try:
155  d = eval(line)
156  except:
157  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
158  continue
159  if isstat1:
160  for key in field_list:
161  print(key, d[key])
162  elif isstat2:
163  if line_number == 1:
164  continue
165  for key in field_list:
166  print(key, d[invstat2_dict[key]])
167  print(" ")
168  f.close()
169 
170 if (not result.search_field is None) and (not result.search_value is None):
171  # check whether the fields exist and convert them to best maching existing
172  # field names
173  found_entries = difflib.get_close_matches(
174  result.search_field,
175  klist,
176  1,
177  match_strictness)
178  if len(found_entries) == 0:
179  raise ValueError("field " + results.search_field + " non found")
180  else:
181  corrected_field = found_entries[0]
182  # print fields values
183  f = open(result.filename, "r")
184  line_number = 0
185  for line in f.readlines():
186  line_number += 1
187  try:
188  d = eval(line)
189  except:
190  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
191  continue
192 
193  if isstat1:
194  if (str(d[corrected_field]) == result.search_value):
195  for key in klist:
196  print(key, d[key])
197  elif isstat2:
198  if line_number == 1:
199  continue
200  if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
201  for key in klist:
202  print(key, d[invstat2_dict[key]])
203  f.close()
204 
205 if not result.print_raw_number is None:
206  # check whether the fields exist and convert them to best maching existing
207  # field names
208  f = open(result.filename, "r")
209  line_number = 0
210  for line in f.readlines():
211  line_number += 1
212  if isstat1:
213  if (line_number == int(result.print_raw_number)):
214  try:
215  d = eval(line)
216  except:
217  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
218  break
219  for key in klist:
220  print(key, d[key])
221 
222  elif isstat2:
223  if (line_number == int(result.print_raw_number) + 1):
224  try:
225  d = eval(line)
226  except:
227  print("# Warning: skipped line number " + str(line_number) + " not a valid line")
228  break
229  for key in klist:
230  print(key, d[invstat2_dict[key]])
231  f.close()