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

@author: luna
"""
8 9
import pickle
import os
10 11
from helpers import ParticleBinSorter

Josef Brandt's avatar
Josef Brandt committed
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
class ResultObject(object):
    """
    An object the actually stores all generated results per sample and can update and report on them.
    """
    def __init__(self, filepath: str):
        super(ResultObject, self).__init__()
        self.filepath: str = filepath
        print(self.sampleName)

    # noinspection PyTypeChecker
    @property
    def sampleName(self) -> str:
        return os.path.basename(self.filepath).split('.')[0]


28

29 30 31


class ResultComparer(object):
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    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
50
            raise Exception  # >0 particles in subsample, whereas none in entire sample. This cannot be!
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            
        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

69
        return numMPParticles