test_methods.py 4.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/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
Josef Brandt's avatar
Josef Brandt committed
16
from methods import RandomSampling, SizeBinFractioning
17
import geometricMethods as gmeth
18 19
from helpers import ParticleBinSorter

Josef Brandt's avatar
Josef Brandt committed
20

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

Josef Brandt's avatar
Josef Brandt committed
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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
46
        sizes.append(sorter.bins[-1]+1)   # the last bin, that goes until infinity
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        
        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)
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 94 95 96 97 98 99 100 101 102 103 104 105


class TestMethodEquality(unittest.TestCase):
    def test_methodEquality(self):
        method1_1: RandomSampling = RandomSampling(None, 0.1)
        method1_2: RandomSampling = RandomSampling(None, 0.2)

        method2_1: SizeBinFractioning = SizeBinFractioning(None, 0.1)
        method2_2: SizeBinFractioning = SizeBinFractioning(None, 0.2)

        method3_1_1: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
        method3_1_1.numBoxesAcross = 3
        method3_1_2: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
        method3_1_2.numBoxesAcross = 5

        method3_2_1: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.2)
        method3_2_1.numBoxesAcross = 3
        method3_2_2: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.2)
        method3_2_2.numBoxesAcross = 5

        method4_1_1: gmeth.SpiralBoxSubsampling = gmeth.SpiralBoxSubsampling(None, 0.1)
        method4_1_1.numBoxes = 5
        method4_1_2: gmeth.SpiralBoxSubsampling = gmeth.SpiralBoxSubsampling(None, 0.1)
        method4_1_2.numBoxes = 10

        method4_2_1: gmeth.SpiralBoxSubsampling = gmeth.SpiralBoxSubsampling(None, 0.2)
        method4_2_1.numBoxes = 5
        method4_2_2: gmeth.SpiralBoxSubsampling = gmeth.SpiralBoxSubsampling(None, 0.2)
        method4_2_2.numBoxes = 10

        methods = [method1_1, method1_2, method2_1, method2_2, method3_1_1, method3_1_2,
                   method3_2_1, method3_2_2, method4_1_1, method4_1_2, method4_2_1, method4_2_2]

        for index1, method1 in enumerate(methods):
            for index2, method2 in enumerate(methods):
                if index1 == index2:
                    self.assertTrue(method1.equals(method2))
                else:
                    self.assertFalse(method1.equals(method2))