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):
p0 = copy(self.lastpos)
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
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'
pixelscale: float = self.pixelscale_bf if mode == 'bf' else self.pixelscale_df
p0[0] -= self.signx*self.imagedim_df[0]/2
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
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
else:
raise ValueError(f'mapToPixel mode: {mode} not understood')
x, y = self.signx*(p[0] - p0[0])/pixelscale, self.signy*(p[1] - p0[1])/pixelscale
# TODO: SANITY CHECK AT REAL INSTRUMENT IF THE BELOW CODE CAN BE REMOVED (WAS SUBSTITUTED BY THE 4 LINES ABOVE)
# if mode == 'df':
# p0[0] -= self.signx*self.imagedim_df[0]/2
# 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
#
# 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
def mapToLength(self, pixelpos, mode='df', force=False, returnz=False):
......
......@@ -195,6 +195,7 @@ class PointCoordinates(QtWidgets.QGridLayout):
def __init__(self, N, ramanctrl, parent=None, names=None):
super().__init__(parent)
self.validpoints = [False]*N
self.dswidgets = []
self.N = 0
self.ramanctrl = ramanctrl
......@@ -206,20 +207,20 @@ class PointCoordinates(QtWidgets.QGridLayout):
@QtCore.pyqtSlot()
def createWidgets(self, N, pointsgiven=[]):
self.validpoints = [False]*N
points = np.zeros((N,3))
points = np.zeros((N, 3))
def connect(button, index):
button.released.connect(QtCore.pyqtSlot()(lambda : self.read(index)))
for i in range(self.N,min(N,len(self.dswidgets))):
self.itemAtPosition(i+1,0).setVisible(True)
self.itemAtPosition(i+1,1).setVisible(True)
self.itemAtPosition(i+1,2).setVisible(True)
self.itemAtPosition(i+1,3).setVisible(True)
self.itemAtPosition(i+1,4).setVisible(True)
self.itemAtPosition(i+1,5).setVisible(True)
self.itemAtPosition(i+1,6).setVisible(True)
button.released.connect(QtCore.pyqtSlot()(lambda: self.read(index)))
for i in range(self.N, min(N, len(self.dswidgets))):
self.itemAtPosition(i+1, 0).setVisible(True)
self.itemAtPosition(i+1, 1).setVisible(True)
self.itemAtPosition(i+1, 2).setVisible(True)
self.itemAtPosition(i+1, 3).setVisible(True)
self.itemAtPosition(i+1, 4).setVisible(True)
self.itemAtPosition(i+1, 5).setVisible(True)
self.itemAtPosition(i+1, 6).setVisible(True)
for i in range(self.N, N):
if self.names is not None:
lx = QtWidgets.QLabel(f"{self.names[i]} -> x:")
......@@ -230,15 +231,12 @@ class PointCoordinates(QtWidgets.QGridLayout):
wx = QtWidgets.QDoubleSpinBox()
wy = QtWidgets.QDoubleSpinBox()
wz = QtWidgets.QDoubleSpinBox()
wx.setDecimals(1)
wy.setDecimals(1)
wz.setDecimals(1)
wx.setRange(-500_000, 500_000)
wy.setRange(-500_000, 500_000)
wz.setRange(-500_000, 500_000)
wx.setValue(points[i,0])
wy.setValue(points[i,1])
wz.setValue(points[i,2])
for index, spinbox in enumerate([wx, wy, wz]):
spinbox.setDecimals(1)
spinbox.setRange(-500_000, 500_000)
spinbox.setValue(points[i, index])
spinbox.setMaximumWidth(75)
self.addWidget(lx, i+1, 0, QtCore.Qt.AlignLeft)
self.addWidget(wx, i+1, 1, QtCore.Qt.AlignRight)
self.addWidget(ly, i+1, 2, QtCore.Qt.AlignLeft)
......@@ -249,15 +247,15 @@ class PointCoordinates(QtWidgets.QGridLayout):
connect(pread, i)
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)):
self.itemAtPosition(i+1,0).setVisible(False)
self.itemAtPosition(i+1,1).setVisible(False)
self.itemAtPosition(i+1,2).setVisible(False)
self.itemAtPosition(i+1,3).setVisible(False)
self.itemAtPosition(i+1,4).setVisible(False)
self.itemAtPosition(i+1,5).setVisible(False)
self.itemAtPosition(i+1,6).setVisible(False)
self.itemAtPosition(i+1, 0).setVisible(False)
self.itemAtPosition(i+1, 1).setVisible(False)
self.itemAtPosition(i+1, 2).setVisible(False)
self.itemAtPosition(i+1, 3).setVisible(False)
self.itemAtPosition(i+1, 4).setVisible(False)
self.itemAtPosition(i+1, 5).setVisible(False)
self.itemAtPosition(i+1, 6).setVisible(False)
self.N = N
for i, p in pointsgiven:
......
......@@ -208,7 +208,7 @@ class SampleView(QtWidgets.QGraphicsView):
self.loadPixmap(self.microscopeMode)
if mode == "OpticalScan":
self.pyramid.startScanMode()
self.oscanwidget.setVisible(True)
self.oscanwidget.show()
self.oscanwidget.resetDataset(self.dataset)
self.oscanwidget.setPyramid(self.pyramid)
if self.detectionwidget is not None:
......@@ -216,6 +216,8 @@ class SampleView(QtWidgets.QGraphicsView):
self.detectionwidget.hide()
elif mode == "ParticleDetection":
if self.oscanwidget.isVisible():
self.oscanwidget.hide()
if self.detectionwidget is None:
self.detectionwidget = ParticleDetectionView(
self.pyramid,
......
......@@ -162,7 +162,7 @@ class ZeissImporter(QtWidgets.QDialog):
def convertZimg(self, dataset, T, pc, zpc):
N = int(round(abs(self.zrange.zn-self.zrange.z0)/self.zrange.dz))
z0, zn = self.zrange.z0, self.zrange.zn
if zn<z0:
if zn < z0:
zn, z0 = z0, zn
# 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
......@@ -200,9 +200,8 @@ class ZeissImporter(QtWidgets.QDialog):
dataset.lastpos = p0center
dataset.maxdim = p0center
zmin, zmax = dataset.zpositions.min(), dataset.zpositions.max()
blur = (blur)*(255.)
blur[blur>255.] = 255.
# zmin, zmax = dataset.zpositions.min(), dataset.zpositions.max()
blur = np.clip(blur*255.0, 0, 255.0)
blur[np.isnan(blur)] = 0.
blur = np.uint8(blur)
......@@ -222,8 +221,8 @@ class ZeissImporter(QtWidgets.QDialog):
pc = points.mean(axis=0)
zpc = zpoints.mean(axis=0)
points -= pc[np.newaxis,:]
zpoints -= zpc[np.newaxis,:]
points -= pc[np.newaxis, :]
zpoints -= zpc[np.newaxis, :]
def getRotMat(angles):
c1, s1 = np.cos(angles[0]), np.sin(angles[0])
......@@ -237,12 +236,11 @@ class ZeissImporter(QtWidgets.QDialog):
# [-45°,45°] for all permutation of markers
permbest = None
pointsbest = None
ppoints = points[:,:].copy()
ppoints = points[:, :].copy()
def err(angles_shift):
T = (getRotMat(angles_shift[:3]).T*Parity).A
return (np.dot(zpoints, T) - angles_shift[np.newaxis,3:] \
- ppoints).ravel()
return (np.dot(zpoints, T) - angles_shift[np.newaxis, 3:] - ppoints).ravel()
angle = np.zeros(3)
opt = least_squares(err, np.concatenate((angle, np.zeros(3))),
......@@ -255,18 +253,18 @@ class ZeissImporter(QtWidgets.QDialog):
optangles = permbest.x[:3]
shift = permbest.x[3:]
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 shift:", shift, flush=True)
print("Transformation err:", e, flush=True)
d = np.linalg.norm(e, axis=1)
accept = True
if np.any(d>1.):
if np.any(d > 1.0):
ret = QtWidgets.QMessageBox.warning(self, 'Warning!',
f'Transformation residuals are large:{d}',
QtWidgets.QMessageBox.Ok|QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel,
QtWidgets.QMessageBox.Ok)
if ret==QtWidgets.QMessageBox.Cancel:
if ret == QtWidgets.QMessageBox.Cancel:
accept = False
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