Skip to content

Commit 9b41879

Browse files
Fix support for node calibration
1 parent 955ec54 commit 9b41879

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

src/calibration.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ end
313313
node::NetworkNode,
314314
climate::Climate,
315315
calib_data::AbstractArray,
316-
metric::AbstractDict{String,F};
316+
metric::Union{C,AbstractDict{String,C}};
317317
kwargs...
318-
) where {F}
318+
) where {C}
319319
320320
Calibrate a given node using the BlackBoxOptim package.
321321
@@ -330,24 +330,27 @@ function calibrate!(
330330
node::NetworkNode,
331331
climate::Climate,
332332
calib_data::AbstractArray,
333-
metric::AbstractDict{String,F};
333+
metric::AbstractDict{String,C};
334334
kwargs...
335-
) where {F}
335+
) where {C}
336+
return calibrate!(node, climate, calib_data, metric[node.name]; kwargs...)
337+
end
338+
function calibrate!(
339+
node::NetworkNode,
340+
climate::Climate,
341+
calib_data::AbstractArray,
342+
metric::C;
343+
kwargs...
344+
) where {C}
336345
_merge_defaults(kwargs)
337346

338-
next_node = sn[first(outlets(sn, node.name))]
339-
340347
extraction = get(kwargs, :extraction, nothing)
341348
exchange = get(kwargs, :exchange, nothing)
342349

343-
# Create context-specific optimization function
344-
if next_node <: DamNode
345-
# If the next node represents a dam, attempt to calibrate considering the outflows
346-
# from the current node and the Dam Levels of the next node.
347-
opt_func = x -> next_node.obj_func(x, climate, node, next_node, calib_data; metric=metric[next_node.name], extraction=extraction, exchange=exchange)
348-
else
349-
opt_func = x -> node.obj_func(x, climate, node, calib_data; metric=metric[node.name], extraction=extraction, exchange=exchange)
350-
end
350+
opt_func = x -> node.obj_func(
351+
x, climate, node, calib_data;
352+
metric=metric, extraction=extraction, exchange=exchange
353+
)
351354

352355
# Get node parameters (default values and bounds)
353356
param_names, x0, param_bounds = param_info(node; with_level=false)
@@ -376,22 +379,23 @@ end
376379
"""
377380
calibrate!(
378381
node::NetworkNode, climate::Climate, calib_data::DataFrame,
379-
metric::AbstractDict{String,F};
382+
metric::Union{C,AbstractDict{String,C}};
380383
kwargs...
381-
)
384+
) where {C}
382385
383386
Calibrate a given node using the BlackBoxOptim package.
384387
385388
# Arguments
386389
- `node::NetworkNode` : Streamfall node
387390
- `climate` : Climate data
388391
- `calib_data` : Calibration data for target node, where column names indicate node names
389-
- `metric::Function` : Optimization function to use. Defaults to RMSE.
392+
- `metric::Function` : Optimization function to use.
390393
"""
391394
function calibrate!(
392-
node::NetworkNode, climate::Climate, calib_data::DataFrame, metric::AbstractDict{String,F};
395+
node::NetworkNode, climate::Climate, calib_data::DataFrame,
396+
metric::Union{C,AbstractDict{String,C}};
393397
kwargs...
394-
) where {F}
398+
) where {C}
395399
return calibrate!(node, climate, calib_data[:, node.name], metric; kwargs...)
396400
end
397401

0 commit comments

Comments
 (0)