Commit a9e96ce0 authored by Josef Brandt's avatar Josef Brandt

Refactoring

parent afa9eac4
......@@ -2,23 +2,28 @@ import copy
import numpy as np
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
from gepard import dataset
from gepard.analysis.particleContainer import ParticleContainer
from cythonModules import rotateContour
from helpers import get_filterDimensions_from_dataset, get_center_from_filter_dimensions
class ParticleVariations(object):
def __init__(self, particleContainer: ParticleContainer, numVariations: int = 10) -> None:
def __init__(self, dataset: dataset.DataSet, numVariations: int = 10) -> None:
super(ParticleVariations, self).__init__()
self.origParticleContainer = particleContainer
self.dataset: dataset.DataSet = dataset
self.origParticleContainer: ParticleContainer = self.dataset.particleContainer
self.numVariations = numVariations
def get_particleContainer_variations(self) -> ParticleContainer:
if self.numVariations > 0:
offset, diameter, [width, height] = get_filterDimensions_from_dataset(self.dataset)
center: np.ndarray = get_center_from_filter_dimensions(offset, diameter)
partContainer: ParticleContainer = self.origParticleContainer
contours: list = partContainer.getParticleContours()
center: tuple = round(np.mean(contours[:][0][0])),\
round(np.mean(contours[:][0][1]))
center: np.ndarray = np.array(center, dtype=np.int32)
# center: tuple = round(np.mean(contours[:][0][0])),\
# round(np.mean(contours[:][0][1]))
# center: np.ndarray = np.array(center, dtype=np.int32)
angles = self._get_angles()
for i in range(self.numVariations):
if i > 0:
......
......@@ -169,6 +169,13 @@ class SubsamplingResult(object):
error = float(np.mean(self.mpCountErrors))
return error
@property
def mpCountErrorStDev(self) -> float:
stdev: float = 0.0
if len(self.mpCountErrors) > 0:
stdev = np.std(self.mpCountErrors)
return stdev
def reset_results(self) -> None:
"""
Deletes all results
......
......@@ -49,12 +49,11 @@ class FilterView(QtWidgets.QGraphicsView):
self._update_particle_contours()
self._fit_to_window()
@helpers.timingDecorator
def update_rotation(self, newRotation: int) -> None:
if newRotation != self.rotation:
angle: float = np.float(newRotation-self.rotation)
center: np.ndarray = np.array([self.filter.circleOffset[0] + self.filter.diameter/2,
self.filter.circleOffset[1] + self.filter.diameter/2], dtype=np.int32)
center: np.ndarray = helpers.get_center_from_filter_dimensions(self.filter.circleOffset,
self.filter.diameter)
for particle in self.dataset.particleContainer.particles:
particle.contour = rc.rotate_contour_around_point(particle.contour, center, angle)
......@@ -62,7 +61,6 @@ class FilterView(QtWidgets.QGraphicsView):
self._update_particle_contours()
self.rotation = newRotation
@helpers.timingDecorator
def _update_particle_contours(self) -> None:
self._remove_particle_contours()
if self.dataset is not None:
......
......@@ -76,7 +76,6 @@ class MainView(QtWidgets.QWidget):
self.activeMode.update_measure_viewItems()
@helpers.timingDecorator
def _load_dataset(self) -> None:
fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Select .pkl file', filter='pkl file (*.pkl)')
if fname[0] != '':
......
......@@ -153,6 +153,16 @@ def get_filterDimensions_from_dataset(dataset: dataset.DataSet) -> tuple:
return offset, diameter, [width, height]
def get_center_from_filter_dimensions(offsetXY: tuple, diameter: float) -> np.ndarray:
"""
Calculates the center coordinates of a filter.
:return:
"""
center: np.ndarray = np.array([round(offsetXY[0] + diameter/2),
round(offsetXY[1] + diameter/2)], dtype=np.int32)
return center
def convert_length_to_pixels(dataset: dataset.DataSet, length: float) -> float:
"""
:param dataset: dataset to use for conversion
......
......@@ -10,21 +10,21 @@ 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')
# 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')
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 = load_results('results1.res')
# results.update_all(force=True)
# save_results('results1.res', results)
......
......@@ -2,9 +2,23 @@ import numpy as np
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
import gepard
from gepard.dataset import DataSet
from gepard.analysis.particleContainer import ParticleContainer
def setMaxDim(dataset: DataSet, imgSize: float, minX: float, maxX: float, minY: float, maxY: float) -> None:
dataset.maxdim = minX + imgSize/2, maxY - imgSize/2, maxX - imgSize/2, minY + imgSize/2
def get_default_DataSet() -> DataSet:
dset: DataSet = DataSet('tests/default.pkl')
dset.imagescanMode = 'df'
dset.imagedim_df = [10, 10]
dset.pixelscale_df = 1.0
setMaxDim(dset, 10, 0, 10, 0, 10)
return dset
def get_default_ParticleContainer() -> ParticleContainer:
particleContainer: ParticleContainer = ParticleContainer(None)
particleContainer.initializeParticles(4)
......
......@@ -2,14 +2,18 @@ import unittest
import numpy as np
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
from gepard import dataset
from gepard.analysis.particleContainer import ParticleContainer
from datasetOperations import ParticleVariations
from helpers_for_test import get_default_ParticleContainer
from helpers_for_test import get_default_ParticleContainer, get_default_DataSet
class TestParticleVariations(unittest.TestCase):
def test_get_particleContainer_variations(self):
dset: dataset.DataSet = get_default_DataSet()
particleContainer: ParticleContainer = get_default_ParticleContainer()
dset.particleContainer = particleContainer
contours = particleContainer.getParticleContours()
center: tuple = round(np.mean(contours[:][0][0])), \
......@@ -17,7 +21,7 @@ class TestParticleVariations(unittest.TestCase):
center: np.ndarray = np.array(center, dtype=np.int32)
for numVariations in [0, 1, 10, 20]:
particleVariations: ParticleVariations = ParticleVariations(particleContainer, numVariations)
particleVariations: ParticleVariations = ParticleVariations(dset, numVariations)
foundContours: list = []
if numVariations == 0:
......@@ -39,7 +43,7 @@ class TestParticleVariations(unittest.TestCase):
self.assertEqual(index, numVariations-1)
def test_get_angles(self):
particleVariations: ParticleVariations = ParticleVariations(None, 2)
particleVariations: ParticleVariations = ParticleVariations(dataset.DataSet('fakepath/fake.pkl'), 2)
angles: list = list(particleVariations._get_angles())
self.assertEqual(angles, [0, 180])
......
......@@ -18,7 +18,7 @@ from gepard.analysis.particleAndMeasurement import Particle, Measurement
from evaluation import TotalResults, SampleResult, SubsamplingResult, get_methods_to_test
import methods as meth
import geometricMethods as gmeth
from helpers_for_test import get_default_ParticleContainer
from helpers_for_test import get_default_ParticleContainer, get_default_DataSet
class TestTotalResults(unittest.TestCase):
......@@ -110,30 +110,30 @@ class TestTotalResults(unittest.TestCase):
firstMethod: meth.RandomSampling = meth.RandomSampling(None, 0.1)
firstResult: SubsamplingResult = SubsamplingResult(firstMethod)
firstResult.mpCountErrors = [0.8]
firstResult.mpCountErrors = [80]
secondMethod: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
secondMethod.numBoxesAcross = 3
secondResult: SubsamplingResult = SubsamplingResult(secondMethod)
secondResult.mpCountErrors = [0.6]
secondResult.mpCountErrors = [60]
thirdMethod: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
thirdMethod.numBoxesAcross = 5
self.assertEqual(thirdMethod.fraction, 0.1)
thirdResult: SubsamplingResult = SubsamplingResult(thirdMethod)
thirdResult.mpCountErrors = [0.4]
thirdResult.mpCountErrors = [40]
thirdMethod2: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
thirdMethod2.numBoxesAcross = 5
self.assertEqual(thirdMethod2.fraction, 0.1)
thirdResult2: SubsamplingResult = SubsamplingResult(thirdMethod)
thirdResult2.mpCountErrors = [0.8]
thirdResult2.mpCountErrors = [80]
thirdMethod3: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.2)
thirdMethod3.numBoxesAcross = 5
self.assertEqual(thirdMethod3.fraction, 0.2)
thirdResult3: SubsamplingResult = SubsamplingResult(thirdMethod3)
thirdResult3.mpCountErrors = [0.5]
thirdResult3.mpCountErrors = [50]
firstSample.results = [firstResult, secondResult, thirdResult, thirdResult3]
secondSample.results = [firstResult, secondResult, thirdResult2, thirdResult3]
......@@ -144,14 +144,14 @@ class TestTotalResults(unittest.TestCase):
res: dict = list(resultDict.values())[i]
if i == 0:
self.assertEqual(list(res.keys()), [0.1])
self.assertAlmostEqual(res[0.1], 0.8)
self.assertAlmostEqual(res[0.1], 80)
if i == 1:
self.assertEqual(list(res.keys()), [0.1])
self.assertAlmostEqual(res[0.1], 0.6)
self.assertAlmostEqual(res[0.1], 60)
if i == 2:
self.assertEqual(list(res.keys()), [0.1, 0.2])
self.assertAlmostEqual(res[0.1], 0.6) # i.e., mean([0.4, 0.8])
self.assertAlmostEqual(res[0.2], 0.5)
self.assertAlmostEqual(res[0.1], 60) # i.e., mean([40, 808])
self.assertAlmostEqual(res[0.2], 50)
filteredResultDict: dict = self.totalResults.get_error_vs_fraction_data(attributes=['to be used'])
self.assertEqual(list(filteredResultDict.keys()), [firstMethod.label, secondMethod.label, thirdMethod.label])
......@@ -159,14 +159,14 @@ class TestTotalResults(unittest.TestCase):
res: dict = list(filteredResultDict.values())[i]
if i == 0:
self.assertEqual(list(res.keys()), [0.1])
self.assertAlmostEqual(res[0.1], 0.8)
self.assertAlmostEqual(res[0.1], 80)
if i == 1:
self.assertEqual(list(res.keys()), [0.1])
self.assertAlmostEqual(res[0.1], 0.6)
self.assertAlmostEqual(res[0.1], 60)
if i == 2:
self.assertEqual(list(res.keys()), [0.1, 0.2])
self.assertAlmostEqual(res[0.1], 0.4) # only the result from the first sample is used, as filtered..
self.assertAlmostEqual(res[0.2], 0.5)
self.assertAlmostEqual(res[0.1], 40) # only the result from the first sample is used, as filtered..
self.assertAlmostEqual(res[0.2], 50)
filteredResultDict: dict = self.totalResults.get_error_vs_fraction_data(methods=['cross'])
self.assertEqual(list(filteredResultDict.keys()), [secondMethod.label, thirdMethod.label])
......@@ -183,7 +183,7 @@ class TestSampleResult(unittest.TestCase):
particleContainer = get_default_ParticleContainer()
self.sampleResult: SampleResult = SampleResult('fakePath/fakeFile.pkl')
self.sampleResult.dataset = gepard.dataset.DataSet('fakePath/fakeFile.pkl')
self.sampleResult.dataset = get_default_DataSet()
self.sampleResult.dataset.particleContainer = particleContainer
self.sampleResult.results.append(SubsamplingResult(meth.RandomSampling(particleContainer, 0.1)))
......@@ -481,7 +481,18 @@ class TestSubsamplingResult(unittest.TestCase):
mpCountError = self.subsamplingResult._get_mp_count_error(origParticles, estimateParticles, 0.5)
self.assertEqual(mpCountError, 100)
def test_get_averaged_stdev(self):
self.subsamplingResult.mpCountErrors = [75, 75, 75]
self.assertEqual(self.subsamplingResult.mpCountErrorStDev, 0)
self.subsamplingResult.mpCountErrors = [50, 75, 100]
self.assertAlmostEqual(self.subsamplingResult.mpCountErrorStDev, 20.412414523193153)
def test_get_averaged_errrs(self):
self.subsamplingResult.mpCountErrors = [50, 75, 100]
self.assertEqual(self.subsamplingResult.mpCountError, 75)
def test_get_error_from_values(self):
exact, estimate = 100, 90
error = self.subsamplingResult._get_error_from_values(exact, estimate)
......
......@@ -15,6 +15,7 @@ from PyQt5 import QtCore, QtGui
import gepard
from gepard.analysis.particleAndMeasurement import Particle
from gepard import dataset
from helpers_for_test import get_default_DataSet, setMaxDim
class TestBinSorter(unittest.TestCase):
......@@ -166,32 +167,27 @@ class TestOther(unittest.TestCase):
class TestDatasetOperations(unittest.TestCase):
def setUp(self) -> None:
self.dataset: dataset.DataSet = dataset.DataSet('test')
self.dataset.imagescanMode = 'df'
self.dataset = get_default_DataSet()
def test_get_filtersize(self):
def setMaxDim():
self.dataset.maxdim = minX + imgdim / 2, maxY - imgdim / 2, maxX - imgdim / 2, minY + imgdim / 2
imgdim = 10
self.dataset.imagedim_df = [imgdim, imgdim]
minX, maxX, minY, maxY = 0, 10, 0, 10
setMaxDim()
setMaxDim(self.dataset, imgdim, minX, maxX, minY, maxY)
offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
self.assertEqual(diameter, 10)
self.assertEqual(offset, (0, 0))
self.assertEqual(widthHeight, [10, 10])
minX, maxX, minY, maxY = -10, 10, -10, 10
setMaxDim()
setMaxDim(self.dataset, imgdim, minX, maxX, minY, maxY)
offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
self.assertEqual(diameter, 20)
self.assertEqual(widthHeight, [20, 20])
self.assertEqual(offset, (0, 0))
minX, maxX, minY, maxY = 0, 20, 0, 10
setMaxDim()
setMaxDim(self.dataset, imgdim, minX, maxX, minY, maxY)
offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(self.dataset)
self.assertEqual(diameter, 10)
self.assertEqual(widthHeight, [20, 10])
......@@ -209,3 +205,11 @@ class TestDatasetOperations(unittest.TestCase):
self.assertEqual(helpers.convert_length_to_pixels(self.dataset, diameter), 20)
self.assertEqual(helpers.convert_length_to_pixels(self.dataset, widthHeight[0]), 40)
self.assertEqual(helpers.convert_length_to_pixels(self.dataset, widthHeight[1]), 20)
def test_get_center_from_filter_dimensions(self):
for offset in [(0, 0), (5, 0), (0, 5), (-5, 10), (-7, -2.5)]:
for diameter in [5, 10, 20]:
center: np.ndarray = helpers.get_center_from_filter_dimensions(offset, diameter)
self.assertEqual(center[0], round(diameter/2 + offset[0]))
self.assertEqual(center[1], round(diameter/2 + offset[1]))
self.assertTrue(type(center[0]) == np.int32) # has to be np.int32 for use in rotate_contour cython
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