#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jan 28 19:35:04 2020 @author: luna """ import unittest from helpers import ParticleBinSorter, box_contains_contour import numpy as np import sys sys.path.append("C://Users//xbrjos//Desktop//Python") import gepard from gepard.analysis.particleAndMeasurement import Particle class TestBinSorter(unittest.TestCase): def setUp(self): self.sorter = ParticleBinSorter() self.bins = self.sorter.bins def test_sort_particles_into_bins(self): particleList = [] upperBinLimit = None for upperBinLimit in self.bins: newParticle = Particle() newParticle.longSize = newParticle.shortSize = upperBinLimit - 1 particleList.append(newParticle) 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) 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))