forked from xdoni4/mkplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
294 lines (274 loc) · 11.5 KB
/
main.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
import os
import sys
import subprocess
import json
import codecs
import math
deps_installed = True
try:
import numpy as np
except ModuleNotFoundError:
deps_installed = False
do_it = input('\n\nNumPy not found, do you want to install? [y/n] ')
while not do_it.lower() in ['y', 'n', 'yes', 'no']:
print("Wrong input, try again")
do_it = input('\n\nMatPlotLib not found, do you want to install? [y/n] ')
if do_it in ['y', 'yes']:
deps_installed = True
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'numpy'])
import numpy as np
else:
input("\nCannot go further")
try:
import matplotlib
import matplotlib.pyplot as plt
except ModuleNotFoundError:
deps_installed = False
do_it = input('\n\nMatPlotLib not found, do you want to install? [y/n] ')
while not do_it.lower() in ['y', 'n', 'yes', 'no']:
print("Wrong input, try again")
do_it = input('\n\nMatPlotLib not found, do you want to install? [y/n] ')
if do_it in ['y', 'yes']:
deps_installed = True
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'matplotlib'])
import matplotlib
import matplotlib.pyplot as plt
else:
input("\nCannot go further")
# from PIL import Image
# matplotlib.use('Qt5Agg')
fig_size_h = 0
fig_size_w = 0
fig_dpi = 0
fig_format = 'png'
class RawSubplotData():
def __init__(self, pltype, plzero, x, y, xerr_mode, xerr, yerr_mode, yerr,
axes_labels, axes_pupils, color, fmt, description):
self.type = pltype
self.zero = plzero
self.color = color
self.x = x
self.y = y
self.xerr_mode = xerr_mode
self.xerr = xerr
self.yerr_mode = yerr_mode
self.yerr = yerr
self.axes_labels = axes_labels
self.axes_pupils = axes_pupils
self.color = color
self.fmt = fmt
self.description = description
def print(self):
print("type: ", self.type, '\n',
"zero: ", self.zero, '\n',
"x: ", self.x, '\n',
"y: ", self.y, '\n',
"xerr_mode: ", self.xerr_mode, '\n',
"xerr: ", self.xerr, '\n',
"yerr_mode: ", self.yerr_mode, '\n',
"yerr: ", self.yerr, '\n',
"axes_labels: ", self.axes_labels, '\n',
"axes_pupils: ", self.axes_pupils, '\n',
"color: ", self.color, '\n',
"shape: ", self.fmt, '\n',
"description: ", self.description, '\n')
class JsonParser:
@classmethod
def read(self, filename):
with codecs.open(filename, "r", "utf-8") as read_file:
data = json.load(read_file)
return data
@classmethod
def parse_object(self, data):
plots = []
for i, plot in enumerate(data["data"], start=0):
array = []
subplots = plot["subplots"]
for subplot in subplots:
array.append(JsonParser.parse_subplot(subplot))
plots.append((array, plot["title"]))
return plots
@classmethod
def parse_subplot(self, subplot):
pltype = subplot["type"]
plzero = False
if pltype.split('_')[-1] == 'zero':
plzero = True
pltype = '_'.join(pltype.split('_')[:-1])
x = np.array(subplot["x"])
y = np.array(subplot["y"])
if pltype == "plot":
xerr_mode = "absolute"
yerr_mode = "absolute"
xerr = np.array([0.0 for i in x])
yerr = np.array([0.0 for i in y])
fmt = "o"
else:
xerr_mode = subplot["xerr_mode"]
yerr_mode = subplot["yerr_mode"]
if xerr_mode == "absolute":
xerr = np.array(subplot["xerr"])
elif xerr_mode == "constant":
xerr = np.array([float(subplot["xerr"]) for i in x])
elif xerr_mode == "relative":
xerr = np.array([float(subplot["xerr"]) * i for i in x])
else:
print("\nWARNING: considering xerr as \"absolute\"")
xerr = np.array(subplot["xerr"])
if yerr_mode == "absolute":
yerr = np.array(subplot["yerr"])
elif yerr_mode == "constant":
yerr = np.array([float(subplot["yerr"]) for i in y])
elif yerr_mode == "relative":
yerr = np.array([float(subplot["yerr"]) * i for i in y])
else:
print("\nWARNING: considering yerr as \"absolute\"")
yerr = np.array(subplot["yerr"])
fmt = subplot["shape"]
color = subplot["color"]
axes_labels = subplot["axes_labels"]
axes_pupils = subplot["axes_pupils"]
description = subplot["description"]
return RawSubplotData(pltype, plzero, x, y, xerr_mode, xerr, yerr_mode, yerr,
axes_labels, axes_pupils, color, fmt, description)
class Plotter:
@classmethod
def plot(self, plots):
self.makedirs()
if fig_size_w and fig_size_h:
fig = plt.figure(figsize=(fig_size_w, fig_size_h))
else:
fig = plt.figure()
axes = []
for i, plot in enumerate(plots):
f = codecs.open("generated_files/coefs.txt", 'a', "utf-8")
f.write(plot[1] + '\n\n')
f.close()
axes.append(fig.add_subplot(1, len(plots), i+1))
for subplot in plot[0]:
Plotter.plot_subplot(axes[i], subplot)
axes[i].set_title(plot[1])
with codecs.open("generated_files/coefs.txt", 'a', "utf-8") as f:
f.write('-----------------------------------------------------\n\n')
img = open("images/fig."+fig_format, 'w')
plt.show()
if fig_dpi:
fig.savefig("images/fig."+fig_format, fig_dpi=fig_dpi, format=fig_format)
else:
fig.savefig("images/fig."+fig_format, format=fig_format)
@classmethod
def plot_subplot(self, ax, s):
x = np.log(s.x) if 'log' in s.type.split('_') and 'x' in s.type.split('_') else s.x
y = np.log(s.y) if 'log' in s.type.split('_') and 'y' in s.type.split('_') else s.y
if s.zero:
_x = np.append(x, x * -1)
_y = np.append(y, y * -1)
else:
_x = x
_y = y
# ax.scatter(0, 0, color='white')
ax.minorticks_on()
ax.grid(True, which='major', linewidth=1)
ax.grid(True, which='minor', linewidth=0.5)
ax.set_xlabel(s.axes_labels[0] + ', ' + s.axes_pupils[0], fontsize=15)
ax.set_ylabel(s.axes_labels[1] + ', ' + s.axes_pupils[1], fontsize=15)
border_left = min(x) - 0.2*(max(x)-min(x))
border_left = 0 if border_left > 0 and s.zero else border_left
border_right = max(x) + 0.2*(max(x)-min(x))
border_right = 0 if border_right < 0 and s.zero else border_right
r = np.linspace(border_left, border_right)
f = codecs.open("generated_files/coefs.txt", 'a', "utf-8")
if s.type == 'lsq':
A = np.vstack([_x, np.ones(len(_y))]).T
k, b = np.linalg.lstsq(A, _y, rcond=None)[0]
sigma_k, sigma_b = Plotter.sigma_eval(_x, _y, k, b)
f.write(s.type + ' ' + s.color + ' "' + s.fmt + '"' +\
': k=' + str(k) + ' b='+str(b) + ' sigma_k='+\
str(sigma_k)+' sigma_b='+str(sigma_b)+'\n\n')
ax.plot(r, k*r+b, color=s.color, label=s.description, linewidth=1)
ax.errorbar(s.x, s.y, s.yerr, s.xerr, fmt=s.fmt, markersize=3,
linewidth=1, color=s.color, ecolor=s.color, capsize=0)
elif s.type == 'dots':
ax.errorbar(s.x, s.y, s.yerr, s.xerr, fmt=s.fmt, markersize=3, linewidth=1,
color=s.color, label=s.description, ecolor=s.color, capsize=0)
elif 'log' in s.type.split('_'):
A = np.vstack([_x, np.ones(len(_y))]).T
k, b = np.linalg.lstsq(A, _y, rcond=None)[0]
sigma_k, sigma_b = Plotter.sigma_eval(_x, _y, k, b)
f.write(s.type + ' ' + s.color + ' "' + s.fmt + '"' +\
': k=' + str(k) + ' b='+str(b) + ' sigma_k='+\
str(sigma_k)+' sigma_b='+str(sigma_b)+'\n\n')
ax.plot(r, k*r+b, color=s.color, label=s.description, linewidth=1)
ax.errorbar(x, y, fmt=s.fmt, markersize=3, linewidth=1,
color=s.color, ecolor=s.color, capsize=0)
elif s.type.rstrip('_0123456789') == 'poly':
coefs = np.polyfit(_x, _y, int(s.type.split('_')[1]))
ys = np.zeros(len(r))
for i, c in enumerate(coefs):
ys += c * r ** (len(coefs)-i-1)
f.write(s.type + ' ' + s.color + ' "' + s.fmt + '"' + ":\n")
for i, c in enumerate(coefs):
f.write("a_"+str(len(coefs)-i-1) + "=" + str(c) + '\n')
f.write('\n')
ax.plot(r, ys, color=s.color, label=s.description, linewidth=1)
ax.errorbar(s.x, s.y, s.yerr, s.xerr, fmt=s.fmt, markersize=3,
linewidth=1, color=s.color, ecolor=s.color, capsize=0)
elif s.type == "plot":
ax.plot(s.x, s.y, linewidth=1, label=s.description, color=s.color)
ax.legend()
f.close()
@staticmethod
def makedirs():
if not os.path.exists('generated_files'):
os.mkdir('generated_files')
fopen = codecs.open("generated_files/coefs.txt", 'a', "utf-8")
fopen.write('-----------------------------------------------------\n\n')
fopen.close()
if not os.path.isdir('images'):
os.mkdir('images')
@classmethod
def sigma_eval(self, x, y, k, b):
xdisp = np.var(x)
ydisp = np.var(y)
sigma_k = np.sqrt((ydisp/xdisp - k ** 2) / (len(x)-2))
sigma_b = sigma_k * np.sqrt(np.average(x * x))
return (sigma_k, sigma_b)
manual_req = False
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg in ['-h', '--help', 'man', '--man']:
with codecs.open("README.md", 'r', 'utf-8') as f:
print(f"\n\n\n{f.read()}\n\n\n")
manual_req = True
else:
args = arg.split('=')
if len(args) > 1:
if args[0] == '--size' and len(args[1].split("x")) > 1:
fig_size_w = float(args[1].split("x")[0])
fig_size_h = float(args[1].split("x")[1])
if args[0] == '--dpi':
fig_dpi = float(args[1])
if args[0] == '--format':
fig_format = args[1]
if __name__ == "__main__" and not manual_req:
try:
if deps_installed:
data = JsonParser.read("conf.json")
plots = JsonParser.parse_object(data)
Plotter.plot(plots)
except TypeError:
print("\nData error: check that number of points in x, y, xerr and yerr matches")
input()
except json.decoder.JSONDecodeError:
print("\nData error: check that all points in x, y, xerr \
and yerr are floating-point numbers")
input()
except KeyError:
print("\nData error: something necessary is missing in conf.json")
input()
except FileNotFoundError:
print("\nData error: conf.json file not found near to main.py")
input()
#except ValueError:
# print("\nData error: necessary subplot data is missing")
# input()