particleAndMeasurement.py 5.31 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
# -*- 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
27 28 29
        self.longSize = np.nan
        self.shortSize = np.nan
        self.height = None
30 31 32
        self.area = None
        self.contour = None
        self.measurements = []
33 34
        self.color = None
        self.shape = None
35
        self.wasManuallyEdited = False   #useful tag for extracting data that can now be considered reliable and obviously was classfiied wrong before by the algorithms
36 37
    
    def addMeasurement(self, refToMeasurement):
38
        refToMeasurement.assignedParticle = self
39
        self.measurements.append(refToMeasurement)
40

41 42 43 44 45 46
    def setAllSpectraToNewAssignment(self, newAssignment):
        for meas in self.measurements:
            meas.setAssignment(newAssignment)
            meas.setHQI(100)

    def getParticleAssignment(self):
47 48 49 50
        assignment = 'NotYetMeasured'
        if len(self.measurements) > 0:
            assignment = self.getMeasAssignmentWithHighestHQI()   #probably another method could be more suitable...
        return assignment
51 52
    
    def getHighestHQI(self):
53 54 55 56 57 58 59
        maxHQI = None
        if len(self.measurements) > 0:
            hqis = []
            for meas in self.measurements:
                hqis.append(meas.getHQI())
            maxHQI = max(hqis)
        return maxHQI
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    
    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]
JosefBrandt's avatar
JosefBrandt committed
83
    
84
    def getParticleSize(self):
85
        if np.isnan(self.longSize):
86 87
            print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
            raise ValueError
88 89
        else:
            return self.longSize
90 91
    
    def getShortParticleSize(self):
92
        if np.isnan(self.shortSize):
93 94
            print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
            raise ValueError
95 96
        else:
            return self.shortSize
97 98 99 100
    
    def getNumberOfMeasurements(self):
        return len(self.measurements)
    
JosefBrandt's avatar
JosefBrandt committed
101 102 103
    def getArea(self):
        return np.round(self.area)
    
104 105 106 107 108 109 110 111 112 113 114 115 116 117
    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)
118

119 120 121 122 123 124 125 126 127 128
    
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
JosefBrandt's avatar
JosefBrandt committed
129
        self.hqi = 0
130 131
        
        self.assignedParticle = None
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
    
    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