test_helpers.py 8.81 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
10
import helpers
Josef Brandt's avatar
Josef Brandt committed
11
import numpy as np
12 13
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
14
from PyQt5 import QtCore, QtGui
15 16
import gepard
from gepard.analysis.particleAndMeasurement import Particle
17
from gepard import dataset
18

Josef Brandt's avatar
Josef Brandt committed
19

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

29 30 31 32
        for upperBinLimit in self.bins:
            newParticle = Particle()
            newParticle.longSize = newParticle.shortSize = upperBinLimit - 1
            particleList.append(newParticle)
Josef Brandt's avatar
Josef Brandt committed
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
        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
68 69 70


class TestOther(unittest.TestCase):
Josef Brandt's avatar
Josef Brandt committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    def test_get_Anger_fraction(self):
        numParticles = helpers.get_Anger_fraction(1E6, sigma=1.65, mpFraction=0.05, errorMargin=0.1)
        self.assertEqual(numParticles, 5147)

        numParticles = helpers.get_Anger_fraction(1E6, sigma=1.65, mpFraction=0.005, errorMargin=0.1)
        self.assertEqual(numParticles, 51394)

        numParticles = helpers.get_Anger_fraction(1E6, sigma=1.65, mpFraction=0.0005, errorMargin=0.1)
        self.assertEqual(numParticles, 352428)

        numParticles = helpers.get_Anger_fraction(1E6, sigma=1.65, mpFraction=0.05, errorMargin=0.2)
        self.assertEqual(numParticles, 1292)

        numParticles = helpers.get_Anger_fraction(1E6, sigma=1.65, mpFraction=0.005, errorMargin=0.3)
        self.assertEqual(numParticles, 5984)

        numParticles = helpers.get_Anger_fraction(1E6, sigma=1.65, mpFraction=0.0005, errorMargin=0.3)
        self.assertEqual(numParticles, 57022)

90
    def test_box_overlaps_contour(self):
Josef Brandt's avatar
Josef Brandt committed
91 92 93 94
        boxXY: tuple = 0, 0
        boxWidthHeight: tuple = 10, 10

        contourPoints = np.array([[[0, 0]], [[5, 5]], [[3, 3]]])  # fully enclosed
95 96 97 98
        self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints))

        contourPoints = np.array([[[1, 1]], [[5, 5]], [[3, 3]]])  # fully enclosed
        self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints))
Josef Brandt's avatar
Josef Brandt committed
99

100 101 102 103
        contourPoints = np.array([[[-5, -5]], [[-1, 5]], [[-5, -10]]])  # outside the box
        self.assertFalse(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints))

        contourPoints = np.array([[[-2.5, 0]], [[5, 0]], [[5, 5]], [[-2.5, 5]]])  # inside more than 50 %
104
        self.assertTrue(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints))
Josef Brandt's avatar
Josef Brandt committed
105

106
        contourPoints = np.array([[[-2.5, 0]], [[2, 0]], [[2, 2]], [[-2.5, 2]]])  # inside less than 50 %
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        self.assertFalse(helpers.box_overlaps_contour(boxXY, boxWidthHeight, contourPoints))

    def test_get_overlapping_fraction(self):
        polygon1: QtGui.QPolygonF = QtGui.QPolygonF()
        polygon1.append(QtCore.QPointF(0, 0))
        polygon1.append(QtCore.QPointF(10, 0))
        polygon1.append(QtCore.QPointF(10, 10))
        polygon1.append(QtCore.QPointF(0, 10))

        polygon2: QtGui.QPolygonF = QtGui.QPolygonF()  # shifted +5 in x, so half of it should overlap
        polygon2.append(QtCore.QPointF(5, 0))
        polygon2.append(QtCore.QPointF(15, 0))
        polygon2.append(QtCore.QPointF(15, 10))
        polygon2.append(QtCore.QPointF(5, 10))
        self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 0.5)

        polygon2: QtGui.QPolygonF = QtGui.QPolygonF()  # made the second poly much larger, this shouldn't have an impact
        polygon2.append(QtCore.QPointF(5, 0))
        polygon2.append(QtCore.QPointF(100, 0))
        polygon2.append(QtCore.QPointF(100, 100))
        polygon2.append(QtCore.QPointF(5, 100))
        self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 0.5)

        polygon2: QtGui.QPolygonF = QtGui.QPolygonF()  # shifted another +5 in x, it should not overlap at all
        polygon2.append(QtCore.QPointF(15, 0))
        polygon2.append(QtCore.QPointF(15, 10))
        polygon2.append(QtCore.QPointF(25, 10))
        polygon2.append(QtCore.QPointF(25, 0))
        self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 0)

        polygon2: QtGui.QPolygonF = QtGui.QPolygonF()  # not the second poly completely encloses the first one
        polygon2.append(QtCore.QPointF(-100, -100))
        polygon2.append(QtCore.QPointF(100, -100))
        polygon2.append(QtCore.QPointF(100, 100))
        polygon2.append(QtCore.QPointF(-100, 100))
        self.assertEqual(helpers.get_overlapping_fraction(polygon1, polygon2), 1.0)

    def test_get_polygon_area(self):
        polygon1: QtGui.QPolygonF = QtGui.QPolygonF()
        polygon1.append(QtCore.QPointF(0, 0))
        polygon1.append(QtCore.QPointF(10, 0))
        polygon1.append(QtCore.QPointF(10, 10))
        polygon1.append(QtCore.QPointF(0, 10))
        self.assertEqual(helpers.get_polygon_area(polygon1), 100)

        polygon1: QtGui.QPolygonF = QtGui.QPolygonF()
        polygon1.append(QtCore.QPointF(0, 0))
        polygon1.append(QtCore.QPointF(20, 0))
        polygon1.append(QtCore.QPointF(20, 10))
        polygon1.append(QtCore.QPointF(0, 10))
        self.assertEqual(helpers.get_polygon_area(polygon1), 200)

        polygon1: QtGui.QPolygonF = QtGui.QPolygonF()
        polygon1.append(QtCore.QPointF(-10, 0))
        polygon1.append(QtCore.QPointF(10, 0))
        polygon1.append(QtCore.QPointF(10, 10))
        polygon1.append(QtCore.QPointF(-10, 10))
        self.assertEqual(helpers.get_polygon_area(polygon1), 200)


class TestDatasetOperations(unittest.TestCase):
    def setUp(self) -> None:
        self.dataset: dataset.DataSet = dataset.DataSet('test')
        self.dataset.imagescanMode = 'df'

    def test_get_filtersize(self):
        def setMaxDim():
            self.dataset.maxdim = minX + imgdim / 2, maxY - imgdim / 2, maxX - imgdim / 2, minY + imgdim / 2

        imgdim = 10
        self.dataset.imagedim_df = [imgdim, imgdim]

        minX, maxX, minY, maxY = 0, 10, 0, 10
        setMaxDim()
        offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
        self.assertEqual(diameter, 10)
        self.assertEqual(offset, (0, 0))
        self.assertEqual(widthHeight, [10, 10])

        minX, maxX, minY, maxY = -10, 10, -10, 10
        setMaxDim()
        offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
        self.assertEqual(diameter, 20)
        self.assertEqual(widthHeight, [20, 20])
        self.assertEqual(offset, (0, 0))

        minX, maxX, minY, maxY = 0, 20, 0, 10
        setMaxDim()
        offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
        self.assertEqual(diameter, 10)
        self.assertEqual(widthHeight, [20, 10])
        self.assertEqual(offset, (5, 0))

    def testconvert_microns_to_pixels(self):
        self.dataset.lastpos = [0, 0]  # in microns
        self.dataset.pixelscale_bf = 0.5  # micrometer / px
        self.dataset.imagedim_bf = [10, 10]  # in microns
        self.dataset.imagescanMode = 'bf'

        diameter = 10  # in microns
        widthHeight = [20, 10]  # in microns

        self.assertEqual(helpers.convert_length_to_pixels(self.dataset, diameter), 20)
        self.assertEqual(helpers.convert_length_to_pixels(self.dataset, widthHeight[0]), 40)
        self.assertEqual(helpers.convert_length_to_pixels(self.dataset, widthHeight[1]), 20)