Skip to content

Commit

Permalink
✨ explore weighted endemism metric
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldansereau committed Feb 10, 2020
1 parent ab5915d commit 4f2eae5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Binary file added fig/quantiles/07_sdm_endemism_quantiles.pdf
Binary file not shown.
Binary file added fig/sdm/07_sdm_endemism.pdf
Binary file not shown.
69 changes: 69 additions & 0 deletions src/07_endemism.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Pkg; Pkg.activate(".")
using Distributed
@time @everywhere include("src/required.jl")

## Conditional arguments
# outcome = "raw" # desired outcome (required)
# outcome = "sdm" # desired outcome (required)
# save_figures = true # should figures be overwritten (optional)

# Make sure "outcome" is defined
if !(@isdefined outcome)
@warn "'outcome' not defined, must be either 'raw' or 'sdm'"
elseif (outcome != "raw" && outcome != "sdm")
@warn "'outcome' invalid, must be either 'raw' or 'sdm'"
else
@info "'outcome' currently set to '$(outcome)'"
end

## Load distributions for all species
@load "data/jld2/$(outcome)-distributions.jld2" distributions
## Load matrix Y
@load "data/jld2/$(outcome)-Y-matrices.jld2" Y Yobs Ytransf inds_obs inds_notobs

## Weighted endemism
# AOO (Area of occurrence): Species add 1/(number of sites occupied) to site scores

# Number of occupied sites per species
n_occ = map(x -> sum(filter(!isnan, x)), eachcol(Y))
# Weight site score by number of occupied sites per species
Yweight = hcat([replace(Y[:,i], 1 => 1/n_occ[i]) for i in 1:size(Y,2)]...)
# Sum scores per site for all species
endemism_scores = sum.(eachrow(Yweight))
# Check stats
describe(filter(!isnan, endemism_scores))
sort(filter(!isnan, endemism_scores), rev=true) # possibly some outliers, scaling problems for visualization
# Reshape to grid format
endemism_grid = reshape(endemism_scores, size(distributions[1]))

# Create SimpleSDMLayer with endemism values
endemism = SimpleSDMResponse(endemism_grid, distributions[1].left, distributions[1].right, distributions[1].bottom, distributions[1].top)

## Plot results
# Raw endemism scores
endemism_plot = plotSDM(endemism, c=:viridis)
heatmap!(endemism_plot,
title = "Endemism ($outcome distributions)",
colorbar_title = "Weigthed endemism (area of occurrence)",
dpi=300)
# Quantile endemism scores
endemism_qplot = plotSDM(quantiles(endemism), c=:viridis)
heatmap!(endemism_qplot,
title = "Endemism quantiles ($outcome distributions)",
colorbar_title = "Weighted endemism quantile (area of occurrence)",
dpi=300)

## Save result
# save_figures = true # should figures be overwritten (optional)
if (@isdefined save_figures) && save_figures == true
@info "Figures saved ($(outcome) lcbd)"
savefig(endemism_plot, "fig/$(outcome)/07_$(outcome)_endemism.pdf")
else
@info "Figures not saved ($(outcome) lcbd)"
end
if (@isdefined save_figures) && save_figures == true
@info "Figures saved ($(outcome) lcbd)"
savefig(endemism_qplot, "fig/quantiles/07_$(outcome)_endemism_quantiles.pdf")
else
@info "Figures not saved ($(outcome) lcbd)"
end

0 comments on commit 4f2eae5

Please sign in to comment.