-
Notifications
You must be signed in to change notification settings - Fork 18
/
process_meshes.py
337 lines (269 loc) · 10.9 KB
/
process_meshes.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import json
import sys
import numpy as np
import matplotlib
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.colors import TwoSlopeNorm
from os.path import splitext, join
def import_mesh(mesh):
if all([x in mesh.keys() for x in ['mesh_min',
'mesh_max',
'probed_matrix']]):
mesh_min = mesh['mesh_min']
mesh_max = mesh['mesh_max']
probed_matrix = mesh['probed_matrix']
n_y_points = len(probed_matrix)
n_x_points = len(probed_matrix[0])
x_coords = np.round(np.linspace(mesh_min[0],
mesh_max[0],
n_x_points,
float),
decimals=1)
y_coords = np.round(np.linspace(mesh_min[1],
mesh_max[1],
n_y_points,
float),
decimals=1)
mesh_points = np.array(probed_matrix, float)
return {'x': x_coords, 'y': y_coords, 'mesh': mesh_points}
def heatmap(data, row_labels, col_labels, ax=None,
cbar_kw={}, cbarlabel="", **kwargs):
"""
Create a heatmap from a numpy array and two lists of labels.
Parameters
----------
data
A 2D numpy array of shape (N, M).
row_labels
A list or array of length N with the labels for the rows.
col_labels
A list or array of length M with the labels for the columns.
ax
A `matplotlib.axes.Axes` instance to which the heatmap is plotted. If
not provided, use current axes or create a new one. Optional.
cbar_kw
A dictionary with arguments to `matplotlib.Figure.colorbar`. Optional.
cbarlabel
The label for the colorbar. Optional.
**kwargs
All other arguments are forwarded to `imshow`.
"""
if not ax:
ax = plt.gca()
# Plot the heatmap
im = ax.imshow(data, **kwargs)
# Create colorbar
cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom")
# We want to show all ticks...
ax.set_xticks(np.arange(data.shape[1]))
ax.set_yticks(np.arange(data.shape[0]))
# ... and label them with the respective list entries.
ax.set_xticklabels(col_labels)
ax.set_yticklabels(row_labels)
# Let the horizontal axes labeling appear on top.
ax.tick_params(top=True, bottom=False,
labeltop=True, labelbottom=False)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=-30, ha="right",
rotation_mode="anchor")
# Turn spines off and create white grid.
for edge, spine in ax.spines.items():
spine.set_visible(False)
ax.set_xticks(np.arange(data.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(data.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="w", linestyle='-', linewidth=3)
ax.tick_params(which="minor", bottom=False, left=False)
return im, cbar
def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
textcolors=["white", "black"],
threshold=None, **textkw):
"""
A function to annotate a heatmap.
Parameters
----------
im
The AxesImage to be labeled.
data
Data used to annotate. If None, the image's data is used. Optional.
valfmt
The format of the annotations inside the heatmap. This should either
use the string format method, e.g. "$ {x:.2f}", or be a
`matplotlib.ticker.Formatter`. Optional.
textcolors
A list or array of two color specifications. The first is used for
values below a threshold, the second for those above. Optional.
threshold
Value in data units according to which the colors from textcolors are
applied. If None (the default) uses the middle of the colormap as
separation. Optional.
**kwargs
All other arguments are forwarded to each call to `text` used to create
the text labels.
"""
if not isinstance(data, (list, np.ndarray)):
data = im.get_array()
# Normalize the threshold to the images color range.
if threshold is not None:
threshold = im.norm(threshold)
else:
threshold = im.norm(data.max())/2.
# Set default alignment to center, but allow it to be
# overwritten by textkw.
kw = dict(horizontalalignment="center",
verticalalignment="center")
kw.update(textkw)
# Get the formatter in case a string is supplied
if isinstance(valfmt, str):
valfmt = matplotlib.ticker.StrMethodFormatter(valfmt)
# Loop over the data and create a `Text` for each "pixel".
# Change the text's color depending on the data.
texts = []
for i in range(data.shape[0]):
for j in range(data.shape[1]):
kw.update(color=textcolors[int(im.norm(data[i, j]) > threshold)])
text = im.axes.text(j, i, valfmt(data[i, j], None), **kw)
texts.append(text)
return texts
def plot_mesh(mesh, title='', subtitle=''):
fig, ax = plt.subplots(figsize=(8,8))
data = mesh['mesh']
# absmax = max(abs(data.min()), abs(data.max()))
absmax = 0.2
norm = TwoSlopeNorm(vmin=-absmax, vcenter=0, vmax=absmax)
im, cbar = heatmap(mesh['mesh'], mesh['x'], mesh['y'], ax=ax,
cmap="RdBu_r", norm=norm, cbarlabel="Z-Offset")
texts = annotate_heatmap(im, valfmt="{x:.4f}")
plt.gca().invert_yaxis()
plt.suptitle(title, fontsize=18)
plt.title(subtitle, fontsize=10)
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
return(fig)
def calc_mesh_delta(mesh_ref, mesh_test):
delta = mesh_test['mesh'] - mesh_ref['mesh']
return {'x': mesh_ref['x'], 'y': mesh_ref['y'], 'mesh': delta}
def read_results_file(results_fp):
with open(results_fp, 'r') as f:
results = json.load(f)
return(results)
def plot_deflections(delta, title=''):
fig, ax = plt.subplots(2)
fig.tight_layout(rect=[0, 0.03, 1, 0.85])
x_mid = delta['mesh'][int(np.floor(delta['mesh'].shape[0]-1)/2)]
x_deflect = - x_mid - min(-x_mid)
y_min = delta['mesh'][:, 0]
y_min_deflect = - y_min - min(-y_min)
y_max = delta['mesh'][:, -1]
y_max_deflect = - y_max - min(-y_max)
ax[0].plot(delta['x'],
x_deflect,
'-',
color="#6d89bf")
ax[0].plot(delta['x'],
np.zeros(delta['x'].shape),
'--',
color="#dddddd")
ax[1].plot(delta['y'],
y_max_deflect,
'-',
color="#c2d1ed")
ax[1].plot(delta['y'],
y_min_deflect,
'-',
color="#1a376e")
ax[1].plot(delta['y'],
np.zeros(delta['y'].shape),
'--',
color="#dddddd")
ax[0].set_title('Imputed deflection: X axis')
ax[1].set_title('Y axis (dark left light right)')
ax[0].set_ylim([-0.2, 0.2])
ax[1].set_ylim([-0.2, 0.2])
plt.suptitle(title, fontsize=12)
return(fig)
def plot_deflection_surface(delta, title=''):
fig, ax = plt.subplots(2)
fig.tight_layout(rect=[0, 0.03, 1, 0.85])
for i in reversed(range(delta['mesh'].shape[0])):
xgrad = cm.get_cmap('Blues_r',
delta['mesh'].shape[0] + 2)
ax[0].plot(delta['x'],
delta['mesh'][i],
'-',
color=xgrad(i),
alpha=1)
for i in reversed(range(delta['mesh'].shape[1])):
ygrad = cm.get_cmap('Blues_r',
delta['mesh'].shape[1] + 2)
ax[1].plot(delta['y'],
delta['mesh'][:,i],
'-',
color=ygrad(i),
alpha=0.8)
ax[0].plot(delta['x'],
np.zeros(delta['x'].shape),
'--',
color="#dddddd")
ax[1].plot(delta['y'],
np.zeros(delta['y'].shape),
'--',
color="#dddddd")
ax[0].set_title('X axis surface plot')
ax[1].set_title('Y axis surface plot')
ax[0].set_ylim([-0.2, 0.2])
ax[1].set_ylim([-0.2, 0.2])
plt.suptitle(title, fontsize=12)
return(fig)
if __name__ == "__main__":
args = sys.argv[1:]
print(len(args))
print(args)
for infile in args:
results = read_results_file(infile)
user = results['metadata']['user']['id']
printer = results['metadata']['user']['printer']
backers = results['metadata']['user']['backers']
x_rails = results['metadata']['user']['x_rails']
subtitle = '{user}: {printer}\n{backers}, {x_rails}'.format(user=user,
printer=printer,
backers=backers,
x_rails=x_rails)
premesh = True
try:
mesh_pre = import_mesh(results['pre_mesh']['mesh'])
except KeyError:
premesh = False
mesh_ref = import_mesh(results['cold_mesh']['mesh'])
mesh_test = import_mesh(results['hot_mesh']['mesh'])
delta = calc_mesh_delta(mesh_ref, mesh_test)
meshplot = plot_mesh(delta,
title='Difference',
subtitle=subtitle)
coldplot = plot_mesh(mesh_ref,
title='Cold frame mesh',
subtitle=subtitle)
postplot = plot_mesh(mesh_test,
title='Hot frame mesh',
subtitle=subtitle)
defplot = plot_deflections(delta,
title=subtitle)
surfplot = plot_deflection_surface(delta,
title=subtitle)
plot_basename = splitext(infile)[0]
meshplot.savefig('.'.join([plot_basename, 'mesh.png']))
coldplot.savefig('.'.join([plot_basename, 'coldmesh.png']))
postplot.savefig('.'.join([plot_basename, 'postmesh.png']))
defplot.savefig('.'.join([plot_basename, 'deflection.png']))
surfplot.savefig('.'.join([plot_basename, 'surface.png']))
if premesh:
preplot = plot_mesh(mesh_pre,
title='Cold bed mesh',
subtitle=subtitle)
bed_delta = calc_mesh_delta(mesh_pre, mesh_ref)
bedplot = meshplot = plot_mesh(bed_delta,
title='Bed heatup diff',
subtitle=subtitle)
preplot.savefig('.'.join([plot_basename, 'bed_coldmesh.png']))
bedplot.savefig('.'.join([plot_basename, 'bed_diffmesh.png']))
plt.close('all')