test_methods.py 3.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 12:54:58 2020

@author: luna
"""

import unittest
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
import numpy as np
import gepard
from gepard.analysis.particleContainer import ParticleContainer
from gepard.analysis.particleAndMeasurement import Particle
from methods import IvlevaSubsampling, RandomSampling, SizeBinFractioning
from helpers import ParticleBinSorter

Josef Brandt's avatar
Josef Brandt committed
19

20 21 22 23 24
def get_default_particle_container(numParticles=1000):
    particleContainer = ParticleContainer(None)
    particleContainer.initializeParticles(numParticles)
    return particleContainer

Josef Brandt's avatar
Josef Brandt committed
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
class TestIvleva(unittest.TestCase):
    def test_get_ivleva_fraction(self):
        self.particleContainer = get_default_particle_container()
        
        ivlevaSampling = IvlevaSubsampling(self.particleContainer, sigma=1.65, mpFraction=0.05, errorMargin=0.1)
        numParticles = ivlevaSampling._get_ivleva_fraction(1E6)
        self.assertEqual(numParticles, 5147)
        
        ivlevaSampling = IvlevaSubsampling(self.particleContainer, sigma=1.65, mpFraction=0.005, errorMargin=0.1)
        numParticles = ivlevaSampling._get_ivleva_fraction(1E6)
        self.assertEqual(numParticles, 51394)
        
        ivlevaSampling = IvlevaSubsampling(self.particleContainer, sigma=1.65, mpFraction=0.0005, errorMargin=0.1)
        numParticles = ivlevaSampling._get_ivleva_fraction(1E6)
        self.assertEqual(numParticles, 352428)
    
        ivlevaSampling = IvlevaSubsampling(self.particleContainer, sigma=1.65, mpFraction=0.05, errorMargin=0.1)
        numParticles = ivlevaSampling._get_ivleva_fraction(1000)
44
        ivlevaParticles = ivlevaSampling.apply_subsampling_method()
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        self.assertEqual(len(ivlevaParticles), numParticles)


class TestRandomParticles(unittest.TestCase):
    def test_get_number_of_random_particles(self):
        randomSampling = RandomSampling(None, desiredFraction=0.1)
        numParticles = randomSampling._get_number_of_random_particles(1000)
        self.assertEqual(numParticles, 100)
        
        numParticles = randomSampling._get_number_of_random_particles(10000)
        self.assertEqual(numParticles, 1000)
        
        randomSampling = RandomSampling(None, desiredFraction=0.5)
        numParticles = randomSampling._get_number_of_random_particles(1000)
        self.assertEqual(numParticles, 500)
        

class TestSizeBinFractioning(unittest.TestCase):
    def setUp(self):
        self.sizeBinFrac = SizeBinFractioning(None)
        sorter = ParticleBinSorter()
        sizes = [limit-1 for limit in sorter.bins]
Josef Brandt's avatar
Josef Brandt committed
67
        sizes.append(sorter.bins[-1]+1)   # the last bin, that goes until infinity
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        
        self.numMPparticlesPerBin = 10
        self.particles = []
        
        for size in sizes:
            for _ in range(self.numMPparticlesPerBin):
                newParticle = Particle()
                newParticle.longSize = newParticle.shortSize = size
                self.particles.append(newParticle)
        
    def test_get_num_subParticles_per_bin(self):
        for fraction in [0.01, 0.1, 0.2, 0.5, 0.95, 0.99]:
            self.sizeBinFrac.fraction = fraction
            numParticlesPerBinExpected: int = np.round(self.sizeBinFrac.fraction * self.numMPparticlesPerBin)
            if numParticlesPerBinExpected == 0:
                numParticlesPerBinExpected = 1
            
            subParticlesPerBin: list = self.sizeBinFrac._get_subParticles_per_bin(self.particles)
            for subParticles in subParticlesPerBin:
                self.assertEqual(len(subParticles), numParticlesPerBinExpected)

Josef Brandt's avatar
Josef Brandt committed
89

90
if __name__ == '__main__':
Josef Brandt's avatar
Josef Brandt committed
91
    unittest.main()