opticalscan.py 27.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.  
If not, see <https://www.gnu.org/licenses/>.
"""

from PyQt5 import QtCore, QtWidgets
import numpy as np
24 25
from multiprocessing import Process, Queue, Event
import queue
26 27 28 29
from imagestitch import imageStacking
import os
import cv2
from helperfunctions import cv2imread_fix, cv2imwrite_fix
30
from time import time
31 32 33
import datetime
import sys

34 35
def scan(path, sol, zpositions, grid, controlclass, dataqueue, 
         stopevent, logpath='', ishdr=False):
36
    if ishdr:
37 38 39 40 41 42 43 44 45 46 47 48
        merge_mertens = cv2.createMergeMertens()      

    fp = None
    if logpath != '':
        try:
            fp = open(os.path.join(logpath, 'scanlog.txt'), 'a')
            sys.stderr = fp
            sys.stdout = fp
        except IOError:
            print('separate loging failed', flush=True)
            pass
    print('starting new optical scan', flush=True)
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
    ramanctrl = controlclass()
    ramanctrl.connect()
    zlist = list(enumerate(zpositions))
    for i, p in enumerate(grid):
        x, y = p
        z = sol[0]*x + sol[1]*y + sol[2]
        for k, zk in (zlist if i%2==0 else zlist[::-1]):
            name = f"image_{i}_{k}.bmp"
            print("time:", time(), flush=True)
            zik = z+zk
            assert not np.isnan(zik)
            print("moving to:", x, y, zik, flush=True)
            ramanctrl.moveToAbsolutePosition(x, y, zik)
            if ishdr:
                img_list = []
                fname = os.path.join(path,f"tmp.bmp")
                values = [5.,25.,100.] 
                for j, val in enumerate(values if (i%2+k%2)%2==0 else reversed(values)):
                    ramanctrl.setBrightness(val)
                    ramanctrl.saveImage(fname)
                    img_list.append(cv2imread_fix(fname))
                res_mertens = merge_mertens.process(img_list)
                res_mertens_8bit = np.clip(res_mertens*255, 0, 255).astype('uint8')
                cv2imwrite_fix(os.path.join(path,name), res_mertens_8bit)                        
            else:
                ramanctrl.saveImage(os.path.join(path,name))
            if stopevent.is_set():
                ramanctrl.disconnect()
                return
        dataqueue.put(i)
    ramanctrl.disconnect()
    if fp is not None:
        fp.close()
83

84 85
    
def loadAndPasteImage(srcnames, fullimage, fullzval, width, height, 
86
                      rotationvalue, p0, p1, p, halfResolution = False):
87 88 89 90
    colimgs = []
    for name in srcnames:
        colimgs.append(cv2.cvtColor(cv2imread_fix(name), cv2.COLOR_BGR2RGB))
    img, zval = imageStacking(colimgs)
91 92 93 94 95

    if halfResolution:              #halve resolution, if fullimage would become too large otherwise
        img = cv2.resize(img, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)
        zval= cv2.resize(zval, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)

96 97 98 99
    x, y = p
    Nx, Ny = int((p1[0]-p0[0]+width)/width*img.shape[1]), int((p0[1]-p1[1]+height)/height*img.shape[0]) + 10  # + 10 because of rotation and hopefully it will be small
    c, s = np.cos(np.radians(rotationvalue)), np.sin(np.radians(rotationvalue))
    dx, dy = (x-p0[0])/width*img.shape[1], (p0[1]-y)/height*img.shape[0]
100
        
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    M = np.float32([[c,s,dx],[-s,c,dy]])
    if fullimage is not None:
        cv2.warpAffine(img, M, (Nx, Ny), fullimage, borderMode=cv2.BORDER_TRANSPARENT)
        cv2.warpAffine(zval, M, (Nx, Ny), fullzval, borderMode=cv2.BORDER_TRANSPARENT)
        dst = fullimage
        zval = fullzval
    else:
        dst = cv2.warpAffine(img, M, (Nx, Ny))
        zval = cv2.warpAffine(zval, M, (Nx, Ny))
    return dst, zval
    

class PointCoordinates(QtWidgets.QGridLayout):
    readPoint = QtCore.pyqtSignal(float, float, float, name='readPoint')
    
116
    def __init__(self, N, ramanctrl, parent=None, names=None):
117 118 119 120
        super().__init__(parent)
        self.dswidgets = []
        self.N = 0
        self.ramanctrl = ramanctrl
121
        self.names = names
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        self.pimageOnly = QtWidgets.QPushButton("Image")
        self.addWidget(self.pimageOnly, 0, 6, QtCore.Qt.AlignRight)
        self.pimageOnly.released.connect(QtCore.pyqtSlot()(lambda : self.read(-1)))
        self.createWidgets(N)
    
    @QtCore.pyqtSlot()    
    def createWidgets(self, N, pointsgiven=[]):
        self.validpoints = [False]*N
        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)
        for i in range(self.N, N):
144 145 146 147
            if self.names is not None:
                lx = QtWidgets.QLabel(f"{self.names[i]} -> x:")
            else:
                lx = QtWidgets.QLabel(f"{i+1} -> x:")
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
            ly = QtWidgets.QLabel("y:")
            lz = QtWidgets.QLabel("z:")
            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])
            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)
            self.addWidget(wy, i+1, 3, QtCore.Qt.AlignRight)
            self.addWidget(lz, i+1, 4, QtCore.Qt.AlignLeft)
            self.addWidget(wz, i+1, 5, QtCore.Qt.AlignRight)
            pread = QtWidgets.QPushButton("read")
            connect(pread, i)
            self.addWidget(pread, i+1, 6, QtCore.Qt.AlignRight)
            
            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.N = N
        for i, p in pointsgiven:
            wx, wy, wz = self.dswidgets[i]
            x, y, z = p
            wx.setValue(x)
            wy.setValue(y)        
            wz.setValue(z)
            self.validpoints[i] = True
        for i in range(len(pointsgiven), N):
            wx, wy, wz = self.dswidgets[i]
            wx.setValue(0)
            wy.setValue(0)        
            wz.setValue(0)
        
        self.update()
        
    def read(self, index):
        x, y, z = self.ramanctrl.getPosition()
        z = self.ramanctrl.getUserZ()
        if index>=0:
            wx, wy, wz = self.dswidgets[index]
            wx.setValue(x)
            wy.setValue(y)        
            wz.setValue(z)
            self.validpoints[index] = True
        self.readPoint.emit(x,y,z)
        
    def getPoints(self):
        points = np.zeros((self.N, 3), dtype=np.double)
        for i in range(self.N):
            if self.validpoints[i]:
                wx, wy, wz = self.dswidgets[i]
                points[i,0] = wx.value()
                points[i,1] = wy.value()
                points[i,2] = wz.value()
            else:
                points[i,:] = np.nan
        return points

class OpticalScan(QtWidgets.QWidget):
221
    imageUpdate = QtCore.pyqtSignal(str, name='imageUpdate')    #str = 'df' (= darkfield) or 'bf' (=bright field)
222 223
    boundaryUpdate = QtCore.pyqtSignal()
    
224
    def __init__(self, ramanctrl, dataset, logpath='', parent=None):
225
        super().__init__(parent, QtCore.Qt.Window)
226
        self.logpath = logpath
227 228
        self.view = parent
        vbox = QtWidgets.QVBoxLayout()
229
        pointgroup = QtWidgets.QGroupBox("Point coordinates [µm]", self)
230 231 232 233 234
        self.ramanctrl = ramanctrl
        self.dataset = dataset
        self.positions = []
        self.process = None
        self.points = PointCoordinates(5, self.ramanctrl, self)
235
        pointgroup.setLayout(self.points)
236 237 238 239 240 241 242 243 244
        self.points.readPoint.connect(self.takePoint)
        
        self.pareaselect = QtWidgets.QPushButton("Area select", self)
        label = QtWidgets.QLabel("Size increase:", self)
        self.radiusincreaseedit = QtWidgets.QDoubleSpinBox(self)
        self.radiusincreaseedit.setMinimum(-1000)
        self.radiusincreaseedit.setMaximum(1000)
        self.radiusincreaseedit.setDecimals(0)
        self.radiusincreaseedit.setSingleStep(20)
245 246
        self.radiusincreaseedit.setMaximumWidth(100)
        self.radiusincreaseedit.valueChanged.connect(self.areaSelect)
247 248 249 250 251 252
        label2 = QtWidgets.QLabel("Maximal focus height [µm]:", self)
        self.zmaxedit = QtWidgets.QDoubleSpinBox(self)
        self.zmaxedit.setMinimum(1)
        self.zmaxedit.setMaximum(1000)
        self.zmaxedit.setDecimals(0)
        self.zmaxedit.setValue(50)
253
        self.zmaxedit.setMaximumWidth(100)
254 255 256 257
        label3 = QtWidgets.QLabel("Focus steps:", self)
        self.nzedit = QtWidgets.QSpinBox(self)
        self.nzedit.setRange(2,10)
        self.nzedit.setValue(3)
258
        self.nzedit.setMaximumWidth(100)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        self.hdrcheck = QtWidgets.QCheckBox("High dynamic range", self)
        self.hdrcheck.setChecked(False)
        
        self.prun = QtWidgets.QPushButton("Run", self)
        self.pexit = QtWidgets.QPushButton("Cancel", self)
        self.pareaselect.released.connect(self.areaSelect)
        self.prun.released.connect(self.run)
        self.pexit.released.connect(self.stopScan)
        self.prun.setEnabled(False)
        
        self.timelabeltext = "Estimated time to finish: "
        self.progressbar = QtWidgets.QProgressBar(self)
        self.progresstime = QtWidgets.QLabel(self.timelabeltext, self)
        self.progresstime.setEnabled(False)
        self.progressbar.setEnabled(False)
        
275 276
        radioGroup = QtWidgets.QGroupBox('Shape')
        radioLayout = QtWidgets.QHBoxLayout()
277
        self.circlerad = QtWidgets.QRadioButton("Circle")
278
        self.circlerad.clicked.connect(self.areaSelect)
279
        self.rectanglerad = QtWidgets.QRadioButton("Rectangle")
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        self.rectanglerad.setChecked(True)
        self.rectanglerad.clicked.connect(self.areaSelect)
        radioLayout.addWidget(self.circlerad)
        radioLayout.addWidget(self.rectanglerad)
        radioGroup.setLayout(radioLayout)

        micModeGroup = QtWidgets.QGroupBox('Mode for Image Acquisition')
        micModeLayout = QtWidgets.QHBoxLayout()
        self.df_btn = QtWidgets.QRadioButton('Darkfield')
        self.df_btn.setChecked(True)
        self.df_btn.clicked.connect(self.areaSelect)
        self.bf_btn = QtWidgets.QRadioButton('Brightfield')
        self.bf_btn.clicked.connect(self.areaSelect)
        micModeLayout.addWidget(self.df_btn)
        micModeLayout.addWidget(self.bf_btn)
        micModeGroup.setLayout(micModeLayout)
296
        
297 298 299 300 301 302
        self.halfResChecker = QtWidgets.QCheckBox('Half resolution')
        self.halfResChecker.setChecked(False)
        self.halfResChecker.setToolTip('Enable for very high resolution images.\nFull resolution slows down the scan too much..')
        
        self.deleteImgChecker = QtWidgets.QCheckBox('Delete image files after run')
        self.deleteImgChecker.setChecked(True)
303
        
304 305 306 307 308 309 310 311 312 313
        self.areaOptionsGroup = QtWidgets.QGroupBox('Area Select Options')
        areaLayout = QtWidgets.QFormLayout()
        areaLayout.addRow(radioGroup)
        areaLayout.addRow(label, self.radiusincreaseedit)
        areaLayout.addRow(micModeGroup)
        if not self.view.ramanSwitchNeeded:
            micModeGroup.setDisabled(True)
        areaLayout.addRow(self.pareaselect)
        self.areaOptionsGroup.setLayout(areaLayout)
        self.areaOptionsGroup.setDisabled(True)
314
        
315 316 317 318 319 320 321 322
        furtherOptionsGroup = QtWidgets.QGroupBox('Further Options')
        furtherOptionsLayout = QtWidgets.QFormLayout()
        furtherOptionsLayout.addRow(label2, self.zmaxedit)
        furtherOptionsLayout.addRow(label3, self.nzedit)
        furtherOptionsLayout.addRow(self.hdrcheck)
        furtherOptionsLayout.addRow(self.deleteImgChecker)
        furtherOptionsLayout.addRow(self.halfResChecker)
        furtherOptionsGroup.setLayout(furtherOptionsLayout)
323 324
        
        
325 326 327 328 329 330 331 332 333
        btnLayout = QtWidgets.QHBoxLayout()
        btnLayout.addWidget(self.prun)
        btnLayout.addWidget(self.pexit)
        btnLayout.addStretch()
        
        vbox.addWidget(pointgroup)
        vbox.addWidget(self.areaOptionsGroup)
        vbox.addWidget(furtherOptionsGroup)
        vbox.addLayout(btnLayout)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        vbox.addWidget(self.progresstime)
        vbox.addWidget(self.progressbar)
        
        self.setLayout(vbox)
        #self.show()
        self.setVisible(False)
        
    @QtCore.pyqtSlot()
    def stopScan(self):
        if self.process is not None and self.process.is_alive():
            reply = QtWidgets.QMessageBox.question(self, 'Stop optical scan?',
                                "Do you want to terminate the running scan?",
                                QtWidgets.QMessageBox.Yes | 
                                QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
            if reply == QtWidgets.QMessageBox.Yes:
                self.timer.stop()
350
                self.processstopevent.set()
351
                self.process.join()
352 353
                self.dataqueue.close()
                self.dataqueue.join_thread()
354 355 356 357
                self.view.unblockUI()
            else:
                return
        self.close()
358
               
359 360 361
    
    @QtCore.pyqtSlot()
    def areaSelect(self):
362 363
        magn = self.ramanctrl.magn
        
364 365 366 367
        if self.circlerad.isChecked() == True:
            xym, r = cv2.minEnclosingCircle(np.array([p[:2] for p in self.dataset.fitpoints], 
                                                     dtype=np.float32))
            r += self.radiusincreaseedit.value()
368
            phi = np.linspace(0, 2*np.pi, magn, endpoint=False)
369 370 371 372 373 374
            self.dataset.boundary = [[xym[0]+r*np.cos(phii), xym[1]+r*np.sin(phii)] for phii in phi]
        else:
            da = self.radiusincreaseedit.value()
            x0, x1 = self.dataset.fitpoints[:,0].min()-da, self.dataset.fitpoints[:,0].max()+da
            y0, y1 = self.dataset.fitpoints[:,1].min()-da, self.dataset.fitpoints[:,1].max()+da
            a = 2*(y1-y0 + x1-x0)
375
            nx, ny = max(int(np.round((x1-x0)/a*magn)),2), max(int(np.round((y1-y0)/a*magn)),2)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
            x, dx = np.linspace(x0, x1, nx, endpoint=False, retstep=True)
            y, dy = np.linspace(y0, y1, ny, endpoint=False, retstep=True)
            self.dataset.boundary = [[xi, yi] for xi, yi in zip(x,y0*np.ones_like(x))] + \
                                    [[xi, yi] for xi, yi in zip(x1*np.ones_like(y),y)] + \
                                    [[xi, yi] for xi, yi in zip(x[::-1]+dx,y1*np.ones_like(x))] + \
                                    [[xi, yi] for xi, yi in zip(x0*np.ones_like(y),y[::-1]+dy)]
            
            
        self.boundaryUpdate.emit()
        self.prun.setEnabled(True)
    
    def resetDataset(self, ds):
        self.dataset = ds
        self.points.createWidgets(5, list(zip(ds.fitindices,ds.fitpoints)))
        if len(self.dataset.fitindices)>1:
391 392
#            self.pareaselect.setEnabled(True)
            self.areaOptionsGroup.setEnabled(True)
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        softwarez = self.ramanctrl.getSoftwareZ()
        if abs(softwarez) >0.1:
            reply = QtWidgets.QMessageBox.critical(self, 'Software z position nonzero',
            "The software z position needs to be set to zero."\
            " Moving z for %4.0f µm relative to current position. Counteract manually before proceeding!"%(-softwarez), 
            QtWidgets.QMessageBox.Yes | 
            QtWidgets.QMessageBox.Abort, QtWidgets.QMessageBox.Abort)

            if reply == QtWidgets.QMessageBox.Yes:
                self.ramanctrl.moveZto(0.0)
            else:
                QtWidgets.QMessageBox.information(self, "Information", 'Scan may stop if the software z position gets to high!',
                                                  QtWidgets.QMessageBox.Ok)
        
    @QtCore.pyqtSlot(float, float, float) 
    def takePoint(self, x, y, z):
        if self.dataset.heightmap is not None:
            reply = QtWidgets.QMessageBox.critical(self, 'Dataset already contains optical scan data',
                "Continuation will invalidate all previous results! Continue anyway?", 
                QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

            if reply != QtWidgets.QMessageBox.Yes:
                return
        self.ramanctrl.saveImage(self.dataset.getTmpImageName())
417
        width, height, rotationvalue = self.ramanctrl.getImageDimensions(self.view.microscopeMode)
418 419 420
        pshift = self.ramanctrl.getRamanPositionShift()
        self.dataset.pshift = pshift
        img = cv2.cvtColor(cv2imread_fix(self.dataset.getTmpImageName()), cv2.COLOR_BGR2RGB)
421 422 423 424 425 426 427 428 429
        if self.halfResChecker.isChecked():
            img = cv2.resize(img, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)
        
        self.dataset.imagedim_bf = self.ramanctrl.getImageDimensions('bf')
        self.dataset.pixelscale_bf = self.dataset.imagedim_bf[0]/img.shape[1]          #=imagedim_width/shape[1]
        self.dataset.imagedim_df = self.ramanctrl.getImageDimensions('df')
        self.dataset.pixelscale_df = self.dataset.imagedim_df[0]/img.shape[1]          #=imagedim_width/shape[1]
        

430 431 432 433 434 435 436
        
        points = self.points.getPoints()
        ind = np.isfinite(points[:,0])
        self.dataset.fitindices = np.arange(points.shape[0])[ind]
        points = points[ind,:].copy()
        self.dataset.fitpoints = points
        if len(points)>1:
437 438
            self.areaOptionsGroup.setEnabled(True)
        
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
        points = np.concatenate(([[x,y,z]], points), axis=0)
        p0 = [points[:,0].min(), points[:,1].max()]
        p1 = [points[:,0].max(), points[:,1].min()]
        if self.dataset.maxdim is not None:
            p0 = [min(p0[0], self.dataset.maxdim[0]), max(p0[1], self.dataset.maxdim[1])]
            p1 = [max(p1[0], self.dataset.maxdim[2]), min(p1[1], self.dataset.maxdim[3])]
            
        Nx, Ny = int((p1[0]-p0[0]+width)/width*img.shape[1]), int((p0[1]-p1[1]+height)/height*img.shape[0]) + 10  # + 10 because of rotation and hopefully it will be small
        c, s = np.cos(np.radians(rotationvalue)), np.sin(np.radians(rotationvalue))
        dx, dy = (x-p0[0])/width*img.shape[1], (p0[1]-y)/height*img.shape[0]
        M = np.float32([[c,s,dx],[-s,c,dy]])
        dst = cv2.warpAffine(img, M, (Nx, Ny))
        if self.view.imgdata is not None and self.dataset.lastpos is not None:
            lp = self.dataset.lastpos
            dx, dy = (lp[0]-p0[0])/width*img.shape[1], (p0[1]-lp[1])/height*img.shape[0]
            full = self.view.imgdata
            M = np.float32([[1,0,dx],[0,1,dy]])
456 457 458 459 460 461
            try: 
                full = cv2.warpAffine(full, M, (Nx, Ny))            #fails, if image dimensions are >32767x32767px...
                dst = cv2.max(full, dst)
            except:
                QtWidgets.QMessageBox.critical(self, 'Error', 'Image is too large\nPlease repeat with "scale image" checked.')
                return
462 463 464 465 466
        
        self.view.imgdata = dst
        self.dataset.lastpos = p0
        self.dataset.maxdim = p0 + p1
        self.dataset.readin = False
467
        self.imageUpdate.emit(self.view.microscopeMode)
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    
    @QtCore.pyqtSlot() 
    def run(self):
        if self.dataset.ramanscansortindex is not None:
            reply = QtWidgets.QMessageBox.critical(self, 'Dataset already contains raman scan points',
                "Continuation will invalidate all previous results! Continue anyway?", 
                QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

            if reply != QtWidgets.QMessageBox.Yes:
                return
        if self.dataset.readin:
            reply = QtWidgets.QMessageBox.critical(self, 'Dataset is newly read from disk!',
                "Coordinate systems might have changed since. Do you want to continue with saved coordinates?", 
                QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

            if reply == QtWidgets.QMessageBox.Yes:
                self.dataset.readin = False
            else:
                return
487
        
Hackmet's avatar
Hackmet committed
488
        self.view.imparent.ramanSwitch.df_btn.setChecked(self.df_btn.isChecked())
489
        self.view.imparent.ramanSwitch.setDisabled(True)
Hackmet's avatar
Hackmet committed
490
        self.view.setMicroscopeMode()
491 492 493 494 495 496 497 498 499 500

        if self.df_btn.isChecked():
            self.view.dataset.imagescanMode = 'df'
        else:
            self.view.dataset.imagescanMode = 'bf'

        #TODO:
        #DISABLE OPTION GROUPS when scanning, reactivate upon cancelling
        
        
501 502 503 504 505 506 507 508 509 510 511
        points = np.float32(self.dataset.fitpoints)
        # convert z to software z, which is relative to current user z
        softwarez = self.ramanctrl.getSoftwareZ()  # get current software z
        points[:,2] += softwarez-self.ramanctrl.getUserZ()
        Nz = self.nzedit.value()
        zmaxstack = self.zmaxedit.value()
        if Nz==1:
            zmaxstack = 0.0
            self.dataset.zpositions = np.array([0.0])
        else:
            self.dataset.zpositions = np.linspace(0, zmaxstack, Nz)
512
        width, height, rotationvalue = self.dataset.imagedim_df
513 514 515 516 517 518 519 520
        print("Width, height, rotation:", width, height, rotationvalue)
        print("Points x:", points[:,0].min(), points[:,0].max())
        print("Points y:", points[:,1].min(), points[:,1].max())
        print("Points z:", points[:,2].min(), points[:,2].max())
        
        A = np.ones((points.shape[0],3))
        A[:,:2] = points[:,:2]
        b = points[:,2]
521
        sol = np.linalg.lstsq(A, b, rcond=None)[0]
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
        self.dataset.heightmap = sol
        print("Fit deviation:", sol[0]*points[:,0]+sol[1]*points[:,1]+sol[2] -points[:,2] )

        path = self.dataset.getScanPath()
        # get zmin and zmax in absolut software z coordinates
        zmin, zmax = None, None
        for i, p in enumerate(self.dataset.grid):
            x, y = p
            z = sol[0]*x + sol[1]*y + sol[2]
            if i==0:
                zmin, zmax = z, z
            else:
                if zmin>z: zmin = z
                if zmax<z: zmax = z
        zmax += zmaxstack        
        # convert to relative movements for user info:
        zmin -= softwarez
        zmax -= softwarez
        reply = QtWidgets.QMessageBox.question(self, 'Starting optical scan',
            "Please switch to dark field optics. Microscope will move"\
            " (%4.0f,%4.0f) µm relative to current position. Proceed?"%(zmin,zmax), 
            QtWidgets.QMessageBox.Yes | 
            QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

        if reply == QtWidgets.QMessageBox.Yes:
547 548 549 550 551
#            if self.halfResChecker.isChecked():
#                #reset pixelscales!!!
#                self.dataset.pixelscale_df /= 2
#                self.dataset.pixelscale_bf /= 2
            
552 553
            self.prun.setEnabled(False)
            self.ramanctrl.disconnect()
554 555
            self.processstopevent = Event()
            self.dataqueue = Queue()
556
            self.process = Process(target=scan, args=(path, sol, self.dataset.zpositions, 
557
                                self.dataset.grid, self.ramanctrl.__class__,
558 559
                                self.dataqueue, self.processstopevent, 
                                self.logpath, self.hdrcheck.isChecked()))
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
            self.process.start()
            self.starttime = time()
            self.progresstime.setEnabled(True)
            self.progressbar.setEnabled(True)
            self.progressbar.setRange(0, len(self.dataset.grid))
            self.progressbar.setValue(0)
            self.view.imgdata = None
            self.view.blockUI()
            grid = np.asarray(self.dataset.grid)
            p0 = [grid[:,0].min(), grid[:,1].max()]
            p1 = [grid[:,0].max(), grid[:,1].min()]
            self.dataset.lastpos = p0
            self.dataset.maxdim = p0 + p1
            self.dataset.mode = "opticalscan"
            self.dataset.save()
            self.timer = QtCore.QTimer(self)
            self.timer.timeout.connect(self.checkOnScan)
            self.timer.setSingleShot(True)
            self.timer.start(1000.)
    
    @QtCore.pyqtSlot()      
    def checkOnScan(self):
582 583 584 585 586 587
        try:
            i = self.dataqueue.get_nowait()
        except queue.Empty:
            i = -1
            
        if i>=0:
588
                    
589 590 591 592
            Ngrid = len(self.dataset.grid)
            names = []
            for k in range(len(self.dataset.zpositions)):
                names.append(os.path.join(self.dataset.getScanPath(),f"image_{i}_{k}.bmp"))
593
            width, height, rotationvalue = (self.dataset.imagedim_df if self.view.imparent.ramanSwitch.df_btn.isChecked() else self.dataset.imagedim_bf)
594 595 596
            p = self.dataset.grid[i]
            p0, p1 = self.dataset.maxdim[:2], self.dataset.maxdim[2:]
            self.view.imgdata, self.dataset.zvalimg = loadAndPasteImage(names, self.view.imgdata, self.dataset.zvalimg, width, height, 
597
                                                                        rotationvalue, p0, p1, p, halfResolution = self.halfResChecker.isChecked())
598 599 600 601 602 603
            self.progressbar.setValue(i+1)
            if i>3:
                timerunning = time()-self.starttime
                ttot = timerunning*Ngrid/(i+1)
                time2go = ttot - timerunning
                self.progresstime.setText(self.timelabeltext + str(datetime.timedelta(seconds=round(time2go))))
604 605
            self.imageUpdate.emit(self.view.microscopeMode)
            
606 607 608 609
            if i==Ngrid-1:
                cv2imwrite_fix(self.dataset.getImageName(), cv2.cvtColor(self.view.imgdata, cv2.COLOR_RGB2BGR))
                self.dataset.saveZvalImg()
                self.process.join()
610 611
                self.dataqueue.close()
                self.dataqueue.join_thread()
612 613 614 615 616 617 618 619
                
                if self.deleteImgChecker.isChecked():
                    path = self.dataset.getScanPath()
                    files = os.listdir(path)
                    for file in files:
                        if file.startswith('image_') and (file.endswith('.bmp') or file.endswith('.tiff')):
                            os.remove(os.path.join(path, file))
                
620 621 622 623 624 625 626 627 628
                self.ramanctrl.connect()
                self.view.saveDataSet()
                self.view.unblockUI()
                self.view.switchMode("ParticleDetection")
                self.progressbar.setValue(0)
                self.progressbar.setEnabled(False)
                self.progresstime.setEnabled(False)
                self.close()
                return
629
        self.timer.start(100.)
630 631
            
if __name__ == "__main__":
632
    from ramancom.simulatedraman import SimulatedRaman
633
    from dataset import DataSet
634 635

    app = QtWidgets.QApplication(sys.argv)
636 637
    ds = DataSet('Test')
    optscan = OpticalScan(SimulatedRaman(), ds)
638 639
    optscan.show()
    sys.exit(app.exec_())