Commit d23662e8 authored by Josef Brandt's avatar Josef Brandt

QuarterRandomBoxSampling

parent 3ea53e47
......@@ -32,7 +32,7 @@ def get_methods_to_test(dataset: dataset.DataSet, fractions: list = []) -> list:
:return: list of measurement Objects that are applicable
"""
if len(fractions) == 0:
fractions: list = [0.02, 0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 0.9]
fractions: list = [0.05, 0.1]
methods: list = []
particleContainer = dataset.particleContainer
......@@ -44,6 +44,7 @@ def get_methods_to_test(dataset: dataset.DataSet, fractions: list = []) -> list:
methods += boxCreator.get_crossBoxSubsamplers_for_fraction(fraction)
methods += boxCreator.get_spiralBoxSubsamplers_for_fraction(fraction)
methods += boxCreator.get_randomBoxSubsamplers_for_fraction(fraction)
methods += boxCreator.get_randomQuarterBoxSubsamplers_for_fraction(fraction)
# methods.append(cmeth.ChemometricSubsampling(particleContainer, fraction))
return methods
......@@ -55,6 +56,7 @@ def update_sample(sample, force: bool, index: int):
sample.update_result_with_methods(methods, force)
return sample, index
def is_MP_particle(particle: Particle) -> bool:
# TODO: UPDATE PATTERNS -> ARE THESE REASONABLE???
isMP: bool = False
......@@ -243,9 +245,9 @@ class SubsamplingResult(object):
class SampleResult(object):
"""
An object the actually stores all generated results per sample and can update and report on them.
An object the stores all generated results per sample and can update and report on them.
"""
def __init__(self, filepath: str, numVariations: int = 10):
def __init__(self, filepath: str, numVariations: int = 1):
super(SampleResult, self).__init__()
self.filepath: str = filepath
self.dataset: dataset.DataSet = None
......@@ -345,6 +347,3 @@ class SampleResult(object):
requestedResult = result
break
return requestedResult
# def _get_result_of_method(self, method: meth.SubsamplingMethod) -> SubsamplingResult:
# return None
\ No newline at end of file
......@@ -62,7 +62,6 @@ class BoxSelectionSubsamplingMethod(SubsamplingMethod):
for topLeftXY in sortedTopLefts:
if helpers.box_overlaps_contour(topLeftXY, boxWidthHeight, particle.contour):
subParticles.append(particle)
break
return subParticles
......@@ -136,10 +135,7 @@ class BoxSelectionCreator(object):
:return list of CrossBoxSubsamplers:
"""
crossBoxSubsamplers = []
offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
diameter = helpers.convert_length_to_pixels(self.dataset, diameter)
offset = helpers.convert_length_to_pixels(self.dataset, offset[0]), \
helpers.convert_length_to_pixels(self.dataset, offset[1])
diameter, offset = self._get_diameter_and_offset()
for numBoxesAcross in [3, 5]:
newBoxSelector: CrossBoxSubSampling = CrossBoxSubSampling(self.dataset.particleContainer, desiredFraction)
......@@ -159,10 +155,7 @@ class BoxSelectionCreator(object):
:return list of SpiralBoxSelectors:
"""
spiralBoxSubsamplers = []
offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
diameter = helpers.convert_length_to_pixels(self.dataset, diameter)
offset = helpers.convert_length_to_pixels(self.dataset, offset[0]), \
helpers.convert_length_to_pixels(self.dataset, offset[1])
diameter, offset = self._get_diameter_and_offset()
for numBoxes in SpiralBoxSubsampling.possibleBoxNumbers:
newBoxSelector: SpiralBoxSubsampling = SpiralBoxSubsampling(self.dataset.particleContainer, desiredFraction)
......@@ -177,6 +170,8 @@ class BoxSelectionCreator(object):
def get_randomBoxSubsamplers_for_fraction(self, desiredFraction: float) -> list:
randomBoxSamplers: list = []
diameter, offset = self._get_diameter_and_offset()
randomBoxSampler: RandomBoxSampling = RandomBoxSampling(None, desiredFraction)
randomBoxSampler.update_max_fractions()
for numBoxes in randomBoxSampler.possibleBoxNumbers:
......@@ -184,10 +179,35 @@ class BoxSelectionCreator(object):
if randomBoxSampler.config_is_valid():
newSampler: RandomBoxSampling = deepcopy(randomBoxSampler)
newSampler.particleContainer = self.dataset.particleContainer
newSampler.filterDiameter = diameter
newSampler.offset = offset
randomBoxSamplers.append(newSampler)
return randomBoxSamplers
def get_randomQuarterBoxSubsamplers_for_fraction(self, desiredFraction: float) -> list:
randomBoxSamplers: list = []
diameter, offset = self._get_diameter_and_offset()
randomBoxSampler: RandomQuarterBoxes = RandomQuarterBoxes(None, desiredFraction)
randomBoxSampler.update_max_fractions()
for numBoxes in randomBoxSampler.possibleBoxNumbers:
randomBoxSampler.numBoxes = numBoxes
if randomBoxSampler.config_is_valid():
newSampler: RandomBoxSampling = deepcopy(randomBoxSampler)
newSampler.particleContainer = self.dataset.particleContainer
newSampler.filterDiameter = diameter
newSampler.offset = offset
randomBoxSamplers.append(newSampler)
return randomBoxSamplers
def _get_diameter_and_offset(self) -> tuple:
offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
diameter: float = helpers.convert_length_to_pixels(self.dataset, diameter)
offset: tuple = helpers.convert_length_to_pixels(self.dataset, offset[0]), \
helpers.convert_length_to_pixels(self.dataset, offset[1])
return diameter, offset
class CrossBoxSubSampling(BoxSelectionSubsamplingMethod):
def __init__(self, particleContainer, desiredFraction: float = 0.1) -> None:
......@@ -364,6 +384,13 @@ class RandomBoxSampling(BoxSelectionSubsamplingMethod):
def label(self) -> str:
return f'Boxes random layout ({self.numBoxes} boxes)'
def equals(self, otherMethod) -> bool:
equals: bool = False
if type(otherMethod) == type(self) and otherMethod.fraction == self.fraction:
if otherMethod.numBoxes == self.numBoxes and otherMethod.__maxAngle == self.__maxAngle:
equals = True
return equals
def get_topLeft_of_boxes(self) -> list:
def get_random_topleft() -> list:
angle = np.random.rand() * self.__maxAngle
......@@ -389,8 +416,10 @@ class RandomBoxSampling(BoxSelectionSubsamplingMethod):
counter: int = 0
while counter < 50:
newTopLeft: list = get_random_topleft()
overlaps: list = [box_overlaps_other_box(newTopLeft, topLeft2, boxSize) for topLeft2 in topLefts]
if not True in overlaps:
for topLeft2 in topLefts:
if box_overlaps_other_box(newTopLeft, topLeft2, boxSize):
break
else: # i.e., if no break occurred
topLefts.append(newTopLeft)
break
counter += 1
......
......@@ -5,6 +5,11 @@ from evaluation import TotalResults
def get_error_vs_frac_plot(totalResults: TotalResults, attributes: list = [], methods: list = []) -> Figure:
if len(attributes) == 0 and len(methods) != 0:
attributes = [[]]*len(methods)
elif len(methods) == 0 and len(attributes) != 0:
methods = [[]]*len(attributes)
assert len(attributes) == len(methods)
fig: Figure = plt.figure()
numRows: int = 1
......
......@@ -12,23 +12,22 @@ SET GEPARD TO EVALUATION BRANCH (WITHOUT THE TILING STUFF), OTHERWISE SOME OF TH
"""
if __name__ == '__main__':
# results: TotalResults = TotalResults()
# pklsInFolders = get_pkls_from_directory(r'C:\Users\xbrjos\Desktop\temp MP\NewDatasets')
#
# for folder in pklsInFolders.keys():
# for samplePath in pklsInFolders[folder]:
# newSampleResult: SampleResult = results.add_sample(samplePath)
# for attr in get_attributes_from_foldername(folder):
# newSampleResult.set_attribute(attr)
#
# t0 = time.time()
# results.update_all()
# print('updating all took', time.time()-t0, 'seconds')
#
# save_results('results1.res', results)
results: TotalResults = load_results('results1.res')
# save_results('results1.res', results)
results: TotalResults = TotalResults()
pklsInFolders = get_pkls_from_directory(r'C:\Users\xbrjos\Desktop\temp MP\NewDatasets')
for folder in pklsInFolders.keys():
for samplePath in pklsInFolders[folder]:
newSampleResult: SampleResult = results.add_sample(samplePath)
for attr in get_attributes_from_foldername(folder):
newSampleResult.set_attribute(attr)
t0 = time.time()
results.update_all()
print('updating all took', time.time()-t0, 'seconds')
save_results('results1.res', results)
# results: TotalResults = load_results('results1.res')
plot: Figure = get_error_vs_frac_plot(results, attributes=[['air', 'water'], ['sediment', 'soil', 'beach', 'slush']],
methods=[['random', 'size']]*2)
plot.show()
\ No newline at end of file
methods=[])
plot.show()
......@@ -259,10 +259,11 @@ class TestSampleResult(unittest.TestCase):
possibleCrossBoxMethods = 2
possibleSpiralBoxMethods = 3
possibleRandomBoxMethods = 3
possibleQuarterRandomBoxMethods = 3
possibleChemometricMethods = 0
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
......@@ -276,11 +277,12 @@ class TestSampleResult(unittest.TestCase):
possibleRandomMethods = 2
possibleCrossBoxMethods = 1
possibleSpiralBoxMethods = 0
possibleChemometricMethods = 0
possibleRandomBoxMethods = 0
possibleQuarterRandomBoxMethods = 0
possibleChemometricMethods = 0
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
......@@ -293,10 +295,11 @@ class TestSampleResult(unittest.TestCase):
possibleCrossBoxMethods = 0
possibleSpiralBoxMethods = 0
possibleRandomBoxMethods = 0
possibleQuarterRandomBoxMethods = 0
possibleChemometricMethods = 0
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
......@@ -310,9 +313,10 @@ class TestSampleResult(unittest.TestCase):
possibleSpiralBoxMethods = 3
possibleChemometricMethods = 0
possibleRandomBoxMethods = 3
possibleQuarterRandomBoxMethods = 3
totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
possibleSpiralBoxMethods + possibleChemometricMethods + \
possibleRandomBoxMethods
possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
self.assertEqual(len(methods), totalPossible)
for desiredFraction in desiredFractions:
self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
......
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