subsampling.py 3.92 KB
Newer Older
1 2 3 4 5 6
import numpy as np
import matplotlib.pyplot as plt
import time
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
from gepard import dataset
Josef Brandt's avatar
Josef Brandt committed
7
import gepardevaluation
8 9

from methods import IvlevaSubsampling, RandomSampling, SizeBinFractioning
10
from geometricMethods import BoxSelectionCreator
11
from helpers import ParticleBinSorter
12
from evaluation import ResultComparer, ResultObject
13 14


Josef Brandt's avatar
Josef Brandt committed
15 16 17 18 19 20 21 22 23 24
"""
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')
25

Josef Brandt's avatar
Josef Brandt committed
26 27 28
# 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'
29

Josef Brandt's avatar
Josef Brandt committed
30
for index, fname in enumerate(workingFiles):
31 32 33
    # dset = dataset.loadData(fname)
    newObj: ResultObject = ResultObject(fname)
    # print('loaded dataset', fname)
Josef Brandt's avatar
Josef Brandt committed
34 35 36 37 38

# boxCreator = BoxSelectionCreator(dset)
# center, size = boxCreator.get_filterDimensions_from_dataset()
# print(center, size)
# print(dset.mapToPixel(center, force=True))
39

40 41
# pc = dset.particleContainer
# origParticles = pc.particles
42

43 44 45 46 47
# resultComparer = ResultComparer()
# numOrigMP = resultComparer._get_number_of_MP_particles(origParticles)
# print(f'orig particles: {len(origParticles)}, of which are mp: {numOrigMP}')
# # ivlevaSampling = IvlevaSubsampling(pc)
# # ivlevaFraction, ivlevaParticles = ivlevaSampling.apply_subsampling_method()
48

49 50 51 52 53
# t0 = time.time()
# fractions = np.arange(0.05, .55, 0.05)
# errors = []
# binErrors = []
# numIterations = 1000
Josef Brandt's avatar
Josef Brandt committed
54

55 56 57 58 59 60 61 62 63 64 65
# for fraction in fractions:
#     print('random sampling, fraction:', fraction)
# #    randomSampling = RandomSampling(pc, desiredFraction=fraction)
#     randomSampling = SizeBinFractioning(pc, fraction)
#     iterErrors = []
#     binIterErrors = []
#     for _ in range(numIterations):
#         randomFraction, randomParticles = randomSampling.apply_subsampling_method()
#         iterErrors.append(resultComparer._get_mp_count_error(origParticles, randomParticles, randomFraction))
#         bins, errorsPerBin = resultComparer._get_mp_count_error_per_bin(origParticles, randomParticles, randomFraction)
#         binIterErrors.append(errorsPerBin)
66

67 68 69 70 71 72
#     errors.append(round(np.mean(iterErrors)*100)) #from fraction to %
#     fractionBinErrors = []
#     for binIndex in range(len(bins)+1):
#         binError = round(np.mean([binIterErrors[i][binIndex] for i in range(numIterations)]) * 100)
#         fractionBinErrors.append(binError)
#     binErrors.append(fractionBinErrors)
73

74 75 76 77 78 79 80 81
# print('random sampling took', np.round(time.time()-t0, 2), 'seonds')
# binLowerLimits = bins.copy()
# binLowerLimits.insert(0, 0)
# plt.subplot(121)
# plt.plot(fractions, errors)
# # plt.title(f'Random Sampling, averaged from {numIterations} trials, orig particle count: {len(origParticles)}')
# plt.xlabel('Fraction measured')
# plt.ylabel('Average error in MP particle count (%)')
82

83 84 85 86 87 88 89 90
# plt.subplot(122)
# for fracMeas, curBinErrors in zip(fractions, binErrors):
#     plt.plot(binLowerLimits, curBinErrors, label=np.round(fracMeas, 1))
# # plt.title('Error in MP count (%) per size bin')
# plt.xlabel('particle size')
# plt.ylabel('Average error in MP particle count (%)')
# plt.legend()
# plt.show()
91

92 93 94

# # sizeBinSampling = SizeBinFractioning(pc)
# # sizeBinParticles = sizeBinSampling.apply_subsampling_method()