Commit ca5e6bad authored by Josef Brandt's avatar Josef Brandt

Reworking particle and analysis handling

Analyses are not primarily stored in each particle, but summarized in particleContainer. Each particle then only holds a reference to each assigned analysis..
parent a974f0be
......@@ -132,30 +132,30 @@ class SpectraPlot(QtWidgets.QGroupBox):
self.setLayout(layout)
def loadSpectraAndInitializeSpecPlot(self): #formerly updateData(self)....
def tryLoadingNumpySpecFile():
specPath = self.dataset.getSpectraFileName()
if os.path.exists(specPath):
return np.load(specPath)
else:
raise ImportError
try:
self.spectra = tryLoadingNumpySpecFile()
except ImportError:
fname = QtWidgets.QFileDialog.getOpenFileName(QtWidgets.QWidget(), 'Select Spectra File', self.dataset.path, 'text file (*.txt)')[0]
try:
self.spectra, spectraNames = importSpectra.importWITecSpectra(fname)
except ImportError:
try:
self.spectra, spectraNames = importSpectra.importRenishawSpectra(fname)
except ImportError:
self.spectra, spectraNames = importSpectra.importPerkinElmerSpectra(fname)
if self.spectra is None:
raise ImportError
else:
np.save(self.dataset.getSpectraFileName(), self.spectra)
# def tryLoadingNumpySpecFile():
# specPath = self.dataset.getSpectraFileName()
# if os.path.exists(specPath):
# return np.load(specPath)
# else:
# raise ImportError
# try:
# self.spectra = tryLoadingNumpySpecFile()
# except ImportError:
#
# fname = QtWidgets.QFileDialog.getOpenFileName(QtWidgets.QWidget(), 'Select Spectra File', self.dataset.path, 'text file (*.txt)')[0]
#
# try:
# self.spectra, spectraNames = importSpectra.importWITecSpectra(fname)
# except ImportError:
# try:
# self.spectra, spectraNames = importSpectra.importRenishawSpectra(fname)
# except ImportError:
# self.spectra, spectraNames = importSpectra.importPerkinElmerSpectra(fname)
#
# if self.spectra is None:
# raise ImportError
# else:
# np.save(self.dataset.getSpectraFileName(), self.spectra)
self.canvas.draw()
......
# -*- 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/>.
"""
import numpy as np
class Particle(object):
def __init__(self):
super(Particle, self).__init__()
self.index = None
self.longSize_ellipse = np.nan
self.shortSize_ellipse = np.nan
self.longSize_box = np.nan
self.shortSize_box = np.nan
self.area = None
self.contour = None
self.measurements = []
self.viewItem = None
def addMeasurement(self, refToMeasurement):
self.measurements.append(refToMeasurement)
# def addExistingMeasurement(self, meas):
# self.measurements.append(meas)
#
# def addEmptyMeasurement(self):
# self.measurements.append(Measurement())
# indexOfNewMeasurment = len(self.measurements)-1
# return indexOfNewMeasurment
# def setMeasurementScanIndex(self, indexOfMeasurment, scanIndex):
# self.measurements[indexOfMeasurment].ramanScanIndex = scanIndex
#
# def setMeasurementPixelCoords(self, indexOfMeasurment, x, y):
# self.measurements[indexOfMeasurment].pixelcoord_x= x
# self.measurements[indexOfMeasurment].pixelcoord_y = y
#
def setAllSpectraToNewAssignment(self, newAssignment):
for meas in self.measurements:
meas.setAssignment(newAssignment)
meas.setHQI(100)
def getParticleAssignment(self):
return self.getMeasAssignmentWithHighestHQI() #probably another method could be more suitable...
def getHighestHQI(self):
hqis = []
for meas in self.measurements:
hqis.append(meas.getHQI())
return max(hqis)
def getHQIOfMeasurementIndex(self, index):
for meas in self.measurements:
if meas.ramanScanIndex == index:
return meas.getHQI()
def getMeasurementIndices(self):
indices = []
for meas in self.measurements:
indices.append(meas.ramanScanIndex)
return indices
def getMeasurements(self):
return self.measurements
def getMeasAssignmentWithHighestHQI(self):
hqis = []
assignments = []
for meas in self.measurements:
hqis.append(meas.getHQI())
assignments.append(meas.getAssignment())
indexOfHighestHQI = hqis.index(max(hqis))
return assignments[indexOfHighestHQI]
def getParticleSize(self):
if not np.isnan(self.longSize_ellipse):
size = self.longSize_ellipse
elif not np.isnan(self.longSize_box):
size = self.longSize_box
else:
print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
raise ValueError
assert size is not None, f'Error, size or particle {self.index} is None'
return round(size)
def getShortParticleSize(self):
if not np.isnan(self.shortSize_ellipse):
return self.shortSize_ellipse
elif not np.isnan(self.shortSize_box):
return self.shortSize_box
else:
print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
raise ValueError
def getNumberOfMeasurements(self):
return len(self.measurements)
def measurementsHaveSameOrigAssignment(self):
allResults = [meas.getOrigAssignment() for meas in self.measurements]
if len(np.unique(allResults)) == 1:
return True
elif len(np.unique(allResults)) > 1:
return False
def getOrigMeasurementAssignments(self):
assignments = [meas.getOrigAssignment() for meas in self.measurements]
return assignments
def applyHQITresholdToMeasurements(self, minHQI):
for measurement in self.measurements:
measurement.applyHQIThreshold(minHQI)
# def recreateViewItem(self):
# pass
class Measurement(object):
def __init__(self):
super(Measurement, self).__init__()
self.ramanScanIndex = None
self.pixelcoord_x= None
self.pixelcoord_y = None
self.assignment_orig = 'Not Evaluated'
self.assignment_afterHQI = None
self.hqi = None
def setAssignment(self, assignment):
self.assignment_orig = assignment
self.applyHQIThreshold()
def setHQI(self, hqi):
self.hqi = hqi
self.applyHQIThreshold()
def applyHQIThreshold(self, minHQI=0):
if self.hqi is not None: #i.e. skip for initial setup, when hqi is not yet aplied...
if self.hqi >= minHQI:
self.assignment_afterHQI = self.assignment_orig
else:
self.assignment_afterHQI = 'unknown'
def getHQI(self):
return self.hqi
def getAssignment(self):
if self.assignment_afterHQI is None:
return self.assignment_orig
else:
return self.assignment_afterHQI
def getOrigAssignment(self):
return self.assignment_orig
def getScanIndex(self):
return self.ramanScanIndex
......@@ -20,17 +20,62 @@ If not, see <https://www.gnu.org/licenses/>.
"""
import numpy as np
import operator
import os
from PyQt5 import QtWidgets
from analysis import importSpectra
from analysis.particleAndMeasurement import Particle, Measurement
class ParticleContainer(object):
def __init__(self, datasetParent):
super(ParticleContainer, self).__init__()
self.datasetParent = datasetParent
self.particles = []
self.measurements = []
self.spectra = None
self.inconsistentParticles = []
self.typeHistogram = None
def addEmptyMeasurement(self):
self.measurements.append(Measurement())
indexOfNewMeas = len(self.measurements)-1
return indexOfNewMeas
def setMeasurementScanIndex(self, indexOfMeasurment, scanIndex):
self.measurements[indexOfMeasurment].ramanScanIndex = scanIndex
def setMeasurementPixelCoords(self, indexOfMeasurment, x, y):
self.measurements[indexOfMeasurment].pixelcoord_x = x
self.measurements[indexOfMeasurment].pixelcoord_y = y
def loadSpectra(self):
def tryLoadingNumpySpecFile():
specPath = self.datasetParent.getSpectraFileName()
if os.path.exists(specPath):
return np.load(specPath)
else:
raise ImportError
try:
self.spectra = tryLoadingNumpySpecFile()
except ImportError:
fname = QtWidgets.QFileDialog.getOpenFileName(QtWidgets.QWidget(), 'Select Spectra File', self.datasetParent.path, 'text file (*.txt)')[0]
try:
self.spectra, spectraNames = importSpectra.importWITecSpectra(fname)
except ImportError:
try:
self.spectra, spectraNames = importSpectra.importRenishawSpectra(fname)
except ImportError:
self.spectra, spectraNames = importSpectra.importPerkinElmerSpectra(fname)
if self.spectra is None:
raise ImportError
else:
np.save(self.datasetParent.getSpectraFileName(), self.spectra)
def initializeParticles(self, numParticles):
self.particles = []
for i in range(numParticles):
......@@ -246,150 +291,3 @@ class ParticleContainer(object):
for newIndex, particle in enumerate(self.particles):
particle.index = newIndex
class Particle(object):
def __init__(self):
super(Particle, self).__init__()
self.index = None
self.longSize_ellipse = np.nan
self.shortSize_ellipse = np.nan
self.longSize_box = np.nan
self.shortSize_box = np.nan
self.area = None
self.contour = None
self.measurements = []
self.viewItem = None
def addExistingMeasurement(self, meas):
self.measurements.append(meas)
def addEmptyMeasurement(self):
self.measurements.append(Measurement())
indexOfNewMeasurment = len(self.measurements)-1
return indexOfNewMeasurment
def setMeasurementScanIndex(self, indexOfMeasurment, scanIndex):
self.measurements[indexOfMeasurment].ramanScanIndex = scanIndex
def setMeasurementPixelCoords(self, indexOfMeasurment, x, y):
self.measurements[indexOfMeasurment].pixelcoord_x= x
self.measurements[indexOfMeasurment].pixelcoord_y = y
def setAllSpectraToNewAssignment(self, newAssignment):
for meas in self.measurements:
meas.setAssignment(newAssignment)
meas.setHQI(100)
def getParticleAssignment(self):
return self.getMeasAssignmentWithHighestHQI() #probably another method could be more suitable...
def getHighestHQI(self):
hqis = []
for meas in self.measurements:
hqis.append(meas.getHQI())
return max(hqis)
def getHQIOfMeasurementIndex(self, index):
for meas in self.measurements:
if meas.ramanScanIndex == index:
return meas.getHQI()
def getMeasurementIndices(self):
indices = []
for meas in self.measurements:
indices.append(meas.ramanScanIndex)
return indices
def getMeasurements(self):
return self.measurements
def getMeasAssignmentWithHighestHQI(self):
hqis = []
assignments = []
for meas in self.measurements:
hqis.append(meas.getHQI())
assignments.append(meas.getAssignment())
indexOfHighestHQI = hqis.index(max(hqis))
return assignments[indexOfHighestHQI]
def getParticleSize(self):
if not np.isnan(self.longSize_ellipse):
size = self.longSize_ellipse
elif not np.isnan(self.longSize_box):
size = self.longSize_box
else:
print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
raise ValueError
assert size is not None, f'Error, size or particle {self.index} is None'
return round(size)
def getShortParticleSize(self):
if not np.isnan(self.shortSize_ellipse):
return self.shortSize_ellipse
elif not np.isnan(self.shortSize_box):
return self.shortSize_box
else:
print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
raise ValueError
def getNumberOfMeasurements(self):
return len(self.measurements)
def measurementsHaveSameOrigAssignment(self):
allResults = [meas.getOrigAssignment() for meas in self.measurements]
if len(np.unique(allResults)) == 1:
return True
elif len(np.unique(allResults)) > 1:
return False
def getOrigMeasurementAssignments(self):
assignments = [meas.getOrigAssignment() for meas in self.measurements]
return assignments
def applyHQITresholdToMeasurements(self, minHQI):
for measurement in self.measurements:
measurement.applyHQIThreshold(minHQI)
# def recreateViewItem(self):
# pass
class Measurement(object):
def __init__(self):
super(Measurement, self).__init__()
self.ramanScanIndex = None
self.pixelcoord_x= None
self.pixelcoord_y = None
self.assignment_orig = 'Not Evaluated'
self.assignment_afterHQI = None
self.hqi = None
def setAssignment(self, assignment):
self.assignment_orig = assignment
self.applyHQIThreshold()
def setHQI(self, hqi):
self.hqi = hqi
self.applyHQIThreshold()
def applyHQIThreshold(self, minHQI=0):
if self.hqi is not None: #i.e. skip for initial setup, when hqi is not yet aplied...
if self.hqi >= minHQI:
self.assignment_afterHQI = self.assignment_orig
else:
self.assignment_afterHQI = 'unknown'
def getHQI(self):
return self.hqi
def getAssignment(self):
if self.assignment_afterHQI is None:
return self.assignment_orig
else:
return self.assignment_afterHQI
def getOrigAssignment(self):
return self.assignment_orig
def getScanIndex(self):
return self.ramanScanIndex
\ No newline at end of file
......@@ -260,6 +260,7 @@ class DataSet(object):
self.particleContainer.setParticleContours(self.particlecontours)
self.particleContainer.setParticleStats(self.particlestats)
self.particleContainer.applyPixelScaleToParticleStats(self.getPixelScale())
if hasattr(self, 'particles2spectra'):
if self.particles2spectra is not None:
measurements2particles = self.particles2spectra
......@@ -269,12 +270,16 @@ class DataSet(object):
measurements2particles = recreateMeasurement2ParticleFromScanIndices()
for particleIndex, listOfScanIndices in enumerate(measurements2particles):
curParticle = self.particleContainer.particles[particleIndex]
for measIndex, scanIndex in enumerate(listOfScanIndices):
curParticle.addEmptyMeasurement()
curParticle = self.particleContainer.getParticleOfIndex(particleIndex)
for scanIndex in listOfScanIndices:
# curParticle.addEmptyMeasurement()
# curParticle.setMeasurementPixelCoords(measIndex, x, y)
# curParticle.setMeasurementScanIndex(measIndex, scanIndex)
indexOfNewMeas = self.particleContainer.addEmptyMeasurement()
x, y = self.ramanpoints[particleIndex][0], self.ramanpoints[particleIndex][1]
curParticle.setMeasurementPixelCoords(measIndex, x, y)
curParticle.setMeasurementScanIndex(measIndex, scanIndex)
self.particleContainer.setMeasurementPixelCoords(indexOfNewMeas, x, y)
self.particleContainer.setMeasurementScanIndex(indexOfNewMeas, scanIndex)
curParticle.addMeasurement(self.particleContainer.measurements[indexOfNewMeas])
for particle in self.particleContainer.particles:
for meas in particle.measurements:
......
......@@ -667,9 +667,15 @@ class ParticleDetectionView(QtWidgets.QWidget):
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)
# curParticle = particleContainer.particles[point.particleIndex]
# curParticle.addEmptyMeasurement()
# curParticle.setMeasurementPixelCoords(index, point.x, point.y)
curParticle = particleContainer.getParticleOfIndex(point.particleIndex)
indexOfNewMeas = particleContainer.addEmptyMeasurement()
particleContainer.setMeasurementPixelCoords(index, point.x, point.y)
curParticle.addMeasurement(particleContainer.measurements[indexOfNewMeas])
self.dataset.ramanpoints = measurementPoints #consider moving that to particleContainer
# self.dataset.particlecontours = contours
......
......@@ -232,8 +232,16 @@ class RamanScanUI(QtWidgets.QWidget):
if reply == QtWidgets.QMessageBox.Yes:
self.dataset.mode = "ramanscan"
measurements2particles = [[int(np.where(cmin == i)[0])] for i in range(len(cmin))]
self.dataset.particleContainer.setParticleMeasurementPoints(measurements2particles)
measurements2particles = [[int(np.where(cmin == i)[0])] for i in range(len(cmin))] #i.e., list of scanIndicesPerParticle
for particleIndex, listOfScanIndices in enumerate(measurements2particles):
# curParticle = self.particleContainer.getParticleOfIndex(particleIndex)
for scanIndex in listOfScanIndices:
self.particleContainer.setMeasurementScanIndex(indexOfNewMeas, scanIndex)
# self.dataset.ramanscansortindex = cmin
self.dataset.saveParticleData()
......
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