particleAndMeasurement.py 4.94 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 36
    
    def addMeasurement(self, refToMeasurement):
37
        refToMeasurement.assignedParticle = self
38
        self.measurements.append(refToMeasurement)
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
    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]
JosefBrandt's avatar
JosefBrandt committed
76
    
77
    def getParticleSize(self):
78
        if np.isnan(self.longSize):
79 80
            print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
            raise ValueError
81 82
        else:
            return self.longSize
83 84
    
    def getShortParticleSize(self):
85
        if np.isnan(self.shortSize):
86 87
            print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
            raise ValueError
88 89
        else:
            return self.shortSize
90 91 92 93
    
    def getNumberOfMeasurements(self):
        return len(self.measurements)
    
JosefBrandt's avatar
JosefBrandt committed
94 95 96
    def getArea(self):
        return np.round(self.area)
    
97 98 99 100 101 102 103 104 105 106 107 108 109 110
    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)
111

112 113 114 115 116 117 118 119 120 121
    
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
122
        self.hqi = 0
123 124
        
        self.assignedParticle = None
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
    
    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