-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_plot.py.old
231 lines (177 loc) · 6.37 KB
/
make_plot.py.old
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
import time
import cPickle as pickle
import os
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import mpld3
import csv
import sys
import glob
from mpld3 import plugins
def insert(originalfile, string):
'''
For appending new text to the beginning
of a file.
'''
with open(originalfile,'r') as f:
with open('newfile.txt','w') as f2:
f2.write(string)
f2.write(f.read())
os.rename('newfile.txt',originalfile)
def make_html_plot_for_file(filename, my_title):
my_fontsize = 20
# read in csv file
if os.stat(filename).st_size:
data = np.genfromtxt(filename, delimiter=',')
else:
# empty data file
data = np.zeros([2, 2])
if len(data.shape) == 1:
return "<h2> " + my_title + " has no data </h2>"
x = data[:, 0]
fig, ax = plt.subplots(figsize=(10, 5))
for k in range(1, data.shape[1]):
y = data[:, k]
# sort data
x_sorted = [x1 for (x1,y1) in sorted(zip(x, y))]
y_sorted = [y1 for (x1,y1) in sorted(zip(x, y))]
lines = ax.plot(x_sorted, y_sorted, '.-', markersize=15, label = 'ion ' + str(k-1))
plt.legend(fontsize = my_fontsize, loc = 'best')
plt.title(my_title, fontsize = my_fontsize)
ax.grid()
plt.xticks(fontsize = my_fontsize)
plt.yticks(fontsize = my_fontsize)
plt.xlim([np.min(x), np.max(x)])
#90% of the time that this function takes is taken up after this line
plugins.clear(fig)
plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(), plugins.Zoom(enabled = True),
plugins.MousePosition(fontsize = my_fontsize))
java_txt = mpld3.fig_to_html(fig)
plt.close()
return java_txt
#################################################################
# main program #
#################################################################
if len(sys.argv) == 1:
current_date = time.strftime("%Y%m%d")
else:
current_date = sys.argv[1]
list_of_files = glob.glob(current_date + '/*.info')
for i,j in enumerate(list_of_files):
# remove any duplicates
name = j.split("_")
if len(name) > 2:
del list_of_files[i]
list_of_files = sorted(list_of_files, reverse=True)
no_of_files = len(list_of_files)
current_file_no = no_of_files/5
print "LENGTH: ", no_of_files
print "\ncurrent_file_no: ", current_file_no
no_of_graphs_plotted = 5 - no_of_files%5
if no_of_graphs_plotted == 5 and current_file_no != 0:
no_of_graphs_plotted = 0
current_file_no -= 1
main_file = open(current_date + '/index.html', 'w')
html_text = '\
<html>\
<frameset cols="20%,80%">\
<frame src="list_of_files.html">\
<frame src="data_{}.html", name="data_frame">\
</frameset>\
</html>\
'.format(current_file_no)
main_file.write(html_text)
main_file.close()
cwd = os.getcwd()
html_text = "<html><body>\n"
if os.path.exists(cwd + "/" + current_date+ "/data_"\
+ str(current_file_no) + ".html"):
with open(current_date + '/data_' + str(current_file_no) + '.html', 'r') as file:
filelines = file.readlines()[1:]
os.remove(current_date + '/data_' + str(current_file_no) + '.html')
file = open(current_date + '/data_' + str(current_file_no) + '.html', 'w')
file.write(html_text)
file_exists = True
else:
file = open(current_date + '/data_' + str(current_file_no) + '.html', 'w')
file.write(html_text)
file_exists = False
if not os.path.exists(cwd + "/" + current_date + "/list_of_files.html"):
list_file = open(current_date + '/list_of_files.html', 'w')
list_file.write(html_text)
else:
list_file = open(current_date + '/list_of_files.html', 'a')
# keep a list of files for which plots have
# already been generated
list_of_files_pickle = cwd + "/" + current_date \
+ "/list_of_files.pickle"
try:
with open(list_of_files_pickle, "rb") as pickle_in:
pickle_list = pickle.load(pickle_in)
except IOError:
pickle_list = []
list_file_txt = ""
plotted_count = 0
print "no_of_graphs_plotted initially: ", no_of_graphs_plotted
for k in range(no_of_files):
info_filename = list_of_files[k]
my_file = open(info_filename, 'r')
# lines[0] is the title
# lines[1] is the path to the file
lines = my_file.read().split("\n")
# Don't make a new plot if it already exists
if lines[0] in pickle_list:
continue
else:
pickle_list.append(lines[0])
java_text = make_html_plot_for_file(lines[1], lines[0]) + "\n"
plotted_count += 1
# write scan number into table of contents file
target_id = lines[0].split(" - ")[2]
list_file_txt += '<a href="data_' + str(current_file_no) + '.html#' + target_id \
+ '" target="data_frame">' + lines[0] + '</a><br><br>\n'
# write graph into main file
file.write('<br id = ' + target_id + ">\n")
file.write(java_text)
file.write("<br><hr>\n")
no_of_graphs_plotted += 1
print "no_of_graphs_plotted: ", no_of_graphs_plotted
if no_of_graphs_plotted == 5:
# change the file
no_of_graphs_plotted = 0
current_file_no -= 1
if current_file_no < 0:
continue
html_text = "</body>\</html>\n"
file.write(html_text)
file.close()
print "no_of_graphs_plotted, finishing a run: ", no_of_graphs_plotted
print "current_file_no: ", current_file_no
# open new file
if not os.path.exists(current_date + '/data_'
+ str(current_file_no) + '.html'):
file = open(current_date + '/data_' + str(current_file_no) + '.html', 'w')
html_text = "<html><body>\n"
file.write(html_text)
else:
break
list_file.close()
insert(current_date + '/list_of_files.html', list_file_txt)
print "TOTAL PLOTTED: ", plotted_count
html_text = "</body>\</html>\n"
if file_exists and filelines != []:
file.writelines(filelines)
file.write("\n")
file.close()
else:
try:
file.write(html_text + "\n")
file.close()
except ValueError:
# file was already closed in loop
pass
list_file.close()
with open(list_of_files_pickle, "wb") as pickle_out:
pickle.dump(pickle_list, pickle_out)