-
Notifications
You must be signed in to change notification settings - Fork 1
/
MAIN.m
197 lines (173 loc) · 9.77 KB
/
MAIN.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function [geoRef,climateData,queryDates,learningDates,refValidation,additionalVars, ...
Weights,sortedDates,synImages,validationMetric,sensitivityResults,optimisedWeights] = MAIN(...
rawDir,outputDir,optiWeightsDir,maskDir,targetVar,climateVars,addVars,normMethods,QdateStart,QdateEnd,LdateStart,LdateEnd,outputTime,targetDim,saveMats, ...
maxThreshold,shortWindow,longWindow,daysRange,nbImages,metricKNN,ensemble,generationType,mps,outputType,coordRefSysCode,parallelComputing, ...
netCDFtoInputs,createGenWeights,kNNsorting,generateImage,bootstrap,bsSaveAll,validationPrep,validation,pixelWise, ...
metricViz,metricV,nanValue,varLegend,varRange,errRange,sensiAnalysis,nbImages_range,longWindow_range,optimPrep,saveOptimPrep,optimisation,nbOptiRuns)
%% Setup
close all
poolobj = gcp('nocreate');
delete(poolobj);
tStart = tic;
if LdateStart > LdateEnd
error('Learning date start > learning date end')
elseif QdateStart > QdateEnd
error('Query date start > Query date end')
end
inDir = strcat(outputDir,'\inputData');
outDir = strcat(outputDir,'\output');
%% Reading the data needed for ranking learning dates using "KNNDataSorting" Function
if sensiAnalysis == false
disp('--- 1. READING DATA ---')
if netCDFtoInputs == true || optimPrep == true || validationPrep == true
disp('Formatting input data...')
rawData = convertRawDataToStructure(targetVar,targetDim,climateVars,addVars,rawDir,inDir);
disp('Extracting georeference informations...')
if targetDim ~= 1
geoRef = extractGeoInfo(targetVar,coordRefSysCode,rawDir,inDir);
else
geoRef = [];
end
disp('Extracting climate informations...')
climateData = extractClimateData(climateVars,rawData,normMethods,QdateStart,QdateEnd,LdateStart,LdateEnd,longWindow,inDir,saveMats);
disp('Extracting Learning dates...')
learningDates = convertStructureToLearningDates(targetVar,LdateStart,LdateEnd,QdateStart,QdateEnd,rawData,climateData,targetDim,optimPrep,inDir,saveMats);
disp('Extracting Query dates...')
[queryDates,learningDates,refValidation] = convertStructureToQueryDates(targetVar,targetDim,QdateStart,QdateEnd,learningDates,climateData,maxThreshold,validationPrep,optimPrep,outputTime,inDir,outDir,saveMats);
disp('Extracting additional variables...')
additionalVars = extractAdditionalVars(addVars,rawData,climateData,QdateStart,QdateEnd,LdateStart,LdateEnd,maxThreshold,inDir);
elseif netCDFtoInputs == false && validationPrep == false
disp('Loading QueryDates.mat file...')
queryDates = load(fullfile(inDir,'queryDates.mat'));
queryDates = queryDates.queryDates;
disp('Loading LearningDates.mat file...')
learningDates = load(fullfile(inDir,'learningDates.mat'));
learningDates = learningDates.learningDates;
disp('Loading climateData.mat file...')
climateData = load(fullfile(inDir,'climateData.mat'));
climateData = climateData.climateData;
disp('Loading additionalVars.mat file...')
additionalVars = load(fullfile(inDir,'additionalVars.mat'));
additionalVars = additionalVars.additionalVars;
disp('Loading GeoRef.mat file...')
if targetDim ~= 1
geoRef = load(fullfile(inDir,'GeoRef.mat'));
geoRef = geoRef.geoRef;
else
geoRef = [];
end
if optimisation == true || validation == true || metricViz == true
disp('Loading refValidation.mat file...')
refValidation = load(fullfile(inDir,'refValidation.mat'));
refValidation = refValidation.refValidation;
else
refValidation = [];
end
end
if createGenWeights == true || optimPrep == true || optimisation == true
disp('Creating generic weights...')
Weights = createWeights(targetVar,climateVars,addVars,inDir);
elseif createGenWeights == false
disp('Loading optimisedWeights.mat file...')
optimisedWeights = load(optiWeightsDir);
Weights = optimisedWeights.optimisedWeights;
end
disp('--- 1. READING DATA DONE ---')
%% The function for Ranking the learning dates
disp('--- 2. KNN DATA SORTING ---')
% Generate ranked Learning Dates for each Query Date
if kNNsorting == true || validationPrep == true || optimPrep == true
if pixelWise == false
sortedDates = kNNDataSorting(targetVar,climateVars,addVars,queryDates,learningDates,climateData,additionalVars,normMethods,shortWindow,longWindow,daysRange,Weights,nbImages,metricKNN,optimPrep,saveOptimPrep,parallelComputing,inDir,saveMats);
else
sortedDates = pixelWise_kNNDataSorting(maskDir,targetVar,climateVars,addVars,queryDates,learningDates,climateData,additionalVars,normMethods,shortWindow,longWindow,daysRange,Weights,nbImages,metricKNN,optimPrep,saveOptimPrep,parallelComputing,inDir);
end
elseif kNNsorting == false && validationPrep == false && (optimPrep == false && optimisation == false)
disp('Loading sortedDates.mat file...')
sortedDates = load(fullfile(inDir,'KNNSorting.mat'));
sortedDates = sortedDates.sortedDates;
elseif optimPrep == false && optimisation == true
disp('Loading KNNDistances.mat file...')
sortedDates = load(fullfile(inDir,'KNNDistances.mat'));
sortedDates = sortedDates.sortedDates;
end
disp('--- 2. KNN DATA SORTING DONE ---')
%% Generation of Synthetic Images
disp('--- 3. SYNTHETIC IMAGES GENERATION ---')
if (generateImage == true || validation == true) && optimisation == false
if pixelWise == false
synImages = generateSynImages(targetVar,targetDim,learningDates,sortedDates,mps,geoRef,outDir,generationType,nanValue,validation,optimisation,bootstrap,bsSaveAll,nbImages,ensemble,outputType);
else
synImages = pixelWise_generateSynImages(maskDir,targetVar,learningDates,sortedDates,geoRef,outDir,generationType,validation,optimisation,bootstrap,bsSaveAll,nbImages,ensemble,outputType);
end
elseif optimisation == true && validation == false
disp('Optimisation run, synthetic image generation skipped...')
synImages = [];
elseif metricViz == true
disp('Loading synValidation.mat file...')
synImages = load(fullfile(outDir,'synValidation.mat'));
synImages = synImages.synImages;
elseif generateImage == false && validation == false
disp('Synthetic image generation skipped...')
synImages = [];
end
disp('--- 3. SYNTHETIC IMAGES GENERATION DONE ---')
%% Validation
if (validation == true || metricViz == true) && optimisation == false
disp('--- 4. VALIDATION ---')
validationMetric = validationMetrics(targetVar,targetDim,metricV,optimisation,refValidation,synImages,bootstrap,ensemble,outDir);
visualiseMetrics(nbImages,pixelWise,targetVar,targetDim,refValidation,synImages,validationMetric,sortedDates,metricV,nanValue,varLegend,varRange,errRange,metricKNN,LdateStart,LdateEnd,QdateStart,QdateEnd,daysRange,bootstrap,outDir);
disp('--- 4. VALIDATION DONE ---')
else
validationMetric = [];
end
end
%% Sensitivity analysis
if sensiAnalysis == true
disp('--- SENSITIVITY ANALYSIS')
disp('Formatting input data...')
rawData = convertRawDataToStructure(targetVar,targetDim,climateVars,addVars,rawDir,inDir);
disp('Extracting georeference informations...')
geoRef = [];
[climateData,queryDates,learningDates,refValidation,additionalVars, ...
Weights,sortedDates,synImages,validationMetric,sensitivityResults] = sensitivityAnalysis(rawData,nbImages_range,longWindow_range,inDir,outDir,targetVar,climateVars,addVars,normMethods,QdateStart,QdateEnd,LdateStart,LdateEnd,outputTime,targetDim, ...
nanValue,maxThreshold,daysRange,metricKNN,ensemble,generationType,parallelComputing,bootstrap,bsSaveAll,metricV);
optimisedWeights = [];
disp('--- SENSITIVITY ANALYSIS DONE')
else
sensitivityResults = [];
end
%% Optimisation
if optimisation == true
disp('--- 4. OPTIMISATION ---')
if saveOptimPrep == true
sortedDates = [];
end
% Get the table variable names
variableNames = string(Weights.Properties.VariableNames);
% Iterate over each variable
for i = 1:numel(variableNames)
bayesWeights(i) = optimizableVariable(variableNames{i}, [0, 1], 'Type', 'real');
end
% Set up the Bayesian optimization
fun = @(x)computeObjectiveOptim(x.(1), x.(2), x.(3), x.(4), x.(5), x.(6), x.(7), x.(8), x.(9), ...
targetVar, addVars, learningDates, sortedDates, refValidation, saveOptimPrep, nbImages, ...
geoRef, generationType, bootstrap, ensemble, metricV, validation, optimisation, inDir, outDir);
% Run the Bayesian optimization
%if parallelComputing == true
% results = bayesopt(fun,bayesWeights,'Verbose',0,'AcquisitionFunctionName','expected-improvement-plus','MaxObjectiveEvaluations',nbOptiRuns,'UseParallel',true);
%else
results = bayesopt(fun,bayesWeights,'Verbose',0,'AcquisitionFunctionName','expected-improvement-plus','MaxObjectiveEvaluations',nbOptiRuns);
%end
% Retrieve the optimal weights
disp(' Saving optimisedWeights.mat...')
optimisedWeights = results.XAtMinObjective;
optimisedWeights = array2table(table2array(optimisedWeights) ./ sum(table2array(optimisedWeights)),'VariableNames', results.XAtMinObjective.Properties.VariableNames)
save(fullfile(inDir,'optimisedWeights.mat'), 'optimisedWeights', '-v7.3','-nocompression');
disp('--- 4. OPTIMISATION DONE ---')
else
optimisedWeights = [];
end
tEnd = toc(tStart);
disp(['--- FINISHED IN ' num2str(tEnd) ' SECONDS ---'])
end