Commit 2ea023ed authored by Josef Brandt's avatar Josef Brandt

Some code cleanup, bugfixes

 - Proper cancellation of optical- or raman scan..
 - Fixed showing/hiding widgets when switching between optical scan and particle detection
parent 8f92cb2a
...@@ -223,23 +223,25 @@ class DataSet(object): ...@@ -223,23 +223,25 @@ class DataSet(object):
p0 = copy(self.lastpos) p0 = copy(self.lastpos)
if self.coordinatetransform is not None: if self.coordinatetransform is not None:
z = 0. if len(p)<3 else p[2] z = 0. if len(p) < 3 else p[2]
T, pc = self.coordinatetransform T, pc = self.coordinatetransform
p = (np.dot(np.array([p[0], p[1], z])-pc, T.T)) p = (np.dot(np.array([p[0], p[1], z])-pc, T.T))
if mode == 'df': assert mode in ['df', 'bf'], f'mapToPixel mode: {mode} not understood'
p0[0] -= self.signx*self.imagedim_df[0]/2 pixelscale: float = self.pixelscale_bf if mode == 'bf' else self.pixelscale_df
p0[1] -= self.signy*self.imagedim_df[1]/2 p0[0] -= self.signx*self.imagedim_df[0]/2
x, y = self.signx*(p[0] - p0[0])/self.pixelscale_df, self.signy*(p[1] - p0[1])/self.pixelscale_df p0[1] -= self.signy*self.imagedim_df[1]/2
x, y = self.signx*(p[0] - p0[0])/pixelscale, self.signy*(p[1] - p0[1])/pixelscale
elif mode == 'bf': # TODO: SANITY CHECK AT REAL INSTRUMENT IF THE BELOW CODE CAN BE REMOVED (WAS SUBSTITUTED BY THE 4 LINES ABOVE)
p0[0] -= self.signx*self.imagedim_bf[0]/2 # if mode == 'df':
p0[1] -= self.signy*self.imagedim_bf[1]/2 # p0[0] -= self.signx*self.imagedim_df[0]/2
x, y = self.signx*(p[0] - p0[0])/self.pixelscale_bf, self.signy*(p[1] - p0[1])/self.pixelscale_bf # p0[1] -= self.signy*self.imagedim_df[1]/2
# x, y = self.signx*(p[0] - p0[0])/self.pixelscale_df, self.signy*(p[1] - p0[1])/self.pixelscale_df
else: #
raise ValueError(f'mapToPixel mode: {mode} not understood') # elif mode == 'bf':
# p0[0] -= self.signx*self.imagedim_bf[0]/2
# p0[1] -= self.signy*self.imagedim_bf[1]/2
# x, y = self.signx*(p[0] - p0[0])/self.pixelscale_bf, self.signy*(p[1] - p0[1])/self.pixelscale_bf
return x, y return x, y
def mapToLength(self, pixelpos, mode='df', force=False, returnz=False): def mapToLength(self, pixelpos, mode='df', force=False, returnz=False):
......
...@@ -195,6 +195,7 @@ class PointCoordinates(QtWidgets.QGridLayout): ...@@ -195,6 +195,7 @@ class PointCoordinates(QtWidgets.QGridLayout):
def __init__(self, N, ramanctrl, parent=None, names=None): def __init__(self, N, ramanctrl, parent=None, names=None):
super().__init__(parent) super().__init__(parent)
self.validpoints = [False]*N
self.dswidgets = [] self.dswidgets = []
self.N = 0 self.N = 0
self.ramanctrl = ramanctrl self.ramanctrl = ramanctrl
...@@ -206,20 +207,20 @@ class PointCoordinates(QtWidgets.QGridLayout): ...@@ -206,20 +207,20 @@ class PointCoordinates(QtWidgets.QGridLayout):
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def createWidgets(self, N, pointsgiven=[]): def createWidgets(self, N, pointsgiven=[]):
self.validpoints = [False]*N points = np.zeros((N, 3))
points = np.zeros((N,3))
def connect(button, index): def connect(button, index):
button.released.connect(QtCore.pyqtSlot()(lambda : self.read(index))) button.released.connect(QtCore.pyqtSlot()(lambda: self.read(index)))
for i in range(self.N,min(N,len(self.dswidgets))): for i in range(self.N, min(N, len(self.dswidgets))):
self.itemAtPosition(i+1,0).setVisible(True) self.itemAtPosition(i+1, 0).setVisible(True)
self.itemAtPosition(i+1,1).setVisible(True) self.itemAtPosition(i+1, 1).setVisible(True)
self.itemAtPosition(i+1,2).setVisible(True) self.itemAtPosition(i+1, 2).setVisible(True)
self.itemAtPosition(i+1,3).setVisible(True) self.itemAtPosition(i+1, 3).setVisible(True)
self.itemAtPosition(i+1,4).setVisible(True) self.itemAtPosition(i+1, 4).setVisible(True)
self.itemAtPosition(i+1,5).setVisible(True) self.itemAtPosition(i+1, 5).setVisible(True)
self.itemAtPosition(i+1,6).setVisible(True) self.itemAtPosition(i+1, 6).setVisible(True)
for i in range(self.N, N): for i in range(self.N, N):
if self.names is not None: if self.names is not None:
lx = QtWidgets.QLabel(f"{self.names[i]} -> x:") lx = QtWidgets.QLabel(f"{self.names[i]} -> x:")
...@@ -230,15 +231,12 @@ class PointCoordinates(QtWidgets.QGridLayout): ...@@ -230,15 +231,12 @@ class PointCoordinates(QtWidgets.QGridLayout):
wx = QtWidgets.QDoubleSpinBox() wx = QtWidgets.QDoubleSpinBox()
wy = QtWidgets.QDoubleSpinBox() wy = QtWidgets.QDoubleSpinBox()
wz = QtWidgets.QDoubleSpinBox() wz = QtWidgets.QDoubleSpinBox()
wx.setDecimals(1) for index, spinbox in enumerate([wx, wy, wz]):
wy.setDecimals(1) spinbox.setDecimals(1)
wz.setDecimals(1) spinbox.setRange(-500_000, 500_000)
wx.setRange(-500_000, 500_000) spinbox.setValue(points[i, index])
wy.setRange(-500_000, 500_000) spinbox.setMaximumWidth(75)
wz.setRange(-500_000, 500_000)
wx.setValue(points[i,0])
wy.setValue(points[i,1])
wz.setValue(points[i,2])
self.addWidget(lx, i+1, 0, QtCore.Qt.AlignLeft) self.addWidget(lx, i+1, 0, QtCore.Qt.AlignLeft)
self.addWidget(wx, i+1, 1, QtCore.Qt.AlignRight) self.addWidget(wx, i+1, 1, QtCore.Qt.AlignRight)
self.addWidget(ly, i+1, 2, QtCore.Qt.AlignLeft) self.addWidget(ly, i+1, 2, QtCore.Qt.AlignLeft)
...@@ -249,15 +247,15 @@ class PointCoordinates(QtWidgets.QGridLayout): ...@@ -249,15 +247,15 @@ class PointCoordinates(QtWidgets.QGridLayout):
connect(pread, i) connect(pread, i)
self.addWidget(pread, i+1, 6, QtCore.Qt.AlignRight) self.addWidget(pread, i+1, 6, QtCore.Qt.AlignRight)
self.dswidgets.append([wx,wy,wz]) self.dswidgets.append([wx, wy, wz])
for i in range(N, len(self.dswidgets)): for i in range(N, len(self.dswidgets)):
self.itemAtPosition(i+1,0).setVisible(False) self.itemAtPosition(i+1, 0).setVisible(False)
self.itemAtPosition(i+1,1).setVisible(False) self.itemAtPosition(i+1, 1).setVisible(False)
self.itemAtPosition(i+1,2).setVisible(False) self.itemAtPosition(i+1, 2).setVisible(False)
self.itemAtPosition(i+1,3).setVisible(False) self.itemAtPosition(i+1, 3).setVisible(False)
self.itemAtPosition(i+1,4).setVisible(False) self.itemAtPosition(i+1, 4).setVisible(False)
self.itemAtPosition(i+1,5).setVisible(False) self.itemAtPosition(i+1, 5).setVisible(False)
self.itemAtPosition(i+1,6).setVisible(False) self.itemAtPosition(i+1, 6).setVisible(False)
self.N = N self.N = N
for i, p in pointsgiven: for i, p in pointsgiven:
......
...@@ -208,7 +208,7 @@ class SampleView(QtWidgets.QGraphicsView): ...@@ -208,7 +208,7 @@ class SampleView(QtWidgets.QGraphicsView):
self.loadPixmap(self.microscopeMode) self.loadPixmap(self.microscopeMode)
if mode == "OpticalScan": if mode == "OpticalScan":
self.pyramid.startScanMode() self.pyramid.startScanMode()
self.oscanwidget.setVisible(True) self.oscanwidget.show()
self.oscanwidget.resetDataset(self.dataset) self.oscanwidget.resetDataset(self.dataset)
self.oscanwidget.setPyramid(self.pyramid) self.oscanwidget.setPyramid(self.pyramid)
if self.detectionwidget is not None: if self.detectionwidget is not None:
...@@ -216,6 +216,8 @@ class SampleView(QtWidgets.QGraphicsView): ...@@ -216,6 +216,8 @@ class SampleView(QtWidgets.QGraphicsView):
self.detectionwidget.hide() self.detectionwidget.hide()
elif mode == "ParticleDetection": elif mode == "ParticleDetection":
if self.oscanwidget.isVisible():
self.oscanwidget.hide()
if self.detectionwidget is None: if self.detectionwidget is None:
self.detectionwidget = ParticleDetectionView( self.detectionwidget = ParticleDetectionView(
self.pyramid, self.pyramid,
......
...@@ -162,7 +162,7 @@ class ZeissImporter(QtWidgets.QDialog): ...@@ -162,7 +162,7 @@ class ZeissImporter(QtWidgets.QDialog):
def convertZimg(self, dataset, T, pc, zpc): def convertZimg(self, dataset, T, pc, zpc):
N = int(round(abs(self.zrange.zn-self.zrange.z0)/self.zrange.dz)) N = int(round(abs(self.zrange.zn-self.zrange.z0)/self.zrange.dz))
z0, zn = self.zrange.z0, self.zrange.zn z0, zn = self.zrange.z0, self.zrange.zn
if zn<z0: if zn < z0:
zn, z0 = z0, zn zn, z0 = z0, zn
# zeiss z axis has large values at the surface and smaller for large particles # zeiss z axis has large values at the surface and smaller for large particles
# that is why we need to invert the zpositions: zimg black corresponds to zn # that is why we need to invert the zpositions: zimg black corresponds to zn
...@@ -200,9 +200,8 @@ class ZeissImporter(QtWidgets.QDialog): ...@@ -200,9 +200,8 @@ class ZeissImporter(QtWidgets.QDialog):
dataset.lastpos = p0center dataset.lastpos = p0center
dataset.maxdim = p0center dataset.maxdim = p0center
zmin, zmax = dataset.zpositions.min(), dataset.zpositions.max() # zmin, zmax = dataset.zpositions.min(), dataset.zpositions.max()
blur = (blur)*(255.) blur = np.clip(blur*255.0, 0, 255.0)
blur[blur>255.] = 255.
blur[np.isnan(blur)] = 0. blur[np.isnan(blur)] = 0.
blur = np.uint8(blur) blur = np.uint8(blur)
...@@ -222,8 +221,8 @@ class ZeissImporter(QtWidgets.QDialog): ...@@ -222,8 +221,8 @@ class ZeissImporter(QtWidgets.QDialog):
pc = points.mean(axis=0) pc = points.mean(axis=0)
zpc = zpoints.mean(axis=0) zpc = zpoints.mean(axis=0)
points -= pc[np.newaxis,:] points -= pc[np.newaxis, :]
zpoints -= zpc[np.newaxis,:] zpoints -= zpc[np.newaxis, :]
def getRotMat(angles): def getRotMat(angles):
c1, s1 = np.cos(angles[0]), np.sin(angles[0]) c1, s1 = np.cos(angles[0]), np.sin(angles[0])
...@@ -237,12 +236,11 @@ class ZeissImporter(QtWidgets.QDialog): ...@@ -237,12 +236,11 @@ class ZeissImporter(QtWidgets.QDialog):
# [-45°,45°] for all permutation of markers # [-45°,45°] for all permutation of markers
permbest = None permbest = None
pointsbest = None pointsbest = None
ppoints = points[:,:].copy() ppoints = points[:, :].copy()
def err(angles_shift): def err(angles_shift):
T = (getRotMat(angles_shift[:3]).T*Parity).A T = (getRotMat(angles_shift[:3]).T*Parity).A
return (np.dot(zpoints, T) - angles_shift[np.newaxis,3:] \ return (np.dot(zpoints, T) - angles_shift[np.newaxis, 3:] - ppoints).ravel()
- ppoints).ravel()
angle = np.zeros(3) angle = np.zeros(3)
opt = least_squares(err, np.concatenate((angle, np.zeros(3))), opt = least_squares(err, np.concatenate((angle, np.zeros(3))),
...@@ -255,18 +253,18 @@ class ZeissImporter(QtWidgets.QDialog): ...@@ -255,18 +253,18 @@ class ZeissImporter(QtWidgets.QDialog):
optangles = permbest.x[:3] optangles = permbest.x[:3]
shift = permbest.x[3:] shift = permbest.x[3:]
T = (getRotMat(optangles).T*Parity).A T = (getRotMat(optangles).T*Parity).A
e = (np.dot(zpoints, T)-shift[np.newaxis,:]-pointsbest) e = (np.dot(zpoints, T) - shift[np.newaxis, :] - pointsbest)
print("Transformation angles:", optangles, flush=True) print("Transformation angles:", optangles, flush=True)
print("Transformation shift:", shift, flush=True) print("Transformation shift:", shift, flush=True)
print("Transformation err:", e, flush=True) print("Transformation err:", e, flush=True)
d = np.linalg.norm(e, axis=1) d = np.linalg.norm(e, axis=1)
accept = True accept = True
if np.any(d>1.): if np.any(d > 1.0):
ret = QtWidgets.QMessageBox.warning(self, 'Warning!', ret = QtWidgets.QMessageBox.warning(self, 'Warning!',
f'Transformation residuals are large:{d}', f'Transformation residuals are large:{d}',
QtWidgets.QMessageBox.Ok|QtWidgets.QMessageBox.Cancel, QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.Ok)
if ret==QtWidgets.QMessageBox.Cancel: if ret == QtWidgets.QMessageBox.Cancel:
accept = False accept = False
return T, pc-shift, zpc, accept return T, pc-shift, zpc, accept
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment