-
Notifications
You must be signed in to change notification settings - Fork 0
/
xxvgplog.py
executable file
·299 lines (276 loc) · 9.34 KB
/
xxvgplog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import os
import sys
import argparse
FEATURES = [
'version 0.1.0 : plot for GMX-XVG file',
'version 0.2.0 : powerful for continuously parse',
'version 0.3.0 : support plumed data',
'version 0.4.0 : make more versatile and calculate min/max/avg',
'version 0.5.0 : deal with when `xaxis` is used',
'version 0.6.0 : fix potential issue for plumed file of multiple FIELDS',
]
VERSION = FEATURES[-1].split()[1]
__version__ = VERSION
def read_gmx_xvg(file):
if not os.path.isfile(file):
print('Fatal: not a file {:}'.format(file))
return
data = []
legends = []
bad = []
fatal = []
bo_plumed = False
with open(file,'rt') as f:
for line in f:
l = line.strip()
if not len(l): continue
if l.startswith('#'):
if l[:2] == '#!': # plumed data
g = l[2:].split()
if g[0] == 'FIELDS':
if bo_plumed:
print('Warning: multiple FIELDS')
print(' -> ',l)
bo_plumed = True
legends = g[1:]
elif l.startswith('@'):
l = l[1:].strip()
if l.startswith('legend'): continue
if 'legend' in l:
g = l[1:].split()
if len(g) >= 3 and g[1] == 'legend':
k = ' '.join(g[2:]).replace('"',' ').replace("'",' ')
legends.append('-'.join(k.split()))
else:
try:
d = list(map(float,l.split()))
except (ValueError,IndexError):
bad.append(l)
else:
data.append(d)
if not bo_plumed and legends:
legends.insert(0,'time')
# check length
if data:
n = len(legends) if legends else len(data[0])
get = []
for i,d in enumerate(data):
if len(d) != n:
fatal.append(' '.join([str(i) for i in d]))
get.append(i)
if len(get):
for i in sorted(get,reverse=True):
data.pop(i)
if not data:
return
full = {
'x' : [d[0] for d in data],
'bad' : bad,
'fatal': fatal,
'size' : n,
}
keys = {}
for i,e in enumerate(legends):
full[e] = t = [d[i] for d in data]
vmin = min(t)
vmax = max(t)
vavg = sum(t) / len(t)
keys[str(i)] = [e,vmin,vmax,vavg]
full['keys'] = keys
return full
def plot_xvg(x,ys,titles=None):
if len(ys) == 1:
fig, ax = plt.subplots()
ax.plot(x,ys[0])
if titles:
ax.set_title(titles[0])
plt.show()
else:
if titles is None:
titles = [None for i in len(ys)]
fig, ax = plt.subplots()
for y,t in zip(ys,titles):
ax.plot(x,y,label=t)
ax.legend()
plt.show()
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=f"""
plot_gmx_xvg {VERSION}
""",
allow_abbrev=False,
)
parser.add_argument(
'-v','--version',
action='version',
version=VERSION
)
parser.add_argument(
'-f','--file',
dest='file',
help='input file'
)
parser.add_argument(
'-g','--group',
dest='group',
metavar='v',
help=(
'column indexes to plot groups, first column will be default to x-axis, '
'thus, 1 means second column, 2 means third column. Names,case-insensitive, '
'can also be used, abbreviation is allowed, thus `temperature` and `temp` or `te` '
'can be the same column if unambiguously identified. Multiple groups '
'are separated by ":", e.g., "1,2,3 : 1,temp : vir-xx, temp"'
)
)
parser.add_argument(
'-i','--interactive',
action='store_true',
help='interactive parse file'
)
parser.add_argument(
'--features',
action='store_true',
help='show development features'
)
if len(sys.argv) == 1:
parser.print_help()
return
w = parser.parse_args(sys.argv[1:])
if w.features:
for i in FEATURES: print(i)
return
full = read_gmx_xvg(w.file)
if not full:
print('Fatal: no valid data: file: {:}'.format(w.file))
return
bad = False
if full['bad']:
bad = True
print('Lines in wrong format: not a number:')
for l in full['bad']:
print(' -> ',l)
if full['fatal']:
bad = True
print('Lines in the same length:')
for l in full['fatal']:
print(' -> ',l)
if bad:
t = input('Want to continue? [y]/N: ')
if not (not len(t.split()) or t.strip().lower() == 'y'):
print('You have decided to quit, nothing will be ploted')
return
if w.interactive:
print('Note: parsing keys: (index -> key min max avg)')
keys = full['keys']
n = full['size']
ask = None
xaxis = 0
xdata = full['x']
outputs = []
for i in range(n):
l = '{:2} -> {:15} {:15.3f} {:15.3f} {:15.3f}'.format(i, *keys[str(i)])
outputs.append(l)
while True:
while True:
print('Note: to reset xaxis, input: xaxis=i')
if ask:
g = ask.split()
ask = None # reset
else:
for i in range(n):
if xaxis == i:
print(outputs[i] + ' (x-axis)')
else:
print(outputs[i])
t = input('Input group to be parsed (use number & space): ')
g = t.replace(',',' ').split()
bo = None
for v in g:
if v.startswith('xaxis='):
try:
k = int(v.replace('xaxis=',' '))
if k < 0 or k > n: raise ValueError
except:
print(' -> wrong setting xaxis, no space is allowed')
bo = False
else:
xaxis = k
xdata = full[keys[str(k)][0]]
bo = True
break
if bo is True:
g.remove(v)
elif bo is False:
continue
if g:
try:
g = list(map(int,g))
except ValueError:
print(' -> wrong input: not a number: re-input')
continue
else:
if any([i<0 or i>n for i in g]):
print(' -> wrong input: not in range: re-input')
continue
select = [keys[str(v)][0] for v in g]
break
else:
continue
plot_xvg(x=xdata,ys=[full[i] for i in select],titles=select)
ask = input('Continue? [y|numbers]/N : ')
ask = ask.replace(',',' ')
if not len(ask.split()) or ask.strip().lower() == 'y': continue
bo = False
for i in ask.split():
try:
v = int(i)
except ValueError:
bo = True
print('You have decided to quit, done')
break
if bo:
break
return
if w.group:
use = list(full.keys())
for k in ['bad','fatal','size','keys']: use.remove(k)
select = []
for g in w.group.split(':'):
p = []
for v in g.replace(',',' ').split():
if v in use or v.isdigit():
if v.isdigit():
if v in full['keys'].keys():
p.append(full['keys'][v][0])
else:
print('Warning: group: unknown key <1>: {:}'.format(v))
else:
p.append(v)
else:
found = ok = False
key = None
for k in use:
if k.startswith(v):
if found:
print('Warning: group: duplicate key: {:}'.format(v))
ok = False
else:
found = True
ok = True
key = k
if found:
if ok:
p.append(key)
else:
print('Warning: group: unknown key <2>: {:}'.format(v))
if p:
select.append(p)
else:
select = [[full['keys']['1'][0]], ]
for g in select:
plot_xvg(x=full['x'], ys=[full[i] for i in g], titles=g)
if __name__ == '__main__':
main()