Repository navigation
Expand file tree
/
Copy pathinitializeHyperparams.m
More file actions
123 lines (85 loc) · 3.47 KB
/
Copy pathinitializeHyperparams.m
File metadata and controls
123 lines (85 loc) · 3.47 KB
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
clear all;
close all;
%% User inputs
Site = 1;
diurnal_lookback = 1; % Enter one diurnal/dry-weather lookback period (in months)
testfolder = ''; % Results will be saved in ./Data/Site[Site][testfolder]/
Fs = 288; % Sensor sampling frequency (number per day)
% Threshold criteria
slope = 0.25; % Threshold for slope of a dry-weather diurnal pattern (trough-to-trough)
timeSlack = 3; % Threshold for length of a dry-weather diurnal pattern (hours +/- 24 hours)
stdMax = 0.48; % Threshold for maximum standard deviation of a dry-weather diurnal pattern
stdMin = 0.25; % Threshold for minimum standard deviation of a dry-weather diurnal pattern
% Dry-weather section for learning hyperparameters
starti = 105121; % Start index
chunk = round(diurnal_lookback*Fs*365/12);
endi = starti+chunk;
section = starti:endi;
%% File prep
load(strcat('./Examples/', 'Site', num2str(Site),'_hist.mat')); % Historical data
filepath = strcat('./Data/');
mkdir(filepath);
filepath = strcat('./Data/', 'Site', num2str(Site,'%02.0f'), testfolder, '/');
mkdir(filepath);
addpath('./DryWeather');
all_datetime = datetime(FlowTime,'ConvertFrom','epochtime','Epoch','1970-01-01');
%% GP prep
run('./gpml-matlab-v3.6-2015-07-07/startup.m')
k2 = @covPeriodic;
k3 = @covRQard;
covfunc = {@covSum, {k2, k3}};
%% Flow filters
filter_diurnal_SOS = load('./Filter/diurn_butter_SOS.mat');
filter_diurnal_G = load('./Filter/diurn_butter_G.mat');
%% Remove high frequency noise
no_noise = smoothts(Flow,'g',300,100);
%% Extract diurnal
diurnals = filtfilt(filter_diurnal_SOS.SOS,filter_diurnal_G.G,no_noise);
%% Data to be used
timeSection = all_datetime(section);
FlowSection = FlowTime(section);
diurnalSection = diurnals(section);
all_good = selectTrainingDays(FlowTime, diurnals, Fs, slope, timeSlack, stdMin, stdMax);
good_i = selectTrainingDays(FlowSection, diurnalSection, Fs, slope, timeSlack, stdMin, stdMax);
all_good_ind = [];
for i = 1:length(all_good)
all_good_ind = [all_good_ind, all_good(i,1):all_good(i,2)];
end
good_ind = [];
for i = 1:length(good_i)
good_ind = [good_ind, good_i(i,1):good_i(i,2)];
end
figure(1)
hold on
plot(all_datetime, no_noise)
plot(all_datetime, diurnals,'k')
plot(all_datetime(all_good_ind),diurnals(all_good_ind),'linewidth',4);
plot(timeSection(good_ind), diurnalSection(good_ind));
legend('filtered raw data','all diurnals','good diurnals','selection')
disp('Check good diurnals which will be used for training; adjust criteria thresholds as needed')
pause
%% Run GP
hyp = [];
hyp.cov = [0 0 1 0 0 -1]; hyp.lik = -2;
y = diurnalSection(good_ind)';
x = timeToWeekdayDecimal(timeSection(good_ind)');
step = floor(length(y)/3500);
if step == 0
step = 1;
end
y = y(1:step:length(x));
x = x(1:step:length(x));
x_star = timeToWeekdayDecimal(timeSection)';
[hyp fX i] = minimize(hyp, @gp, -100, @infExact, [], covfunc, @likGauss, x, y);
[mu s2] = gp(hyp, @infExact, [], covfunc, @likGauss, x, y, x_star);
%% Plot results for final check
figure(1)
plot(timeSection,mu)
legend('filtered raw data','all diurnals','good diurnals','selection','results')
%% Save hyperparameters
no_noise = no_noise(section);
diurnals = diurnalSection;
all_datetime = timeSection;
FlowTime = FlowSection;
HyperFile = strcat(filepath,'HypInit',num2str(Site,'%02.0f'),'.mat');
save(HyperFile,'hyp','slope','timeSlack','stdMax','stdMin');