Skip to content

Commit 4841f44

Browse files
Standardize interfaces
1 parent a560076 commit 4841f44

File tree

9 files changed

+61
-42
lines changed

9 files changed

+61
-42
lines changed

Project.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
3232
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
3333
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
3434
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
35-
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
3635
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
3736
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3837
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
@@ -41,14 +40,14 @@ ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
4140
[weakdeps]
4241
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
4342
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
44-
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
43+
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
4544
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
4645

4746
[extensions]
4847
GraphMakieExt = ["Makie", "GraphMakie"]
4948
MakieExt = "Makie"
50-
GraphPlotExt = ["Plots", "GraphPlot"]
51-
PlotsExt = "Plots"
49+
GraphPlotExt = ["StatsPlots", "GraphPlot"]
50+
PlotsExt = "StatsPlots"
5251

5352
[compat]
5453
BlackBoxOptim = "0.5, 0.6"
@@ -79,3 +78,5 @@ StatsFuns = "1.3"
7978
YAML = "0.4"
8079
ZipFile = "0.10"
8180
julia = "1"
81+
StatsPlots = "0.15"
82+
GraphPlot = "0.6"

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ The examples below are run from the [examples](https://github.com/ConnectedSyste
8989
```julia
9090
using Statistics
9191
using CSV, DataFrames, YAML
92-
using Plots
92+
using StatsPlots
9393
using Streamfall
9494

95+
example_data_dir = joinpath(dirname(dirname(pathof(Streamfall))), "test/data")
96+
9597
# Load data file which holds observed streamflow, precipitation and PET data
96-
obs_data = CSV.read("../test/data/cotter/climate/CAMELS-AUS_410730.csv", DataFrame; comment="#")
98+
obs_data = CSV.read(
99+
joinpath(example_data_dir, "cotter/climate/CAMELS-AUS_410730.csv"),
100+
DataFrame;
101+
comment="#"
102+
)
97103
# 18808×8 DataFrame
98104
# Row │ year month day Date 410730_P 410730_PET 410730_max_T 410730_Q
99105
# │ Int64 Int64 Int64 Date Float64 Float64 Float64 Float64
@@ -213,17 +219,18 @@ model.
213219

214220
```julia
215221
using CSV, DataFrames, YAML
216-
using Plots
222+
using StatsPlots
217223
using Streamfall
218224

219225

220226
# Load a network from a file, providing a name for the network and the file path.
221227
# Creates a graph representation of the stream with associated metadata.
222-
sn = load_network("Example Network", "../test/data/campaspe/campaspe_network.yml")
228+
example_data_dir = joinpath(dirname(dirname(pathof(Streamfall))), "test/data")
229+
sn = load_network("Example Network", joinpath(example_data_dir, "campaspe/campaspe_network.yml"))
223230

224231
# Load climate data, in this case from a CSV file with data for all nodes.
225232
# Indicate which columns are precipitation and evaporation data based on partial identifiers
226-
climate = Climate("../test/data/campaspe/climate/climate.csv", "_rain", "_evap")
233+
climate = Climate(joinpath(example_data_dir, "campaspe/climate/climate.csv"), "_rain", "_evap")
227234

228235
# This runs an entire stream network
229236
@info "Running an example stream..."

ext/GraphPlotExt/GraphPlotExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module GraphPlotExt
22

3-
using Plots, GraphPlot
3+
using StatsPlots, GraphPlot
44
using Streamfall
55
import Streamfall: vertices
66
import Streamfall: StreamfallNetwork

ext/MakieExt/MakieExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function Streamfall.Viz.quickplot(
7171
show_range = burn_in:last_e
7272
return Streamfall.Viz.quickplot(obs[show_range], sim[show_range], date[show_range], label, log; metric=metric)
7373
end
74-
function Streamfall.Viz.quickplot(obs::Vector, sim::Vector, xticklabels::Vector, label="Modeled", log=false; metric=Streamfall.mKGE)
74+
function Streamfall.Viz.quickplot(obs::Vector, sim::Vector, xticklabels::Vector; label="Modeled", log=false, metric=Streamfall.mKGE)
7575
@assert length(xticklabels) == length(obs) || "x-axis tick label length and observed lengths do not match!"
7676
@assert length(xticklabels) == length(sim) || "x-axis tick label length and simulated lengths do not match!"
7777

ext/PlotsExt/PlotsExt.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module PlotsExt
22

3-
using Plots, StatsPlots
4-
using Plots.Measures
3+
using StatsPlots
4+
using StatsPlots.Plots.Measures
55

66
using DataFrames, Dates, Statistics, Distributions, LaTeXStrings
77
import Bootstrap: bootstrap, BalancedSampling
88

99
using Streamfall
1010
import Streamfall: Analysis.TemporalCrossSection
11+
import Streamfall.Viz: symlog
1112

1213
function Streamfall.Viz.plot(node::NetworkNode, climate::Climate)
1314
return Plots.plot(
@@ -50,23 +51,23 @@ function Streamfall.Viz.quickplot(node::NetworkNode, climate::Climate)
5051

5152
@assert length(all_dates) == length(node.outflow) || "Date length and result lengths do not match!"
5253

53-
fig = plot(all_dates, node.outflow)
54+
fig = Plots.plot(all_dates, node.outflow)
5455

5556
return fig
5657
end
57-
function Streamfall.Viz.quickplot(obs, node::NetworkNode, climate::Climate, label="", log=false; burn_in=1, limit=nothing, metric=Streamfall.mKGE)
58-
return Streamfall.Viz.quickplot(obs, node.outflow, climate, label, log; burn_in=burn_in, limit=limit, metric=metric)
58+
function Streamfall.Viz.quickplot(obs::Vector, node::NetworkNode, climate::Climate; label::String="", log::Bool=false, burn_in=1, limit=nothing, metric=Streamfall.mKGE)
59+
return Streamfall.Viz.quickplot(obs, node.outflow, climate; label, log, burn_in, limit, metric)
5960
end
60-
function Streamfall.Viz.quickplot(obs::DataFrame, sim::Vector, climate::Climate, label="", log=false; burn_in=1, limit=nothing, metric=Streamfall.mKGE)
61+
function Streamfall.Viz.quickplot(obs::DataFrame, sim::Vector, climate::Climate; label::String="", log::Bool=false, burn_in=1, limit=nothing, metric=Streamfall.mKGE)
6162
return Streamfall.Viz.quickplot(Matrix(obs[:, Not("Date")])[:, 1], sim, climate, label, log; burn_in, limit, metric)
6263
end
63-
function Streamfall.Viz.quickplot(obs::Vector, sim::Vector, climate::Climate, label="", log=false; burn_in=1, limit=nothing, metric=Streamfall.mKGE)
64+
function Streamfall.Viz.quickplot(obs::Vector, sim::Vector, climate::Climate; label::String="", log::Bool=false, burn_in=1, limit=nothing, metric=Streamfall.mKGE)
6465
date = timesteps(climate)
6566
last_e = !isnothing(limit) ? limit : lastindex(obs)
6667
show_range = burn_in:last_e
67-
return quickplot(obs[show_range], sim[show_range], date[show_range], label, log; metric=metric)
68+
return quickplot(obs[show_range], sim[show_range], date[show_range]; label, log, metric)
6869
end
69-
function Streamfall.Viz.quickplot(obs::Vector, sim::Vector, xticklabels::Vector, label="Modeled", log=false; metric=Streamfall.mKGE)
70+
function Streamfall.Viz.quickplot(obs::Vector, sim::Vector, xticklabels::Vector; label="Modeled", log=false, metric=Streamfall.mKGE)
7071
@assert length(xticklabels) == length(obs) || "x-axis tick label length and observed lengths do not match!"
7172
@assert length(xticklabels) == length(sim) || "x-axis tick label length and simulated lengths do not match!"
7273

@@ -167,7 +168,7 @@ function Streamfall.Viz.temporal_cross_section(
167168
logscale = [:log, :log10]
168169
tmp = nothing
169170

170-
xsect_res = TemporalCrossSection(dates, obs, period)
171+
xsect_res = TemporalCrossSection(dates, obs; period)
171172

172173
if :yscale in arg_keys || :yaxis in arg_keys
173174
tmp = (:yscale in arg_keys) ? kwargs[:yscale] : kwargs[:yaxis]
@@ -178,7 +179,7 @@ function Streamfall.Viz.temporal_cross_section(
178179
# Format function for y-axis tick labels (e.g., 10^x)
179180
format_func = y -> (y != 0) ? L"%$(Int(round(sign(y)) * 10))^{%$(round(abs(y), digits=1))}" : L"0"
180181

181-
log_xsect_res = TemporalCrossSection(dates, log_obs, period)
182+
log_xsect_res = TemporalCrossSection(dates, log_obs; period)
182183
target = log_xsect_res.cross_section
183184
else
184185
target = xsect_res.cross_section
@@ -273,7 +274,7 @@ function Streamfall.Viz.temporal_cross_section(
273274
logscale = [:log, :log10]
274275
tmp = nothing
275276

276-
xsect_res = TemporalCrossSection(dates, obs, sim, period)
277+
xsect_res = TemporalCrossSection(dates, obs, sim; period)
277278
target = xsect_res.cross_section
278279

279280
if :yscale in arg_keys || :yaxis in arg_keys
@@ -286,7 +287,7 @@ function Streamfall.Viz.temporal_cross_section(
286287
# Format function for y-axis tick labels (e.g., 10^x)
287288
format_func = y -> (y != 0) ? L"%$(Int(round(sign(y)) * 10))^{%$(round(abs(y), digits=1))}" : L"0"
288289

289-
log_xsect_res = TemporalCrossSection(dates, log_obs, log_sim, period)
290+
log_xsect_res = TemporalCrossSection(dates, log_obs, log_sim; period)
290291
target = log_xsect_res.cross_section
291292
end
292293
end

src/Analysis/uncertainty.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ using OrderedCollections
44
import ..ME, ..MAE
55

66

7-
mutable struct TemporalCrossSection{A<:Dates.Date, B<:Real, D<:OrderedDict}
7+
mutable struct TemporalCrossSection{A<:Dates.Date,B<:Real,D<:OrderedDict}
88
dates::Array{A}
99
ts::Array{B}
1010
subperiods::D
11-
cross_section::Union{DataFrame, Nothing}
11+
cross_section::Union{DataFrame,Nothing}
1212
end
1313

1414

15-
function TemporalCrossSection(dates::Array, ts::Array, period::Function=monthday)
15+
function TemporalCrossSection(dates::Array, ts::Array; period::Function=monthday)
1616
df = DataFrame(Date=dates, data=ts)
1717
sp = sort(unique(period.(dates)))
1818

1919
# Remove leap days (when using monthday)
20-
deleteat!(sp, findall(x -> x == (2,29), sp))
20+
deleteat!(sp, findall(x -> x == (2, 29), sp))
2121

2222
res = OrderedDict(
23-
sp_i => df[period.(df.Date) .== [sp_i], :].data for sp_i in sp
23+
sp_i => df[period.(df.Date).==[sp_i], :].data for sp_i in sp
2424
)
2525

2626
return TemporalCrossSection(dates, ts, res, nothing)
@@ -30,10 +30,12 @@ end
3030
"""
3131
TemporalCrossSection constructor that handles two time series
3232
"""
33-
function TemporalCrossSection(dates::Array, obs::Array, sim::Array,
34-
period::Function=monthday)
33+
function TemporalCrossSection(
34+
dates::Array, obs::Array, sim::Array;
35+
period::Function=monthday
36+
)
3537
Y = ME.(obs, sim)
36-
return TemporalCrossSection(dates, Y, period)
38+
return TemporalCrossSection(dates, Y; period)
3739
end
3840

3941

@@ -56,7 +58,7 @@ function cross_section(tr::TemporalCrossSection)
5658
end
5759

5860
cols = [:abs_min, :lower_95, :lower_75, :median,
59-
:upper_75, :upper_95, :abs_max, :mean, :std]
61+
:upper_75, :upper_95, :abs_max, :mean, :std]
6062

6163
x_section = DataFrame(boxframe, cols)
6264
x_section[:, :subperiod] = collect(keys(sp))

src/Streamfall.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ using Statistics
44
using Graphs, MetaGraphs, Distributed, DataFrames
55

66

7-
const MODPATH = @__DIR__
8-
97
include("Network.jl")
108
include("Nodes/Node.jl")
119
include("Climate.jl")

src/viz/Viz.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ function plot_residuals end
1212
function save_figure end
1313
function save_figure! end
1414

15+
16+
"""Symmetrical log values.
17+
18+
https://kar.kent.ac.uk/32810/2/2012_Bi-symmetric-log-transformation_v5.pdf
19+
https://discourse.julialang.org/t/symmetrical-log-plot/45709/3
20+
"""
21+
function symlog(y)
22+
return sign.(y) .* log10.(1.0 .+ abs.(y))
23+
end
24+
1525
export plot, plot!, quickplot
1626
export temporal_cross_section, plot_residuals
1727
export save_figure, save_figure!

src/viz/_plotting.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using Plots, StatsPlots
2-
using Plots.Measures
3-
import Plots: plot, plot!
1+
using StatsPlots
2+
using StatsPlots.Plots
3+
import StatsPlots: plot, plot!
44
using DataFrames, Dates, Statistics, Distributions, LaTeXStrings
55
import Bootstrap: bootstrap, BalancedSampling
66

@@ -142,7 +142,7 @@ function temporal_cross_section(dates, obs;
142142
logscale = [:log, :log10]
143143
tmp = nothing
144144

145-
xsect_res = TemporalCrossSection(dates, obs, period)
145+
xsect_res = TemporalCrossSection(dates, obs; period)
146146

147147
if :yscale in arg_keys || :yaxis in arg_keys
148148
tmp = (:yscale in arg_keys) ? kwargs[:yscale] : kwargs[:yaxis]
@@ -153,7 +153,7 @@ function temporal_cross_section(dates, obs;
153153
# Format function for y-axis tick labels (e.g., 10^x)
154154
format_func = y -> (y != 0) ? L"%$(Int(round(sign(y)) * 10))^{%$(round(abs(y), digits=1))}" : L"0"
155155

156-
log_xsect_res = TemporalCrossSection(dates, log_obs, period)
156+
log_xsect_res = TemporalCrossSection(dates, log_obs; period)
157157
target = log_xsect_res.cross_section
158158
else
159159
target = xsect_res.cross_section
@@ -250,7 +250,7 @@ function temporal_cross_section(
250250
logscale = [:log, :log10]
251251
tmp = nothing
252252

253-
xsect_res = TemporalCrossSection(dates, obs, sim, period)
253+
xsect_res = TemporalCrossSection(dates, obs, sim; period)
254254
target = xsect_res.cross_section
255255

256256
if :yscale in arg_keys || :yaxis in arg_keys
@@ -263,7 +263,7 @@ function temporal_cross_section(
263263
# Format function for y-axis tick labels (e.g., 10^x)
264264
format_func = y -> (y != 0) ? L"%$(Int(round(sign(y)) * 10))^{%$(round(abs(y), digits=1))}" : L"0"
265265

266-
log_xsect_res = TemporalCrossSection(dates, log_obs, log_sim, period)
266+
log_xsect_res = TemporalCrossSection(dates, log_obs, log_sim; period)
267267
target = log_xsect_res.cross_section
268268
end
269269
end

0 commit comments

Comments
 (0)