Skip to content

Commit

Permalink
Add counterfactual minus guided/unguided diff at location level
Browse files Browse the repository at this point in the history
  • Loading branch information
Zapiano committed Mar 27, 2024
1 parent 5e807de commit 6d23b6a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
20 changes: 18 additions & 2 deletions ext/AvizExt/viz/spatial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function create_map!(
color_map::Union{Symbol, Vector{Symbol}, RGBA{Float32}, Vector{RGBA{Float32}}}=:grayC,
legend_params::Union{Tuple, Nothing}=nothing,
axis_opts::Dict=Dict(),
)
)::Union{GridLayout,GridPosition}
axis_opts[:title] = get(axis_opts, :title, "Study Area")
axis_opts[:xlabel] = get(axis_opts, :xlabel, "Longitude")
axis_opts[:ylabel] = get(axis_opts, :ylabel, "Latitude")
Expand All @@ -58,10 +58,11 @@ function create_map!(

spatial.yticklabelpad = 50
spatial.ytickalign = 10
min_val = @lift(minimum($data))
max_val = @lift(maximum($data))

# Plot geodata polygons using data as internal color
color_range = (0.0, max_val[])
color_range = min_val[] < 0 ? (min_val[], max_val[]) : (0, max_val[])

poly!(
spatial,
Expand Down Expand Up @@ -215,6 +216,21 @@ function ADRIA.viz.map!(
)
end

function ADRIA.viz.diff_map(
rs::ResultSet,
diff_outcome::YAXArray{Float64};
opts::Dict{Symbol,<:Any}=Dict{Symbol,Any}(),
fig_opts::Dict{Symbol,<:Any}=Dict{Symbol,Any}(),
axis_opts::Dict{Symbol,<:Any}=Dict{Symbol,Any}()
)
# TODO hande the cases where thes only positive or only negative values
min_val, max_res = extrema(diff_outcome)
mid_val = -min_val / (max_res - min_val)
div_cmap::Vector{RGBA{Float32}} = diverging_palette(10, 200; mid=mid_val)
opts[:color_map] = div_cmap
return ADRIA.viz.map(rs, diff_outcome; axis_opts=axis_opts, opts=opts, fig_opts=fig_opts)
end

"""
make_geojson_copy(ds::Union{ResultSet,Domain})::String
Expand Down
4 changes: 1 addition & 3 deletions src/metrics/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ using DataFrames

using ADRIA: coral_spec, colony_mean_area, ResultSet, timesteps, site_k_area, site_area


abstract type Outcome end


Expand Down Expand Up @@ -608,12 +607,11 @@ function _relative_shelter_volume(rs::ResultSet)::YAXArray
end
relative_shelter_volume = Metric(_relative_shelter_volume, (:timesteps, :sites, :scenarios))


include("pareto.jl")
include("ranks.jl")
include("reef_indices.jl")
include("scenario.jl")
include("site_level.jl")
include("spatial.jl")
include("temporal.jl")
include("utils.jl")

Expand Down
40 changes: 40 additions & 0 deletions src/metrics/site_level.jl → src/metrics/spatial.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Bootstrap
using Random

"""Functions and methods to produce location-level summaries."""

"""
Expand Down Expand Up @@ -106,3 +109,40 @@ function summarize(
)::YAXArray where {D,T,N,A}
return summarize(data[timesteps=timesteps], alongs_axis, metric)
end

"""
cf_difference_map(rs, scens, metric)
Give the mean bootstraped difference from the counterfactual for a given metric.
"""
function cf_difference_map(rs, scens, metric)
# Extract some metric
outcomes = metric(rs)
locations = rs.site_data

# Mean over all timesteps
outcomes_mean = dropdims(mean(outcomes, dims=:timesteps), dims=:timesteps)

cf_outcomes = outcomes_mean[scenarios=scens.guided .== -1]
ug_outcomes = outcomes_mean[scenarios=scens.guided .== 0]
gd_outcomes = outcomes_mean[scenarios=scens.guided .> 0]

n_locs = length(outcomes.sites)

ug_result = ZeroDataCube(; T=Float64, sites=locations.reef_siteid)
gd_result = ZeroDataCube(; T=Float64, sites=locations.reef_siteid)
n_scens = size(cf_outcomes, :scenarios)
for loc in 1:n_locs
cf_shuf_set1 = shuffle(1:n_scens)
ug_shuf_set = shuffle(1:n_scens)
ug_diff = collect(ug_outcomes[loc, ug_shuf_set] .- cf_outcomes[loc, cf_shuf_set1])
ug_result[loc] = bootstrap(median, ug_diff, BalancedSampling(100)).t0[1]

cf_shuf_set2 = shuffle(1:n_scens)
gd_shuf_set = shuffle(1:n_scens)
gd_diff = collect(gd_outcomes[loc, gd_shuf_set] .- cf_outcomes[loc, cf_shuf_set2])
gd_result[loc] = bootstrap(median, gd_diff, BalancedSampling(100)).t0[1]
end

return ug_result, gd_result
end
1 change: 1 addition & 0 deletions src/viz/viz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ function ranks_to_frequencies!() end
# Spatial
function map() end
function map!() end
function diff_map() end

end # module

0 comments on commit 6d23b6a

Please sign in to comment.