3 from __future__
import print_function
7 from IMP._compat_argparse
import argparse
12 p = argparse.ArgumentParser(
13 description=
"Process output data file saved as dictionaries. "
14 "It has two modalities: print selected fields for all "
15 "lines or print a particular line where a field has a "
16 "given value. Example of usage: process_output.py "
17 "--soft -s To E S -f log.3.native-2-no-red. "
18 "process_output.py --soft --search_field EV0 "
19 "--search_value 5.67750116023 -f log.3.native-2-no-red")
20 p.add_argument(
'-f', action=
"store", dest=
"filename",
21 help=
"file name to process")
22 p.add_argument(
'-s', dest=
"fields", nargs=
"+",
23 help=
"Specify all fields to be printed. Multiple flags "
24 "will append a list of fields to be printed")
25 p.add_argument(
'-t', dest=
"single_column_field",
26 help=
"Specify a single column field to be printed. It "
27 "will be printed as a column. If the field name is "
28 "not complete, it will print all fields whose name "
29 "contain the queried string.")
30 p.add_argument(
'-p', action=
"store_true", dest=
"print_fields",
31 default=
False, help=
"print the fields contained in the file")
32 p.add_argument(
'--head', action=
"store_true", dest=
"print_header",
34 help=
"print the fields contained in the file (only stat2)")
35 p.add_argument(
'-n', action=
"store", dest=
"print_raw_number",
36 help=
"print the selected raw")
37 p.add_argument(
'--soft', action=
"store_true", dest=
"soft_match", default=
False,
38 help=
"Soft match. Closest matching field will be printed, "
39 "e.g. S will give Step_Number, En will give energy, etc. ")
40 p.add_argument(
'--search_field', dest=
"search_field",
41 help=
"Search a line from the file. Specify the field to "
43 p.add_argument(
'--search_value', dest=
"search_value",
44 help=
"Search a line from the file. Specify the value to "
46 p.add_argument(
'--nframe', action=
"store_true", dest=
"nframe", default=
False,
47 help=
"Print the frame number as initial column")
49 result = p.parse_args()
55 if not result.filename
is None:
56 f = open(result.filename,
"r")
58 raise ValueError(
"No file name provided. Use -h for help")
61 for line
in f.readlines():
63 klist = list(d.keys())
65 if "STAT2HEADER" in klist:
69 if "STAT2HEADER" in str(k):
70 if result.print_header:
76 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
78 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
81 invstat2_dict.update({stat2_dict[k]: k})
90 if result.print_fields:
95 print(key[0:100],
"... omitting the rest of the string (>100 characters)")
100 match_strictness = 1.0
101 if result.soft_match:
102 match_strictness = 0.1
105 if not result.fields
is None:
109 for field
in result.fields:
110 found_entries = difflib.get_close_matches(
115 if len(found_entries) == 0:
116 raise ValueError(
"field " + field +
" non found")
118 field_list.append(found_entries[0])
121 s0 =
' '.join([
"%20s" % (field)
for field
in field_list])
125 f = open(result.filename,
"r")
127 for line
in f.readlines():
132 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
135 s0 =
' '.join([
"%20s" % (str(d[field]))
for field
in field_list])
139 s0 =
' '.join([
"%20s" % (str(d[invstat2_dict[field]]))
140 for field
in field_list])
141 if not result.nframe:
144 print(str(line_number)+
" > " + s0)
148 if not result.single_column_field
is None:
151 if result.single_column_field
in k:
154 f = open(result.filename,
"r")
156 for line
in f.readlines():
161 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
164 for key
in field_list:
169 for key
in field_list:
170 print(key, d[invstat2_dict[key]])
174 if (
not result.search_field
is None)
and (
not result.search_value
is None):
177 found_entries = difflib.get_close_matches(
182 if len(found_entries) == 0:
183 raise ValueError(
"field " + results.search_field +
" non found")
185 corrected_field = found_entries[0]
187 f = open(result.filename,
"r")
189 for line
in f.readlines():
194 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
198 if (str(d[corrected_field]) == result.search_value):
204 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
206 print(key, d[invstat2_dict[key]])
209 if not result.print_raw_number
is None:
212 f = open(result.filename,
"r")
214 for line
in f.readlines():
217 if (line_number == int(result.print_raw_number)):
221 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
227 if (line_number == int(result.print_raw_number) + 1):
231 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
234 print(key, d[invstat2_dict[key]])