Commit 0b768ad2 authored by JosefBrandt's avatar JosefBrandt

First phase of refactoring

parent 8fc15b1e
This diff is collapsed.
......@@ -37,7 +37,7 @@ class ExpExcelDialog(QtWidgets.QDialog):
self.datastats = datastats
self.particles = self.datastats.getParticleStats()
self.polymers = self.datastats.particleResults
self.polymers = self.datastats.particleContainer
self.additives = self.datastats.currentAdditives
self.hqis = self.datastats.hqis
......@@ -262,8 +262,8 @@ class ParticleTypeView(QtWidgets.QScrollArea):
self.setAlignment(QtCore.Qt.AlignHCenter)
self.widgets = []
def updateTypes(self, types):
print("Updating polymer type view", flush=True)
def updateTypeHistogram(self, types):
# print("Updating polymer type view", flush=True)
for pi in self.widgets:
self.indicatorbox.removeWidget(pi)
pi.setParent(None)
......@@ -277,7 +277,7 @@ class ParticleTypeView(QtWidgets.QScrollArea):
for index, entry in enumerate(types):
num, text, color = entry
print("num, text, color:", num, text, color, flush=True)
# print("num, text, color:", num, text, color, flush=True)
pi = ParticleIndicator(num, numtotal, color, text)
self.indicatorbox.addWidget(pi)
pi.clicked.connect(getIndexFunction(index))
......
......@@ -35,13 +35,16 @@ from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as Navigatio
from matplotlib.figure import Figure
import functools
from scipy.linalg import solveh_banded
from pathlib import Path
class DataBaseWindow(QtWidgets.QMainWindow):
def __init__(self, parent):
super(DataBaseWindow, self).__init__()
self.parent = parent
self.path = os.path.join(Path.home(), 'gepard', 'databases')
logpath = QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.AppLocalDataLocation)
self.path = os.path.join(logpath, 'databases')
self.importPath = self.path
if not os.path.exists(self.path):
os.mkdir(self.path)
......
This diff is collapsed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 28 20:33:14 2019
@author: brandt
"""
import numpy as np
'''Return Spectra array (First column: Wavenumbers, all other columns, intensities)
and names for all in file contained spectra'''
#TODO: Include sanity checks for correct file format?
def importWITecSpectra(fname):
def firstColumnOnlyHasUniqueNumbers(data):
#this is the case for WITec Spectra sets, but not for Renishaw
if len(np.unique(data[:, 0])) == len(data[:, 0]):
return True
else:
return False
data = np.loadtxt(fname)
names = [f'Spectrum {i+1}' for i in range(data.shape[1]-1)]
if firstColumnOnlyHasUniqueNumbers(data):
return data, names
else:
raise ImportError
def importRenishawSpectra(fname):
data = np.loadtxt(fname)
rawSpectra = data[:, 2:4]
spectraIndices = np.where(rawSpectra[:, 0] == rawSpectra[0, 0])[0]
positions = np.zeros((len(spectraIndices), 2))
spectra = []
names = []
spectra = np.zeros((spectraIndices[1], len(spectraIndices)+1))
spectra[:, 0] = rawSpectra[0:spectraIndices[1], 0]
for i in range(len(spectraIndices)):
names.append(f'Spectrum {i+1}')
positions[i, :] = [data[spectraIndices[i], 0], data[spectraIndices[i], 1]]
if i < len(spectraIndices)-1:
spectra[:, i+1] = rawSpectra[spectraIndices[i]:spectraIndices[i+1], 1]
else:
spectra[:, i+1] = rawSpectra[spectraIndices[i]:, 1]
spectra = np.flipud(spectra)
wavenumbers = spectra[:, 0]
spectra = spectra[:, 1:]
return np.transpose(np.vstack((wavenumbers, spectra))), names
def importPerkinElmerSpectra(fname):
names = []
spectra = []
with open(fname) as fp:
for index, line in enumerate(fp.readlines()):
if index == 0:
for name in line.split(';'):
names.append(name.split('.sp')[0])
elif index > 1:
spectra.append(line.split(';'))
return np.array(spectra, dtype=np.float), names[1:]
......@@ -20,7 +20,7 @@ class SQLExport(QtWidgets.QDialog):
self.setWindowTitle('Export to SQL Database')
self.datastats = datastats
self.polymerList = self.datastats.particleResults
self.polymerList = self.datastats.particleContainer
particlestats = self.datastats.getParticleStats()
self.longSizes = np.round(np.array([i[0] if np.isnan(i[2]) else i[2] for i in particlestats]), 1)
self.shortSize = np.round(np.array([i[1] if np.isnan(i[3]) else i[3] for i in particlestats]), 1)
......
......@@ -24,8 +24,9 @@ import numpy as np
import cv2
from helperfunctions import cv2imread_fix, cv2imwrite_fix
from copy import copy
from analysis.datastats import ParticleContainer
currentversion = 2
currentversion = 3
def loadData(fname):
retds = None
......@@ -142,21 +143,22 @@ class DataSet(object):
'compactness': 0.1,
'seedRad': 3}
self.ramanpoints = []
self.particlecontours = []
self.particlestats = []
self.ramanscansortindex = None
# self.ramanpoints = []
# self.particlecontours = []
# self.particlestats = []
# self.ramanscansortindex = None
self.particleContainer = ParticleContainer(self)
self.ramanscandone = False
self.results = {'polymers': None,
'hqis': None,
'additives': None,
'additive_hqis': None}
# self.results = {'polymers': None,
# 'hqis': None,
# 'additives': None,
# 'additive_hqis': None}
self.resultParams = {'minHQI': 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.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.colorSeed = 'default'
self.resultsUploadedToSQL = []
......@@ -231,7 +233,6 @@ class DataSet(object):
self.version = 1
if self.version == 1:
print("Converting legacy version 1 to 2")
if hasattr(self, 'pixelscale'):
......@@ -246,9 +247,42 @@ class DataSet(object):
del self.imagedim
self.version = 2
if self.version == 2:
def recreateMeasurement2ParticleFromScanIndices():
measurements2particles = [[int(np.where(self.ramanscansortindex == i)[0])] for i in range(len(self.ramanscansortindex))]
return measurements2particles
self.particleContainer.initializeParticles(len(self.particlestats))
self.particleContainer.setParticlecontours(self.particlecontours)
self.particleContainer.setParticleStats(self.particlestats)
if hasattr(self, 'particles2spectra'):
if self.particles2spectra is not None:
measurements2particles = self.particles2spectra
else:
measurements2particles = recreateMeasurement2ParticleFromScanIndices()
else:
measurements2particles = recreateMeasurement2ParticleFromScanIndices()
for particleIndex, listOfScanIndices in enumerate(measurements2particles):
curParticle = self.particleContainer.particles[particleIndex]
for measIndex, scanIndex in enumerate(listOfScanIndices):
curParticle.addEmptyMeasurement()
x, y = self.ramanpoints[particleIndex][0], self.ramanpoints[particleIndex][1]
curParticle.setMeasurementPixelCoords(measIndex, x, y)
curParticle.setMeasurementScanIndex(measIndex, scanIndex)
for particle in self.particleContainer.particles:
for meas in particle.measurements:
specIndex = meas.ramanScanIndex
meas.setAssignemtAndHQI(self.results['polymers'][specIndex], self.results['hqis'][specIndex])
# self.version = 3
# add later conversion for higher version numbers here
def getSubImage(self, img, index, draw=True):
contour = self.particlecontours[index]
x0, x1 = contour[:,0,0].min(), contour[:,0,0].max()
......@@ -334,7 +368,10 @@ class DataSet(object):
def updatePath(self):
self.path = os.path.split(self.fname)[0]
self.name = os.path.splitext(os.path.basename(self.fname))[0]
def getSpectraFileName(self):
return os.path.join(self.path + 'spectra.npy')
def getImageName(self):
return os.path.join(self.path, 'fullimage.tif')
......
......@@ -652,18 +652,31 @@ class ParticleDetectionView(QtWidgets.QWidget):
kwargs[name] = valuefunc()
seedradius = self.seedradiusedit.value()
self.seg.setParameters(**kwargs)
measurementpoints, contours, particlestats = self.seg.apply2Image(self.img,
measurementPoints, contours, particlestats = self.seg.apply2Image(self.img,
seedpoints,
deletepoints,
seedradius)
if measurementpoints is None: # computation was canceled
if measurementPoints is None: # computation was canceled
return
if self.dataset is not None:
self.dataset.ramanscandone = False
self.dataset.ramanpoints = measurementpoints
self.dataset.particlecontours = contours
self.dataset.particlestats = particlestats
self.dataset.ramanscansortindex = []
numParticles = len(contours)
particleContainer = self.dataset.particleContainer
particleContainer.initializeParticles(numParticles)
particleContainer.setParticlecontours(contours)
particleContainer.setParticleStats(particlestats)
for particleIndex in measurementPoints:
measPoints = measurementPoints[particleIndex]
for index, point in enumerate(measPoints):
curParticle = particleContainer.particles[point.particleIndex]
curParticle.addEmptyMeasurement()
curParticle.setMeasurementPixelCoords(index, point.x, point.y)
# self.dataset.ramanpoints = measurementpoints
# self.dataset.particlecontours = contours
# self.dataset.particlestats = particlestats
# self.dataset.ramanscansortindex = []
self.dataset.mode = "prepareraman"
self.dataset.save()
......
......@@ -101,12 +101,12 @@ class MeasureParticleWindow(QtWidgets.QMainWindow):
fileName = QtWidgets.QFileDialog.getSaveFileName(self, "Create New Project",
defaultPath, "*.pkl")[0]
if fileName:
if fileName.find(' ') < 0:
self.fname = str(fileName)
self.view.new(self.fname)
self.scalingChanged(1.)
else:
QtWidgets.QMessageBox.critical(self, "Error", "File path must not contain spaces.")
# if fileName.find(' ') < 0:
self.fname = str(fileName) #TODO: No spaces for Renishaw Interface!!
self.view.new(self.fname)
self.scalingChanged(1.)
# else:
# QtWidgets.QMessageBox.critical(self, "Error", "File path must not contain spaces.")
@QtCore.pyqtSlot()
def about(self):
......@@ -283,7 +283,6 @@ class MeasureParticleWindow(QtWidgets.QMainWindow):
self.menuBar().addMenu(self.toolsMenu)
self.menuBar().addMenu(self.helpMenu)
def createToolBar(self):
self.toolbar = QtWidgets.QToolBar("Tools")
self.toolbar.setIconSize(QtCore.QSize(100,50))
......@@ -318,11 +317,10 @@ if __name__ == '__main__':
if not os.path.exists(logpath):
os.mkdir(logpath)
logname = os.path.join(logpath, 'logfile.txt')
fp = open(logname, "a")
sys.stderr = fp
sys.stdout = fp
#
# fp = open(logname, "a")
# sys.stderr = fp
# sys.stdout = fp
print("starting GEPARD at: " + strftime("%d %b %Y %H:%M:%S", localtime()),
flush=True)
......
......@@ -43,7 +43,7 @@ def scan(path, sol, zpositions, grid, controlclass, dataqueue,
sys.stderr = fp
sys.stdout = fp
except IOError:
print('separate loging failed', flush=True)
print('separate logging failed', flush=True)
pass
print('starting new optical scan', flush=True)
......
......@@ -162,9 +162,10 @@ class RamanScanUI(QtWidgets.QWidget):
def resetDataset(self, ds):
self.dataset = ds
if len(self.dataset.ramanpoints)>0:
numParticles = self.dataset.particleContainer.getNumberOfParticles()
if len(numParticles)>0:
self.prun.setEnabled(True)
self.setWindowTitle(str(len(ds.ramanpoints)) + " Particles")
self.setWindowTitle(str(numParticles) + " Particles")
@QtCore.pyqtSlot()
def stopScan(self):
......@@ -200,7 +201,7 @@ class RamanScanUI(QtWidgets.QWidget):
self.view.imparent.ramanSwitch.hide()
self.view.setMicroscopeMode()
points = np.asarray(self.dataset.ramanpoints)
points = np.asarray(self.dataset.particleContainer.getMeasurementPixelCoords())
ramanSettings = {'filename': self.dataset.name,
'numPoints': len(points),
'path': self.dataset.path}
......@@ -231,7 +232,10 @@ class RamanScanUI(QtWidgets.QWidget):
if reply == QtWidgets.QMessageBox.Yes:
self.dataset.mode = "ramanscan"
self.dataset.ramanscansortindex = cmin
measurements2particles = [[int(np.where(cmin == i)[0])] for i in range(len(cmin))]
self.dataset.particleContainer.setParticleMeasurementPoints(measurements2particles)
# self.dataset.ramanscansortindex = cmin
self.dataset.saveParticleData()
self.view.saveDataSet()
self.view.prepareAnalysis()
......
......@@ -41,6 +41,12 @@ class Parameter(object):
self.show = show
self.linkedParameter = linkedParameter
class MeasurementPoint(object):
def __init__(self, particleIndex, x, y):
self.particleIndex = particleIndex
self.x = x
self.y = y
class Segmentation(object):
def __init__(self, dataset=None, parent=None):
self.cancelcomputation = False
......@@ -272,7 +278,7 @@ class Segmentation(object):
return long, short, longellipse, shortellipse, cv2.contourArea(cnt)
def measurementPoints(self, binParticle, numPoints=1):
def getMeasurementPoints(self, binParticle, numPoints=1):
binParticle = cv2.copyMakeBorder(binParticle, 1, 1, 1, 1, 0)
dist = cv2.distanceTransform(np.uint8(binParticle), cv2.DIST_L2,3)
ind = np.argmax(dist)
......@@ -444,12 +450,12 @@ class Segmentation(object):
return None, None, None
particlestats = []
measurementpoints = []
measurementPoints = {}
tmpcontours = [contours[i] for i in range(len(contours)) if hierarchy[0,i,3]<0]
contours = []
for i, cnt in enumerate(tmpcontours):
for particleIndex, cnt in enumerate(tmpcontours):
label = markers[cnt[0,0,1],cnt[0,0,0]]
if label==0:
continue
......@@ -458,10 +464,14 @@ class Segmentation(object):
y0, y1 = cnt[:,0,1].min(), cnt[:,0,1].max()
subimg = (markers[y0:y1+1,x0:x1+1]).copy()
subimg[subimg!=label] = 0
y, x = self.measurementPoints(subimg)
y, x = self.getMeasurementPoints(subimg)
contours.append(cnt)
measurementPoints[particleIndex] = []
for index in range(0, len(x)):
measurementpoints.append([x[index] + x0, y[index] + y0])
newMeasPoint = MeasurementPoint(particleIndex, x[index] + x0, y[index] + y0)
measurementPoints[particleIndex].append(newMeasPoint)
# getMeasurementPoints.append([x[index] + x0, y[index] + y0])
print(len(np.unique(markers))-1, len(contours))
print("stats")
......@@ -472,4 +482,4 @@ class Segmentation(object):
tf = time()
print("particle detection took:", tf-t0, "seconds")
return measurementpoints, contours, particlestats
return measurementPoints, contours, particlestats
......@@ -120,10 +120,10 @@ class SegmentationContours(QtWidgets.QGraphicsItem):
combineActs = []
assignments = []
for index in self.selectedContours:
# partIndex = int(np.where(self.parent.dataset.ramanscansortindex == index)[0])
partIndex = index
assignments.append(self.parent.analysiswidget.datastats.particleResults[partIndex])
assignments.append(self.parent.dataset.particleContainer.particles[partIndex].getParticleAssignment()) #TODO: Entangle...
assignments.append("other")
for assignment in np.unique(np.array(assignments)):
combineActs.append(combineMenu.addAction(assignment))
......
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