particleAndMeasurement.py 5.81 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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
# -*- 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