Skip to content

Commit

Permalink
Merge pull request #55 from PoisotLab/tp/sdt
Browse files Browse the repository at this point in the history
Vignette update
  • Loading branch information
tpoisot authored Feb 13, 2023
2 parents cebab4e + bc018bf commit 369203e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1.6.0]
julia-version: [1.8.0]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
Expand Down
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name = "SpatialBoundaries"
uuid = "8d2ba62a-3d23-4a2b-b692-6b63e9267be2"
authors = ["Tanya Strydom <[email protected]>", "Timothée Poisot <[email protected]>"]
version = "0.0.4"
version = "0.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SpeciesDistributionToolkit = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
VoronoiDelaunay = "72f80fcb-8c52-57d9-aff0-40c1a3526986"

[compat]
Requires = "1"
SpeciesDistributionToolkit = "0.0.1"
StatsBase = "0.33"
VoronoiDelaunay = "0.4"
julia = "1.6"
julia = "1.8"
5 changes: 3 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
NeutralLandscapes = "71847384-8354-4223-ac08-659a5128069f"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
SimpleSDMLayers = "2c645270-77db-11e9-22c3-0f302a89c64c"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
SpeciesDistributionToolkit = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"

[compat]
SimpleSDMLayers = "0.8"
SpeciesDistributionToolkit = "0.0"
51 changes: 24 additions & 27 deletions docs/src/vignettes/simplesdmlayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,39 @@
# $y$, $z$) and the output data would be typed as `TriangulationWomble` objects.

using SpatialBoundaries
using SimpleSDMLayers
using Plots
using SpeciesDistributionToolkit
using CairoMakie
import Plots # Required for radial histograms

# First we can start by defining the extent of the Southwestern islands of
# Hawaii, which can be used to restrict the extraction of the various landcover
# layers from the EarthEnv database. We do the actual layers querying using
# `SimpleSDMLayers`:

hawaii = (left = -160.2, right = -154.5, bottom = 18.6, top = 22.5)
landcover = SimpleSDMPredictor(EarthEnv, LandCover, 1:12; hawaii...)
dataprovider = RasterData(EarthEnv, LandCover)
landcover_classes = SimpleSDMDatasets.layers(dataprovider)
landcover = [SimpleSDMPredictor(dataprovider; layer=class, full=true, hawaii...) for class in landcover_classes]

# We can remove all the areas that contain 100% water from the landcover data as
# our question of interest is restricted to the terrestrial realm. We do this by
# using the "Open Water" layer to mask over each of the landcover layers
# individually:

ow_index = findfirst(isequal("Open Water"), layernames(EarthEnv, LandCover))
not_water = broadcast(!isequal(100), landcover[ow_index])
ow_index = findfirst(isequal("Open Water"), landcover_classes)
not_water = landcover[ow_index] .!== 0x64
lc = [mask(not_water, layer) for layer in landcover]

# As layers one through four of the EarthEnv data are concerned with data on
# woody cover (*i.e.* "Evergreen/Deciduous Needleleaf Trees", "Evergreen
# Broadleaf Trees", "Deciduous Broadleaf Trees", and "Mixed/Other Trees") we
# will work with only these layers. For a quick idea we of what the raw
# landcover data looks like we can sum these four layers and plot the total
# woody cover for the Southwestern islands.
# woody cover:

tree_lc = convert(Float32, sum(lc[1:4]))
plot(tree_lc; c = :Greens, frame = :box, leg = false)
classes_with_trees = findall(contains.(landcover_classes, "Trees"))
tree_lc = convert(Float32, reduce(+, lc[classes_with_trees]))
heatmap(tree_lc; colormap=:linear_kbgyw_5_98_c62_n256)

# Although we have previously summed the four landcover layers for the actual
# wombling part we will apply the wombling function to each layer before we
Expand All @@ -57,15 +61,15 @@ plot(tree_lc; c = :Greens, frame = :box, leg = false)
# woody cover layers. This will give as a vector containing four `LatticeWomble`
# objects (since the input data was in the form of a matrix).

wombled_layers = broadcast(wombling, (lc[1:4]))
wombled_layers = wombling.(lc[classes_with_trees]);

# As we are interested in overall woody cover for Southwestern islands we can
# take the `wombled_layers` vector and use them with the `mean` function to get
# the overall mean wombling value of the rate and direction of change for woody
# cover. This will 'flatten' the four wombled layers into a single
# `LatticeWomble` object.

wombled_mean = mean(wombled_layers)
wombled_mean = mean(wombled_layers);

# From the `wombled_mean` object we can 'extract' the layers for both the mean
# rate and direction of change. For ease of plotting we will also convert these
Expand All @@ -81,21 +85,14 @@ rate, direction = SimpleSDMPredictor(wombled_mean)
# and this is simply for ease of plotting.

b = similar(rate)
b.grid[boundaries(wombled_mean, 0.1; ignorezero = true)] .= 1.0

for t in 0.1
b.grid[boundaries(wombled_mean, t; ignorezero = true)] .= t
end
# We will overlay the identified boundaries (in green) over the rate of change
# (in levels of grey):

# In addition to being used to help find candidate boundary cells we can also
# use this object (`b`) as masking layer when visualising wombling outputs. In
# this case we can view the `rate` layer in a similar fashion to the original
# landcover layer but by masking it with `b` we only plot the candidate
# boundaries *i.e.* the cells with the top 10% of highest rate of change values.

plot(
plot(rate; c = :grey85, frame = :box),
mask(b, rate);
c = :black, frame = :box, colorbar = false)
heatmap(rate, colormap=[:grey95, :grey5])
heatmap!(b, colormap=[:transparent, :green])
current_figure()

# For this example we will plot the direction of change as radial plots to get
# an idea of the prominent direction of change. Here we will plot *all* the
Expand All @@ -120,8 +117,8 @@ direction_candidate = mask(b, direction)
# collect the cells and convert them from degrees to radians. Then we can start
# by plotting the direction of change of *all* cells.

stephist(
deg2rad.(collect(direction_all));
Plots.stephist(
deg2rad.(values(direction_all));
proj=:polar,
lab="",
c=:teal,
Expand All @@ -132,8 +129,8 @@ stephist(
# Followed by plotting the direction of change only for cells that are
# considered as candidate boundary cells.

stephist(
deg2rad.(collect(direction_candidate));
Plots.stephist(
deg2rad.(values(direction_candidate));
proj=:polar,
lab="",
c=:red,
Expand Down
9 changes: 2 additions & 7 deletions src/SpatialBoundaries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ using VoronoiDelaunay
using LinearAlgebra
using Statistics
using StatsBase
using Requires
using SpeciesDistributionToolkit

include(joinpath("types", "Womble.jl"))
export Womble, TriangulationWomble, LatticeWomble

include(joinpath("lib", "rateofchange.jl"))

include(joinpath("lib", "wombling.jl"))
include(joinpath("extensions", "SpeciesDistributionToolkit.jl"))
export wombling

include(joinpath("lib", "boundaries.jl"))
Expand All @@ -21,10 +22,4 @@ export boundaries
include(joinpath("lib", "overallmean.jl"))
export mean

function __init__()
@require SimpleSDMLayers = "2c645270-77db-11e9-22c3-0f302a89c64c" include(
joinpath("extensions", "SimpleSDMLayers.jl"),
)
end

end # module
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Performs a lattice wombling on a `SimpleSDMLayer`.
"""
function wombling(layer::T; convert_to::Type = Float64) where {T <: SimpleSDMLayers.SimpleSDMLayer}
function wombling(layer::T; convert_to::Type = Float64) where {T <: SimpleSDMLayer}
try
global nan = convert(convert_to, NaN)
catch
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[deps]
NeutralLandscapes = "71847384-8354-4223-ac08-659a5128069f"
SimpleSDMLayers = "2c645270-77db-11e9-22c3-0f302a89c64c"
SpeciesDistributionToolkit = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7 changes: 3 additions & 4 deletions test/units/R1_simplesdmlayers.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module SBTestSimpleSDMLayers

using SpatialBoundaries
using SimpleSDMLayers
using SpeciesDistributionToolkit
using Test

precipitation = SimpleSDMPredictor(
WorldClim,
BioClim,
12;
RasterData(WorldClim2, BioClim),
layer=12;
left = -80.0,
right = -56.0,
bottom = 44.0,
Expand Down

4 comments on commit 369203e

@tpoisot
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Changing package repo URL not allowed, please submit a pull request with the URL change to the target registry and retry.

@tpoisot
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/77667

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 369203e7ffe676b791089b91ea91d11568b95523
git push origin v0.1.0

Please sign in to comment.