particleAndMeasurement.py 5.34 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
        self.area = None
        self.contour = None
Josef Brandt's avatar
Josef Brandt committed
32
        self.aperture = None
33
        self.measurements = []
34 35
        self.color = None
        self.shape = None
36
        self.wasManuallyEdited = False   #useful tag for extracting data that can now be considered reliable and obviously was classfiied wrong before by the algorithms
37 38
    
    def addMeasurement(self, refToMeasurement):
39
        refToMeasurement.assignedParticle = self
40
        self.measurements.append(refToMeasurement)
41

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

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

120 121 122 123 124 125 126 127 128 129
    
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
130
        self.hqi = 0
131 132
        
        self.assignedParticle = None
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
    
    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