-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualiseMetrics.m
726 lines (674 loc) · 36.2 KB
/
visualiseMetrics.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
function visualiseMetrics(nbImages,pixelWise,targetVar,targetDim,refValidation,synImages,validationMetric,sortedDates,metricV,nanValue,varLegend,varRange,errRange,metricKNN,LdateStart,LdateEnd,QdateStart,QdateEnd,daysRange,bootstrap,outDir)
%
%
%
% REDO DOCUMENTATION
%
%
%
startLdate = char(datetime(LdateStart,'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy'));
startQdate = char(datetime(QdateStart,'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy')-days(1));
endLdate = char(datetime(LdateEnd,'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy'));
endQdate = char(datetime(QdateEnd,'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy')+days(1));
targetVarL = lower(targetVar);
for k = 1:numel(targetVar)
if bootstrap == true
dates = datetime(cell2mat(validationMetric.(targetVarL(k))(:,1)),'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy');
% Validation data
singleDataVal = cell2mat(validationMetric.(targetVarL(k))(:,3));
maxValuesVal = zeros(size(validationMetric.(targetVarL(k)), 1), 1);
meanValuesVal = zeros(size(validationMetric.(targetVarL(k)), 1), 1);
minValuesVal = zeros(size(validationMetric.(targetVarL(k)), 1), 1);
for i = 1:size(validationMetric.(targetVarL(k)), 1)
valuesVal = sort(validationMetric.(targetVarL(k)){i, 2}); % Extract values from the second column of the cell
maxValuesVal(i) = valuesVal(end); % Compute the maximum value
meanValuesVal(i) = mean(valuesVal);
minValuesVal(i) = valuesVal(1); % Compute the minimum value
end
% Synthetic data
varBs = strcat(targetVar(k),'_Bootstrap');
varBD = strcat(targetVar(k),'_BestDistance');
refData = squeeze(mean(mean(refValidation.(targetVarL(k)),1,'omitnan'),2,'omitnan')); % Extract mean of ref variable
synData = squeeze(mean(mean(synImages.(targetVarL(k)),1,'omitnan'),2,'omitnan'));
maxValues = zeros(size(validationMetric.(targetVarL(k)), 1), 1);
meanValues = zeros(size(validationMetric.(targetVarL(k)), 1), 1);
minValues = zeros(size(validationMetric.(targetVarL(k)), 1), 1);
for i = 1:size(validationMetric.(targetVarL(k)), 1)
synValues = sort(squeeze(mean(mean(synImages.(varBs(k)){i},'omitnan'),'omitnan'))); % Extract mean of variable and sort
maxValues(i) = synValues(end); % Compute the maximum value
meanValues(i) = mean(synValues);
minValues(i) = synValues(1); % Compute the minimum value
end
bestDist = synImages.(varBD);
% -----------------------------------------------------------------------------------------------------
figure('WindowState', 'maximized');
hold on
inBetweenRegionX = [dates', fliplr(dates')];
inBetweenRegionY = [maxValues', fliplr(minValues')];
patch(inBetweenRegionX, inBetweenRegionY, 'k', 'LineStyle', 'none', 'FaceAlpha', 0.25)
%plot(dates, meanValues, 'k--', 'LineWidth', 1)
plot(dates, synData, 'k-', 'LineWidth', 1)
plot(dates, refData, 'r-', 'LineWidth', 1)
hold off
title([convertStringsToChars(targetVar(k)) ' - MEAN'])
r = corr(synData,refData);
%nseSynRef = 1-(sum((synData-refData).^2)/sum((synData-mean(synData)).^2));
alpha = std(synData)/std(refData);
beta = mean(synData)/mean(refData);
kgeSynRef = 1-(sqrt((r-1)^2 + (alpha-1)^2 + (beta-1)^2));
str = {['KGE: ' num2str(kgeSynRef,'%.5f')] ['r: ' num2str(r,'%.5f') ', \alpha: ' num2str(alpha,'%.5f') ', \beta: ' num2str(beta,'%.5f')]};
if strcmp(startLdate, startQdate)
subtitle([['Learning periode: ' endQdate '-' endLdate] str])
elseif strcmp(endQdate, endLdate)
subtitle([['Learning periode: ' startLdate '-' startQdate] str])
else
subtitle([['Learning periode: ' startLdate '-' startQdate ' - ' endQdate '-' endLdate] str])
end
xlabel('Date')
ylabel(strcat("Mean ", targetVar(k)))
%legend('Synthetic data spread','Synthetic data mean','Deterministic mean','Reference data mean')
legend('Synthetic data spread','Deterministic mean','Reference data mean')
set(gcf, 'color', 'white');
grid on
saveas(gcf,strcat(outDir,['\bsValidation_AVG_' convertStringsToChars(targetVar(k)) '.png']))
% -----------------------------------------------------------------------------------------------------------------------------
figure('WindowState', 'maximized');
hold on
inBetweenRegionX = [dates', fliplr(dates')];
inBetweenRegionY = [maxValuesVal', fliplr(minValuesVal')];
patch(inBetweenRegionX, inBetweenRegionY, 'k', 'LineStyle', 'none', 'FaceAlpha', 0.25)
%plot(dates, meanValuesVal, 'k--', 'LineWidth', 1)
plot(dates, singleDataVal, 'k-', 'LineWidth', 1)
yyaxis right
plot(dates, bestDist, 'r', 'LineWidth', 1)
if metricKNN == 1
ylabel('RMSE')
elseif metricKNN == 2
ylabel('MAE')
elseif metricKNN == 3
ylabel('1-bSPEM')
elseif metricKNN == 4
ylabel('Hellinger distance')
elseif metricKNN == 5
ylabel('0.5*(1-bSPEM) + 0.5*Hellinger')
elseif metricKNN == 6
ylabel('SPAEF')
end
% for i = 1:numel(dates)
% ensemble = ensembleData{i};
% for j = 1:numel(ensemble)
% plot(dates(i), ensemble(j), 'k.'); % Plot the value against the date
% end
% end
hold off
%xtickangle(45); % Rotate x-axis labels for better readability
%yline(mean(validationMetric.(var(k))(:,2)),'-',['Mean: ' num2str(mean(validationMetric.(var(k))(:,2)))],'Color','r')
%ylim([0 1.4])
yyaxis left
if metricV == 1
title([convertStringsToChars(targetVar(k)) ' - MAE'])
elseif metricV == 2
title([convertStringsToChars(targetVar(k)) ' - RMSE'])
elseif metricV == 3
title([convertStringsToChars(targetVar(k)) ' - SPEM'])
elseif metricV == 4
title([convertStringsToChars(targetVar(k)) ' - SPAEF'])
elseif metricV == 5
title([convertStringsToChars(targetVar(k)) ' - KGE'])
end
%str = {['Mean RMSE: ' num2str(mean(singleDataVal),'%.5f')], ['Mean ensemble RMSE: ' num2str(mean(meanValuesVal),'%.5f')]};
str = {['Mean RMSE: ' num2str(mean(singleDataVal),'%.5f')], ['RMSE - MAE correlation: ' num2str(corr(singleDataVal,bestDist),'%.5f')]};
if strcmp(startLdate, startQdate)
subtitle([['Learning periode: ' endQdate '-' endLdate] str])
elseif strcmp(endQdate, endLdate)
subtitle([['Learning periode: ' startLdate '-' startQdate] str])
else
subtitle([['Learning periode: ' startLdate '-' startQdate ' - ' endQdate '-' endLdate] str])
end
xlabel('Date')
if metricV == 1
ylabel('MAE')
elseif metricV == 2
ylabel('RMSE')
elseif metricV == 3
ylabel('SPEM')
elseif metricV == 4
ylabel('SPAEF')
elseif metricV == 5
ylabel('KGE')
end
ax = gca;
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = 'r';
linkprop(ax.YAxis, 'Limits');
%legend('Stochastic ensembles','Stochastic mean','Deterministic','Best distance')
legend('Stochastic ensembles','Deterministic','Best distance')
set(gcf, 'color', 'white');
grid on
saveas(gcf,strcat(outDir,['\bsValidation_RMSE_' convertStringsToChars(targetVar(k)) '.png']))
else
if targetDim == 1
figure('WindowState', 'maximized');
plot(datetime(validationMetric.(targetVarL(k))(:,1),'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy'), ...
refValidation.(targetVarL(k)),'Color','r','DisplayName','Reference');
hold on
plot(datetime(validationMetric.(targetVarL(k))(:,1),'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy'), ...
synImages.(targetVarL(k)),'Color','b','DisplayName','Synthetic')
hold off
if strcmp(startLdate, startQdate)
subtitle(['Learning periode: ' endQdate '-' endLdate])
elseif strcmp(endQdate, endLdate)
subtitle(['Learning periode: ' startLdate '-' startQdate])
else
subtitle(['Learning periode: ' startLdate '-' startQdate ' - ' endQdate '-' endLdate])
end
xlabel('Date')
ylabel('Discharge [m^{3}/s]')
title(targetVar)
legend()
set(gcf, 'color', 'white');
grid on
saveas(gcf,strcat(outDir,['\' convertStringsToChars(targetVar(k)) '.png']))
end
figure('WindowState', 'maximized');
plot(datetime(validationMetric.(targetVarL(k))(:,1),'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy'), ...
validationMetric.(targetVarL(k))(:,2));
yline(mean(validationMetric.(targetVarL(k))(:,2)),'-',['Mean: ' num2str(mean(validationMetric.(targetVarL(k))(:,2)))],'Color','r')
%ylim([0 1.4])
if metricV == 1
title([convertStringsToChars(targetVar(k)) ' - MAE (mean: ' num2str(mean(validationMetric.(targetVarL(k))(:,2))) ')'])
elseif metricV == 2
title([convertStringsToChars(targetVar(k)) ' - RMSE (mean: ' num2str(mean(validationMetric.(targetVarL(k))(:,2))) ')'])
elseif metricV == 3
title([convertStringsToChars(targetVar(k)) ' - SPEM (mean: ' num2str(mean(validationMetric.(targetVarL(k))(:,2))) ')'])
elseif metricV == 4
title([convertStringsToChars(targetVar(k)) ' - SPAEF (mean: ' num2str(mean(validationMetric.(targetVarL(k))(:,2))) ')'])
elseif metricV == 5
title([convertStringsToChars(targetVar(k)) ' - KGE (mean: ' num2str(mean(validationMetric.(targetVarL(k))(:,2))) ')'])
end
if strcmp(startLdate, startQdate)
subtitle(['Learning periode: ' endQdate '-' endLdate])
elseif strcmp(endQdate, endLdate)
subtitle(['Learning periode: ' startLdate '-' startQdate])
else
subtitle(['Learning periode: ' startLdate '-' startQdate ' - ' endQdate '-' endLdate])
end
xlabel('Date')
if metricV == 1
ylabel('MAE')
elseif metricV == 2
ylabel('RMSE')
elseif metricV == 3
ylabel('SPEM')
elseif metricV == 4
ylabel('SPAEF')
elseif metricV == 5
ylabel('KGE')
end
set(gcf, 'color', 'white');
grid on
saveas(gcf,strcat(outDir,['\validation_' convertStringsToChars(targetVar(k)) '.png']))
% --------------------------------------------------------------------
if targetDim ~= 1
refData = refValidation.(targetVarL(k));
synData = synImages.(targetVarL(k));
if ~isnan(nanValue)
refData(refData == nanValue) = nan;
synData(isnan(refData)) = nan;
end
meanRefData = squeeze(mean(mean(refData,1,'omitnan'),2,'omitnan')); % Extract mean of ref variable
meanSynData = squeeze(mean(mean(synData,1,'omitnan'),2,'omitnan'));
figure('WindowState', 'maximized');
date = datetime(validationMetric.(targetVarL(k))(:,1),'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy');
plot(date, meanRefData, 'r-', date, meanSynData, 'k-');
legend('Reference','Synthetic','Location','southeast')
xlabel('Date')
ylabel(varLegend)
title(['Mean ' convertStringsToChars(targetVarL(k))])
r = corr(meanSynData,meanRefData);
%nseSynRef = 1-(sum((synData-refData).^2)/sum((synData-mean(synData)).^2));
alpha = std(meanSynData)/std(meanRefData);
beta = mean(meanSynData)/mean(meanRefData);
kgeSynRef = 1-(sqrt((r-1)^2 + (alpha-1)^2 + (beta-1)^2));
str = {['KGE: ' num2str(kgeSynRef,'%.5f')] ['r: ' num2str(r,'%.5f') ', \alpha: ' num2str(alpha,'%.5f') ', \beta: ' num2str(beta,'%.5f')]};
subtitle(str)
grid on
box off
%legend boxoff
set(gcf, 'color', 'white');
saveas(gcf,strcat(outDir,['\correlation_' convertStringsToChars(targetVar(k)) '.png']))
% --------------------------------------------------------------------
% % Set the filter order (adjust as needed)
% filterOrder = 3;
% % Set the cutoff frequency for the high-pass filter (adjust as needed)
% cutoffFrequency = 0.01; % Adjust this value based on your data characteristics
% % Design a Butterworth high-pass filter
% [b, a] = butter(filterOrder, cutoffFrequency, 'high');
%
% % Apply the filter to both reference and synthetic datasets
% refDataHighPass = filtfilt(b, a, double(meanRefData));
% synDataHighPass = filtfilt(b, a, double(meanSynData));
%
% % Plot the original and high-pass filtered data
% figure('WindowState', 'maximized');
% date = datetime(validationMetric.(targetVarL(k))(:, 1), 'ConvertFrom', 'yyyyMMdd', 'Format', 'dd/MM/yyyy');
% plot(date, refDataHighPass, 'r-', date, synDataHighPass, 'k-');
% legend('Reference', 'Synthetic', 'Location', 'southeast');
% title('High-Pass Filtered Data');
% xlabel('Date');
% ylabel(strcat('Detrended ', varLegend));
% grid on;
% r = corr(synDataHighPass,refDataHighPass);
% %nseSynRef = 1-(sum((synData-refData).^2)/sum((synData-mean(synData)).^2));
% alpha = std(synDataHighPass)/std(refDataHighPass);
% beta = mean(synDataHighPass)/mean(refDataHighPass);
% kgeSynRef = 1-(sqrt((r-1)^2 + (alpha-1)^2 + (beta-1)^2));
% str = {['KGE: ' num2str(kgeSynRef,'%.5f')] ['r: ' num2str(r,'%.5f') ', \alpha: ' num2str(alpha,'%.5f') ', \beta: ' num2str(beta,'%.5f')]};
% subtitle(str)
% grid on
% box off
% %legend boxoff
% set(gcf, 'color', 'white');
% saveas(gcf,strcat(outDir,['\correlation_' convertStringsToChars(targetVar(k)) '_highpass.png']))
% --------------------------------------------------------------------
varRefData = squeeze(var(refData,0,[1 2],'omitnan'));
varSynData = squeeze(var(synData,0,[1 2],'omitnan'));
figure('WindowState', 'maximized');
date = datetime(validationMetric.(targetVarL(k))(:,1),'ConvertFrom','yyyyMMdd','Format','dd/MM/yyyy');
plot(date, varRefData, 'r-', date, varSynData, 'k-');
legend('Reference','Synthetic','Location','northeast')
xlabel('Date')
ylabel(strcat(varLegend,'^2'))
title([convertStringsToChars(targetVar(k)) ' variance'])
r = corr(varSynData,varRefData);
%nseSynRef = 1-(sum((synData-refData).^2)/sum((synData-mean(synData)).^2));
alpha = std(varSynData)/std(varRefData);
beta = mean(varSynData)/mean(varRefData);
kgeSynRef = 1-(sqrt((r-1)^2 + (alpha-1)^2 + (beta-1)^2));
str = {['KGE: ' num2str(kgeSynRef,'%.5f')] ['r: ' num2str(r,'%.5f') ', \alpha: ' num2str(alpha,'%.5f') ', \beta: ' num2str(beta,'%.5f')]};
subtitle(str)
grid on
box off
%legend boxoff
set(gcf, 'color', 'white');
saveas(gcf,strcat(outDir,['\variance_' convertStringsToChars(targetVar(k)) '.png']))
% -------------------------------------------------------------------------
refData = refValidation.(targetVarL(k));
synData = synImages.(targetVarL(k));
absDayErr = zeros(size(synData));
dayErr = absDayErr;
relErr = absDayErr;
for i = 1:size(synData,3)
absDayErr(:,:,i) = abs(synData(:,:,i) - refData(:,:,i));
dayErr(:,:,i) = synData(:,:,i) - refData(:,:,i);
relErr(:,:,i) = abs((synData(:,:,i) - refData(:,:,i))./refData(:,:,i));
end
meanError = mean(absDayErr,3);
meanBias = mean(dayErr,3);
meanRelErr = mean(relErr,3);
figure;
figMean = imagesc(meanError);
set(figMean, 'AlphaData', ~isnan(meanError))
colormap(gca, turbo(256));
caxis([0.2 0.6])
title('Mean absolute error')
subtitle(['Mean MAE: ' num2str(mean(mean(meanError,'omitnan'),'omitnan'),'%1.5f')])
set(gcf, 'color', 'white');
hcb=colorbar;
set(get(hcb,'label'),'string','Mean absolute error [mm/day]','Rotation',90);
axis equal off
saveas(gcf,strcat(outDir,['\mae_' convertStringsToChars(targetVar(k)) '.png']))
figure;
figMean = imagesc(meanBias);
set(figMean, 'AlphaData', ~isnan(meanBias))
colormap(gca, turbo(256));
caxis([-0.2 0.2])
title('Bias')
subtitle(['Mean bias: ' num2str(mean(mean(meanBias,'omitnan'),'omitnan'),'%1.5f')])
set(gcf, 'color', 'white');
hcb=colorbar;
set(get(hcb,'label'),'string','Bias [mm/day]','Rotation',90);
axis equal off
saveas(gcf,strcat(outDir,['\bias_' convertStringsToChars(targetVar(k)) '.png']))
figure;
figMean = imagesc(meanRelErr);
set(figMean, 'AlphaData', ~isnan(meanRelErr))
colormap(gca, turbo(256));
caxis([0 1])
title('Mean relative error')
subtitle(['Mean: ' num2str(mean(mean(meanRelErr,'omitnan'),'omitnan'),'%1.5f')])
set(gcf, 'color', 'white');
hcb=colorbar;
set(get(hcb,'label'),'string','Mean relative error','Rotation',90);
axis equal off
saveas(gcf,strcat(outDir,['\mre_' convertStringsToChars(targetVar(k)) '.png']))
% -------------------------------------------------------------------------
synDates = synImages.date;
dates = datetime(synDates, 'ConvertFrom', 'yyyyMMdd', 'format', 'dd/MM/yyyy');
cellData = synImages.(strcat(targetVarL(k), "_Distances")); % Assuming this is a cell array with 25x1 arrays
minValues = cellfun(@min, cellData);
maxValues = cellfun(@max, cellData);
medianValues = cellfun(@median, cellData);
figure;
hold on;
fill([dates; flipud(dates)], [minValues; flipud(maxValues)], 'k', 'FaceAlpha', 0.2, 'EdgeColor', 'none');
for i = 1:numel(cellData)
scatter(repmat(dates(i), 25, 1), cellData{i}, 20, 'k', 'filled', 'MarkerFaceAlpha', 0.5);
end
plot(dates, medianValues, 'r--', 'LineWidth', 1.5);
xlabel('Date');
switch metricKNN
case 1
ylabel('RMSE');
case 2
ylabel('MAE');
case 3
ylabel('1-bSPEM');
case 4
ylabel('Hellinger Distance');
case 5
ylabel('0.5*(1-bSPEM) + 0.5*Hellinger');
case 6
ylabel('SPAEF');
end
title('Daily distance of the k candidates');
grid on;
hold off;
set(gcf, 'color', 'white');
saveas(gcf,strcat(outDir,['\distance_' convertStringsToChars(targetVar(k)) '.png']))
% -------------------------------------------------------------------------
% Set the output GIF file name
if pixelWise == false
gifVal = fullfile(outDir,['\validation_' convertStringsToChars(targetVar(k)) '.gif']);
refDates = refValidation.date;
synDates = synImages.date;
dates = datetime(synDates,'ConvertFrom','yyyyMMdd','format','dd/MM/yyyy');
bdName = [convertStringsToChars(targetVar(k)) '_BestDistance'];
analogs = sortedDates(:,2);
currentDOY = nan(numel(analogs{1}),numel(dates));
meanDOY = nan(numel(dates),1);
diffDOY = nan(numel(analogs{1}),1);
diffBest = nan(numel(dates),1);
bestAnalog = nan(numel(dates),1);
bestDist = synImages.(bdName);
currentBest = nan(size(synDates));
q1DOY = [];
q3DOY = [];
minOut = [];
maxOut = [];
minCurDOY = nan(numel(dates),1);
maxCurDOY = nan(numel(dates),1);
% Create an empty figure
figure('WindowState', 'maximized');
% Loop over each file in the synthetic directory and find the corresponding
% file in the reference directory
for i = 1:size(synData,3)
% Find the corresponding file in the reference directory with the same name
referenceIndex = find(refDates == synDates(i));
analogDOY = day(datetime(sort(analogs{i},'descend'),'ConvertFrom','yyyyMMdd'),'dayofyear');
bestAnalog = day(datetime(analogs{i}(1),'ConvertFrom','yyyyMMdd'),'dayofyear');
refDOY = day(datetime(refDates(i),'ConvertFrom','yyyyMMdd'),'dayofyear');
% Check for circular transition
% Compute the minimum and maximum range boundaries
minRange = refDOY - daysRange;
maxRange = refDOY + daysRange;
% Handle circular transition for minRangeQ
if minRange <= 0
minRange = 365 + minRange;
end
% Handle circular transition for maxRangeQ
if maxRange > 365
maxRange = maxRange - 365;
end
% Construct the rangeTot array
if minRange < maxRange
rangeTot = minRange:maxRange;
else
rangeQmin = minRange:365;
rangeQmax = 1:maxRange;
rangeTot = [rangeQmin rangeQmax];
end
if refDOY == 366
refDOY = 1;
end
rangeTot = rangeTot';
% Find the index of refDOY and analogDOY(i) in rangeTot
indexRef = find(rangeTot == refDOY);
for di = 1:numel(analogDOY)
if analogDOY(di) == 366
analogDOY(di) = 1;
end
indexAnalog = find(rangeTot == analogDOY(di));
diffDOY(di) = indexRef - indexAnalog;
end
if bestAnalog == 366
bestAnalog = 1;
end
idxBestAnalog = find(rangeTot == bestAnalog);
diffBest(i) = indexRef - idxBestAnalog;
% If a matching file is found, display the two images side by side
if ~isempty(referenceIndex)
% Load the two images
synthetic = synData(:,:,i);
%synthetic(synthetic==min(min(synthetic))) = NaN;
%synthetic(synthetic==-999) = NaN;
reference = refData(:,:,referenceIndex);
%reference(reference==min(min(reference))) = NaN;
%reference(reference==-999) = NaN;
sgtitle(targetVar(k))
% Create a figure with three subplots
subplot(3,3,[1,4]);
img1 = imshow(synthetic);
colormap(gca, turbo(256));
set(img1, 'AlphaData', ~isnan(synthetic))
%caxis([0 maxColor])
caxis(varRange)
axis equal
title('Synthetic');
%colorbar(gca,'southoutside')
subplot(3,3,[2,5]);
img2 = imshow(reference);
colormap(gca, turbo(256));
set(img2, 'AlphaData', ~isnan(synthetic))
%caxis([0 maxColor])
caxis(varRange)
axis equal
title('Reference');
h = colorbar(gca,'southoutside');
% ERROR MAP
error = synthetic - reference;
subplot(3,3,[3,6])
errMap = imshow(error);
set(errMap, 'AlphaData', ~isnan(synthetic))
colormap(gca, coolwarm(256));
caxis(errRange)
title('Error');
axis equal
h_err = colorbar(gca,'southoutside');
% Add a colorbar to the reference image subplot
%h = colorbar('southoutside');
set(h, 'Position', [0.13 0.4 0.5 0.03]);
set(get(h,'label'),'string',varLegend);
set(h_err, 'Position', [0.7 0.4 0.205 0.03])
set(get(h_err,'label'),'string',varLegend);
% DOY difference
subplot(3,3,[7,8,9])
currentDOY(:,i) = diffDOY;
meanDOY(i) = mean(currentDOY(:,i));
%if numel(dates)<60
% boxchart(currentDOY);%,synDates(i));
%else
if nbImages > 1
inBetweenRegionX = [(1:i), fliplr(1:i)];
%inBetweenRegionX = [1:numel(dates), fliplr(1:numel(dates))];
quartileDOY = quantile(currentDOY(:,i),4);
q1DOY = [q1DOY quartileDOY(1)];
q3DOY = [q3DOY quartileDOY(3)];
inBetweenRegionY = [q3DOY, fliplr(q1DOY)];
itqDOY(i) = iqr(currentDOY(:,i));
minOut = [minOut, min(currentDOY(currentDOY(:,i) > (q1DOY(i) - itqDOY(i)),i))];
maxOut = [maxOut, max(currentDOY(currentDOY(:,i) < (q3DOY(i) + itqDOY(i)),i))];
inBetweenRegionY2 = [minOut, fliplr(maxOut)];
minCurDOY(i) = min(currentDOY(:,i));
maxCurDOY(i) = max(currentDOY(:,i));
patch(inBetweenRegionX, inBetweenRegionY, 'k', 'LineStyle', 'none', 'FaceAlpha', 0.15)
hold on
patch(inBetweenRegionX, inBetweenRegionY2, 'k', 'LineStyle', 'none', 'FaceAlpha', 0.1)
hold on
plot(1:i,minCurDOY(1:i),'LineStyle','--','Color','k')
hold on
plot(1:i,maxCurDOY(1:i),'LineStyle','--','Color','k')
end
%end
hold on
if nbImages > 1
plot(1:i,meanDOY(1:i),"Color",'red')
end
scatter(1:i,diffBest(1:i),"red")
%plot(dates(1:i),meanDOY(1:i),"Color",'red')
hold off
xlim([1 numel(dates)])
%xlim([min(dates) max(dates)])
ylim([-daysRange-10 daysRange+10])
title(['Mean DOY difference: ' num2str(mean(diffDOY),'%2.0f')]);
ylabel('DOY difference')
xlabel('Date')
grid on
ax = gca();
%ax.XTick = categorical(1:0.5:numel(dates));
%ax.XTickLabels = char(dates);
% % Best candidate MAE
% subplot(3,3,[7,8,9])
% currentBest(1:i) = bestDist(1:i);
% plot(dates,currentBest);
% hold on
% plot(dates(i),bestDist(i),"Marker","o","Color",'red')
% hold off
% xlim([min(dates) max(dates)])
% ylim([0.5 2])
% title(['Best candidate MAE: ' num2str(bestDist(i),'%1.5f')]);
% ylabel('MAE')
% xlabel('Date')
% grid on
% Set the title of the figure to the name of the images
if metricV == 1
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'MAE: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 2
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'RMSE: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 3
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'SPEM: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 4
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'SPAEF: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 5
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'KGE: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
end
% Save the current frame as a GIF
set(gcf, 'color', 'white');
frame = getframe(gcf);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
if i == 1
imwrite(imind, cm, gifVal, 'gif', 'Loopcount', size(synData,3), 'DelayTime', 0.1);
else
imwrite(imind, cm, gifVal, 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
end
end
end
else
% -------------------------------------------------------------------------
% Set the output GIF file name
gifVal = fullfile(outDir,['\validation_' convertStringsToChars(targetVar(k)) '.gif']);
refDates = refValidation.date;
synDates = synImages.date;
dates = datetime(synDates,'ConvertFrom','yyyyMMdd','format','dd/MM/yyyy');
% Create an empty figure
figure('WindowState', 'maximized');
% Loop over each file in the synthetic directory and find the corresponding
% file in the reference directory
for i = 1:size(synData,3)
% Find the corresponding file in the reference directory with the same name
referenceIndex = find(refDates == synDates(i));
% If a matching file is found, display the two images side by side
if ~isempty(referenceIndex)
% Load the two images
synthetic = synData(:,:,i);
%synthetic(synthetic==min(min(synthetic))) = NaN;
%synthetic(synthetic==-999) = NaN;
reference = refData(:,:,referenceIndex);
%reference(reference==min(min(reference))) = NaN;
%reference(reference==-999) = NaN;
sgtitle(targetVar(k))
% Create a figure with three subplots
subplot(1,3,1);
img1 = imshow(synthetic);
colormap(gca, turbo(256));
set(img1, 'AlphaData', ~isnan(synthetic))
%caxis([0 maxColor])
caxis(varRange)
axis equal
title('Synthetic');
%colorbar(gca,'southoutside')
subplot(1,3,2);
img2 = imshow(reference);
colormap(gca, turbo(256));
set(img2, 'AlphaData', ~isnan(synthetic))
%caxis([0 maxColor])
caxis(varRange)
axis equal
title('Reference');
h = colorbar(gca,'southoutside');
% ERROR MAP
error = synthetic - reference;
subplot(1,3,3)
errMap = imshow(error);
set(errMap, 'AlphaData', ~isnan(synthetic))
colormap(gca, coolwarm(256));
caxis(errRange)
title('Error');
axis equal
h_err = colorbar(gca,'southoutside');
% Add a colorbar to the reference image subplot
%h = colorbar('southoutside');
set(h, 'Position', [0.13 0.2 0.5 0.03]);
set(get(h,'label'),'string',varLegend);
set(h_err, 'Position', [0.7 0.2 0.205 0.03])
set(get(h_err,'label'),'string',varLegend);
% Set the title of the figure to the name of the images
if metricV == 1
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'MAE: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 2
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'RMSE: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 3
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'SPEM: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 4
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'SPAEF: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
elseif metricV == 5
sgtitle({['{\bf\fontsize{14}' char(dates(i)) '}'], ...
['{\fontsize{13}' 'KGE: ' num2str(validationMetric.(targetVarL(k))(i,2),'%1.5f') '}']})
end
% Save the current frame as a GIF
set(gcf, 'color', 'white');
frame = getframe(gcf);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
if i == 1
imwrite(imind, cm, gifVal, 'gif', 'Loopcount', size(synData,3), 'DelayTime', 0.1);
else
imwrite(imind, cm, gifVal, 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
end
end
end
end
end
end
end
end