evaluation.py 1.88 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 22 13:57:28 2020

@author: luna
"""
from helpers import ParticleBinSorter

Josef Brandt's avatar
Josef Brandt committed
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class ResultComparer(object):

    def _get_mp_count_error_per_bin(self, allParticles, subParticles, fractionMeasured):
        binSorter = ParticleBinSorter()
        allParticlesInBins = binSorter.sort_particles_into_bins(allParticles)
        subParticlesInBins = binSorter.sort_particles_into_bins(subParticles)
        mpCountErrorsPerBin = []
        for allParticleBin, subParticleBin in zip(allParticlesInBins, subParticlesInBins):
            mpCountErrorsPerBin.append(self._get_mp_count_error(allParticleBin, subParticleBin, fractionMeasured))
        return binSorter.bins, mpCountErrorsPerBin
        
    def _get_mp_count_error(self, allParticles, subParticles, fractionMeasured): 
        numMPOrig = self._get_number_of_MP_particles(allParticles)
        numMPEstimate = self._get_number_of_MP_particles(subParticles) / fractionMeasured
        
        if numMPOrig != 0:
            mpCountError = self._get_error_from_values(numMPOrig, numMPEstimate)
        elif numMPEstimate == 0:
            mpCountError = 0
        else:
Josef Brandt's avatar
Josef Brandt committed
31
            raise Exception  # >0 particles in subsample, whereas none in entire sample. This cannot be!
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
            
        return mpCountError
    
    def _get_error_from_values(self, exact, estimate):
        assert(exact != 0)
        return abs(exact - estimate) / exact
    
    def _get_number_of_MP_particles(self, particleList):
        mpPatterns = ['poly', 'rubber', 'pb', 'pr', 'pg', 'py', 'pv']
        numMPParticles = 0
        
        for particle in particleList:
            assignment = particle.getParticleAssignment()
            for pattern in mpPatterns:
                if assignment.lower().find(pattern) != -1:
                    numMPParticles += 1
                    break

        return numMPParticles