Skip to content

Commit c893323

Browse files
Update tests with latest changes
1 parent 3d532af commit c893323

File tree

6 files changed

+81
-83
lines changed

6 files changed

+81
-83
lines changed

examples/run_nodes.jl

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,67 @@ data_path = joinpath(here, "../test/data/campaspe/")
99
# Load and generate stream network
1010
sn = load_network("Example Network", joinpath(data_path, "campaspe_network.yml"))
1111

12-
# Load climate data
13-
date_format = "YYYY-mm-dd"
12+
# The Campaspe catchment is represented as a network of eight nodes, including one dam.
13+
# All nodes use the IHACRES_CMD rainfall-runoff model.
14+
plot_network(sn)
15+
16+
# Load climate data - in this case from a CSV file with data for all nodes.
1417
climate_data = CSV.read(
15-
joinpath(data_path, "climate/climate_historic.csv"),
18+
joinpath(data_path, "climate", "climate.csv"),
1619
DataFrame;
17-
comment="#",
18-
dateformat=date_format
20+
comment="#"
1921
)
2022

21-
dam_level_fn = joinpath(data_path, "gauges/406000_historic_levels_for_fit.csv")
22-
dam_releases_fn = joinpath(data_path, "gauges/406000_historic_outflow.csv")
23-
hist_dam_levels = CSV.read(dam_level_fn, DataFrame; dateformat=date_format)
24-
hist_dam_releases = CSV.read(dam_releases_fn, DataFrame; dateformat=date_format)
25-
26-
rename!(hist_dam_releases, ["406000_outflow_[ML]" => "406000_releases_[ML]"])
23+
# Indicate which columns are precipitation and evaporation data based on partial identifiers
24+
climate = Climate(climate_data, "_rain", "_evap")
2725

28-
# Subset to same range
29-
climate_data, hist_dam_levels, hist_dam_releases = Streamfall.align_time_frame(
30-
climate_data,
31-
hist_dam_levels,
32-
hist_dam_releases
26+
# Historic flows and dam level data
27+
calib_data = CSV.read(
28+
joinpath(data_path, "gauges", "outflow_and_level.csv"),
29+
DataFrame;
30+
comment="#"
3331
)
3432

35-
climate = Climate(climate_data, "_rain", "_evap")
33+
# Historic extractions from the dam
34+
extraction_data = CSV.read(
35+
joinpath(data_path, "gauges", "dam_extraction.csv"),
36+
DataFrame;
37+
comment="#"
38+
)
3639

3740
@info "Running example stream..."
3841

39-
reset!(sn)
40-
4142
dam_id, dam_node = sn["406000"]
42-
Streamfall.run_node!(sn, dam_id, climate; extraction=hist_dam_releases)
43+
Streamfall.run_node!(sn, dam_id, climate; extraction=extraction_data)
4344

44-
h_data = hist_dam_levels[:, "Dam Level [mAHD]"]
45-
n_data = dam_node.level
45+
# Extract data for comparison with 1-year burn-in period
46+
dam_obs = calib_data[:, "406000"][366:end]
47+
dam_sim = dam_node.level[366:end]
4648

47-
nnse_score = Streamfall.NNSE(h_data, n_data)
48-
nse_score = Streamfall.NSE(h_data, n_data)
49-
rmse_score = Streamfall.RMSE(h_data, n_data)
49+
nnse_score = Streamfall.NNSE(dam_obs, dam_sim)
50+
nse_score = Streamfall.NSE(dam_obs, dam_sim)
51+
rmse_score = Streamfall.RMSE(dam_obs, dam_sim)
5052

5153
@info "Obj Func Scores:" rmse_score nnse_score nse_score
5254

5355
nse = round(nse_score, digits=4)
5456
rmse = round(rmse_score, digits=4)
5557

56-
5758
import Dates: month, monthday, yearmonth
5859

59-
Streamfall.temporal_cross_section(climate_data.Date, h_data, n_data; period=monthday)
60-
savefig("temporal_xsection_monthday_ME.png")
60+
climate_burnin = climate_data[366:end, :Date]
61+
Streamfall.temporal_cross_section(climate_burnin, dam_obs, dam_sim; period=monthday)
62+
# savefig("temporal_xsection_monthday_ME.png")
6163

62-
Streamfall.temporal_cross_section(climate_data.Date, h_data, n_data; period=yearmonth)
63-
savefig("temporal_xsection_yearmonth_ME.png")
64+
Streamfall.temporal_cross_section(climate_burnin, dam_obs, dam_sim; period=yearmonth)
65+
# savefig("temporal_xsection_yearmonth_ME.png")
6466

6567
# Displaying results and saving figure
66-
# plot(h_data,
68+
# plot(dam_obs,
6769
# legend=:bottomleft,
6870
# title="Calibrated IHACRES\n(RMSE: $(rmse); NSE: $(nse))",
6971
# label="Historic", xlabel="Day", ylabel="Dam Level [mAHD]")
7072

71-
# display(plot!(n_data, label="IHACRES"))
73+
# display(plot!(dam_sim, label="IHACRES"))
7274

7375
# savefig("calibrated_example.png")

test/data/campaspe/campaspe_network.yml

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
parameters:
55
d: 134.06756067826717
66
d2: 1.547403668682315
7-
e: 1.4999999999988058
7+
e: 1.0
88
f: 0.9782888516330152
99
a: 1.878670093410396
1010
b: 0.09999999999990782
@@ -18,14 +18,14 @@
1818
node_type: "IHACRESBilinearNode"
1919
area: 1985.73
2020
parameters:
21-
d: 362.4510877543127
22-
d2: 1.7219537856770573
23-
e: 0.7005591278054522
24-
f: 1.4769086447109885
25-
a: 5.065870561569431
26-
b: 0.0966380343175847
27-
storage_coef: 4.534349325043295
28-
alpha: 0.7372499431622979
21+
d: 205.9296682315772
22+
d2: 1.5219519661190162
23+
e: 1.0
24+
f: 1.1612900313855028
25+
a: 9.999999999175559
26+
b: 0.0011479482142380236
27+
storage_coef: 9.99999999952744
28+
alpha: 0.8917155227322869
2929
initial_storage: 100.0
3030
inlets: ~
3131
outlets:
@@ -38,8 +38,6 @@
3838
initial_storage: 0.0
3939
max_storage: 306000.0
4040
functions:
41-
# Define methods that convert volume to level, area, discharge, and outflow.
42-
# Methods defined here must take volume as input
4341
calc_dam_level: |-
4442
function c_dam_level(volume)
4543
return 156.8 + 0.9463 * volume^0.2922
@@ -48,7 +46,6 @@
4846
function c_dam_area(volume)
4947
return 0.0021 * volume^0.762
5048
end
51-
# Discharge calculation must take volume and max_storage parameters
5249
calc_dam_discharge: |-
5350
function c_dam_discharge(volume, max_storage)
5451
discharge = 0.0
@@ -58,7 +55,6 @@
5855
5956
return max(0.0, discharge)
6057
end
61-
# Outflow must accept discharge and extraction inputs
6258
calc_dam_outflow: |-
6359
function c_dam_outflow(discharge, irrigation_extraction)
6460
return discharge + irrigation_extraction
@@ -73,7 +69,7 @@
7369
parameters:
7470
d: 34.44232768248571
7571
d2: 0.032214212326022326
76-
e: 0.2152471390979227
72+
e: 1.0
7773
f: 2.9999999999999147
7874
a: 1.270124950108292
7975
b: 0.0999999117504344
@@ -91,7 +87,7 @@
9187
parameters:
9288
d: 48.38395701197697
9389
d2: 2.293996656199082
94-
e: 1.1158650210585603
90+
e: 1.0
9591
f: 1.4405724030125124
9692
a: 3.7408302145617087
9793
b: 0.0011959104715538137
@@ -107,7 +103,7 @@
107103
parameters:
108104
d: 152.62331896278664
109105
d2: 0.5399110983080897
110-
e: 1.4729023737877482
106+
e: 1.0
111107
f: 1.6458403364664287
112108
a: 9.400598795169456
113109
b: 0.04492942366402773
@@ -125,7 +121,7 @@
125121
parameters:
126122
d: 230.10173616444285
127123
d2: 0.3985548134635746
128-
e: 1.3664818534158052
124+
e: 1.0
129125
f: 1.525077957112293
130126
a: 4.114196548002251
131127
b: 0.026191008249266696
@@ -142,7 +138,7 @@
142138
parameters:
143139
d: 110.53645289579511
144140
d2: 4.354852331236118
145-
e: 1.254596727030465
141+
e: 1.0
146142
f: 2.5833889457293613
147143
a: 0.30247124389394187
148144
b: 0.012073002123341786
-5.07 KB
Loading
-2.24 KB
Loading

test/test_data_op.jl

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,42 @@ using Test
44
using Dates, DataFrames, CSV
55
using Streamfall
66

7+
@testset "Data alignment" begin
78

8-
here = @__DIR__
9-
climate_data = joinpath(here, "data/campaspe/climate/climate_historic.csv")
10-
dam_data_loc = joinpath(here, "data/campaspe/gauges")
11-
12-
# Read in test data
13-
climate_data = CSV.File(climate_data, comment="#", dateformat="YYYY-mm-dd") |> DataFrame
14-
hist_dam_levels = CSV.File(joinpath(dam_data_loc, "406000_historic_levels_for_fit.csv"), dateformat="YYYY-mm-dd") |> DataFrame
15-
hist_dam_releases = CSV.File(joinpath(dam_data_loc, "406000_historic_outflow.csv"), dateformat="YYYY-mm-dd") |> DataFrame
9+
here = @__DIR__
10+
climate_data = joinpath(here, "data/campaspe/climate/climate_historic.csv")
11+
dam_data_loc = joinpath(here, "data/campaspe/gauges")
1612

13+
# Read in test data
14+
climate_data = CSV.File(climate_data, comment="#", dateformat="YYYY-mm-dd") |> DataFrame
15+
hist_dam_levels = CSV.File(joinpath(dam_data_loc, "406000_historic_levels_for_fit.csv"), dateformat="YYYY-mm-dd") |> DataFrame
16+
hist_dam_releases = CSV.File(joinpath(dam_data_loc, "406000_historic_outflow.csv"), dateformat="YYYY-mm-dd") |> DataFrame
1717

18-
@testset "Min/max date finding" begin
19-
min_date, max_date = Streamfall.find_common_timeframe(climate_data, hist_dam_levels, hist_dam_releases)
18+
@testset "Min/max date finding" begin
19+
min_date, max_date = Streamfall.find_common_timeframe(climate_data, hist_dam_levels, hist_dam_releases)
2020

21-
@test min_date == Date("1981-01-01")
22-
@test max_date == Date("2015-07-30")
23-
end
24-
25-
@testset "Data alignment" begin
21+
@test min_date == Date("1981-01-01")
22+
@test max_date == Date("2015-07-30")
23+
end
2624

27-
# Mismatched start/end dates
28-
dam_levels = hist_dam_levels[10:end, :]
29-
dam_releases = hist_dam_releases[20:10000, :]
25+
@testset "Aligning mismatched datasets" begin
26+
# Mismatched start/end dates
27+
dam_levels = hist_dam_levels[10:end, :]
28+
dam_releases = hist_dam_releases[20:10000, :]
3029

31-
# Subset to common time period
32-
c, dl, dr = Streamfall.align_time_frame(climate_data, dam_levels, dam_releases)
30+
# Subset to common time period
31+
c, dl, dr = Streamfall.align_time_frame(climate_data, dam_levels, dam_releases)
3332

34-
# Ensure time periods are the same
35-
@test c.Date[1] == dl.Date[1] == dr.Date[1]
36-
@test c.Date[end] == dl.Date[end] == dr.Date[end]
33+
# Ensure time periods are the same
34+
@test c.Date[1] == dl.Date[1] == dr.Date[1]
35+
@test c.Date[end] == dl.Date[end] == dr.Date[end]
3736

38-
# Ensure originals have not been modified
39-
@test climate_data.Date[1] != c.Date[1]
40-
@test hist_dam_levels.Date[1] != dl.Date[1]
41-
@test hist_dam_releases.Date[1] != dr.Date[1]
37+
# Ensure originals have not been modified
38+
@test climate_data.Date[1] != c.Date[1]
39+
@test hist_dam_levels.Date[1] != dl.Date[1]
40+
@test hist_dam_releases.Date[1] != dr.Date[1]
4241

43-
@test hist_dam_levels.Date[end] != dl.Date[end]
44-
@test hist_dam_releases.Date[end] != dr.Date[end]
42+
@test hist_dam_levels.Date[end] != dl.Date[end]
43+
@test hist_dam_releases.Date[end] != dr.Date[end]
44+
end
4545
end

test/test_networks.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ end
113113
# Ensure example does not error out
114114

115115
reset!(sn)
116-
run_basin!(sn, climate; extraction=hist_dam_releases)
117-
@test Streamfall.RMSE(n_data, sn[dam_id].level) == 0.0
116+
run_basin!(sn, climate; extraction=extraction_data)
117+
@test Streamfall.RMSE(dam_obs, sn[dam_id].level[366:end]) < 2.5
118118

119119

120120
reset!(sn)
121-
Streamfall.run_node!(sn, dam_id, climate; extraction=hist_dam_releases)
122-
@test Streamfall.RMSE(n_data, sn[dam_id].level) == 0.0
121+
Streamfall.run_node!(sn, dam_id, climate; extraction=extraction_data)
122+
@test Streamfall.RMSE(dam_obs, sn[dam_id].level[366:end]) < 2.5
123123

124124
# Ensure example results haven't changed much...
125125
# @test nse_score >= 0.95 && nse_score < 1.0 && rmse < 2.0

0 commit comments

Comments
 (0)