test_methods.py 3.67 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 27 28 29 30 31 32 33 34 35 36 37 38 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#!/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

def get_default_particle_container(numParticles=1000):
    particleContainer = ParticleContainer(None)
    particleContainer.initializeParticles(numParticles)
    return particleContainer

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)
        ivlevaFraction, ivlevaParticles = ivlevaSampling.apply_subsampling_method()
        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]
        sizes.append(sorter.bins[-1]+1)   #the last bin, that goes until infinity
        
        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)

        
        
        
    
        
if __name__ == '__main__':
    unittest.main()