Commit 0553ff15 authored by Josef Brandt's avatar Josef Brandt

First Stage of Refactoring..

parent 064d26eb
This diff is collapsed.
......@@ -90,13 +90,8 @@ class SpiralBoxMode(MeasureMode):
def update_measure_viewItems(self) -> None:
self.spiralBoxGenerator.filterDiameter = self.filterView.filter.diameter
self.spiralBoxGenerator.boxSize = self.uiControls.boxSizeSpinbox.value()
# minBoxSize: float = self.spiralBoxGenerator.filterDiameter*0.1
# if self.spiralBoxGenerator.boxSize < minBoxSize:
# self.spiralBoxGenerator.boxSize = minBoxSize
# self.uiControls.boxSizeSpinbox.setValue(int(round(minBoxSize)))
self.spiralBoxGenerator.numBoxes = self.uiControls.numBoxesSpinbox.value()
self.spiralBoxGenerator.fraction = self.uiControls.coverageSpinbox.value() / 100
topLefts: list = self.spiralBoxGenerator.get_topLeft_of_boxes()
boxSize = self.spiralBoxGenerator.boxSize
......@@ -115,20 +110,23 @@ class SpiralBoxControls(QtWidgets.QGroupBox):
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
layout.addWidget(QtWidgets.QLabel('Box Size:'))
self.boxSizeSpinbox = QtWidgets.QSpinBox()
self.boxSizeSpinbox.setValue(50)
self.boxSizeSpinbox.setMaximum(10000)
self.boxSizeSpinbox.valueChanged.connect(self._config_changed)
layout.addWidget(self.boxSizeSpinbox)
layout.addStretch()
layout.addWidget(QtWidgets.QLabel('Num Boxes:'))
self.numBoxesSpinbox = QtWidgets.QSpinBox()
self.numBoxesSpinbox.setValue(10)
self.numBoxesSpinbox.valueChanged.connect(self._config_changed)
layout.addWidget(self.numBoxesSpinbox)
layout.addStretch()
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):
self.measureModeParent.update_measure_viewItems()
if self.numBoxesSpinbox.value() > 0:
self.measureModeParent.update_measure_viewItems()
......@@ -16,6 +16,14 @@ class SubsamplingMethod(object):
self.particleContainer = particleConatainer
self.fraction = desiredFraction
@property
def label(self) -> str:
"""
A specific label that can be used for plots, for instance.
:return:
"""
raise NotImplementedError
def apply_subsampling_method(self) -> tuple:
"""
Takes all particles from the supplied particle conatiner and returns a new list of particles that
......@@ -27,6 +35,10 @@ class SubsamplingMethod(object):
class RandomSampling(SubsamplingMethod):
@property
def label(self) -> str:
return 'Random Subsampling'
def apply_subsampling_method(self):
numOrigParticles = len(self.particleContainer.particles)
numParticles = self._get_number_of_random_particles(numOrigParticles)
......@@ -43,7 +55,11 @@ class IvlevaSubsampling(SubsamplingMethod):
self.sigma = sigma
self.estimatedMPFraction = mpFraction
self.errorMargin = errorMargin
@property
def label(self) -> str:
return 'Random fraction, Anger et al.'
def apply_subsampling_method(self):
N = self.particleContainer.getNumberOfParticles()
numParticlesMeasured = self._get_ivleva_fraction(N)
......@@ -62,7 +78,11 @@ class SizeBinFractioning(SubsamplingMethod):
def __init__(self, particleConatiner, desiredfraction: float = 0.2):
super(SizeBinFractioning, self).__init__(particleConatiner, desiredfraction)
self.sorter: ParticleBinSorter = ParticleBinSorter()
@property
def label(self) -> str:
return 'SizeBin Random Subsampling'
def apply_subsampling_method(self):
subParticlesPerBin: list = self._get_subParticles_per_bin(self.particleContainer.particles)
subParticles: list = []
......
......@@ -4,26 +4,37 @@ import time
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
from gepard import dataset
import gepardevaluation
from methods import IvlevaSubsampling, RandomSampling, SizeBinFractioning
from geometricMethods import BoxSelectionCreator
from helpers import ParticleBinSorter
from evaluation import ResultComparer
fname: str = r'C:\Users\xbrjos\Desktop\temp MP\190313_Soil_5_A_50_5_1_50_1\190313_Soil_5_A_50_5_1_50_1.pkl'
# fname: str = r'C:\Users\xbrjos\Desktop\temp MP\190326_MCII_WWTP_SB_50_2\190326_MCII_WWTP_SB_50_2.pkl'
# fname: str = r'C:\Users\xbrjos\Desktop\temp MP\190326_MCII_WWTP_SB_50_1\190326_MCII_WWTP_SB_50_1.pkl'
# fname: str = r'C:\Users\xbrjos\Desktop\temp MP\KWS_CT_3_ds1_all_10_2\KWS_CT_3_ds1_all_10_2.pkl' #legacy convert not working..
# fname: str = r'C:\Users\xbrjos\Desktop\temp MP\190201_BSB_Stroomi_ds2_R1_R2_50\190201_BSB_Stroomi_ds2_R1_R2_50.pkl' #zvalues image missing, legacy convert fails..
"""
IMPORTANT!!!
SET GEPARD TO EVALUATION BRANCH (WITHOUT THE TILING STUFF), OTHERWISE SOME OF THE LEGACY CONVERTS MIGHT FAIL..
"""
workingFiles: list = []
workingFiles.append(r'C:\Users\xbrjos\Desktop\temp MP\190313_Soil_5_A_50_5_1_50_1\190313_Soil_5_A_50_5_1_50_1.pkl')
workingFiles.append(r'C:\Users\xbrjos\Desktop\temp MP\181018_Microcatch-St.6_50um\181018_Microcatch-St.6_50um.pkl')
workingFiles.append(r'C:\Users\xbrjos\Desktop\temp MP\190326_MCII_WWTP_SB_50_2\190326_MCII_WWTP_SB_50_2.pkl')
workingFiles.append(r'C:\Users\xbrjos\Desktop\temp MP\190326_MCII_WWTP_SB_50_1\190326_MCII_WWTP_SB_50_1.pkl')
workingFiles.append(r'C:\Users\xbrjos\Desktop\temp MP\190201_BSB_Stroomi_ds2_R1_R2_50\190201_BSB_Stroomi_ds2_R1_R2_50.pkl')
dset = dataset.loadData(fname)
print('loaded dataset')
# These do not work, due to no ramanscansortindex??
# fname: str = r'C:\Users\xbrjos\Desktop\temp MP\KWS_CT_3_ds1_all_10_2\KWS_CT_3_ds1_all_10_2.pkl'
# fname: str = r'C:\Users\xbrjos\Desktop\temp MP\KWS_CT_3_ds1_all_10_2\KWS_CT_3_ds1_all_10_2.pkl'
boxCreator = BoxSelectionCreator(dset)
center, size = boxCreator.get_filterDimensions_from_dataset()
print(center, size)
print(dset.mapToPixel(center, force=True))
for index, fname in enumerate(workingFiles):
dset = dataset.loadData(fname)
print('loaded dataset', fname)
# boxCreator = BoxSelectionCreator(dset)
# center, size = boxCreator.get_filterDimensions_from_dataset()
# print(center, size)
# print(dset.mapToPixel(center, force=True))
# pc = dset.particleContainer
# origParticles = pc.particles
......
......@@ -76,7 +76,7 @@ class TestSelectSpiralBoxes(unittest.TestCase):
def test_move_and_scale_toplefts(self):
self.spiralBoxSelector.filterHeight = 100
self.spiralBoxSelector.filterDiameter = 100
self.spiralBoxSelector.boxSize = 10
self._set_boxSelectorFraction_to_reach_boxSize(10)
topLefts = [(45, 45), (0, 45), (90, 45)]
newTopLefts = self.spiralBoxSelector._move_and_scale_toplefts(topLefts)
......@@ -86,26 +86,24 @@ class TestSelectSpiralBoxes(unittest.TestCase):
def test_get_max_distance_of_boxCenter_to_center(self):
boxCenter = 0, 0
self.spiralBoxSelector.boxSize = 1
self._set_boxSelectorFraction_to_reach_boxSize(1)
maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(0.5**2 + 0.5**2))
self.spiralBoxSelector.boxSize = 2
self._set_boxSelectorFraction_to_reach_boxSize(2)
maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(2))
boxCenter = 1, 0
self.spiralBoxSelector.boxSize = 2
maxDistance: float = self.spiralBoxSelector._get_max_distance_of_boxCenter_to_center(boxCenter)
self.assertEqual(maxDistance, np.sqrt(2**2 + 1**2))
boxCenter = -1, -3
self.spiralBoxSelector.boxSize = 2
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):
self.spiralBoxSelector.boxSize = 10
self._set_boxSelectorFraction_to_reach_boxSize(10)
topLefts: list = [(0, 0), (10, 10), (20, 10)]
overlaps: bool = self.spiralBoxSelector._boxes_are_overlapping(topLefts)
......@@ -123,37 +121,65 @@ class TestSelectSpiralBoxes(unittest.TestCase):
overlaps: bool = self.spiralBoxSelector._boxes_are_overlapping(topLefts)
self.assertEqual(overlaps, True)
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
class TestBoxCreator(unittest.TestCase):
def setUp(self) -> None:
self.dataset: dataset.DataSet = dataset.DataSet('test')
self.boxCreator: BoxSelectionCreator = BoxSelectionCreator(self.dataset)
# def test_get_crossBoxSelectors_for_fraction(self):
# def getBoxSize(numBoxes):
# totalBoxArea: float = filterArea * desiredFraction
# areaPerBox: float = totalBoxArea / numBoxes
# boxSize: float = areaPerBox ** 0.5
# return boxSize
#
# self.dataset.lastpos = [0, 0]
# self.dataset.boundary = np.array([[0, 0], [0, 10], [10, 0], [10, 10]])
# self.dataset.imagescanMode = 'df'
# self.dataset.pixelscale_df = 1.0
# self.dataset.imagedim_df = [10, 10]
#
# filterArea: float = np.pi * 5**2
#
# desiredFraction: float = 0.1
# expectedResults: dict = {} # key: numBoxesAcross, value: boxSize
# for numBoxesAcross in [3, 5]:
# expectedResults[numBoxesAcross] = getBoxSize(2*numBoxesAcross - 1)
#
# crossBoxSelectors: list = self.boxCreator.get_crossBoxSelectors_for_fraction(desiredFraction)
# self.assertEqual(len(crossBoxSelectors), 2)
# for boxSelector in crossBoxSelectors:
# self.assertEqual(expectedResults[boxSelector.numBoxesAcross], boxSelector.boxSize)
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]
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
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