-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput_data_widget.py
More file actions
378 lines (329 loc) · 16.2 KB
/
input_data_widget.py
File metadata and controls
378 lines (329 loc) · 16.2 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import pickle
import database
import file_parser
import misc_functions
import numpy as np
from scipy import ndimage, misc
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from PyQt5.QtCore import pyqtSignal, Qt, QObject
from PyQt5 import uic, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog
Ui_InputDataWidget, QWidget_InputData = uic.loadUiType("ui_input_data_widget.ui")
default_e_field_path = 'volume_box_default.pkl'
default_nerve_shape_path = 'volume_shape_default.pkl'
# This class does:
# - load field (from CST or E-Field-Matrix python file)
# - save field (E-Field-Matrix python file)
# - show field in widget
# - manipulate field (user input, offset)
class InputDataWidget(QWidget_InputData, Ui_InputDataWidget):
e_field_changed = pyqtSignal()
nerve_shape_changed = pyqtSignal()
def __init__(self, parent=None):
super(InputDataWidget, self).__init__(parent)
self.setupUi(self)
self.updated_xlims = ()
self.updated_ylims = ()
self.add_e_field_plot(plt.figure())
self.add_nerve_shape_plot(plt.figure())
# self.configure_layer_slider()
# self.nerve_shape = self.load_default_nerve_shape()
self.custom_nerve = None
self.nerve_shape = None
self.e_field = None
# self.nerve_shape = database.NerveShape(0,0,0,[],[],[],[],[],[])
self.scaling = None
self.load_e_field_button.clicked.connect(self.load_e_field)
self.save_e_field_button.clicked.connect(self.save_e_field)
self.smooth_push_button.clicked.connect(self.smooth_e_field)
self.ex_button.clicked.connect(self.update_e_field_plot)
self.ey_button.clicked.connect(self.update_e_field_plot)
self.ez_button.clicked.connect(self.update_e_field_plot)
self.load_nerve_shape_button.clicked.connect(self.load_nerve_shape)
self.save_nerve_shape_button.clicked.connect(self.save_nerve_shape)
self.xy_button.clicked.connect(self.update_nerve_shape_plot)
self.xz_button.clicked.connect(self.update_nerve_shape_plot)
self.yz_button.clicked.connect(self.update_nerve_shape_plot)
self.smooth_nerve_shape_button.clicked.connect(self.smooth_nerve_shape)
self.e_field_layer_slider.valueChanged.connect(self.update_e_field_plot)
# ------------------------------------------------------------------------------------------------------------------
# File operations
# ------------------------------------------------------------------------------------------------------------------
def openFileNameDialog(self, file_type):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "",
file_type, options=options)
if fileName:
return fileName
def saveFileNameDialog(self, file_type):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(self, "QFileDialog.getOpenFileName()", "",
file_type, options=options)
if fileName:
return fileName
def load_e_field(self):
filename = self.openFileNameDialog("Pickle Files (*.pkl);; Text Files (*.txt *.csv)")
# filename = self.openFileNameDialog("All Files (*);;Python Files (*.py)")
if not filename:
# TODO: warning
return
if filename.endswith('.pkl'):
with open(filename, 'rb') as e:
self.e_field = pickle.load(e)
elif filename.endswith('.txt'):
storage = database.DataBase()
# parser = file_parser.NewCSTFileParser("", filename)
# parser.parse_file(storage)
# storage.convert_units(1e3) # convert mm from CST to um used for cable
parser = file_parser.S4LFileParser("", filename)
parser.parse_file(storage)
storage.convert_units(1e6) # convert m from S4L to um used for cable
self.e_field = storage.generate_e_field_matrix()
self.e_field_changed.emit()
self.update_e_field_plot()
def save_e_field(self):
filename = self.saveFileNameDialog("Pickle Files (*.pkl)")
if not filename:
# TODO: warning
return
if filename[-4:] == ".pkl":
with open(filename, 'wb') as f:
pickle.dump(self.e_field, f)
else:
with open(filename + ".pkl", 'wb') as f:
pickle.dump(self.e_field, f)
def load_nerve_shape(self):
filename = self.openFileNameDialog("Pickle Files (*.pkl);; Text Files (*.txt *.csv)")
if not filename:
# TODO: warning
return
if filename.endswith('.pkl'):
with open(filename, 'rb') as e:
self.nerve_shape = pickle.load(e)
elif filename.endswith('.txt'):
storage = database.DataBase()
parser = file_parser.NewCSTFileParser("", filename)
parser.parse_file(storage)
storage.convert_units(1e3) # convert mm from CST to um used for cable
self.nerve_shape = storage.generate_nerve_shape()
elif filename.endswith('.csv'):
storage = database.DataBase()
parser = file_parser.CSVNerveShapeParser("", filename)
parser.parse_file(storage)
# storage.convert_units(1e3) # convert m from S4L to um used for cable
self.nerve_shape = storage.generate_nerve_shape()
else:
print('Error: Loading nerve shape')
self.nerve_shape_changed.emit()
self.update_nerve_shape_plot()
def save_nerve_shape(self):
filename = self.saveFileNameDialog("Pickle Files (*.pkl)")
if not filename:
# TODO: warning
return
if filename[-4:] == ".pkl":
with open(filename, 'wb') as f:
pickle.dump(self.nerve_shape, f)
else:
with open(filename + ".pkl", 'wb') as f:
pickle.dump(self.nerve_shape, f)
# ------------------------------------------------------------------------------------------------------------------
# Update main.py
# ------------------------------------------------------------------------------------------------------------------
def get_current_e_field_plot(self):
return self.plot_e_field(self.e_field, self.e_field_layer_slider.value())
def get_e_field_with_custom_nerve_plot(self):
if self.custom_nerve:
return self.plot_2d_field_with_cable(self.e_field, self.e_field_layer_slider.value(), self.custom_nerve, self.scaling)
else:
return self.plot_e_field(self.e_field, self.e_field_layer_slider.value())
def get_nerve_shape_plot(self):
return self.plot_3d_nerve_shape(self.nerve_shape)
# ------------------------------------------------------------------------------------------------------------------
# Internal plot
# ------------------------------------------------------------------------------------------------------------------
def update_e_field_plot(self):
if not self.e_field:
return
if self.configure_layer_slider():
self.layer_label.setText(str(self.e_field.z[self.e_field_layer_slider.value()]))
fig = self.plot_e_field(self.e_field, self.e_field_layer_slider.value())
self.e_field_layer_slider.setEnabled(True)
else:
fig = self.plot_e_field(self.e_field, 0)
self.remove_e_field_plot()
self.add_e_field_plot(fig)
def update_nerve_shape_plot(self):
if not self.nerve_shape:
return
if self.xy_button.isChecked():
fig = self.plot_xy_nerve_shape(self.nerve_shape)
elif self.xz_button.isChecked():
fig = self.plot_xz_nerve_shape(self.nerve_shape)
else:
fig = self.plot_yz_nerve_shape(self.nerve_shape)
self.remove_nerve_shape_plot()
self.add_nerve_shape_plot(fig)
def add_e_field_plot(self, fig):
self.efield_canvas = FigureCanvas(fig)
self.efield_canvas.setFocusPolicy(Qt.StrongFocus)
self.efield_canvas.setFocus()
self.e_field_layout.addWidget(self.efield_canvas)
self.efield_canvas.draw()
self.toolbar = NavigationToolbar(self.efield_canvas,
self.e_field_widget, coordinates=True)
self.e_field_layout.addWidget(self.toolbar)
def remove_e_field_plot(self, ):
# if hasattr(self, 'canvas'):
self.e_field_layout.removeWidget(self.efield_canvas)
self.efield_canvas.close()
self.e_field_layout.removeWidget(self.toolbar)
self.toolbar.close()
def add_nerve_shape_plot(self, fig):
self.nerve_shape_canvas = FigureCanvas(fig)
self.nerve_shape_canvas.setFocusPolicy(Qt.StrongFocus)
self.nerve_shape_canvas.setFocus()
self.nerve_shape_layout.addWidget(self.nerve_shape_canvas)
self.nerve_shape_canvas.draw()
self.toolbar = NavigationToolbar(self.nerve_shape_canvas,
self.nerve_shape_widget, coordinates=True)
self.nerve_shape_layout.addWidget(self.toolbar)
def remove_nerve_shape_plot(self, ):
# if hasattr(self, 'canvas'):
self.nerve_shape_layout.removeWidget(self.nerve_shape_canvas)
self.nerve_shape_canvas.close()
self.nerve_shape_layout.removeWidget(self.toolbar)
self.toolbar.close()
# ------------------------------------------------------------------------------------------------------------------
# Create figures for e_field_only, e_field_only_with_custom_nerve, nerve_shape_only, and e_field_with nerve_shape
# ------------------------------------------------------------------------------------------------------------------
def plot_e_field(self, e_field, layer):
fig1 = Figure()
ax1f1 = fig1.add_subplot(111)
if self.ex_button.isChecked():
pos=ax1f1.imshow(e_field.e_x[:,:,layer], extent=[min(e_field.x)/1e3, max(e_field.x)/1e3, max(e_field.y)/1e3, min(e_field.y)/1e3])
elif self.ez_button.isChecked():
pos=ax1f1.imshow(e_field.e_z[:,:,layer], extent=[min(e_field.x)/1e3, max(e_field.x)/1e3, max(e_field.y)/1e3, min(e_field.y)/1e3])
else:
pos=ax1f1.imshow(e_field.e_y[:,:,layer], extent=[min(e_field.x)/1e3, max(e_field.x)/1e3, max(e_field.y)/1e3, min(e_field.y)/1e3])
ax1f1.set_xlabel('x')
ax1f1.set_ylabel('y')
fig1.colorbar(pos)
ax1f1.callbacks.connect('xlim_changed', self.on_xlims_change)
ax1f1.callbacks.connect('ylim_changed', self.on_ylims_change)
return fig1
def plot_3d_nerve_shape(self, nerve_shape):
fig1 = Figure()
ax1f1 = fig1.add_subplot(111, projection='3d')
ax1f1.scatter3D(nerve_shape.x, nerve_shape.y, nerve_shape.z)
return fig1
def plot_xy_nerve_shape(self, nerve_shape):
fig = Figure()
ax1 = fig.add_subplot(111)
ax1.plot(nerve_shape.x, nerve_shape.y)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
return fig
def plot_xz_nerve_shape(self, nerve_shape):
fig = Figure()
ax1 = fig.add_subplot(111)
ax1.plot(nerve_shape.x, nerve_shape.z)
ax1.set_xlabel('x')
ax1.set_ylabel('z')
return fig
def plot_yz_nerve_shape(self, nerve_shape):
fig = Figure()
ax1 = fig.add_subplot(111)
ax1.plot(nerve_shape.y, nerve_shape.z)
ax1.set_xlabel('y')
ax1.set_ylabel('z')
return fig
# def plot_2d_field_with_cable(self, e_field, layer, nerve, scale):
# e_modified = e_field.e_y[:,:,layer].copy()
# xdim = round(len(e_field.x)/2)
# ydim = round(len(e_field.y) / 2)
#
# xrange = nerve.length * np.cos(nerve.angle / 360 * 2 * np.pi)
# yrange = nerve.length * np.sin(nerve.angle / 360 * 2 * np.pi)
# test_1 = nerve.y[0]/scale
# test_2 = abs(e_field.y[1] - e_field.y[0])
# test_3 = int((nerve.y[0] / scale + ydim) / abs(e_field.y[1] - e_field.y[0]))
# img_mod = cv2.line(e_modified, (int(nerve.x[0]/scale + xdim), int( (nerve.y[0]/scale + ydim) / abs(e_field.y[1]/scale - e_field.y[0]/scale) )), (int(nerve.x[0]/scale + xdim +
# xrange/scale), int(nerve.y[0]/scale + ydim + yrange/scale)), (255, 0, 0), 5)
#
# fig1 = Figure()
# # cv2.imshow("Line", img_mod)
# ax1f1 = fig1.add_subplot(111)
# ax1f1.imshow(e_modified, extent=[min(e_field.y)/scale, max(e_field.y)/scale, max(e_field.x)/scale, min(e_field.x)/scale])
# return fig1
def e_field_plot_with_nerve_shape(self, e_field, nerve_shape):
pass
def on_xlims_change(self, event_ax):
print("updated xlims: ", event_ax.get_xlim())
self.updated_xlims = event_ax.get_xlim()
# self.cut_e_field()
def on_ylims_change(self, event_ax):
print("updated ylims: ", event_ax.get_ylim())
self.updated_ylims = event_ax.get_ylim()
# self.cut_e_field()
def configure_layer_slider(self):
if self.ex_button.isChecked():
if (min(self.e_field.e_x.shape)-1) > 1:
self.e_field_layer_slider.setRange(0, len(self.e_field.e_x[1,1,:])-1)
return True
else:
self.e_field_layer_slider.setEnabled(False)
return False
elif self.ez_button.isChecked():
if (min(self.e_field.e_z.shape)-1) > 1:
self.e_field_layer_slider.setRange(0, len(self.e_field.e_z[1,1,:])-1)
return True
else:
self.e_field_layer_slider.setEnabled(False)
return False
else:
if (min(self.e_field.e_y.shape)-1) > 1:
self.e_field_layer_slider.setRange(0, len(self.e_field.e_y[1,1,:])-1)
return True
else:
self.e_field_layer_slider.setEnabled(False)
return False
# ------------------------------------------------------------------------------------------------------------------
# Modify E-field
# ------------------------------------------------------------------------------------------------------------------
def smooth_e_field(self):
if not self.e_field:
return
filtered_e_field = ndimage.uniform_filter(self.e_field.e_x, size=20)
self.e_field.e_x = filtered_e_field
filtered_e_field = ndimage.uniform_filter(self.e_field.e_y, size=20)
self.e_field.e_y = filtered_e_field
filtered_e_field = ndimage.uniform_filter(self.e_field.e_z, size=20)
self.e_field.e_z = filtered_e_field
self.e_field_changed.emit()
self.update_e_field_plot()
def smooth_nerve_shape(self):
if not self.nerve_shape:
return
self.nerve_shape.x = np.asarray(misc_functions.moving_average(self.nerve_shape.x, 15))
self.nerve_shape.y = np.asarray(misc_functions.moving_average(self.nerve_shape.y, 15))
self.nerve_shape.z = np.asarray(misc_functions.moving_average(self.nerve_shape.z, 15))
sorted_indices = np.argsort(self.nerve_shape.z)
self.nerve_shape.x = np.take_along_axis(self.nerve_shape.x, sorted_indices, axis=0)
self.nerve_shape.y = np.take_along_axis(self.nerve_shape.y, sorted_indices, axis=0)
self.nerve_shape.z = np.take_along_axis(self.nerve_shape.z, sorted_indices, axis=0)
# self.nerve_shape.e_x = np.take_along_axis(self.nerve_shape.e_x, sorted_indices, axis=0)
# self.nerve_shape.e_y = np.take_along_axis(self.nerve_shape.e_y, sorted_indices, axis=0)
# self.nerve_shape.e_z = np.take_along_axis(self.nerve_shape.e_z, sorted_indices, axis=0)
self.nerve_shape_changed.emit()
self.update_nerve_shape_plot()