-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
executable file
·318 lines (257 loc) · 8.66 KB
/
plot.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
"""Simple script for plotting .agr (Grace) files created with VMD.
Visual Molecular Dynamics allows generation of .agr files which are no longer
properly interpreted by xmgrace software. This small tool allows to quickly
plot data from an agr file (using matplotlib underneath) or to export the data
to other formats such as csv, svg or png.
"""
import argparse
from collections import OrderedDict
from collections import namedtuple
import csv
from os.path import basename
from shlex import split
SUPPORTED_FORMATS = ['csv', 'svg', 'png', 'jpg']
Vector = namedtuple('Vector', ('x', 'y'))
class Layer:
"""Layer represents a single variable in agr file, with its:
- name (extracted from legend line)
- data (so all points from corresponding ampersand-limited section)
"""
def __init__(self, name):
self.name = name
self.data = OrderedDict()
@property
def keys(self):
return list(self.data.keys())
@property
def values(self):
return list(self.data.values())
class VmdAgrPlot:
"""VmdAgrPlot corresponds to a single agr file, which should be provided
on initialization. Allows to create plots and export to csv format.
"""
def __init__(self, path, restrict_to=None):
self.title = ''
self.layers = []
self.restrict_to = restrict_to
self.load(path)
@property
def chosen_layers(self):
return [
layer
for layer in self.layers
if (
not self.restrict_to or
layer.name in self.restrict_to
)
]
def parse_plot_configs(self, agr_file):
position = 0
for line in agr_file:
if line.startswith('@'):
# keep track on position in file
position += len(line)
else:
# if we hit the end of config section escape the loop
break
line = line.strip()
command, *values = split(line)
command = command.lstrip('@')
if command == 'title':
title = values[0]
self.title = title
elif command.startswith('s') and values[0] == 'legend':
layer_name = values[1]
self.layers.append(Layer(layer_name))
elif command == 'type':
assert values[0] == 'xy'
else:
print('Unrecognized command: %s' % command)
# go to previous position (one line back) and finish the work
agr_file.seek(position)
def parse_coordinates(self, agr_file):
for layer in self.chosen_layers:
for line in agr_file:
line = line.strip()
if line == '&':
break
x, y = [float(c) for c in line.split()]
if x in layer.data:
print(
'Data inconsistency: %s occurs twice as x'
% x
)
layer.data[x] = y
def load(self, agr_file):
"""Parses given file using helper functions.
There are following assumptions about file structure:
- all configuration lines (@) are placed on top
- after each data section there is a terminal ampersand (&)
"""
self.parse_plot_configs(agr_file)
self.parse_coordinates(agr_file)
def to_csv(self, path):
"""Saves plot data into a csv file under given path.
Each variable/layer will be represented by two columns in output file:
'variable.x' and 'variable.y'. Such representation makes sense because
.agr files created with VMD commonly represent how different variables
were changing during simulation; then x-points represent subsequent
simulation frames and are consistent between different variables."""
headers = [
'%s.%s' % (layer.name, coord)
for layer in self.chosen_layers
for coord in ('x', 'y')
]
with open(path, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, headers)
writer.writeheader()
for layer in self.chosen_layers:
name = layer.name
for x, y in layer.data.items():
writer.writerow({
name + '.x': x,
name + '.y': y
})
def plot(
self, scale=Vector(1, 1), axes_labels=None,
legend_pos=None, labels=None
):
"""Creates a matplotlib plot with given parameters.
Returns:
matplotlib pyplot if successfuly generated,
False if plot generation failed.
"""
try:
import matplotlib.pyplot as plt
except ImportError:
print(
'Matplotlib not found. Matplotlib is required for plotting. '
'Use --do_not_plot to run this tool without plot generation. '
'Note that without plotting only export to csv is possible.'
)
return False
if labels:
if len(labels) != len(self.chosen_layers):
print(
'There should be exactly as many labels '
'given as there are variables chosen.'
)
return False
for i, layer in enumerate(self.chosen_layers):
plt.plot(
[scale.x * x for x in layer.keys],
[scale.y * y for y in layer.values],
label=(
labels[i]
if labels
else layer.name
)
)
plt.title(self.title)
if axes_labels:
plt.xlabel(axes_labels.x)
plt.ylabel(axes_labels.y)
if legend_pos:
plt.legend(bbox_to_anchor=legend_pos)
return plt
def create_argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'input_file',
type=argparse.FileType('r'),
help='Path to .agr file'
)
parser.add_argument(
'--do_not_plot',
action='store_true',
help='Skip plot generation (useful if matplotlib is not available)'
)
parser.add_argument(
'-t', '--title',
default=None,
type=str,
help='If not specified it title from file will be used'
)
parser.add_argument(
'-x', '--axis_x',
default='frame',
help='What text to show below x axis'
)
parser.add_argument(
'-y', '--axis_y',
default='y',
help='What text to show beside y axis'
)
parser.add_argument(
'-r', '--restrict_to',
default=None,
type=str,
nargs='+',
help='Specify which variables should be plotted'
)
parser.add_argument(
'-e', '--export',
type=str,
default=None,
choices=SUPPORTED_FORMATS,
help='Export plot to specified file format.'
)
parser.add_argument(
'-s', '--scale',
default=(1, 1),
type=float,
nargs=2,
help='Scale of the plot (two floats: x, y)'
)
parser.add_argument(
'-l', '--legend_position',
default=(1, 1),
type=float,
nargs=2,
help='Position of the legend (two floats: x, y)'
)
parser.add_argument(
'--labels',
default=None,
type=str,
nargs='+',
help='Text to use on labels instead of variable names'
)
return parser
def main():
arg_parser = create_argument_parser()
args = arg_parser.parse_args()
agr_plot = VmdAgrPlot(
args.input_file,
restrict_to=args.restrict_to
)
# override title if given
if args.title:
agr_plot.title = args.title
if not args.do_not_plot or args.export:
plot = agr_plot.plot(
scale=Vector(*args.scale),
legend_pos=Vector(*args.legend_position),
axes_labels=Vector(args.axis_x, args.axis_y),
labels=args.labels
)
if not plot:
print('Plot generation failed')
return
if args.export:
filename = basename(args.input_file.name)
export_filename = '%s.%s' % (filename, args.export)
if args.export == 'csv':
agr_plot.to_csv(export_filename)
else:
plot.savefig(export_filename)
layers_count = len(agr_plot.chosen_layers)
print(
'Exported %s layer(s) to %s.' %
(layers_count, export_filename)
)
if not args.do_not_plot:
plot.show()
if __name__ == '__main__':
main()