-
Notifications
You must be signed in to change notification settings - Fork 2
/
cost_postproc.py
144 lines (118 loc) · 5.75 KB
/
cost_postproc.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
from scipy.interpolate import griddata
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from mpl_toolkits.axes_grid1 import make_axes_locatable
import pprint
from matplotlib.ticker import FuncFormatter
import itertools
def process(frame, xdata, ydata, func, verification = False, params = None , plot_on = True, printing_on = True,
uncertainty_func = None, uncertainty_params = None ,p0 = None, bounds = (0., np.inf)):
#E.g.
#xdata = [frame['k'].values, frame['tslc'].values, frame['n'].values]
#ydata = frame[subroutine].values
if not verification:
popt, pcov = curve_fit(func, xdata, ydata, bounds=bounds, p0 = p0)
perr = np.sqrt(np.diag(pcov))
if printing_on:
print('Params: ', popt)
print('Interpolation covariance:', pcov)
print('Error 1std:', perr)
plot_rows = 2
else:
popt = params
perr = None
plot_rows = 3
x, y = xdata[:2]
z = ydata #frame['QPE'].values
grid_x, grid_y = np.mgrid[min(x):max(x):200j, min(y):max(y):200j]
points = np.array(xdata[:2]).T
grid_z = griddata(points, z, (grid_x, grid_y), method='linear')
comma_fmt = FuncFormatter(lambda x, p: format(int(x), ','))
if plot_on:
fig = plt.figure(figsize = (8,int(4*plot_rows) ))
fig.add_subplot(plot_rows, 2, 1)
ax = plt.gca()
ax.scatter(x, y, marker = '+', c= 100-z, s = 50, cmap='gray')
#ax.set_xlabel(xlabel, size = 18)
#ax.set_ylabel(ylabel, size = 18)
a = (max(xdata[0])-min(xdata[0]))/(max(xdata[1])-min(xdata[1]))
im = ax.imshow(grid_z.T, cmap = 'jet', extent=(min(xdata[0]), max(xdata[0]),min(xdata[1]), max(xdata[1]))
,aspect = 'auto',
origin='lower')
plt.xlabel('k')
plt.ylabel('r')
plt.title('Interpolated experimental data', fontsize = 14, pad = 10)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im, cax=cax, format=comma_fmt)
fig.add_subplot( plot_rows, 2, 2)
ax1 = plt.gca()
interpolated = func([grid_x, grid_y, frame['n'].values[0]], *popt )
im1 = ax1.imshow(interpolated.T ,cmap = 'jet', origin='lower',
extent=(min(xdata[0]), max(xdata[0]),min(xdata[1]), max(xdata[1])),aspect = 'auto')
plt.grid(True)
plt.xlabel('k')
plt.ylabel('r')
plt.title('Model prediction', fontsize = 14, pad = 10)
divider = make_axes_locatable(ax1)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im1, cax=cax, format=comma_fmt)
#plt.tight_layout()
fig.add_subplot(plot_rows,2, 3)
ax2 = plt.gca()
interpolated = func([grid_x, grid_y, frame['n'].values[0]], *popt )
im2 = ax2.imshow(100.*(interpolated.T -grid_z.T)/grid_z.T ,cmap = 'jet', origin='lower',
extent=(min(xdata[0]), max(xdata[0]),min(xdata[1]), max(xdata[1])),aspect = 'auto')
plt.grid(True)
plt.xlabel('k')
plt.ylabel('r')
plt.title('Relative error [%]', fontsize = 14, pad = 10)
divider = make_axes_locatable(ax2)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im2, cax=cax, format = '%d%%')
#plt.tight_layout()
fig.add_subplot(plot_rows, 2, 4)
ax3 = plt.gca()
interpolated = func([grid_x, grid_y, frame['n'].values[0]], *popt )
im3 = ax3.imshow((interpolated.T -grid_z.T) ,cmap = 'jet', origin='lower',
extent=(min(xdata[0]), max(xdata[0]),min(xdata[1]), max(xdata[1])),aspect = 'auto')
plt.grid(True)
plt.xlabel('k')
plt.ylabel('r')
plt.title('Absolute error', fontsize = 14, pad = 10)
divider = make_axes_locatable(ax3)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im3, cax=cax, format=comma_fmt)
#plt.tight_layout()
if verification:
fig.add_subplot(plot_rows,2, 5)
ax4 = plt.gca()
interpolated = uncertainty_func([grid_x, grid_y, frame['n'].values[0]], *popt, uncertainty_params )
im4 = ax4.imshow(100.*(interpolated.T/grid_z.T) ,cmap = 'jet', origin='lower',
extent=(min(xdata[0]), max(xdata[0]),min(xdata[1]), max(xdata[1])),aspect = 'auto')
plt.grid(True)
plt.xlabel('k')
plt.ylabel('r')
plt.title('Relative uncertainty[%]', fontsize = 14, pad = 10)
divider = make_axes_locatable(ax4)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im4, cax=cax, format = '%2.f%%')
fig.add_subplot(plot_rows,2, 6)
ax5 = plt.gca()
interpolated = uncertainty_func([grid_x, grid_y, frame['n'].values[0]], *popt, uncertainty_params )
im5 = ax5.imshow(interpolated.T ,cmap = 'jet', origin='lower',
extent=(min(xdata[0]), max(xdata[0]),min(xdata[1]), max(xdata[1])),aspect = 'auto')
plt.grid(True)
plt.xlabel('k')
plt.ylabel('r')
plt.title('Absolute uncertainty', fontsize = 14, pad = 10)
divider = make_axes_locatable(ax5)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im5, cax=cax, format=comma_fmt)
print('Number of data-points:',len(frame['n'].values) )
fig.tight_layout()
fig.suptitle('Circuit depth model, n=' + str(frame['n'].values[0]), y = 1.03, fontsize = 15)
return popt, perr