Commit 8e5899e5 authored by Hackmet's avatar Hackmet

Several Bugfixes

parent c438bcca
...@@ -19,7 +19,6 @@ along with this program, see COPYING. ...@@ -19,7 +19,6 @@ along with this program, see COPYING.
If not, see <https://www.gnu.org/licenses/>. If not, see <https://www.gnu.org/licenses/>.
""" """
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui
from PIL import ImageFont
import numpy as np import numpy as np
...@@ -60,14 +59,13 @@ class Legend(QtWidgets.QMdiSubWindow): ...@@ -60,14 +59,13 @@ class Legend(QtWidgets.QMdiSubWindow):
if numEntries > 0: if numEntries > 0:
def getSize(fontsize, text, tileSize, spacer): def getSize(fontsize, text, tileSize, spacer):
# font = ImageFont.truetype('arial.ttf', fontsize) font = QtGui.QFont()
# size = font.getsize(text) font.setPixelSize(fontSize)
size = 5*len(text), fontsize+2 fm = QtGui.QFontMetrics(font)
width, height = size[0]*1.5 + tileSize + spacer, numEntries * (tileSize+1*spacer) + 2*spacer pixelwidth = fm.width(text)
width, height = pixelwidth + tileSize + 3*spacer, numEntries * (tileSize+1*spacer) + 2*spacer
return width, height return width, height
fontSize, tileSize, spacer = self.fontSize, self.tileSize, self.spacer fontSize, tileSize, spacer = self.fontSize, self.tileSize, self.spacer
longestEntry = max([i[0] for i in self.items], key=len) longestEntry = max([i[0] for i in self.items], key=len)
width, height = getSize(fontSize, longestEntry, tileSize, spacer) width, height = getSize(fontSize, longestEntry, tileSize, spacer)
......
...@@ -40,8 +40,12 @@ from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as Navigatio ...@@ -40,8 +40,12 @@ from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as Navigatio
import pandas as pd import pandas as pd
from analysis.loadresults import LoadWITecResults from analysis.loadresults import LoadWITecResults
from analysis.sqlexport import SQLExport
from analysis.editParticles import ParticleEditor from analysis.editParticles import ParticleEditor
try:
from analysis.sqlexport import SQLExport
sqlEnabled = True
except:
sqlEnabled = False
class ParticleAnalysis(QtWidgets.QWidget): class ParticleAnalysis(QtWidgets.QWidget):
...@@ -263,7 +267,6 @@ class ParticleAnalysis(QtWidgets.QWidget): ...@@ -263,7 +267,6 @@ class ParticleAnalysis(QtWidgets.QWidget):
self.layout_SArea.addWidget(self.resultCheckBoxes) self.layout_SArea.addWidget(self.resultCheckBoxes)
# self.layout_SArea.addStretch(1) # self.layout_SArea.addStretch(1)
self.exportbtn= QtWidgets.QPushButton('Export Results') self.exportbtn= QtWidgets.QPushButton('Export Results')
self.exportbtn.clicked.connect(self.exportData) self.exportbtn.clicked.connect(self.exportData)
self.exportbtn.setDisabled(True) self.exportbtn.setDisabled(True)
...@@ -286,7 +289,23 @@ class ParticleAnalysis(QtWidgets.QWidget): ...@@ -286,7 +289,23 @@ class ParticleAnalysis(QtWidgets.QWidget):
def loadSpectra(self, fname): def loadSpectra(self, fname):
try: try:
return np.loadtxt(fname) specs = np.loadtxt(fname)
#if spectra are already in correct format (WITec, first column: wavenumbers, other columns, intensities),
#we take them, otherwise we have to convert from Renishaw export format...
if len(np.unique(specs[:, 0])) == len(specs[:, 0]): #--> only unique numbers -> this is the wavenumber column, we have the witec format
return specs
else:
#columns 0 and 1 are x and y coordinates. We dont need them...
startWavenumber = specs[0, 2]
startIndices = np.where(specs[:, 2] == startWavenumber)[0]
spectra = np.zeros((startIndices[1], len(startIndices)+1)) #create array with shape (numWavenumbers, numSpectra+1) (first column holds wavenumbers)
spectra[:, 0] = specs[startIndices[0]:startIndices[1], 2]
for i in range(len(startIndices)-1):
spectra[:, i+1] = specs[startIndices[i]:startIndices[i+1], 3]
#aaand the last spectrum:
spectra[:, -1] = specs[startIndices[-1]:, 3]
return np.flip(spectra, 0) #Renishaw goes from highest to lowest wavenumber, out of whatever reason...
except: except:
return None return None
...@@ -301,14 +320,25 @@ class ParticleAnalysis(QtWidgets.QWidget): ...@@ -301,14 +320,25 @@ class ParticleAnalysis(QtWidgets.QWidget):
self.colorSeed = 'default' self.colorSeed = 'default'
#load Spectra #load Spectra
self.spectra = self.loadSpectra(os.path.join(self.parent.dataset.path, self.parent.dataset.name + '_000_Spec.Data 1.txt')) if self.parent.dataset.spectraPath is None:
fname = os.path.join(self.parent.dataset.path, self.parent.dataset.name + '_000_Spec.Data 1.txt')
else:
fname = self.parent.dataset.spectraPath
self.spectra = self.loadSpectra(fname)
if self.spectra is None: if self.spectra is None:
fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Select Spectra File', self.parent.dataset.path, 'text file (*.txt)')[0] fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Select Spectra File', self.parent.dataset.path, 'text file (*.txt)')[0]
self.spectra = self.loadSpectra(fname) self.spectra = self.loadSpectra(fname)
if self.spectra is None: if self.spectra is None:
QtWidgets.QMessageBox.critical(self, 'ERROR!', 'spectra file could not be opened with np.loadtxt...') QtWidgets.QMessageBox.critical(self, 'ERROR!', 'spectra file could not be opened with np.loadtxt...')
return return
self.parent.dataset.spectraPath = fname
self.spec_ax.set_xbound(100, (3400 if self.spectra[-1, 0] > 3400 else self.spectra[-1, 0])) self.spec_ax.set_xbound(100, (3400 if self.spectra[-1, 0] > 3400 else self.spectra[-1, 0]))
self.specCanvas.draw()
####fake data!!!
if self.spectraResults is None:
self.spectraResults = ['empty']*(self.spectra.shape[1]-1)
self.hqis = [100]*(self.spectra.shape[1]-1)
self.loadParticleData() self.loadParticleData()
...@@ -380,7 +410,9 @@ class ParticleAnalysis(QtWidgets.QWidget): ...@@ -380,7 +410,9 @@ class ParticleAnalysis(QtWidgets.QWidget):
self.particleResults = [None]*len(self.particlestats) self.particleResults = [None]*len(self.particlestats)
self.typehistogram = {i: 0 for i in self.uniquePolymers} self.typehistogram = {i: 0 for i in self.uniquePolymers}
assert len(self.particles2spectra) == len(self.particlestats), 'inconsistent data!!' if len(self.particles2spectra) != len(self.particlestats):
QtWidgets.QMessageBox.critical(self, 'Error', 'Inconsistent particle data. Please restore backup!')
return
for particleID, specList in enumerate(self.particles2spectra): for particleID, specList in enumerate(self.particles2spectra):
assignment = self.currentPolymers[specList[0]] #we take the first result as particle result. Hence, all spectra per particle have to have the same result assignment = self.currentPolymers[specList[0]] #we take the first result as particle result. Hence, all spectra per particle have to have the same result
...@@ -498,6 +530,7 @@ class ParticleAnalysis(QtWidgets.QWidget): ...@@ -498,6 +530,7 @@ class ParticleAnalysis(QtWidgets.QWidget):
self.spec_ax.set_xlabel('Wavenumber (cm-1)', fontsize = 15) self.spec_ax.set_xlabel('Wavenumber (cm-1)', fontsize = 15)
self.spec_ax.set_ylabel('Counts', fontsize = 15) self.spec_ax.set_ylabel('Counts', fontsize = 15)
self.spec_ax.set_title('ScanPoint Number {}, Size = {} µm'.format(specIndex+1, np.round(self.particlestats[self.currentParticleIndex][2], 1))) self.spec_ax.set_title('ScanPoint Number {}, Size = {} µm'.format(specIndex+1, np.round(self.particlestats[self.currentParticleIndex][2], 1)))
self.spec_ax.set_xbound(100, (3400 if self.spectra[-1, 0] > 3400 else self.spectra[-1, 0]))
self.spec_ax.figure.canvas.draw() self.spec_ax.figure.canvas.draw()
self.parent.centerOnRamanIndex(specIndex, centerOn=centerOn, highlightContour=highlightContour) self.parent.centerOnRamanIndex(specIndex, centerOn=centerOn, highlightContour=highlightContour)
self.parent.highLightRamanIndex(specIndex) self.parent.highLightRamanIndex(specIndex)
...@@ -805,7 +838,10 @@ class ExportDialog(QtWidgets.QWidget): ...@@ -805,7 +838,10 @@ class ExportDialog(QtWidgets.QWidget):
self.sqlbtn = QtWidgets.QPushButton('Export to SQL Database') self.sqlbtn = QtWidgets.QPushButton('Export to SQL Database')
self.sqlbtn.resize(self.sqlbtn.sizeHint()) self.sqlbtn.resize(self.sqlbtn.sizeHint())
if sqlEnabled:
self.sqlbtn.clicked.connect(self.toSQL) self.sqlbtn.clicked.connect(self.toSQL)
else:
self.sqlbtn.setDisabled(True)
self.sqlExport = None self.sqlExport = None
......
...@@ -26,16 +26,33 @@ If not, see <https://www.gnu.org/licenses/>. ...@@ -26,16 +26,33 @@ If not, see <https://www.gnu.org/licenses/>.
""" """
import numpy as np import numpy as np
import cv2 import cv2
from PyQt5 import QtWidgets
#import matplotlib.pyplot as plt #import matplotlib.pyplot as plt
class ParticleEditor(object): class ParticleEditor(object):
def __init__(self, parent): def __init__(self, parent):
self.parent = parent #the assigned analysis widget self.parent = parent #the assigned analysis widget
self.backupFreq = 3 #save a backup every n actions
self.neverBackedUp = True
self.actionCounter = 0
def createSafetyBackup(self): def createSafetyBackup(self):
self.actionCounter += 1
if self.actionCounter == self.backupFreq-1 or self.neverBackedUp:
print('backing up')
self.parent.parent.dataset.saveBackup() self.parent.parent.dataset.saveBackup()
self.neverBackedUp = False
self.actionCounter = 0
def getNewEntry(self):
text, okClicked = QtWidgets.QInputDialog.getText(self.parent.parent, "Custom assignment", "Enter new assignment")
if okClicked and text != '':
return text
def combineParticles(self, contourIndices, new_assignment): def combineParticles(self, contourIndices, new_assignment):
if new_assignment == 'other':
new_assignment = self.getNewEntry()
contourIndices = sorted(contourIndices) #we want to keep the contour with lowest index contourIndices = sorted(contourIndices) #we want to keep the contour with lowest index
print('selected contours:', contourIndices) print('selected contours:', contourIndices)
self.createSafetyBackup() self.createSafetyBackup()
...@@ -76,7 +93,6 @@ class ParticleEditor(object): ...@@ -76,7 +93,6 @@ class ParticleEditor(object):
sortindices = self.parent.parent.dataset.ramanscansortindex sortindices = self.parent.parent.dataset.ramanscansortindex
self.parent.parent.dataset.particles2spectra = [[int(np.where(sortindices == i)[0])] for i in range(len(sortindices))] self.parent.parent.dataset.particles2spectra = [[int(np.where(sortindices == i)[0])] for i in range(len(sortindices))]
#Contour indices are the same as the original particlestats, which are contained in the dataset. #Contour indices are the same as the original particlestats, which are contained in the dataset.
#We have to modify that and reload in the analysisview #We have to modify that and reload in the analysisview
#first, overwrite first index with new particlestats #first, overwrite first index with new particlestats
...@@ -89,7 +105,6 @@ class ParticleEditor(object): ...@@ -89,7 +105,6 @@ class ParticleEditor(object):
self.parent.parent.dataset.particlecontours[contourIndices[0]] = newContour self.parent.parent.dataset.particlecontours[contourIndices[0]] = newContour
self.parent.parent.dataset.particlecontours = [i for ind, i in enumerate(self.parent.parent.dataset.particlecontours) if ind not in contourIndices[1:]] self.parent.parent.dataset.particlecontours = [i for ind, i in enumerate(self.parent.parent.dataset.particlecontours) if ind not in contourIndices[1:]]
#update particle2spectra_list #update particle2spectra_list
#what is the current particle index?? #what is the current particle index??
specIndices = [] specIndices = []
...@@ -98,13 +113,12 @@ class ParticleEditor(object): ...@@ -98,13 +113,12 @@ class ParticleEditor(object):
specIndices.append(self.parent.particles2spectra[index]) specIndices.append(self.parent.particles2spectra[index])
#flatten index list (in case, that a nested list was created...) #flatten index list (in case, that a nested list was created...)
specIndices = list(np.unique(np.array(specIndices))) specIndices = list(np.concatenate(specIndices))
for i in specIndices: for i in specIndices:
self.parent.spectraResults[i] = new_assignment self.parent.spectraResults[i] = new_assignment
self.parent.hqis[i] = 100 #avoid sorting them out again by hqi-filter... self.parent.hqis[i] = 100 #avoid sorting them out again by hqi-filter...
print(f'spectrum {i} of particle{contourIndices[0]} is now {new_assignment}') print(f'spectrum {i} of particle{contourIndices[0]} is now {new_assignment}')
#modify particles2spectra.. #modify particles2spectra..
self.parent.parent.dataset.particles2spectra[contourIndices[0]] = specIndices self.parent.parent.dataset.particles2spectra[contourIndices[0]] = specIndices
for index in reversed(contourIndices[1:]): for index in reversed(contourIndices[1:]):
...@@ -116,11 +130,13 @@ class ParticleEditor(object): ...@@ -116,11 +130,13 @@ class ParticleEditor(object):
#update contours in sampleview #update contours in sampleview
self.parent.parent.contouritem.resetContours(self.parent.parent.dataset.particlecontours) self.parent.parent.contouritem.resetContours(self.parent.parent.dataset.particlecontours)
self.parent.loadParticleData() self.parent.loadParticleData()
def reassignParticles(self, contourindices, new_assignment): def reassignParticles(self, contourindices, new_assignment):
if new_assignment == 'other':
new_assignment = self.getNewEntry()
self.createSafetyBackup() self.createSafetyBackup()
for partIndex in contourindices: for partIndex in contourindices:
for specIndex in self.parent.particles2spectra[partIndex]: for specIndex in self.parent.particles2spectra[partIndex]:
...@@ -128,7 +144,7 @@ class ParticleEditor(object): ...@@ -128,7 +144,7 @@ class ParticleEditor(object):
self.parent.spectraResults[specIndex] = new_assignment self.parent.spectraResults[specIndex] = new_assignment
self.parent.hqis[specIndex] = 100 self.parent.hqis[specIndex] = 100
self.parent.createHistogramData() self.parent.loadParticleData()
def deleteParticles(self): def deleteParticles(self):
......
...@@ -102,7 +102,7 @@ class DataSet(object): ...@@ -102,7 +102,7 @@ class DataSet(object):
self.resultParams = {'minHQI': None, self.resultParams = {'minHQI': None,
'compHQI': None} 'compHQI': None}
self.spectraPath = None
self.particles2spectra = None #links idParticle to corresponding idSpectra (i.e., first measured particle (ID=0) is linked to spectra indices 0 and 1) self.particles2spectra = None #links idParticle to corresponding idSpectra (i.e., first measured particle (ID=0) is linked to spectra indices 0 and 1)
self.colorSeed = 'default' self.colorSeed = 'default'
self.resultsUploadedToSQL = [] self.resultsUploadedToSQL = []
...@@ -177,12 +177,12 @@ class DataSet(object): ...@@ -177,12 +177,12 @@ class DataSet(object):
print('pixelscale was', self.pixelscale) print('pixelscale was', self.pixelscale)
self.pixelscale_bf = self.pixelscale self.pixelscale_bf = self.pixelscale
self.pixelscale_df = self.pixelscale self.pixelscale_df = self.pixelscale
# del self.pixelscale del self.pixelscale
if hasattr(self, 'imagedim'): if hasattr(self, 'imagedim'):
self.imagedim_bf = self.imagedim self.imagedim_bf = self.imagedim
self.imagedim_df = self.imagedim self.imagedim_df = self.imagedim
# del self.imagedim del self.imagedim
self.version = 2 self.version = 2
......
...@@ -25,6 +25,7 @@ from ramancom.ramancontrol import defaultPath ...@@ -25,6 +25,7 @@ from ramancom.ramancontrol import defaultPath
from ramancom.ramanSwitch import RamanSwitch from ramancom.ramanSwitch import RamanSwitch
from analysis.analysisWidgets import Legend from analysis.analysisWidgets import Legend
import os import os
from pathlib import Path
class MeasureParticleWindow(QtWidgets.QMainWindow): class MeasureParticleWindow(QtWidgets.QMainWindow):
...@@ -287,12 +288,17 @@ if __name__ == '__main__': ...@@ -287,12 +288,17 @@ if __name__ == '__main__':
import sys import sys
from time import localtime, strftime from time import localtime, strftime
logpath = os.path.join(Path.home(),'gepard')
if not os.path.exists(logpath):
os.mkdir(logpath)
logname = os.path.join(logpath, 'logfile.txt')
# logname = os.path.join(os.path.split(__file__)[0], os.path.join("logfile.txt")) # logname = os.path.join(os.path.split(__file__)[0], os.path.join("logfile.txt"))
# fp = open(logname, "a") fp = open(logname, "a")
# sys.stderr = fp sys.stderr = fp
# sys.stdout = fp sys.stdout = fp
print("starting GEPARD at: " + strftime("%d %b %Y %H:%M:%S", localtime())) print("starting GEPARD at: " + strftime("%d %b %Y %H:%M:%S", localtime()))
sys.stdout.flush()
app = QtWidgets.QApplication(sys.argv) app = QtWidgets.QApplication(sys.argv)
meas = MeasureParticleWindow() meas = MeasureParticleWindow()
meas.showMaximized() meas.showMaximized()
......
...@@ -30,6 +30,7 @@ from helperfunctions import cv2imread_fix, cv2imwrite_fix ...@@ -30,6 +30,7 @@ from helperfunctions import cv2imread_fix, cv2imwrite_fix
from time import sleep, time from time import sleep, time
import datetime import datetime
import sys import sys
from pathlib import Path
#def scan(path, sol, zpositions, grid, controlclass, connection, ishdr=False): #def scan(path, sol, zpositions, grid, controlclass, connection, ishdr=False):
def scan(path, sol, zpositions, grid, controlclass, dataqueue, stopevent, ishdr=False): def scan(path, sol, zpositions, grid, controlclass, dataqueue, stopevent, ishdr=False):
...@@ -37,7 +38,9 @@ def scan(path, sol, zpositions, grid, controlclass, dataqueue, stopevent, ishdr= ...@@ -37,7 +38,9 @@ def scan(path, sol, zpositions, grid, controlclass, dataqueue, stopevent, ishdr=
sys.stdout.flush() sys.stdout.flush()
if ishdr: if ishdr:
merge_mertens = cv2.createMergeMertens() merge_mertens = cv2.createMergeMertens()
with open("scanlog.txt", "a") as fp:
logpath = os.path.join(Path.home(),'gepard', 'scanlog.txt')
with open(logpath, "a") as fp:
sys.stderr = fp sys.stderr = fp
sys.stdout = fp sys.stdout = fp
......
...@@ -35,8 +35,6 @@ except: ...@@ -35,8 +35,6 @@ except:
from ramanbase import RamanBase from ramanbase import RamanBase
from configRaman import RamanSettingParam from configRaman import RamanSettingParam
from socket import gethostname from socket import gethostname
from PyQt5 import QtWidgets
class WITecCOM(RamanBase): class WITecCOM(RamanBase):
......
...@@ -27,6 +27,8 @@ from time import sleep, time ...@@ -27,6 +27,8 @@ from time import sleep, time
from external import tsp from external import tsp
import datetime import datetime
import sys import sys
import os
from pathlib import Path
def reorder(points, N=20): def reorder(points, N=20):
y0, y1 = points[:,1].min(), points[:,1].max() y0, y1 = points[:,1].min(), points[:,1].max()
...@@ -47,7 +49,8 @@ def reorder(points, N=20): ...@@ -47,7 +49,8 @@ def reorder(points, N=20):
return newind return newind
def scan(ramanSettings, positions, controlclass, dataqueue, stopevent): def scan(ramanSettings, positions, controlclass, dataqueue, stopevent):
with open("ramanscanlog.txt", "a") as fp: logpath = os.path.join(Path.home(),'gepard', 'ramanscanlog.txt')
with open(logpath, "a") as fp:
sys.stderr = fp sys.stderr = fp
sys.stdout = fp sys.stdout = fp
......
...@@ -63,15 +63,22 @@ class SegmentationContours(QtWidgets.QGraphicsItem): ...@@ -63,15 +63,22 @@ class SegmentationContours(QtWidgets.QGraphicsItem):
def paint(self, painter, option, widget): def paint(self, painter, option, widget):
painter.setPen(QtCore.Qt.green) painter.setPen(QtCore.Qt.green)
lenColorList = len(self.colorList)
for index, c in enumerate(self.contours): for index, c in enumerate(self.contours):
if index not in self.selectedContours: if index not in self.selectedContours:
if lenColorList > 0:
color = self.colorList[index] color = self.colorList[index]
painter.setBrush(color)
painter.setPen(QtGui.QColor(int(color.red()*0.7), int(color.green()*0.7), int(color.blue()*0.7), color.alpha())) painter.setPen(QtGui.QColor(int(color.red()*0.7), int(color.green()*0.7), int(color.blue()*0.7), color.alpha()))
else:
color = QtCore.Qt.green
painter.setPen(color)
painter.setBrush(color)
painter.drawPolygon(c) painter.drawPolygon(c)
else: else:
if lenColorList > 0:
alpha = self.colorList[index].alpha() alpha = self.colorList[index].alpha()
else:
alpha = 255
painter.setBrush(QtGui.QColor(200, 200, 200, alpha)) painter.setBrush(QtGui.QColor(200, 200, 200, alpha))
painter.setPen(QtCore.Qt.white) painter.setPen(QtCore.Qt.white)
painter.drawPolygon(c) painter.drawPolygon(c)
...@@ -136,17 +143,17 @@ class SegmentationContours(QtWidgets.QGraphicsItem): ...@@ -136,17 +143,17 @@ class SegmentationContours(QtWidgets.QGraphicsItem):
print('deleting') print('deleting')
elif action in combineActs: elif action in combineActs:
newAssignment = action.text() newAssignment = action.text()
if newAssignment == "other": # if newAssignment == "other":
QtWidgets.QMessageBox.about(self.parent, "Not yet implemented", "we are getting there...") # QtWidgets.QMessageBox.about(self.parent, "Not yet implemented", "we are getting there...")
return # return
self.parent.analysiswidget.editor.combineParticles(self.selectedContours, newAssignment) self.parent.analysiswidget.editor.combineParticles(self.selectedContours, newAssignment)
elif action in reassignActs: elif action in reassignActs:
newAssignment = action.text() newAssignment = action.text()
if newAssignment == "other": # if newAssignment == "other":
QtWidgets.QMessageBox.about(self.parent, "Not yet implemented", "we are getting there...") # QtWidgets.QMessageBox.about(self.parent, "Not yet implemented", "we are getting there...")
return # return
self.parent.analysiswidget.editor.reassignParticles(self.selectedContours, newAssignment) self.parent.analysiswidget.editor.reassignParticles(self.selectedContours, newAssignment)
......
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