Skip to content

Commit

Permalink
Formatting and add additional documentation to new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosejoycrocker committed Jan 23, 2024
1 parent f7f6063 commit 9a4056a
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 142 deletions.
117 changes: 67 additions & 50 deletions src/decision/dMCDA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ end

"""
DMCDA_vars(domain::Domain, criteria::NamedDimsArray,
site_ids::AbstractArray, leftover_space::AbstractArray, area_to_seed::Float64,
waves::AbstractArray, dhws::AbstractArray)::DMCDA_vars
site_ids::AbstractArray, leftover_space::AbstractArray, area_to_seed::Float64,
waves::AbstractArray, dhws::AbstractArray)::DMCDA_vars
DMCDA_vars(domain::Domain, criteria::NamedDimsArray, site_ids::AbstractArray,
leftover_space::AbstractArray, area_to_seed::Float64)::DMCDA_vars
leftover_space::AbstractArray, area_to_seed::Float64)::DMCDA_vars
DMCDA_vars(domain::Domain, criteria::DataFrameRow, site_ids::AbstractArray,
leftover_space::AbstractArray, area_to_seed::Float64)::DMCDA_vars
DMCDA_vars(domain::Domain, criteria::DataFrameRow, site_ids::AbstractArray,
leftover_space::AbstractArray, area_to_seed::Float64)::DMCDA_vars
DMCDA_vars(domain::Domain, criteria::DataFrameRow, site_ids::AbstractArray,
leftover_space::AbstractArray, area_to_seed::Float64,
waves::AbstractArray, dhw::AbstractArray)::DMCDA_vars
leftover_space::AbstractArray, area_to_seed::Float64,
waves::AbstractArray, dhw::AbstractArray)::DMCDA_vars
Constuctors for DMCDA variables.
"""
Expand Down Expand Up @@ -161,7 +161,7 @@ function DMCDA_vars(
waves::AbstractArray,
dhw::AbstractArray,
)::DMCDA_vars
criteria_vec::NamedDimsArray = NamedDimsArray(collect(criteria); rows = names(criteria))
criteria_vec::NamedDimsArray = NamedDimsArray(collect(criteria); rows=names(criteria))
return DMCDA_vars(
domain, criteria_vec, site_ids, leftover_space, area_to_seed, waves, dhw
)
Expand All @@ -173,12 +173,12 @@ function DMCDA_vars(
leftover_space::AbstractArray,
area_to_seed::Float64,
)::DMCDA_vars
criteria_vec::NamedDimsArray = NamedDimsArray(collect(criteria); rows = names(criteria))
criteria_vec::NamedDimsArray = NamedDimsArray(collect(criteria); rows=names(criteria))
return DMCDA_vars(domain, criteria_vec, site_ids, leftover_space, area_to_seed)
end

"""
mcda_normalize(x::Vector)::Vector
mcda_normalize(x::Vector)::Vector
Normalize a Vector (wse/wsh) for MCDA.
"""
Expand All @@ -192,16 +192,16 @@ end
Normalize a Matrix (SE/SH) for MCDA.
"""
function mcda_normalize(x::Matrix)::Matrix
return x ./ sqrt.(sum(x .^ 2; dims = 1))
return x ./ sqrt.(sum(x .^ 2; dims=1))
end

"""
mcda_normalize(x::DataFrame)::DataFrame
mcda_normalize(x::DataFrame)::DataFrame
Normalize weights for a set of scenarios (wse/wsh) for MCDA.
"""
function mcda_normalize(x::DataFrame)::DataFrame
return x ./ sum(Matrix(x); dims = 2)
return x ./ sum(Matrix(x); dims=2)
end

"""
Expand Down Expand Up @@ -240,7 +240,7 @@ function rank_sites!(
mcda_func::Union{Function, Type{<:MCDMMethod}},
rank_col)::Tuple{Vector{Int64}, Matrix{Union{Float64, Int64}}}
# Filter out all non-preferred sites
selector = vec(.!all(S[:, 2:end] .== 0; dims = 1))
selector = vec(.!all(S[:, 2:end] .== 0; dims=1))

# weights in order of: in_conn, out_conn, wave, heat, predecessors, low cover
weights = weights[selector]
Expand Down Expand Up @@ -302,7 +302,7 @@ function retrieve_ranks(
scores::Vector,
maximize::Bool,
)::Matrix{Union{Float64, Int64}}
s_order::Vector{Int64} = sortperm(scores; rev = maximize)
s_order::Vector{Int64} = sortperm(scores; rev=maximize)
return Union{Float64, Int64}[Int64.(site_ids[s_order]) scores[s_order]]
end

Expand All @@ -315,15 +315,15 @@ Sites are then filtered based on heat and wave stress risk.
Where no sites are filtered, size of ``A := n_sites × 9 criteria``.
Columns indicate:
1. Site ID
2. Incoming node connectivity centrality
3. Outgoing node connectivity centrality
4. Wave stress
5. Heat stress
6. Priority predecessors
7. GBRMPA zoning criteria
8. Available area (relative to max cover)
9. Location depth
1. Site ID
2. Incoming node connectivity centrality
3. Outgoing node connectivity centrality
4. Wave stress
5. Heat stress
6. Priority predecessors
7. GBRMPA zoning criteria
8. Available area (relative to max cover)
9. Location depth
# Arguments
- `site_ids` : Unique site ids.
Expand Down Expand Up @@ -382,7 +382,7 @@ function create_decision_matrix(
rule = (A[:, 4] .<= risk_tol) .& (A[:, 5] .> risk_tol)
A[rule, 5] .= NaN

filtered = vec(.!any(isnan.(A); dims = 2))
filtered = vec(.!any(isnan.(A); dims=2))

# Remove rows with NaNs
A = A[filtered, :]
Expand Down Expand Up @@ -460,13 +460,13 @@ function create_seed_matrix(
SE[SE[:, 8] .<= 0.0, 8] .= NaN # Filter out sites with no space

# Filter out identified locations
SE = SE[vec(.!any(isnan.(SE); dims = 2)), :]
SE = SE[vec(.!any(isnan.(SE); dims=2)), :]

return SE, wse
end

"""
create_fog_matrix(A, wt_conn_fog , wt_waves_fog, wt_heat_fog, wt_predec_fog, wt_hi_cover)
create_fog_matrix(A, wt_conn_fog , wt_waves_fog, wt_heat_fog, wt_predec_fog, wt_hi_cover)
Create shading specific decision matrix and apply weightings.
Expand Down Expand Up @@ -552,10 +552,10 @@ end
# Returns
Tuple :
- `pref_seed_locs` : Vector, Indices of preferred seeding locations
- `pref_fog_locs` : Vector, Indices of preferred shading locations
- `rankings` : Matrix[n_sites ⋅ 3] where columns are site_id, seeding_rank, shading_rank
Values of 0 indicate sites that were not considered
- `pref_seed_locs` : Vector, Indices of preferred seeding locations
- `pref_fog_locs` : Vector, Indices of preferred shading locations
- `rankings` : Matrix[n_sites ⋅ 3] where columns are site_id, seeding_rank, shading_rank
Values of 0 indicate sites that were not considered
"""
function guided_site_selection(
d_vars::DMCDA_vars,
Expand All @@ -568,8 +568,10 @@ function guided_site_selection(
in_conn::Vector{Float64},
out_conn::Vector{Float64},
strong_pred::Vector{Int64};
methods_mcda = mcda_methods()
)::Tuple{Vector{T}, Vector{T}, Matrix{T}} where {
methods_mcda=mcda_methods()
)::Tuple{
Vector{T}, Vector{T}, Matrix{T}
} where {
T <: Int64, IA <: AbstractArray{<:Int64}, IB <: AbstractArray{<:Int64}, B <: Bool
}
site_ids = copy(d_vars.site_ids)
Expand All @@ -585,7 +587,9 @@ function guided_site_selection(
end

n_iv_locs::Int64 = d_vars.n_site_int
priority_sites::Array{Int64} = d_vars.priority_sites[in.(d_vars.priority_sites, [site_ids])]
priority_sites::Array{Int64} = d_vars.priority_sites[in.(
d_vars.priority_sites, [site_ids]
)]
priority_zones::Array{String} = d_vars.priority_zones

zones = d_vars.zones[site_ids]
Expand All @@ -612,7 +616,9 @@ function guided_site_selection(
zone_preds_temp::Vector{Int64} = strong_pred[zones .== z_name]
for s::Int64 in unique(zone_preds_temp)
# for each predecessor site, add zone_weights * (no. of zone sites the site is a strongest predecessor for)
zone_preds[site_ids .== s] .= zone_preds[site_ids .== s] .+ (zone_weights[k] .* sum(zone_preds_temp .== s))
zone_preds[site_ids .== s] .=
zone_preds[site_ids .== s] .+
(zone_weights[k] .* sum(zone_preds_temp .== s))
end
# add zone_weights for sites in the zone (whether a strongest predecessor of a zone or not)
zone_sites[zones .== z_name] .= zone_weights[k]
Expand Down Expand Up @@ -732,7 +738,7 @@ end
Tuple :
- `pref_locs` : Vector, Indices of preferred intervention locations
- `rankings` : Matrix[n_sites ⋅ 3] where columns are site_id, seeding_rank, shading_rank
Values of 0 indicate sites that were not considered
Values of 0 indicate sites that were not considered
"""
function constrain_reef_cluster(
reefs::Union{Vector{String}, Vector{Float64}},
Expand All @@ -753,7 +759,9 @@ function constrain_reef_cluster(
local num_locs::Int64
for _ in 1:max_iters
# If enough space for seeding corals, keep n_site_int, else expand as needed
num_locs = max(findfirst(>=(area_to_seed), cumsum(available_space[loc_ordered_ids])), n_iv_locs)
num_locs = max(
findfirst(>=(area_to_seed), cumsum(available_space[loc_ordered_ids])), n_iv_locs
)

pref_locs = loc_ordered_ids[1:num_locs]

Expand All @@ -766,7 +774,7 @@ function constrain_reef_cluster(
pref_reefs = reefs[pref_locs] # Reefs that selected locations sit within

# Number of times a reef appears within each location
reef_occurances = vec(sum(pref_reefs .== unique_reefs; dims = 1))
reef_occurances = vec(sum(pref_reefs .== unique_reefs; dims=1))

# If more than n_reefs locations in a reef, swap out the worst locations
reefs_swap = unique_reefs[(reef_occurances .> max_members)]
Expand All @@ -779,9 +787,11 @@ function constrain_reef_cluster(

# Find locations in reefs which need replacement, and find the ids of lowest
# ranked locations in this set
locs_to_replace = vcat([
pref_locs[pref_reefs .== reef][replace_start:end] for reef in reefs_swap
]...)
locs_to_replace = vcat(
[
pref_locs[pref_reefs .== reef][replace_start:end] for reef in reefs_swap
]...,
)

# Acceptable reefs to switch out for
reef_switch_ids = unique_reefs[(reef_occurances .+ 1) .<= max_members]
Expand All @@ -797,10 +807,13 @@ function constrain_reef_cluster(

# Indices of the subset of locations which can be added which also sit within an
# allowed reef
add_locs_ind = findall(dropdims(any(
reshape(reefs[alternate_loc_ids], 1, length(reefs[alternate_loc_ids]))
.==
reef_switch_ids; dims = 1); dims = 1))
add_locs_ind = findall(
dropdims(
any(
reshape(reefs[alternate_loc_ids], 1, length(reefs[alternate_loc_ids]))
.==
reef_switch_ids; dims=1); dims=1),
)

# New preferred location set
locs_to_add_inds = add_locs_ind[1:length(locs_to_replace)]
Expand Down Expand Up @@ -864,13 +877,15 @@ function unguided_site_selection(

if seed_years
pref_seed_locs = zeros(Int64, n_site_int)
pref_seed_locs[1:s_n_site_int] .= StatsBase.sample(candidate_sites, s_n_site_int; replace = false)
pref_seed_locs[1:s_n_site_int] .= StatsBase.sample(
candidate_sites, s_n_site_int; replace=false
)
end

if fog_years
pref_fog_locs = zeros(Int64, n_site_int)
pref_fog_locs[1:s_n_site_int] .= StatsBase.sample(
candidate_sites, s_n_site_int; replace = false
candidate_sites, s_n_site_int; replace=false
)
end

Expand All @@ -890,14 +905,16 @@ Calculates mean over specified dimensions plus half the standard deviation.
# Returns
Weighted combination of mean and standard deviation of the projected environmental
conditions (e.g., DHWs, wave stress, etc):
(μ * w) + (σ * (1 - w))
(μ * w) + (σ * (1 - w))
"""
function summary_stat_env(
env_layer::AbstractArray,
dims::Union{Int64, Symbol, Tuple{Symbol, Symbol}};
w = 0.5,
w=0.5,
)::Vector{Float64}
return vec((mean(env_layer; dims = dims) .* w) .+ (std(env_layer; dims = dims) .* (1.0 - w)))
return vec(
(mean(env_layer; dims=dims) .* w) .+ (std(env_layer; dims=dims) .* (1.0 - w))
)
end

"""
Expand Down
Loading

0 comments on commit 9a4056a

Please sign in to comment.