Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Metric errors when applied to a single simulation #365

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/io/ResultSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ end
Extract parameters for a specific model component from exported model specification.
"""
function component_params(rs::ResultSet, component::T)::DataFrame where {T}
return spec[rs.model_spec.component.==string(component), :]
spec = rs.model_spec
return spec[spec.component.==string(component), :]
end
function component_params(rs::ResultSet, components::Vector{T})::DataFrame where {T}
spec = rs.model_spec
Expand Down
11 changes: 10 additions & 1 deletion src/io/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,16 @@ function fix_factor!(d::Domain, factor::Symbol, val::Real)::Nothing
end
function fix_factor!(d::Domain; factors...)
for (factor, val) in factors
fix_factor!(d, factor, val)
try
fix_factor!(d, factor, val)
catch err
if !(err isa MethodError)
rethrow(err)
end

# Try setting value as an integer
fix_factor!(d, factor, Int64(val))
end
end
end

Expand Down
30 changes: 21 additions & 9 deletions src/metrics/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ Metric(f, d) = Metric(f, d, "")
Make Metric callable with arbitary arguments that are passed to associated function.
"""
function (f::Metric)(raw, args...; kwargs...)
local res
try
return f.func(NamedDimsArray{(:timesteps, :species, :sites, :scenarios)[1:Base.ndims(raw)]}(raw), args...; kwargs...)
res = f.func(NamedDimsArray{(:timesteps, :species, :sites, :scenarios)[1:Base.ndims(raw)]}(raw), args...; kwargs...)
catch
raw = NamedDimsArray{(f.dims...)}(raw)

return f.func(raw, args...; kwargs...)
res = f.func(raw, args...; kwargs...)

try
# Try renaming dimensions to match expectations
res = NamedDimsArray{(f.dims...)}(res)
catch err
if !(err isa MethodError)
rethrow(err)
end
end
end

return res
end
function (f::Metric)(rs::ResultSet, args...; kwargs...)
return f.func(rs, args...; kwargs...)
Expand Down Expand Up @@ -102,7 +112,7 @@ Convenience method that slices the data in the specified manner.
- `args` : Additional positional arguments to pass into `metric`
- `dims` : dummy keyword argument, not used but defined to allow use with other methods
"""
function call_metric(metric::Function, data::NamedDimsArray, args...; kwargs...)
function call_metric(metric::Union{Function,Metric}, data::NamedDimsArray, args...; kwargs...)
dims = haskey(kwargs, :dims) ? kwargs[:dims] : nothing
if isnothing(dims)
return metric(slice_results(data; kwargs...), args...)
Expand Down Expand Up @@ -389,7 +399,7 @@ function _colony_Lcm2_to_m3m2(inputs::NamedDimsArray)::Tuple{Vector{Float64},Vec

# Extract assumed colony area (in cm^2) for each taxa/size class from scenario inputs
# Have to be careful to extract data in the correct order, matching coral id
colony_area_cm2::Array{Float64} = Array{Float64}(inputs(cs_p.coral_id .* "_colony_area_cm2"))
colony_area_cm2::Vector{Float64} = vec(inputs(cs_p.coral_id .* "_colony_area_cm2"))

# Colony planar area parameters (see second column of Table 1 in Urbina-Barreto et al., [1])
# First column is `b`, second column is `a`
Expand Down Expand Up @@ -538,7 +548,7 @@ function _absolute_shelter_volume(X::AbstractArray{T,4}, site_area::Vector{T}, i
return _absolute_shelter_volume(X, site_area, ins)
end
function _absolute_shelter_volume(X::AbstractArray{T,3}, site_area::Vector{T}, inputs::DataFrameRow)::AbstractArray{T} where {T<:Real}
ins = NamedDimsArray(Matrix(inputs), scenarios=1:1, params=names(df))
ins = NamedDimsArray(Matrix(Vector(inputs)'), scenarios=1:1, params=names(inputs))
return _absolute_shelter_volume(X, site_area, ins)
end
function _absolute_shelter_volume(X::AbstractArray{T,3}, site_area::Vector{T}, inputs::NamedDimsArray)::AbstractArray{T} where {T<:Real}
Expand Down Expand Up @@ -614,7 +624,8 @@ function _relative_shelter_volume(X::AbstractArray{T,3}, site_area::Vector{T}, k
end
function _relative_shelter_volume(X::AbstractArray{T,3}, site_area::Vector{T}, k_area::Vector{T}, inputs::Union{DataFrame,DataFrameRow})::AbstractArray{T} where {T<:Real}
# Collate for a single scenario
ins = NamedDimsArray(inputs, scenarios=1:size(inputs, 1), factors=names(inputs))
nscens = inputs isa DataFrameRow ? 1 : size(inputs, 1)
ins = NamedDimsArray(Matrix(Vector(inputs)'), scenarios=1:nscens, factors=names(inputs))
return _relative_shelter_volume(X, site_area, k_area, ins)
end
function _relative_shelter_volume(X::AbstractArray{T,4}, site_area::Vector{T}, k_area::Vector{T}, inputs::NamedDimsArray)::NamedDimsArray where {T<:Real}
Expand All @@ -640,7 +651,8 @@ function _relative_shelter_volume(X::AbstractArray{T,4}, site_area::Vector{T}, k
return RSV
end
function _relative_shelter_volume(X::AbstractArray{T,4}, site_area::Vector{T}, k_area::Vector{T}, inputs::Union{DataFrame,DataFrameRow})::NamedDimsArray where {T<:Real}
ins = NamedDimsArray(Matrix(inputs), scenarios=1:size(inputs, 1), factors=names(inputs))
nscens = inputs <: DataFrameRow ? 1 : size(inputs, 1)
ins = NamedDimsArray(Matrix(inputs), scenarios=1:nscens, factors=names(inputs))
return _relative_shelter_volume(X, site_area, k_area, ins)
end

Expand Down
3 changes: 3 additions & 0 deletions src/metrics/scenario.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ TODO: Produce summary stats. Currently returns just the mean.

Calculate the cluster-wide total absolute coral cover for each scenario.
"""
function _scenario_total_cover(X::AbstractArray; kwargs...)
return dropdims(sum(slice_results(X; kwargs...), dims=:sites), dims=:sites)
end
function _scenario_total_cover(rs::ResultSet; kwargs...)
return dropdims(sum(slice_results(total_absolute_cover(rs); kwargs...), dims=:sites), dims=:sites)
end
Expand Down