From bddc18b2d7415ee6feed6b3108c2e1f8b960e1c3 Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 3 Dec 2018 10:20:27 +0100 Subject: [PATCH] RamanScanUI also works with the more stable dataqueue setup. --- WITecCOM.py | 5 +++-- dataset.py | 2 +- helperfunctions.py | 14 ++++++++----- ramanscanui.py | 52 ++++++++++++++++++++++++++-------------------- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/WITecCOM.py b/WITecCOM.py index 05efbfe..92f113a 100644 --- a/WITecCOM.py +++ b/WITecCOM.py @@ -208,8 +208,9 @@ class WITecCOM(RamanBase): while distance > epsxy:# and (lastpos is None or lastpos!=curpos): curpos = self.getPosition() distance = max(abs(curpos[0]-x), abs(curpos[1]-y)) - if ((time()-t0>0.5) and max(abs(curpos[0]-initpos[0]), abs(curpos[1]-initpos[1]))10.): - print("WARNING: signal ignored:", time()-t0, x, y, curpos, initpos) +# if ((time()-t0>0.5) and max(abs(curpos[0]-initpos[0]), abs(curpos[1]-initpos[1]))10.): + if ((time()-t0>2) and max(abs(curpos[0]-initpos[0]), abs(curpos[1]-initpos[1]))10.): + print("WARNING: signal ignored; time: {} s, x, y: {}, {}, curPos: {}, initPos: {}".format((time()-t0), x, y, curpos, initpos)) sys.stdout.flush() break sleep(.01) diff --git a/dataset.py b/dataset.py index cc07dc1..4ac7cb0 100644 --- a/dataset.py +++ b/dataset.py @@ -257,4 +257,4 @@ class DataSet(object): def save(self): saveData(self, self.fname) - \ No newline at end of file + \ No newline at end of file diff --git a/helperfunctions.py b/helperfunctions.py index 38bffa4..9472294 100644 --- a/helperfunctions.py +++ b/helperfunctions.py @@ -24,11 +24,15 @@ import cv2 import os def cv2imread_fix(fname, flags=cv2.IMREAD_COLOR): - with open(fname, "rb") as fp: - cont = fp.read() - img = cv2.imdecode(np.fromstring(cont, dtype=np.uint8), flags) - return img - return None + if not os.path.exists(fname): #This seems to be a source of potential errors. Having these lines here probably aids finding errors for other groups? + print('Error, image file not found\Please check if the used save-Image command returns before the image was fully written to disk.') + return + + with open(fname, "rb") as fp: + cont = fp.read() + img = cv2.imdecode(np.fromstring(cont, dtype=np.uint8), flags) + return img + return None def cv2imwrite_fix(fname, img, params=None): pathname, ext = os.path.splitext(fname) diff --git a/ramanscanui.py b/ramanscanui.py index db05b10..a9ccaa1 100644 --- a/ramanscanui.py +++ b/ramanscanui.py @@ -21,10 +21,11 @@ If not, see . from PyQt5 import QtCore, QtWidgets import numpy as np -from multiprocessing import Process, Pipe -from time import sleep, time -from externalmodules import tsp +from multiprocessing import Process, Queue, Event +import queue +from time import sleep, time, localtime, strftime import datetime +from externalmodules import tsp import sys def reorder(points, N=20): @@ -45,7 +46,8 @@ def reorder(points, N=20): assert np.unique(newind).shape[0]==allind.shape[0] return newind -def scan(name, accu, inttime, positions, controlclass, connection): +def scan(name, accu, inttime, positions, controlclass, dataqueue, stopevent): + with open("ramanscanlog.txt", "a") as fp: sys.stderr = fp sys.stdout = fp @@ -53,22 +55,20 @@ def scan(name, accu, inttime, positions, controlclass, connection): ramanctrl = controlclass() ramanctrl.connect() ramanctrl.initiateTimeSeriesScan(name, len(positions), accu, inttime) + print("starting Raman Scan at: " + strftime("%d %b %Y %H:%M:%S", localtime())) for i, p in enumerate(positions): x, y, z = p - print("time:", time()) - print("position:", x, y, z) + + print('Measuring particle index {} at location {}, time = {}'.format(i, (x, y, z), strftime("%H:%M:%S", localtime()))) + sys.stdout.flush() #remove after testing ramanctrl.moveToAbsolutePosition(x, y, z) ramanctrl.nextTimeSeriesScan(i) - if connection.poll(): - instruction = connection.recv() - if instruction=="stop": - ramanctrl.disconnect() - return - connection.send(i) + + if stopevent.is_set(): + ramanctrl.disconnect() + return + dataqueue.put(i) ramanctrl.disconnect() - while not connection.poll(): - sleep(.1) - connection.recv() class RamanScanUI(QtWidgets.QWidget): imageUpdate = QtCore.pyqtSignal(name='imageUpdate') @@ -148,8 +148,11 @@ class RamanScanUI(QtWidgets.QWidget): QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: self.timer.stop() - self.connection.send("stop") +# self.connection.send("stop") + self.processstopevent.set() self.process.join() + self.dataqueue.close() + self.dataqueue.join_thread() self.view.unblockUI() else: return @@ -205,9 +208,9 @@ class RamanScanUI(QtWidgets.QWidget): self.progressbar.setRange(0, len(scanpoints)) self.progressbar.setValue(0) self.ramanctrl.disconnect() - parent_conn, child_conn = Pipe() - self.connection = parent_conn - self.process = Process(target=scan, args=(self.dataset.name, accu, inttime, scanpoints, self.ramanctrl.__class__, child_conn)) + self.processstopevent = Event() + self.dataqueue = Queue() + self.process = Process(target=scan, args=(self.dataset.name, accu, inttime, scanpoints, self.ramanctrl.__class__, self.dataqueue, self.processstopevent)) self.process.start() self.starttime = time() self.timer = QtCore.QTimer(self) @@ -217,8 +220,11 @@ class RamanScanUI(QtWidgets.QWidget): @QtCore.pyqtSlot() def checkOnScan(self): - if self.connection.poll(): - i = self.connection.recv() + try: + i = self.dataqueue.get_nowait() + except queue.Empty: + i = -1 + if i >= 0: self.progressbar.setValue(i+1) self.view.highLightRamanIndex(i+1) Npoints = len(self.dataset.ramanpoints) @@ -228,9 +234,9 @@ class RamanScanUI(QtWidgets.QWidget): time2go = ttot - timerunning self.progresstime.setText(self.timelabeltext + str(datetime.timedelta(seconds=round(time2go)))) if i==Npoints-1: - self.connection.send("stop") self.process.join() - self.connection.close() + self.dataqueue.close() + self.dataqueue.join_thread() self.dataset.ramanscandone = True self.view.saveDataSet() self.view.unblockUI() -- GitLab