particleAndMeasurement.py 5.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- 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
22
from viewitems import SegmentationContour, RamanScanIndicator
23
from .particleCharacterization import getParticleColor
24 25 26 27 28 29 30 31 32 33 34 35

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 = []
36 37
        self.color = None
        self.shape = None
38 39
    
    def addMeasurement(self, refToMeasurement):
40
        refToMeasurement.assignedParticle = self
41
        self.measurements.append(refToMeasurement)
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
    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
79
    
80 81 82 83 84 85 86 87
    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
88 89
        assert size is not None, f'Error, size of particle {self.index} is None'
        return size
90 91 92
    
    def getShortParticleSize(self):
        if not np.isnan(self.shortSize_ellipse):
93
            size = self.shortSize_ellipse
94
        elif not np.isnan(self.shortSize_box):
95
            size = self.shortSize_box
96 97 98
        else:
            print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
            raise ValueError
99 100
        assert size is not None, f'Error, short size of particle {self.index} is None'
        return size
101 102 103 104
    
    def getNumberOfMeasurements(self):
        return len(self.measurements)
    
JosefBrandt's avatar
JosefBrandt committed
105 106 107
    def getArea(self):
        return np.round(self.area)
    
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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)
    
123 124
    def recreateViewItem(self):
        self.viewItem = SegmentationContour()
125 126 127 128 129 130 131 132 133 134 135
            
    
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
136
        self.hqi = 0
137 138
        
        self.assignedParticle = None
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
    
    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