test_methods.py 6.7 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


class TestMethodEquality(unittest.TestCase):
70
    def test_methodEquality_and_patterns(self):
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
        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))
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

        randomPatterns: list = ['random', 'ranDOm']
        sizeBinPatterns: list = ['size', 'Size', 'sizeBin']
        crossBoxPatterns: list = ['cross', 'crossLayout']
        spiralBoxPatterns: list = ['spiral', 'spiralLayout']
        antiPatterns: list = ['bin', 'box', 'crossBox', 'layout']  # pattern 'layout' is ambiguous...

        for randomMeth in [method1_1, method1_2]:
            for pos in randomPatterns:
                self.assertTrue(randomMeth.matches_pattern(pos))
            negPatterns = sizeBinPatterns + crossBoxPatterns + spiralBoxPatterns + antiPatterns
            for neg in negPatterns:
                self.assertFalse(randomMeth.matches_pattern(neg))
            self.assertTrue(randomMeth.matches_any_pattern(randomPatterns + negPatterns))
            self.assertFalse(randomMeth.matches_any_pattern(negPatterns))

        for sizeBinMeth in [method2_1, method2_2]:
            for pos in sizeBinPatterns:
                self.assertTrue(sizeBinMeth.matches_pattern(pos))
            negPatterns = randomPatterns + crossBoxPatterns + spiralBoxPatterns + antiPatterns
            for neg in negPatterns:
                self.assertFalse(sizeBinMeth.matches_pattern(neg))
            self.assertTrue(sizeBinMeth.matches_any_pattern(sizeBinPatterns + negPatterns))
            self.assertFalse(sizeBinMeth.matches_any_pattern(negPatterns))

        for crossBoxMethod in [method3_1_1, method3_1_2, method3_2_1, method3_2_2]:
            for pos in crossBoxPatterns:
                self.assertTrue(crossBoxMethod.matches_pattern(pos))
            negPatterns = randomPatterns + sizeBinPatterns + spiralBoxPatterns + antiPatterns
            for neg in negPatterns:
                self.assertFalse(crossBoxMethod.matches_pattern(neg))
            self.assertTrue(crossBoxMethod.matches_any_pattern(crossBoxPatterns + negPatterns))
            self.assertFalse(crossBoxMethod.matches_any_pattern(negPatterns))

        for spiralBoxMethod in [method4_1_1, method4_1_2, method4_2_1, method4_2_2]:
            for pos in spiralBoxPatterns:
                self.assertTrue(spiralBoxMethod.matches_pattern(pos))
            negPatterns = randomPatterns + sizeBinPatterns + crossBoxPatterns + antiPatterns
            for neg in negPatterns:
                self.assertFalse(spiralBoxMethod.matches_pattern(neg))
            self.assertTrue(spiralBoxMethod.matches_any_pattern(spiralBoxPatterns + negPatterns))
            self.assertFalse(spiralBoxMethod.matches_any_pattern(negPatterns))