test_helpers.py 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 19:35:04 2020

@author: luna
"""

import unittest
Josef Brandt's avatar
Josef Brandt committed
10 11
from helpers import ParticleBinSorter, box_contains_contour
import numpy as np
12 13 14 15 16
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
import gepard
from gepard.analysis.particleAndMeasurement import Particle

Josef Brandt's avatar
Josef Brandt committed
17

18 19 20 21 22 23 24
class TestBinSorter(unittest.TestCase):
    def setUp(self):
        self.sorter = ParticleBinSorter()
        self.bins = self.sorter.bins
        
    def test_sort_particles_into_bins(self):
        particleList = []
Josef Brandt's avatar
Josef Brandt committed
25 26
        upperBinLimit = None

27 28 29 30
        for upperBinLimit in self.bins:
            newParticle = Particle()
            newParticle.longSize = newParticle.shortSize = upperBinLimit - 1
            particleList.append(newParticle)
Josef Brandt's avatar
Josef Brandt committed
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
        lastParticle = Particle()
        lastParticle.longSize = lastParticle.shortSize = upperBinLimit + 1
        particleList.append(lastParticle)
        
        particlesInBins = self.sorter.sort_particles_into_bins(particleList)
        self.assertEqual(len(particlesInBins), len(self.bins)+1)
        for binContent in particlesInBins:
            self.assertEqual(len(binContent), 1)
            
    def test_get_empty_bins(self):
        emptyBins = self.sorter._get_empty_bins()
        self.assertEqual(len(emptyBins), len(self.bins)+1)
            
    def test_get_binIndex_of_particle(self):
        particle = Particle()
        particle.longSize = particle.shortSize = 0
        binIndex = self.sorter._get_binIndex_of_particle(particle)
        self.assertEqual(binIndex, 0)
        
        particle.longSize = particle.shortSize = 5
        binIndex = self.sorter._get_binIndex_of_particle(particle)
        self.assertEqual(binIndex, 0)
        
        particle.longSize = particle.shortSize = 5.01
        binIndex = self.sorter._get_binIndex_of_particle(particle)
        self.assertEqual(binIndex, 1)
        
        particle.longSize = particle.shortSize = 100
        binIndex = self.sorter._get_binIndex_of_particle(particle)
        self.assertEqual(binIndex, 4)
        
        particle.longSize = particle.shortSize = 1000
        binIndex = self.sorter._get_binIndex_of_particle(particle)
        self.assertEqual(binIndex, 7)
Josef Brandt's avatar
Josef Brandt committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80


class TestOther(unittest.TestCase):
    def test_box_contains_contour(self):
        boxXY: tuple = 0, 0
        boxWidthHeight: tuple = 10, 10

        contourPoints = np.array([[[0, 0]], [[5, 5]], [[3, 3]]])  # fully enclosed
        self.assertTrue(box_contains_contour(boxXY, boxWidthHeight, contourPoints))

        contourPoints = np.array([[[-5, -5]], [[0, 5]], [[-5, -10]]])  # only one point touches border
        self.assertTrue(box_contains_contour(boxXY, boxWidthHeight, contourPoints))

        contourPoints = np.array([[[-5, -5]], [[-1, 5]], [[-5, -10]]])  # outside the box
        self.assertFalse(box_contains_contour(boxXY, boxWidthHeight, contourPoints))