Commit de30ddf1 authored by Josef Brandt's avatar Josef Brandt

RandomBoxSampling

parent c070f4ab
......@@ -43,6 +43,7 @@ def get_methods_to_test(dataset: dataset.DataSet, fractions: list = []) -> list:
boxCreator: gmeth.BoxSelectionCreator = gmeth.BoxSelectionCreator(dataset)
methods += boxCreator.get_crossBoxSubsamplers_for_fraction(fraction)
methods += boxCreator.get_spiralBoxSubsamplers_for_fraction(fraction)
methods += boxCreator.get_randomBoxSubsamplers_for_fraction(fraction)
# methods.append(cmeth.ChemometricSubsampling(particleContainer, fraction))
return methods
......
This diff is collapsed.
......@@ -4,7 +4,7 @@ sys.path.append("C://Users//xbrjos//Desktop//Python")
import gepard
from gepard import dataset
from gui.filterView import FilterView
from gui.measureModes import MeasureMode, CrossBoxMode, CrossBoxesControls, SpiralBoxMode
from gui.measureModes import *
import helpers
from evaluation import SubsamplingResult
......@@ -55,10 +55,15 @@ class MainView(QtWidgets.QWidget):
self._switch_to_default_mode()
def _add_measure_modes(self) -> None:
self.measureModes['spiralSelection'] = SpiralBoxMode(self.filterView)
self.measureModes['crossSelection'] = CrossBoxMode(self.filterView)
self.modeSelector.addItem('spiralSelection')
self.modeSelector.addItem('crossSelection')
self.measureModes['Spiral Box Selection'] = SpiralBoxMode(self.filterView)
self.measureModes['Cross Box Selection'] = CrossBoxMode(self.filterView)
self.measureModes['Random Box Selection'] = RandomBoxMode(self.filterView)
self.measureModes['Random Particle Selection'] = RandomMeasureMode(self.filterView)
self.modeSelector.addItem('Spiral Box Selection')
self.modeSelector.addItem('Cross Box Selection')
self.modeSelector.addItem('Random Box Selection')
self.modeSelector.addItem('Random Particle Selection')
for mode in self.measureModes.values():
mode.updatedResult.connect(self.infoWidget.update_results)
......@@ -101,8 +106,8 @@ class MainView(QtWidgets.QWidget):
self.filterView.filter.update_filterSize(width, height, diameter, (offsetx, offsety))
for mode in self.measureModes.values():
mode.boxSelectionMethod.particleContainer = dset.particleContainer
mode.boxSelectionMethod.offset = (offsetx, offsety)
mode.method.particleContainer = dset.particleContainer
mode.method.offset = (offsetx, offsety)
self.filterView.update_from_dataset(dset)
self.activeMode.update_measure_viewItems()
......
from PyQt5 import QtCore, QtWidgets
from gui.filterView import FilterView, MeasureBoxGraphItem
from geometricMethods import BoxSelectionSubsamplingMethod, CrossBoxSubSampling, SpiralBoxSubsampling
from methods import *
from geometricMethods import *
from evaluation import SubsamplingResult
class MeasureMode(QtCore.QObject):
updatedResult: QtCore.pyqtSignal = QtCore.pyqtSignal(SubsamplingResult)
def __init__(self, relatedFilterView: FilterView):
def __init__(self, relatedFilterView: FilterView) -> None:
super(MeasureMode, self).__init__()
self.filterView: FilterView = relatedFilterView
self.uiControls: QtWidgets.QGroupBox = QtWidgets.QGroupBox()
self.boxSelectionMethod: BoxSelectionSubsamplingMethod = None
self.subsamplingResult: SubsamplingResult = SubsamplingResult(self.boxSelectionMethod)
self.method: SubsamplingMethod = None
self.subsamplingResult: SubsamplingResult = SubsamplingResult(self.method)
self.subParticles: list = []
def get_control_groupBox(self) -> QtWidgets.QGroupBox:
return self.uiControls
def update_measure_viewItems(self) -> None:
raise NotImplementedError
self.method.filterDiameter = self.filterView.filter.diameter
self.method.numBoxes = self.uiControls.numBoxesSpinbox.value()
self.method.fraction = self.uiControls.coverageSpinbox.value() / 100
topLefts: list = self.method.get_topLeft_of_boxes()
boxSize = self.method.boxSize
self.filterView.update_measure_boxes(topLefts, boxSize)
self.send_measuredParticles_to_filterview()
def send_measuredParticles_to_filterview(self) -> None:
if self.boxSelectionMethod.particleContainer is not None:
subParticles = self.boxSelectionMethod.apply_subsampling_method()
self.subsamplingResult.method = self.boxSelectionMethod
if self.method.particleContainer is not None:
subParticles = self.method.apply_subsampling_method()
self.subsamplingResult.method = self.method
self.subsamplingResult.reset_results()
self.subsamplingResult.add_result(self.boxSelectionMethod.particleContainer.particles, subParticles)
self.subsamplingResult.add_result(self.method.particleContainer.particles, subParticles)
self.updatedResult.emit(self.subsamplingResult)
self.filterView.update_measured_particles(subParticles)
class RandomMeasureMode(MeasureMode):
updatedResult: QtCore.pyqtSignal = QtCore.pyqtSignal(SubsamplingResult)
def __init__(self, filterView: FilterView):
super(RandomMeasureMode, self).__init__(filterView)
self.method: RandomSampling = RandomSampling(None)
self.uiControls = ParticleModeControlGroup(self, 'Random Particle Measurement')
def update_measure_viewItems(self) -> None:
self.method.fraction = self.uiControls.coverageSpinbox.value() / 100
self.filterView.update_measure_boxes([], 0.0)
self.send_measuredParticles_to_filterview()
class ParticleModeControlGroup(QtWidgets.QGroupBox):
def __init__(self, measureModeParent: MeasureMode, title: str) -> None:
super(ParticleModeControlGroup, self).__init__()
self.measureModeParent = measureModeParent
self.setTitle(title)
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
layout.addWidget(QtWidgets.QLabel('Desired Coverage (%)'))
self.coverageSpinbox = QtWidgets.QSpinBox()
self.coverageSpinbox.setFixedWidth(50)
self.coverageSpinbox.setMinimum(0)
self.coverageSpinbox.setMaximum(100)
self.coverageSpinbox.setValue(10)
self.coverageSpinbox.valueChanged.connect(self._config_changed)
layout.addWidget(self.coverageSpinbox)
def _config_changed(self) -> None:
self.measureModeParent.update_measure_viewItems()
class CrossBoxMode(MeasureMode):
def __init__(self, *args):
super(CrossBoxMode, self).__init__(*args)
self.uiControls = CrossBoxesControls(self)
self.boxSelectionMethod: CrossBoxSubSampling = CrossBoxSubSampling(None)
self.method: CrossBoxSubSampling = CrossBoxSubSampling(None)
self.update_measure_viewItems()
def update_measure_viewItems(self) -> None:
self.boxSelectionMethod.filterDiameter = self.filterView.filter.diameter
self.boxSelectionMethod.numBoxesAcross = int(self.uiControls.numBoxesSelector.currentText())
self.method.filterDiameter = self.filterView.filter.diameter
self.method.numBoxesAcross = int(self.uiControls.numBoxesSelector.currentText())
desiredCoverage: int = self.uiControls.coverageSpinbox.value()
maxCoverage: int = int(self.boxSelectionMethod.get_maximum_achievable_fraction() * 100)
maxCoverage: int = int(self.method.get_maximum_achievable_fraction() * 100)
self.uiControls.set_to_max_possible_coverage(maxCoverage)
if desiredCoverage > maxCoverage:
desiredCoverage = maxCoverage
self.boxSelectionMethod.fraction = desiredCoverage / 100
self.method.fraction = desiredCoverage / 100
topLefts: list = self.boxSelectionMethod.get_topLeft_of_boxes()
boxSize = self.boxSelectionMethod.boxSize
topLefts: list = self.method.get_topLeft_of_boxes()
boxSize = self.method.boxSize
self.filterView.update_measure_boxes(topLefts, boxSize)
self.send_measuredParticles_to_filterview()
......@@ -96,33 +141,14 @@ class CrossBoxesControls(QtWidgets.QGroupBox):
self.coverageSpinbox.valueChanged.connect(self._config_changed)
class SpiralBoxMode(MeasureMode):
def __init__(self, *args):
super(SpiralBoxMode, self).__init__(*args)
self.uiControls: SpiralBoxControls = SpiralBoxControls(self)
self.boxSelectionMethod: SpiralBoxSubsampling = SpiralBoxSubsampling(None)
self.update_measure_viewItems()
def update_measure_viewItems(self) -> None:
self.boxSelectionMethod.filterDiameter = self.filterView.filter.diameter
self.boxSelectionMethod.numBoxes = self.uiControls.numBoxesSpinbox.value()
self.boxSelectionMethod.fraction = self.uiControls.coverageSpinbox.value() / 100
topLefts: list = self.boxSelectionMethod.get_topLeft_of_boxes()
boxSize = self.boxSelectionMethod.boxSize
self.filterView.update_measure_boxes(topLefts, boxSize)
self.send_measuredParticles_to_filterview()
class SpiralBoxControls(QtWidgets.QGroupBox):
class BoxControlGroup(QtWidgets.QGroupBox):
"""
Gives a groupbox with the controls for setting up the cross boxes.
"""
def __init__(self, measureModeParent: MeasureMode):
super(SpiralBoxControls, self).__init__()
self.setTitle('Spiral Box Controls')
def __init__(self, measureModeParent: MeasureMode, title: str) -> None:
super(BoxControlGroup, self).__init__()
self.setTitle(title)
self.measureModeParent = measureModeParent
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
......@@ -144,6 +170,38 @@ class SpiralBoxControls(QtWidgets.QGroupBox):
self.coverageSpinbox.valueChanged.connect(self._config_changed)
layout.addWidget(self.coverageSpinbox)
def _config_changed(self):
def _config_changed(self) -> None:
if self.numBoxesSpinbox.value() > 0:
numBoxes: int = self.numBoxesSpinbox.value()
self.measureModeParent.method.numBoxes = numBoxes
maxCoverage: float = self.measureModeParent.method.get_maximum_achievable_fraction()
self.set_to_max_possible_coverage(round(maxCoverage * 100))
self.measureModeParent.update_measure_viewItems()
def set_to_max_possible_coverage(self, maxCoverage: int) -> None:
"""
Adjusts maximum of coverage spinbox and, if necessary, caps the current value.
:param maxCoverage: the maximum pssible converage IN PERCENT!!
:return:
"""
self.coverageSpinbox.setMaximum(maxCoverage)
if maxCoverage < self.coverageSpinbox.value():
self.coverageSpinbox.valueChanged.disconnect()
self.coverageSpinbox.setValue(maxCoverage)
self.coverageSpinbox.valueChanged.connect(self._config_changed)
class SpiralBoxMode(MeasureMode):
def __init__(self, *args):
super(SpiralBoxMode, self).__init__(*args)
self.uiControls: BoxControlGroup = BoxControlGroup(self, 'Spiral Box Controls')
self.method: SpiralBoxSubsampling = SpiralBoxSubsampling(None)
self.update_measure_viewItems()
class RandomBoxMode(MeasureMode):
def __init__(self, *args):
super(RandomBoxMode, self).__init__(*args)
self.uiControls: BoxControlGroup = BoxControlGroup(self, 'Random Box Controls')
self.method: RandomBoxSampling = RandomBoxSampling(None)
self.update_measure_viewItems()
......@@ -11,6 +11,8 @@ from helpers import ParticleBinSorter
class SubsamplingMethod(object):
randomSeed = 15203018
def __init__(self, particleConatainer, desiredFraction: float = 0.2):
super(SubsamplingMethod, self).__init__()
self.particleContainer = particleConatainer
......@@ -65,6 +67,14 @@ class SubsamplingMethod(object):
matches = (self.label.lower().find(pattern.lower()) != -1)
return matches
def config_is_valid(self) -> bool:
isValid: bool = False
if self.fraction <= self.get_maximum_achievable_fraction():
isValid = True
return isValid
def get_maximum_achievable_fraction(self) -> float:
raise NotImplementedError
class RandomSampling(SubsamplingMethod):
@property
......@@ -82,6 +92,9 @@ class RandomSampling(SubsamplingMethod):
def equals(self, otherMethod) -> bool:
return type(otherMethod) == type(self) and otherMethod.fraction == self.fraction
def get_maximum_achievable_fraction(self) -> float:
return 1.0
class SizeBinFractioning(SubsamplingMethod):
......@@ -117,3 +130,6 @@ class SizeBinFractioning(SubsamplingMethod):
def equals(self, otherMethod) -> bool:
return type(otherMethod) == type(self) and otherMethod.fraction == self.fraction
def get_maximum_achievable_fraction(self) -> float:
return 1.0
\ No newline at end of file
......@@ -28,3 +28,4 @@ def get_default_ParticleContainer() -> ParticleContainer:
contours.append(np.array([[[x, 0]], [[x+10, 0]], [[x+10, 10]], [[x, 10]]], dtype=np.int32))
particleContainer.setParticleContours(contours)
return particleContainer
......@@ -258,14 +258,18 @@ class TestSampleResult(unittest.TestCase):
possibleRandomMethods = 2
possibleCrossBoxMethods = 2
possibleSpiralBoxMethods = 3
possibleRandomBoxMethods = 3
possibleChemometricMethods = 0
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, gmeth.CrossBoxSubSampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, gmeth.SpiralBoxSubsampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, gmeth.RandomBoxSampling(dset, desiredFraction)))
desiredFraction = 0.5
methods = get_methods_to_test(dset, [desiredFraction])
......@@ -273,8 +277,10 @@ class TestSampleResult(unittest.TestCase):
possibleCrossBoxMethods = 1
possibleSpiralBoxMethods = 0
possibleChemometricMethods = 0
possibleRandomBoxMethods = 0
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
......@@ -286,9 +292,11 @@ class TestSampleResult(unittest.TestCase):
possibleRandomMethods = 2
possibleCrossBoxMethods = 0
possibleSpiralBoxMethods = 0
possibleRandomBoxMethods = 0
possibleChemometricMethods = 0
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
......@@ -301,8 +309,10 @@ class TestSampleResult(unittest.TestCase):
possibleCrossBoxMethods = 3
possibleSpiralBoxMethods = 3
possibleChemometricMethods = 0
possibleRandomBoxMethods = 3
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
for desiredFraction in desiredFractions:
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
......@@ -492,6 +502,7 @@ class TestSubsamplingResult(unittest.TestCase):
return nonMPParticles
def _get_MP_particle(self) -> Particle:
random.seed(15203018)
polymerNames = ['Poly (methyl methacrylate',
'Polyethylene',
'Silicone rubber',
......
import sys
import unittest
import numpy as np
from geometricMethods import BoxSelectionSubsamplingMethod, CrossBoxSubSampling, SpiralBoxSubsampling, BoxSelectionCreator
from geometricMethods import *
sys.path.append("C://Users//xbrjos//Desktop//Python")
from gepard import dataset
class TestBoxSelector(unittest.TestCase):
def setUp(self) -> None:
self.boxSelector: BoxSelectionSubsamplingMethod = BoxSelectionSubsamplingMethod(None)
......@@ -39,6 +37,21 @@ class TestSelectCrossBoxes(unittest.TestCase):
def setUp(self) -> None:
self.crossBoxSubsampler = CrossBoxSubSampling(None)
def test_get_max_fraction(self):
self.crossBoxSubsampler.numBoxesAcross = 3
self.assertAlmostEqual(self.crossBoxSubsampler.get_maximum_achievable_fraction(), 0.5364176686762333)
self.crossBoxSubsampler.numBoxesAcross = 5
self.assertAlmostEqual(self.crossBoxSubsampler.get_maximum_achievable_fraction(), 0.3841550054130974)
def test_is_config_valid(self):
self.crossBoxSubsampler.numBoxesAcross = 3
self.crossBoxSubsampler.fraction = 0.5
self.assertTrue(self.crossBoxSubsampler.config_is_valid())
self.crossBoxSubsampler.fraction = 0.55
self.assertFalse(self.crossBoxSubsampler.config_is_valid())
def test_get_topLeft_of_boxes(self):
self.crossBoxSubsampler.filterDiameter = 100
self.crossBoxSubsampler.fraction = 0.1
......@@ -112,22 +125,36 @@ class TestSelectSpiralBoxes(unittest.TestCase):
self.assertEqual(newTopLefts[1], (0, 45))
self.assertEqual(newTopLefts[2], (90, 45))
def test_get_max_distance_of_boxCenter_to_center(self):
def test_get_max_fraction(self):
for numBoxes in self.spiralBoxSubsampler.possibleBoxNumbers:
self.spiralBoxSubsampler.numBoxes = numBoxes
maxFrac: float = determine_max_achievable_frac(self.spiralBoxSubsampler, numBoxes)
self.assertEqual(self.spiralBoxSubsampler.get_maximum_achievable_fraction(), maxFrac)
# now test for numBoxes that are not indicated in the possibleBoxNumbers class list.
# if assertion fails, please adjust values in below list to cover other numbers than tested before
for numBoxes in [1, 3, 6, 11]:
assert numBoxes not in self.spiralBoxSubsampler.possibleBoxNumbers
self.spiralBoxSubsampler.numBoxes = numBoxes
maxFrac: float = determine_max_achievable_frac(self.spiralBoxSubsampler, numBoxes)
self.assertEqual(self.spiralBoxSubsampler.get_maximum_achievable_fraction(), maxFrac)
def test_get_max_distance_of_box_to_center(self):
boxCenter = 0, 0
self._set_boxSelectorFraction_to_reach_boxSize(1)
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_boxCenter_to_center(boxCenter)
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_box_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(0.5**2 + 0.5**2))
self._set_boxSelectorFraction_to_reach_boxSize(2)
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_boxCenter_to_center(boxCenter)
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_box_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(2))
boxCenter = 1, 0
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_boxCenter_to_center(boxCenter)
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_box_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(2**2 + 1**2))
boxCenter = -1, -3
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_boxCenter_to_center(boxCenter)
maxDistance: float = self.spiralBoxSubsampler._get_max_distance_of_box_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(4**2 + 2**2))
def test_boxes_are_overlapping(self):
......@@ -161,6 +188,43 @@ class TestSelectSpiralBoxes(unittest.TestCase):
assert self.spiralBoxSubsampler.boxSize == desiredBoxSize
class TestRandomBoxes(unittest.TestCase):
def setUp(self) -> None:
self.randBoxes: RandomBoxSampling = RandomBoxSampling(None, 0.1)
self.randBoxes.maxTries = 10
def test_get_top_lefts(self):
self.randBoxes.update_max_fractions()
radius: float = self.randBoxes.filterDiameter/2
center: tuple = radius, radius
for frac in [0.1, 0.3, 0.5]:
self.randBoxes.fraction = frac
for numBoxes in self.randBoxes.possibleBoxNumbers:
self.randBoxes.numBoxes = numBoxes
if self.randBoxes.config_is_valid():
topLefts: list = self.randBoxes.get_topLeft_of_boxes()
self.assertEqual(len(topLefts), numBoxes)
boxSize: float = self.randBoxes.boxSize
for topLeft in topLefts:
points: list = [[topLeft[0], topLeft[1]], [topLeft[0], topLeft[1]+boxSize],
[topLeft[0]+boxSize, topLeft[1]], [topLeft[0]+boxSize, topLeft[1]+boxSize]]
for point in points:
dist = np.linalg.norm([point[0]-center[0], point[1]-center[1]])
self.assertTrue(point[0] >= 0 and point[1] >= 0)
self.assertTrue(dist <= radius)
def test_label(self):
self.assertTrue(type(self.randBoxes.label), str)
def test_get_max_fraction(self):
for numBoxes in self.randBoxes.possibleBoxNumbers:
self.randBoxes.numBoxes = numBoxes
maxFrac: float = determine_max_achievable_frac(self.randBoxes, numBoxes)
self.assertEqual(self.randBoxes.get_maximum_achievable_fraction(), maxFrac)
class TestBoxCreator(unittest.TestCase):
def setUp(self) -> None:
self.dataset: dataset.DataSet = dataset.DataSet('test')
......@@ -178,6 +242,8 @@ class TestBoxCreator(unittest.TestCase):
desiredFraction: float = 0.1
crossBoxSubsamplers: list = self.boxCreator.get_crossBoxSubsamplers_for_fraction(desiredFraction)
self._assert_correct_partCont(crossBoxSubsamplers)
self.assertEqual(len(crossBoxSubsamplers), 2)
numBoxesAcrossList: list = []
for boxSelector in crossBoxSubsamplers:
......@@ -190,25 +256,55 @@ class TestBoxCreator(unittest.TestCase):
self.assertTrue(5 in numBoxesAcrossList)
desiredFraction = 0.5 # only the version with 3 boxes works here
crossBoxSubsamplers: list = self.boxCreator.get_crossBoxSubsamplers_for_fraction(desiredFraction)
crossBoxSubsamplers = self.boxCreator.get_crossBoxSubsamplers_for_fraction(desiredFraction)
self._assert_correct_partCont(crossBoxSubsamplers)
self.assertEqual(len(crossBoxSubsamplers), 1)
self.assertEqual(crossBoxSubsamplers[0].numBoxesAcross, 3)
desiredFraction = 0.55 # no boxes work for that.
crossBoxSubsamplers: list = self.boxCreator.get_crossBoxSubsamplers_for_fraction(desiredFraction)
crossBoxSubsamplers = self.boxCreator.get_crossBoxSubsamplers_for_fraction(desiredFraction)
self.assertEqual(len(crossBoxSubsamplers), 0)
def test_get_spiralBoxSubsamplers_for_fraction(self):
desiredFraction: float = 0.1
spiralBoxSubsamplers = self.boxCreator.get_spiralBoxSubsamplers_for_fraction(desiredFraction)
self._assert_correct_partCont(spiralBoxSubsamplers)
self.assertTrue(len(spiralBoxSubsamplers) == len(SpiralBoxSubsampling.possibleBoxNumbers)) # all boxNumbers work here
for selector in spiralBoxSubsamplers:
self.assertEqual(selector.boxSize, self._getBoxSize(selector.numBoxes, desiredFraction))
desiredFraction: float = 0.5
desiredFraction: float = 0.4
spiralBoxSubsamplers = self.boxCreator.get_spiralBoxSubsamplers_for_fraction(desiredFraction)
self.assertTrue(len(spiralBoxSubsamplers) == 0) # no boxNumbers work here
def test_get_randBoxSubsampler_for_Fraction(self):
randBoxSampler: RandomBoxSampling = RandomBoxSampling(None)
randBoxSampler.update_max_fractions()
maxFracs: dict = randBoxSampler.maxFractions
for frac in np.linspace(0.1, 0.6, 5):
numValid: int = 0
validNumBoxes: list = []
for numBoxes in randBoxSampler.possibleBoxNumbers:
if maxFracs[numBoxes] >= frac:
numValid += 1
validNumBoxes.append(numBoxes)
possibleMehotds: list = self.boxCreator.get_randomBoxSubsamplers_for_fraction(frac)
self.assertEqual(len(possibleMehotds), numValid)
self._assert_correct_partCont(possibleMehotds)
for meth in possibleMehotds:
self.assertTrue(meth.numBoxes in validNumBoxes)
def _assert_correct_partCont(self, listOfMethods: list) -> None:
"""
Iterates through the list of subsampling methods and tests if the particle Container is set correctly
:param listOfMethods:
:return:
"""
for partCont in [sampler.particleContainer for sampler in listOfMethods]:
self.assertTrue(partCont is self.dataset.particleContainer)
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment