test_geometricMethods.py 3.39 KB
Newer Older
1
import unittest
Josef Brandt's avatar
Josef Brandt committed
2 3
from geometricMethods import CrossBoxSelector

4 5 6 7 8 9 10 11 12 13

class TestSelectBoxes(unittest.TestCase):
    def setUp(self) -> None:
        self.crossBoxSelector = CrossBoxSelector(None)

    def test_get_topLeft_of_boxes(self):
        self.crossBoxSelector.filterWidth = self.crossBoxSelector.filterHeight = 100
        self.crossBoxSelector.fraction = 0.1

        self.crossBoxSelector.numBoxesAcross = 3
Josef Brandt's avatar
Josef Brandt committed
14
        topLeftCorners: list = self.crossBoxSelector.get_topLeft_of_boxes()
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        self.assertEqual(len(topLeftCorners), 5)

        self.crossBoxSelector.numBoxesAcross = 5
        topLeftCorners = self.crossBoxSelector.get_topLeft_of_boxes()
        self.assertEqual(len(topLeftCorners), 9)

    def test_get_tile_topLefts(self):
        self.crossBoxSelector.filterHeight = self.crossBoxSelector.filterWidth = 100
        self.crossBoxSelector.numBoxesAcross = 3
        maxBoxSize: float = self.crossBoxSelector.filterWidth/self.crossBoxSelector.numBoxesAcross
        horizontalTileStarts: list = self.crossBoxSelector._get_horizontal_box_starts(maxBoxSize)
        verticalTileStarts: list = self.crossBoxSelector._get_vertical_box_starts(maxBoxSize)
        self.assertEqual(horizontalTileStarts, [0, 100 / 3, 2 * 100 / 3])
        self.assertEqual(horizontalTileStarts, verticalTileStarts)

        halfBoxSize: float = maxBoxSize / 2
        horizontalTileStarts: list = self.crossBoxSelector._get_horizontal_box_starts(halfBoxSize)
        verticalTileStarts: list = self.crossBoxSelector._get_vertical_box_starts(halfBoxSize)
        expectedOffset = (maxBoxSize-halfBoxSize) / 2
        self.assertEqual(horizontalTileStarts, [0 + expectedOffset, 100/3 + expectedOffset, 2*100/3 + expectedOffset])
        self.assertEqual(horizontalTileStarts, verticalTileStarts)

        self.crossBoxSelector.numBoxesAcross = 5
        maxBoxSize = self.crossBoxSelector.filterWidth / self.crossBoxSelector.numBoxesAcross
        horizontalTileStarts = self.crossBoxSelector._get_horizontal_box_starts(maxBoxSize)
        verticalTileStarts: list = self.crossBoxSelector._get_vertical_box_starts(maxBoxSize)
        self.assertEqual(horizontalTileStarts, [0, 100/5, 2*100/5, 3*100/5, 4*100/5])
        self.assertEqual(horizontalTileStarts, verticalTileStarts)

        halfBoxSize: float = maxBoxSize / 2
        horizontalTileStarts: list = self.crossBoxSelector._get_horizontal_box_starts(halfBoxSize)
        verticalTileStarts: list = self.crossBoxSelector._get_vertical_box_starts(halfBoxSize)
        expectedOffset = (maxBoxSize - halfBoxSize) / 2
Josef Brandt's avatar
Josef Brandt committed
48 49
        self.assertEqual(horizontalTileStarts, [0+expectedOffset, 100/5+expectedOffset, 2*100/5+expectedOffset,
                                                3*100/5+expectedOffset, 4*100/5+expectedOffset])
50 51 52 53 54 55 56 57 58 59 60 61 62 63
        self.assertEqual(horizontalTileStarts, verticalTileStarts)

    def test_get_box_size(self) -> None:
        self.crossBoxSelector.filterHeight = self.crossBoxSelector.filterWidth = 100  # in pixel
        self.crossBoxSelector.fraction = 0.1
        self.crossBoxSelector.numBoxesAcross = 3

        filterArea: float = self.crossBoxSelector.filterHeight * self.crossBoxSelector.filterWidth
        totalBoxArea: float = filterArea*self.crossBoxSelector.fraction

        numBoxes: int = 2*self.crossBoxSelector.numBoxesAcross - 1
        areaPerBox: float = totalBoxArea / numBoxes
        boxSize: float = areaPerBox**0.5

Josef Brandt's avatar
Josef Brandt committed
64
        self.assertEqual(self.crossBoxSelector.boxSize, boxSize)