test_evaluation.py 26.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 22 13:58:25 2020

@author: luna
"""

import unittest
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
import gepard
13
from gepard.analysis.particleContainer import ParticleContainer
14

15
from evaluation import TotalResults, SampleResult, SubsamplingResult, get_methods_to_test
Josef Brandt's avatar
Josef Brandt committed
16 17
import methods as meth
import geometricMethods as gmeth
Josef Brandt's avatar
Josef Brandt committed
18 19
from chemometrics.chemometricMethods import TrainedSubsampling
from helpers_for_test import get_default_ParticleContainer, get_default_DataSet, get_MP_particles, get_non_MP_particles, get_MP_particle, get_non_MP_particle
20

Josef Brandt's avatar
Josef Brandt committed
21 22 23 24 25 26

class TestTotalResults(unittest.TestCase):
    def setUp(self) -> None:
        self.totalResults = TotalResults()

    def test_add_sample(self):
27
        newResult: SampleResult = self.totalResults.add_sample('fakePath/fakeFolder/fakeFile.pkl')
Josef Brandt's avatar
Josef Brandt committed
28
        self.assertEqual(len(self.totalResults.sampleResults), 1)
29
        self.assertTrue(type(newResult) == SampleResult)
Josef Brandt's avatar
Josef Brandt committed
30

31
        newResult = self.totalResults.add_sample('fakePath/fakeFolder/fakeFile.pkl')  # the same file should not be added again
Josef Brandt's avatar
Josef Brandt committed
32
        self.assertEqual(len(self.totalResults.sampleResults), 1)
33
        self.assertTrue(newResult is None)
Josef Brandt's avatar
Josef Brandt committed
34

35
        newResult = self.totalResults.add_sample('fakePath/fakeFolder/fakeFile2.pkl')  # another should be added, though
Josef Brandt's avatar
Josef Brandt committed
36
        self.assertEqual(len(self.totalResults.sampleResults), 2)
37
        self.assertTrue(type(newResult) == SampleResult)
Josef Brandt's avatar
Josef Brandt committed
38

39
        newResult = self.totalResults.add_sample('fakePath/fakeFolder/fakeFile2.txt')  # invalid extention, not added...
Josef Brandt's avatar
Josef Brandt committed
40
        self.assertEqual(len(self.totalResults.sampleResults), 2)
41
        self.assertTrue(newResult is None)
Josef Brandt's avatar
Josef Brandt committed
42

43
    def test_get_error_vs_fraction_data(self):
44
        firstSample: SampleResult = self.totalResults.add_sample('sample1.pkl')
45
        firstSample.set_attribute('to be used')
46
        secondSample: SampleResult = self.totalResults.add_sample('sample2.pkl')
47
        secondSample.set_attribute('not to be used')
48 49 50

        firstMethod: meth.RandomSampling = meth.RandomSampling(None, 0.1)
        firstResult: SubsamplingResult = SubsamplingResult(firstMethod)
51
        firstResult.mpCountErrors = [70, 90]  # mean = 80, stdev = 10
52 53 54 55

        secondMethod: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
        secondMethod.numBoxesAcross = 3
        secondResult: SubsamplingResult = SubsamplingResult(secondMethod)
56
        secondResult.mpCountErrors = [50, 70]  # mean = 60, stdev = 10
57 58 59 60 61

        thirdMethod: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
        thirdMethod.numBoxesAcross = 5
        self.assertEqual(thirdMethod.fraction, 0.1)
        thirdResult: SubsamplingResult = SubsamplingResult(thirdMethod)
62
        thirdResult.mpCountErrors = [30, 50]  # mean = 40, stdev = 10
63 64 65 66 67

        thirdMethod2: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.1)
        thirdMethod2.numBoxesAcross = 5
        self.assertEqual(thirdMethod2.fraction, 0.1)
        thirdResult2: SubsamplingResult = SubsamplingResult(thirdMethod)
68
        thirdResult2.mpCountErrors = [60, 100]  # mean = 80, stdev = 20
69 70 71 72 73

        thirdMethod3: gmeth.CrossBoxSubSampling = gmeth.CrossBoxSubSampling(None, 0.2)
        thirdMethod3.numBoxesAcross = 5
        self.assertEqual(thirdMethod3.fraction, 0.2)
        thirdResult3: SubsamplingResult = SubsamplingResult(thirdMethod3)
74
        thirdResult3.mpCountErrors = [30, 50, 70]  # mean = 50, stdev = 16.32993161855452
75 76 77 78 79 80

        firstSample.results = [firstResult, secondResult, thirdResult, thirdResult3]
        secondSample.results = [firstResult, secondResult, thirdResult2, thirdResult3]

        resultDict: dict = self.totalResults.get_error_vs_fraction_data()
        self.assertEqual(list(resultDict.keys()), [firstMethod.label, secondMethod.label, thirdMethod.label])
81 82 83

        for i, key in enumerate(resultDict.keys()):
            res: dict = resultDict[key]
84 85
            if i == 0:
                self.assertEqual(list(res.keys()), [0.1])
86 87 88
                mean, stdev = res[0.1]
                self.assertAlmostEqual(mean, 80)
                self.assertAlmostEqual(stdev, 10)
89 90
            if i == 1:
                self.assertEqual(list(res.keys()), [0.1])
91 92 93
                mean, stdev = res[0.1]
                self.assertAlmostEqual(mean, 60)
                self.assertAlmostEqual(stdev, 10)
94 95
            if i == 2:
                self.assertEqual(list(res.keys()), [0.1, 0.2])
96 97 98 99 100 101
                mean, stdev = res[0.1]
                self.assertAlmostEqual(mean, 60)  # i.e., mean([40, 80])
                self.assertAlmostEqual(stdev, 15)  # i.e., mean([10, 20])
                mean, stdev = res[0.2]
                self.assertAlmostEqual(mean, 50)
                self.assertAlmostEqual(stdev, 16.32993161855452)
102 103 104

        filteredResultDict: dict = self.totalResults.get_error_vs_fraction_data(attributes=['to be used'])
        self.assertEqual(list(filteredResultDict.keys()), [firstMethod.label, secondMethod.label, thirdMethod.label])
105 106 107
        for i, key in enumerate(filteredResultDict.keys()):

            res: dict = filteredResultDict[key]
108
            if i == 0:
109
                self.assertEqual(key, firstMethod.label)
110
                self.assertEqual(list(res.keys()), [0.1])
111 112 113
                mean, stdev = res[0.1]
                self.assertAlmostEqual(mean, 80)
                self.assertAlmostEqual(stdev, 10)
114
            if i == 1:
115
                self.assertEqual(key, secondMethod.label)
116
                self.assertEqual(list(res.keys()), [0.1])
117 118 119
                mean, stdev = res[0.1]
                self.assertAlmostEqual(mean, 60)
                self.assertAlmostEqual(stdev, 10)
120
            if i == 2:
121
                self.assertEqual(key, thirdMethod.label)
122
                self.assertEqual(list(res.keys()), [0.1, 0.2])
123 124 125 126 127 128
                mean, stdev = res[0.1]
                self.assertAlmostEqual(mean, 40)  # only the result from the first sample is used, as filtered..
                self.assertAlmostEqual(stdev, 10)
                mean, stdev = res[0.2]
                self.assertAlmostEqual(mean, 50)
                self.assertAlmostEqual(stdev, 16.32993161855452)
129 130 131 132 133 134 135 136 137

        filteredResultDict: dict = self.totalResults.get_error_vs_fraction_data(methods=['cross'])
        self.assertEqual(list(filteredResultDict.keys()), [secondMethod.label, thirdMethod.label])

        filteredResultDict: dict = self.totalResults.get_error_vs_fraction_data(methods=['Cross'])
        self.assertEqual(list(filteredResultDict.keys()), [secondMethod.label, thirdMethod.label])

        filteredResultDict: dict = self.totalResults.get_error_vs_fraction_data(methods=['random'])
        self.assertEqual(list(filteredResultDict.keys()), [firstMethod.label])
138

Josef Brandt's avatar
Josef Brandt committed
139 140 141

class TestSampleResult(unittest.TestCase):
    def setUp(self) -> None:
142 143
        particleContainer = get_default_ParticleContainer()

Josef Brandt's avatar
Josef Brandt committed
144
        self.sampleResult: SampleResult = SampleResult('fakePath/fakeFile.pkl')
Josef Brandt's avatar
Josef Brandt committed
145
        self.sampleResult.dataset = get_default_DataSet()
146 147
        self.sampleResult.dataset.particleContainer = particleContainer
        self.sampleResult.results.append(SubsamplingResult(meth.RandomSampling(particleContainer, 0.1)))
148 149 150 151 152 153 154 155 156 157 158 159

        newMethod = gmeth.SpiralBoxSubsampling(None, 0.1)
        newMethod.numBoxes = 10
        self.sampleResult.results.append(SubsamplingResult(newMethod))

        newMethod = gmeth.SpiralBoxSubsampling(None, 0.1)
        newMethod.numBoxes = 15
        self.sampleResult.results.append(SubsamplingResult(newMethod))

        newMethod = gmeth.SpiralBoxSubsampling(None, 0.3)
        newMethod.numBoxes = 10
        self.sampleResult.results.append(SubsamplingResult(newMethod))
Josef Brandt's avatar
Josef Brandt committed
160 161 162

    def test_sampleResults_added_correctly(self):
        method: meth.SubsamplingMethod = self.sampleResult.results[0].method
163 164
        self.assertEqual(type(method), meth.RandomSampling)
        self.assertEqual(method.fraction, 0.1)
Josef Brandt's avatar
Josef Brandt committed
165 166

        method: meth.SubsamplingMethod = self.sampleResult.results[1].method
167 168 169
        self.assertEqual(type(method), gmeth.SpiralBoxSubsampling)
        self.assertEqual(method.fraction, 0.1)
        self.assertEqual(method.numBoxes, 10)
Josef Brandt's avatar
Josef Brandt committed
170 171

        method: meth.SubsamplingMethod = self.sampleResult.results[2].method
172 173 174 175 176 177 178 179
        self.assertEqual(type(method), gmeth.SpiralBoxSubsampling)
        self.assertEqual(method.fraction, 0.1)
        self.assertEqual(method.numBoxes, 15)

        method: meth.SubsamplingMethod = self.sampleResult.results[3].method
        self.assertEqual(type(method), gmeth.SpiralBoxSubsampling)
        self.assertEqual(method.fraction, 0.3)
        self.assertEqual(method.numBoxes, 10)
Josef Brandt's avatar
Josef Brandt committed
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    def test_get_result_of_method(self):
        particleContainer = get_default_ParticleContainer()
        newMethod: meth.SubsamplingMethod = meth.RandomSampling(particleContainer, 0.1)
        result: SubsamplingResult = self.sampleResult._get_result_of_method(newMethod)
        self.assertTrue(result is not None)
        self.assertTrue(result.method.equals(newMethod))

        newMethod = meth.RandomSampling(particleContainer, 0.2)
        result = self.sampleResult._get_result_of_method(newMethod)
        self.assertTrue(result is None)

        newMethod = gmeth.SpiralBoxSubsampling(particleContainer, 0.1)
        result = self.sampleResult._get_result_of_method(newMethod)
        self.assertTrue(result is not None)
        self.assertTrue(result.method.equals(newMethod))
Josef Brandt's avatar
Josef Brandt committed
196

197 198 199
        newMethod = gmeth.SpiralBoxSubsampling(particleContainer, 0.2)
        result = self.sampleResult._get_result_of_method(newMethod)
        self.assertTrue(result is None)
Josef Brandt's avatar
Josef Brandt committed
200

201 202 203
        newMethod = gmeth.CrossBoxSubSampling(particleContainer, 0.3)
        result = self.sampleResult._get_result_of_method(newMethod)
        self.assertTrue(result is None)
Josef Brandt's avatar
Josef Brandt committed
204 205

    def test_remove_result_of_method(self):
206
        particleContainer = get_default_ParticleContainer()
207
        numOrigResults = len(self.sampleResult.results)
208
        self.sampleResult._remove_result_of_method(meth.RandomSampling(particleContainer, 0.1))
209
        self.assertEqual(len(self.sampleResult.results), numOrigResults-1)
Josef Brandt's avatar
Josef Brandt committed
210

211
        self.sampleResult._remove_result_of_method(gmeth.SpiralBoxSubsampling(particleContainer, 0.1))
212
        self.assertEqual(len(self.sampleResult.results), numOrigResults-2)
Josef Brandt's avatar
Josef Brandt committed
213

214
        self.sampleResult._remove_result_of_method(gmeth.SpiralBoxSubsampling(particleContainer, 0.2))  # this is one is not present...
215 216 217 218 219
        self.assertEqual(len(self.sampleResult.results), numOrigResults-2)

    def test_attributes(self):
        self.sampleResult.set_attribute('soil')
        self.assertTrue(self.sampleResult.has_attribute('soil'))
220 221
        self.assertTrue(self.sampleResult.has_attribute('Soil'))  # we want to be case insensitive
        self.assertTrue(self.sampleResult.has_attribute('SOIL'))
222 223 224

        self.sampleResult.set_attribute('soil')  # the attribute is already there and shall not be added again
        self.assertEqual(len(self.sampleResult.attributes), 1)
Josef Brandt's avatar
Josef Brandt committed
225

226 227 228
        self.sampleResult.set_attribute('10µmFilter')
        self.assertEqual(len(self.sampleResult.attributes), 2)
        self.assertTrue(self.sampleResult.has_attribute('10µmFilter'))
Josef Brandt's avatar
Josef Brandt committed
229

230 231 232 233 234 235
        self.assertTrue(self.sampleResult.has_any_attribute(['soil', 'water']))
        self.assertTrue(self.sampleResult.has_any_attribute(['soil', 'water', '10µmFilter']))
        self.assertTrue(self.sampleResult.has_any_attribute(['water', '10µmFilter']))
        self.assertFalse(self.sampleResult.has_any_attribute(['water', 'sediment']))
        self.assertFalse(self.sampleResult.has_any_attribute(['beach']))

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    def test_get_methods_to_test(self):
        def containsMethod(listOfMethods: list, template: meth.SubsamplingMethod) -> bool:
            contains: bool = False
            for method in listOfMethods:
                if type(method) == type(template) and method.fraction == template.fraction:
                    contains = True
                    break
            return contains

        dset: gepard.dataset.DataSet = gepard.dataset.DataSet('fakepath')

        imgdim = 10
        dset.imagescanMode = 'df'
        dset.imagedim_df = [imgdim, imgdim]
        dset.pixelscale_df = 1.0
        minX, maxX, minY, maxY = 0, 1000, 0, 1000
        dset.maxdim = minX + imgdim / 2, maxY - imgdim / 2, maxX - imgdim / 2, minY + imgdim / 2

        desiredFraction = 0.1
Josef Brandt's avatar
Josef Brandt committed
255
        methods = get_methods_to_test(dset, [desiredFraction], maxTries=10)
256 257 258
        possibleRandomMethods = 2
        possibleCrossBoxMethods = 2
        possibleSpiralBoxMethods = 3
Josef Brandt's avatar
Josef Brandt committed
259
        possibleRandomBoxMethods = 3
Josef Brandt's avatar
Josef Brandt committed
260
        possibleQuarterRandomBoxMethods = 3
Josef Brandt's avatar
Josef Brandt committed
261
        possibleChemometricMethods = 1
262
        totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
Josef Brandt's avatar
Josef Brandt committed
263
                        possibleSpiralBoxMethods + possibleChemometricMethods + \
Josef Brandt's avatar
Josef Brandt committed
264
                        possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
Josef Brandt's avatar
Josef Brandt committed
265

266 267 268 269 270
        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)))
Josef Brandt's avatar
Josef Brandt committed
271
        self.assertTrue(containsMethod(methods, gmeth.RandomBoxSampling(dset, desiredFraction)))
272 273

        desiredFraction = 0.5
Josef Brandt's avatar
Josef Brandt committed
274
        methods = get_methods_to_test(dset, [desiredFraction], maxTries=10)
275 276 277
        possibleRandomMethods = 2
        possibleCrossBoxMethods = 1
        possibleSpiralBoxMethods = 0
Josef Brandt's avatar
Josef Brandt committed
278
        possibleRandomBoxMethods = 0
Josef Brandt's avatar
Josef Brandt committed
279
        possibleQuarterRandomBoxMethods = 0
Josef Brandt's avatar
Josef Brandt committed
280
        possibleChemometricMethods = 1
281
        totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
Josef Brandt's avatar
Josef Brandt committed
282
                        possibleSpiralBoxMethods + possibleChemometricMethods + \
Josef Brandt's avatar
Josef Brandt committed
283
                        possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
284 285 286 287 288 289 290
        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.assertFalse(containsMethod(methods, gmeth.SpiralBoxSubsampling(dset, desiredFraction)))

        desiredFraction = 0.9
Josef Brandt's avatar
Josef Brandt committed
291
        methods = get_methods_to_test(dset, [desiredFraction], maxTries=10)
292 293 294
        possibleRandomMethods = 2
        possibleCrossBoxMethods = 0
        possibleSpiralBoxMethods = 0
Josef Brandt's avatar
Josef Brandt committed
295
        possibleRandomBoxMethods = 0
Josef Brandt's avatar
Josef Brandt committed
296
        possibleQuarterRandomBoxMethods = 0
Josef Brandt's avatar
Josef Brandt committed
297
        possibleChemometricMethods = 1
298
        totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
Josef Brandt's avatar
Josef Brandt committed
299
                        possibleSpiralBoxMethods + possibleChemometricMethods + \
Josef Brandt's avatar
Josef Brandt committed
300
                        possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
301 302 303 304 305 306 307
        self.assertEqual(len(methods), totalPossible)
        self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
        self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
        self.assertFalse(containsMethod(methods, gmeth.CrossBoxSubSampling(dset, desiredFraction)))
        self.assertFalse(containsMethod(methods, gmeth.SpiralBoxSubsampling(dset, desiredFraction)))

        desiredFractions = [0.1, 0.5]
Josef Brandt's avatar
Josef Brandt committed
308
        methods = get_methods_to_test(dset, desiredFractions, maxTries=10)
309 310 311
        possibleRandomMethods = 4
        possibleCrossBoxMethods = 3
        possibleSpiralBoxMethods = 3
Josef Brandt's avatar
Josef Brandt committed
312
        possibleChemometricMethods = 2
Josef Brandt's avatar
Josef Brandt committed
313
        possibleRandomBoxMethods = 3
Josef Brandt's avatar
Josef Brandt committed
314
        possibleQuarterRandomBoxMethods = 3
315
        totalPossible = possibleCrossBoxMethods + possibleRandomMethods + \
Josef Brandt's avatar
Josef Brandt committed
316
                        possibleSpiralBoxMethods + possibleChemometricMethods + \
Josef Brandt's avatar
Josef Brandt committed
317
                        possibleRandomBoxMethods + possibleQuarterRandomBoxMethods
318 319 320 321 322 323 324 325 326 327
        self.assertEqual(len(methods), totalPossible)
        for desiredFraction in desiredFractions:
            self.assertTrue(containsMethod(methods, meth.RandomSampling(dset, desiredFraction)))
            self.assertTrue(containsMethod(methods, meth.SizeBinFractioning(dset, desiredFraction)))
            self.assertTrue(containsMethod(methods, gmeth.CrossBoxSubSampling(dset, desiredFraction)))
            if desiredFraction == 0.1:
                self.assertTrue(containsMethod(methods, gmeth.SpiralBoxSubsampling(dset, desiredFraction)))
            else:
                self.assertFalse(containsMethod(methods, gmeth.SpiralBoxSubsampling(dset, desiredFraction)))

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    def test_update_result_with_methods(self):
        particleContainer = get_default_ParticleContainer()
        for numVariations in [1, 5, 20]:
            self.sampleResult.numVariations = numVariations
            for result in self.sampleResult.results:
                result.reset_results()

            methods: list = []
            method1 = gmeth.SpiralBoxSubsampling(particleContainer, 0.1)
            method1.numBoxes = 10
            methods.append(method1)

            method2 = gmeth.SpiralBoxSubsampling(particleContainer, 0.1)
            method2.numBoxes = 15
            methods.append(method2)

            updatedMethods: list = self.sampleResult.update_result_with_methods(methods)
            self.assertEqual(len(updatedMethods), 0)  # because the methods were there already

            for result in self.sampleResult.results:
                self.assertEqual(len(result.mpCountErrors), 0)  # because the added results haven't set any

            updatedMethods = self.sampleResult.update_result_with_methods(methods, force=True)
            self.assertEqual(len(updatedMethods), 2)  # because now we force the update
            self.assertTrue(method1 in updatedMethods)
            self.assertTrue(method2 in updatedMethods)

            firstParticleContainer: ParticleContainer = None
            for result in self.sampleResult.results:
                if result.method.equals(method1) or result.method.equals(method2):
                    if firstParticleContainer is None:
                        firstParticleContainer = result.method.particleContainer
                    else:
                        self.assertTrue(result.method.particleContainer is not firstParticleContainer)
                    self.assertEqual(len(result.mpCountErrors), numVariations)
                else:
                    self.assertEqual(len(result.mpCountErrors), 0)  # these were not updated

366

Josef Brandt's avatar
Josef Brandt committed
367
class TestSubsamplingResult(unittest.TestCase):
368
    def setUp(self):
Josef Brandt's avatar
Josef Brandt committed
369 370
        self.subsamplingResult: SubsamplingResult = SubsamplingResult(meth.RandomSampling(None, 0.1))

371 372
    def test_add_result(self):
        self.assertEqual(len(self.subsamplingResult.mpCountErrors), 0)
Josef Brandt's avatar
Josef Brandt committed
373 374
        origParticles: list = get_MP_particles(100)
        subParticles: list = get_MP_particles(15)  # at fraction of 0.1, 10 particles would be expected
375 376 377
        self.subsamplingResult.add_result(origParticles, subParticles)
        self.assertEqual(len(self.subsamplingResult.mpCountErrors), 1)
        self.assertEqual(self.subsamplingResult.mpCountErrors[0], 50)
378 379 380
        self.assertEqual(self.subsamplingResult.origMPCount, 100)
        self.assertEqual(self.subsamplingResult.estimMPCounts, [150])
        self.assertAlmostEqual(self.subsamplingResult.estimMPCount, 150)
381

Josef Brandt's avatar
Josef Brandt committed
382
        subParticles = get_MP_particles(10)  # at fraction of 0.1, 10 particles would be expected
383 384 385 386
        self.subsamplingResult.add_result(origParticles, subParticles)
        self.assertEqual(len(self.subsamplingResult.mpCountErrors), 2)
        self.assertEqual(self.subsamplingResult.mpCountErrors[0], 50)
        self.assertEqual(self.subsamplingResult.mpCountErrors[1], 0)
387 388 389
        self.assertAlmostEqual(self.subsamplingResult.mpCountError, 25)
        self.assertEqual(self.subsamplingResult.estimMPCounts, [150, 100])
        self.assertEqual(self.subsamplingResult.estimMPCount, 125)
390

Josef Brandt's avatar
Josef Brandt committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404
        self.subsamplingResult.mpCountErrors = []
        self.subsamplingResult.estimMPCounts = []
        # TODO: REIMPLEMENT, WHEN trained sampling IS WORKING
        # trainedSampling: TrainedSubsampling = TrainedSubsampling(get_default_ParticleContainer, 0.1)
        # trainedSampling.score = 0.5
        # self.subsamplingResult.method = trainedSampling
        # self.subsamplingResult.add_result(origParticles, subParticles)
        # self.assertEqual(self.subsamplingResult.mpCountErrors[0], 0)
        #
        # trainedSampling.score = 1.0
        # self.subsamplingResult.add_result(origParticles, subParticles)
        # self.assertEqual(self.subsamplingResult.mpCountErrors[1], 50)


405 406
    def test_reset_results(self):
        self.subsamplingResult.mpCountErrors = [10, 30, 20]
407
        self.subsamplingResult.estimMPCounts = [2, 5, 3]
408 409
        self.subsamplingResult.reset_results()
        self.assertEqual(self.subsamplingResult.mpCountErrors, [])
410
        self.assertEqual(self.subsamplingResult.estimMPCounts, [])
411

412
    def test_get_error_per_bin(self):
Josef Brandt's avatar
Josef Brandt committed
413 414
        def get_full_and_sub_particles():
            allParticles = []
415 416 417
            subParticles = []
            for particleSize in particleSizes:
                for _ in range(numParticlesPerSizeFull):
Josef Brandt's avatar
Josef Brandt committed
418
                    mpParticle = get_MP_particle()
419
                    mpParticle.longSize = mpParticle.shortSize = particleSize
Josef Brandt's avatar
Josef Brandt committed
420
                    allParticles.append(mpParticle)
421 422
                
                for _ in range(numParticlesPerSizeSub):
Josef Brandt's avatar
Josef Brandt committed
423
                    mpParticle = get_MP_particle()
424 425 426
                    mpParticle.longSize = mpParticle.shortSize = particleSize
                    subParticles.append(mpParticle)
            
Josef Brandt's avatar
Josef Brandt committed
427
            return allParticles, subParticles
428 429 430 431 432 433
                    
        binSizes = [5, 10, 20, 50, 100, 200, 500]
        particleSizes = [upperLimit - 1 for upperLimit in binSizes]
        
        numParticlesPerSizeFull = 20
        numParticlesPerSizeSub = 10
Josef Brandt's avatar
Josef Brandt committed
434
        fullParticles, subParticles = get_full_and_sub_particles()
435
        
Josef Brandt's avatar
Josef Brandt committed
436 437
        # assume everything was measured
        bins, mpCountErrorsPerBin = self.subsamplingResult._get_mp_count_error_per_bin(fullParticles, subParticles, 1.)
438 439 440

        for binIndex, binError in enumerate(mpCountErrorsPerBin):
            if binIndex <= 6:
441
                self.assertEqual(binError, 50)
Josef Brandt's avatar
Josef Brandt committed
442
            else:   # it's the last and largest bin, no particles where added there
443 444
                self.assertEqual(binError, 0)
        
Josef Brandt's avatar
Josef Brandt committed
445 446
        # assume only 50 % was measured
        bins, mpCountErrorsPerBin = self.subsamplingResult._get_mp_count_error_per_bin(fullParticles, subParticles, 0.5)
447 448 449 450 451

        for binIndex, binError in enumerate(mpCountErrorsPerBin):
            self.assertEqual(binError, 0)

    def test_get_number_of_MP_particles(self):
Josef Brandt's avatar
Josef Brandt committed
452
        mpParticles = get_MP_particles(5)
453 454
        numMPParticles = len(mpParticles)
        
Josef Brandt's avatar
Josef Brandt committed
455
        nonMPparticles = get_non_MP_particles(50)
456 457 458
        
        allParticles = mpParticles + nonMPparticles
        
Josef Brandt's avatar
Josef Brandt committed
459
        calculatedNumMPParticles = self.subsamplingResult._get_number_of_MP_particles(allParticles)
460 461 462
        self.assertEqual(numMPParticles, calculatedNumMPParticles)

    def test_get_mp_count_error(self):
Josef Brandt's avatar
Josef Brandt committed
463 464
        mpParticles1 = get_MP_particles(20)
        nonMPparticles1 = get_non_MP_particles(20)
465 466
        origParticles = mpParticles1 + nonMPparticles1
        
Josef Brandt's avatar
Josef Brandt committed
467
        mpParticles2 = get_MP_particles(30)
468
        estimateParticles = mpParticles2 + nonMPparticles1
Josef Brandt's avatar
Josef Brandt committed
469
        mpCountError = self.subsamplingResult._get_mp_count_error(origParticles, estimateParticles, 1.0)
470
        self.assertEqual(mpCountError, 50)
471
        
Josef Brandt's avatar
Josef Brandt committed
472
        mpParticles2 = get_MP_particles(20)
473
        estimateParticles = mpParticles2 + nonMPparticles1
Josef Brandt's avatar
Josef Brandt committed
474
        mpCountError = self.subsamplingResult._get_mp_count_error(origParticles, estimateParticles, 1.0)
475 476
        self.assertEqual(mpCountError, 0)
        
Josef Brandt's avatar
Josef Brandt committed
477
        mpCountError = self.subsamplingResult._get_mp_count_error(origParticles, estimateParticles, 0.5)
478
        self.assertEqual(mpCountError, 100)
Josef Brandt's avatar
Josef Brandt committed
479 480 481 482 483 484 485 486

    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)
        
487
    def test_get_averaged_erros(self):
Josef Brandt's avatar
Josef Brandt committed
488 489 490
        self.subsamplingResult.mpCountErrors = [50, 75, 100]
        self.assertEqual(self.subsamplingResult.mpCountError, 75)

491 492
    def test_get_error_from_values(self):
        exact, estimate = 100, 90
Josef Brandt's avatar
Josef Brandt committed
493
        error = self.subsamplingResult._get_error_from_values(exact, estimate)
494
        self.assertEqual(error, 10)
495 496
       
        exact, estimate = 100, 110
Josef Brandt's avatar
Josef Brandt committed
497
        error = self.subsamplingResult._get_error_from_values(exact, estimate)
498
        self.assertEqual(error, 10)
499 500
        
        exact, estimate = 100, 50
Josef Brandt's avatar
Josef Brandt committed
501
        error = self.subsamplingResult._get_error_from_values(exact, estimate)
502
        self.assertEqual(error, 50)
503 504
        
        exact, estimate = 100, 150
Josef Brandt's avatar
Josef Brandt committed
505
        error = self.subsamplingResult._get_error_from_values(exact, estimate)
506
        self.assertEqual(error, 50)
Josef Brandt's avatar
Josef Brandt committed
507 508 509 510 511 512 513 514 515 516

        exact, estimate = 100, 200
        error = self.subsamplingResult._get_error_from_values(exact, estimate)
        self.assertEqual(error, 100)

        exact, estimate = 50, 100
        error = self.subsamplingResult._get_error_from_values(exact, estimate)
        self.assertEqual(error, 100)


517
        
Josef Brandt's avatar
Josef Brandt committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    # def _get_MP_particles(self, numParticles) -> list:
    #     mpParticles = []
    #     for _ in range(numParticles):
    #         mpParticles.append(self._get_MP_particle())
    #     return mpParticles
    #
    # def _get_non_MP_particles(self, numParticles) -> list:
    #     nonMPParticles = []
    #     for _ in range(numParticles):
    #         nonMPParticles.append(self._get_non_MP_particle())
    #     return nonMPParticles
    #
    # def _get_MP_particle(self) -> Particle:
    #     random.seed(15203018)
    #     polymerNames = ['Poly (methyl methacrylate',
    #                     'Polyethylene',
    #                     'Silicone rubber',
    #                     'PB15',
    #                     'PY13',
    #                     'PR20']
    #     polymName = random.sample(polymerNames, 1)[0]
    #     newParticle: Particle = Particle()
    #     newMeas = Measurement()
    #     newMeas.setAssignment(polymName)
    #     newParticle.addMeasurement(newMeas)
    #     return newParticle
    #
    # def _get_non_MP_particle(self) -> Particle:
    #     newParticle: Particle = Particle()
    #     newParticle.addMeasurement(Measurement())
    #     return newParticle