test_geometricMethods.py 9.97 KB
Newer Older
1
import sys
2
import unittest
Josef Brandt's avatar
Josef Brandt committed
3
import numpy as np
4
from geometricMethods import BoxSelectionSubsamplingMethod, CrossBoxSelector, SpiralSelector, BoxSelectionCreator
5 6 7

sys.path.append("C://Users//xbrjos//Desktop//Python")
from gepard import dataset
Josef Brandt's avatar
Josef Brandt committed
8

9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
class TestBoxSelector(unittest.TestCase):
    def setUp(self) -> None:
        self.boxSelector: BoxSelectionSubsamplingMethod = BoxSelectionSubsamplingMethod(None)

    def test_apply_offset_to_toplefts(self):
        topLefts = [(0, 0), (10, 10), (-5, -5), (0, 20)]
        self.boxSelector.offset = (0, 0)
        newTopLefts: list = self.boxSelector._apply_offset_to_toplefts(topLefts)
        self.assertEqual(newTopLefts[0], (0, 0))
        self.assertEqual(newTopLefts[1], (10, 10))
        self.assertEqual(newTopLefts[2], (-5, -5))
        self.assertEqual(newTopLefts[3], (0, 20))

        self.boxSelector.offset = (10, 0)
        newTopLefts: list = self.boxSelector._apply_offset_to_toplefts(topLefts)
        self.assertEqual(newTopLefts[0], (10, 0))
        self.assertEqual(newTopLefts[1], (20, 10))
        self.assertEqual(newTopLefts[2], (5, -5))
        self.assertEqual(newTopLefts[3], (10, 20))

        self.boxSelector.offset = (10, -20)
        newTopLefts: list = self.boxSelector._apply_offset_to_toplefts(topLefts)
        self.assertEqual(newTopLefts[0], (10, -20))
        self.assertEqual(newTopLefts[1], (20, -10))
        self.assertEqual(newTopLefts[2], (5, -25))
        self.assertEqual(newTopLefts[3], (10, 0))

Josef Brandt's avatar
Josef Brandt committed
37
class TestSelectCrossBoxes(unittest.TestCase):
38 39 40 41
    def setUp(self) -> None:
        self.crossBoxSelector = CrossBoxSelector(None)

    def test_get_topLeft_of_boxes(self):
42
        self.crossBoxSelector.filterDiameter = 100
43 44 45
        self.crossBoxSelector.fraction = 0.1

        self.crossBoxSelector.numBoxesAcross = 3
Josef Brandt's avatar
Josef Brandt committed
46
        topLeftCorners: list = self.crossBoxSelector.get_topLeft_of_boxes()
47 48 49 50 51 52 53
        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):
54
        self.crossBoxSelector.filterDiameter = 100
55
        self.crossBoxSelector.numBoxesAcross = 3
56
        maxBoxSize: float = self.crossBoxSelector.filterDiameter/self.crossBoxSelector.numBoxesAcross
57 58 59 60 61 62 63 64 65 66 67 68 69
        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
70
        maxBoxSize = self.crossBoxSelector.filterDiameter / self.crossBoxSelector.numBoxesAcross
71 72 73 74 75 76 77 78 79
        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
80 81
        self.assertEqual(horizontalTileStarts, [0+expectedOffset, 100/5+expectedOffset, 2*100/5+expectedOffset,
                                                3*100/5+expectedOffset, 4*100/5+expectedOffset])
82 83 84
        self.assertEqual(horizontalTileStarts, verticalTileStarts)

    def test_get_box_size(self) -> None:
85
        self.crossBoxSelector.filterDiameter = 100  # in pixel
86 87 88
        self.crossBoxSelector.fraction = 0.1
        self.crossBoxSelector.numBoxesAcross = 3

89
        filterArea: float = np.pi * (self.crossBoxSelector.filterDiameter/2)**2
90 91 92 93 94 95
        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
96
        self.assertEqual(self.crossBoxSelector.boxSize, boxSize)
Josef Brandt's avatar
Josef Brandt committed
97 98 99 100 101 102 103 104


class TestSelectSpiralBoxes(unittest.TestCase):
    def setUp(self) -> None:
        self.spiralBoxSelector: SpiralSelector = SpiralSelector(None)

    def test_move_and_scale_toplefts(self):
        self.spiralBoxSelector.filterHeight = 100
105
        self.spiralBoxSelector.filterDiameter = 100
Josef Brandt's avatar
Josef Brandt committed
106
        self._set_boxSelectorFraction_to_reach_boxSize(10)
Josef Brandt's avatar
Josef Brandt committed
107 108 109 110 111 112 113 114 115

        topLefts = [(45, 45), (0, 45), (90, 45)]
        newTopLefts = self.spiralBoxSelector._move_and_scale_toplefts(topLefts)
        self.assertEqual(newTopLefts[0], (45, 45))
        self.assertEqual(newTopLefts[1], (0, 45))
        self.assertEqual(newTopLefts[2], (90, 45))

    def test_get_max_distance_of_boxCenter_to_center(self):
        boxCenter = 0, 0
Josef Brandt's avatar
Josef Brandt committed
116
        self._set_boxSelectorFraction_to_reach_boxSize(1)
Josef Brandt's avatar
Josef Brandt committed
117 118 119
        maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
        self.assertEqual(maxDistance, np.sqrt(0.5**2 + 0.5**2))

Josef Brandt's avatar
Josef Brandt committed
120
        self._set_boxSelectorFraction_to_reach_boxSize(2)
Josef Brandt's avatar
Josef Brandt committed
121 122 123 124 125 126 127 128 129 130 131 132
        maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
        self.assertEqual(maxDistance, np.sqrt(2))

        boxCenter = 1, 0
        maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
        self.assertEqual(maxDistance, np.sqrt(2**2 + 1**2))

        boxCenter = -1, -3
        maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
        self.assertEqual(maxDistance, np.sqrt(4**2 + 2**2))

    def test_boxes_are_overlapping(self):
Josef Brandt's avatar
Josef Brandt committed
133
        self._set_boxSelectorFraction_to_reach_boxSize(10)
Josef Brandt's avatar
Josef Brandt committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

        topLefts: list = [(0, 0), (10, 10), (20, 10)]
        overlaps: bool = self.spiralBoxSelector._boxes_are_overlapping(topLefts)
        self.assertEqual(overlaps, False)

        topLefts: list = [(0, 0), (9, 10)]
        overlaps: bool = self.spiralBoxSelector._boxes_are_overlapping(topLefts)
        self.assertEqual(overlaps, False)

        topLefts: list = [(0, 0), (9, 9)]
        overlaps: bool = self.spiralBoxSelector._boxes_are_overlapping(topLefts)
        self.assertEqual(overlaps, True)

        topLefts: list = [(-2, 0), (5, 9)]
        overlaps: bool = self.spiralBoxSelector._boxes_are_overlapping(topLefts)
        self.assertEqual(overlaps, True)
150

Josef Brandt's avatar
Josef Brandt committed
151 152 153 154 155 156 157 158 159 160 161
    def _set_boxSelectorFraction_to_reach_boxSize(self, desiredBoxSize: float) -> None:
        """
        For convenience, the SpiralBoxSelector is configured to have the desired box Size by adjusting the fraction.
        :param desiredBoxSize:
        :return:
        """
        areaPerBox: float = desiredBoxSize**2
        totalBoxArea: float = areaPerBox * self.spiralBoxSelector.numBoxes
        self.spiralBoxSelector.fraction = totalBoxArea / self.spiralBoxSelector.filterArea
        assert self.spiralBoxSelector.boxSize == desiredBoxSize
        
162 163 164 165

class TestBoxCreator(unittest.TestCase):
    def setUp(self) -> None:
        self.dataset: dataset.DataSet = dataset.DataSet('test')
Josef Brandt's avatar
Josef Brandt committed
166 167 168 169 170 171
        self.dataset.imagescanMode = 'df'
        self.dataset.pixelscale_df = 1.0
        imgdim = 1000
        minX, maxX, minY, maxY = 0, 1000, 0, 1000  # These are the actual borders!
        self.dataset.maxdim = minX + imgdim / 2, maxY - imgdim / 2, maxX - imgdim / 2, minY + imgdim / 2
        self.dataset.imagedim_df = [imgdim, imgdim]
172

Josef Brandt's avatar
Josef Brandt committed
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 212
        self.boxCreator: BoxSelectionCreator = BoxSelectionCreator(self.dataset)
        self.filterArea: float = np.pi * 500**2  # the maxDims are 1000 in each direction, so radius is 500

    def test_get_crossBoxSelectors_for_fraction(self):
        desiredFraction: float = 0.1

        crossBoxSelectors: list = self.boxCreator.get_crossBoxSelectors_for_fraction(desiredFraction)
        self.assertEqual(len(crossBoxSelectors), 2)
        numBoxesAcrossList: list = []
        for boxSelector in crossBoxSelectors:
            numBoxesAcross = boxSelector.numBoxesAcross
            numBoxesAcrossList.append(numBoxesAcross)
            numBoxes = 2*numBoxesAcross - 1
            self.assertEqual(boxSelector.boxSize, self._getBoxSize(numBoxes, desiredFraction))

        self.assertTrue(3 in numBoxesAcrossList)
        self.assertTrue(5 in numBoxesAcrossList)

    def test_get_spiralBoxSelectors_for_fraction(self):
        desiredFraction: float = 0.1
        spiralBoxSelectors = self.boxCreator.get_spiralBoxSelectors_for_fraction(desiredFraction)
        self.assertTrue(len(spiralBoxSelectors) == len(SpiralSelector.possibleBoxNumbers))  # all boxNumbers work here
        for selector in spiralBoxSelectors:
            self.assertEqual(selector.boxSize, self._getBoxSize(selector.numBoxes, desiredFraction))

        desiredFraction: float = 0.5
        spiralBoxSelectors = self.boxCreator.get_spiralBoxSelectors_for_fraction(desiredFraction)
        self.assertTrue(len(spiralBoxSelectors) == 0)  # no boxNumbers work here

    def _getBoxSize(self, numBoxes: int, desiredFraction: float) -> float:
        """
        Calculates the size of each box (= width = height), given a certain filter area and number of Boxes
        :param numBoxes:
        :param desiredFraction:
        :return size (i.e. width = height) of each box:
        """
        totalBoxArea: float = self.filterArea * desiredFraction
        areaPerBox: float = totalBoxArea / numBoxes
        boxSize: float = areaPerBox ** 0.5
        return boxSize