Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Masking/sliding improvements #83

Merged
merged 5 commits into from
Mar 2, 2021
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SimpleSDMLayers"
uuid = "2c645270-77db-11e9-22c3-0f302a89c64c"
authors = ["Timothée Poisot <[email protected]>", "Gabriel Dansereau <[email protected]>"]
version = "0.4.6"
version = "0.4.7"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand Down
17 changes: 15 additions & 2 deletions src/integrations/GBIF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ function Base.getindex(layer::T, records::GBIF.GBIFRecords) where {T <: SimpleSD
return convert(Vector{eltype(layer)}, filter(!isnothing, [layer[records[i]] for i in 1:length(records)]))
end

"""
Base.getindex(layer::T, records::Vector{GBIF.GBIFRecord}) where {T <: SimpleSDMLayer}

Returns the values of a layer at all occurrences in a `GBIFRecord` array.
"""
function Base.getindex(layer::T, records::Vector{GBIF.GBIFRecord}) where {T <: SimpleSDMLayer}
return [layer[record] for record in records]
end

"""
mask!(layer::SimpleSDMResponse{T}, records::GBIF.GBIFRecords) where {T <: AbstractBool}

Expand All @@ -94,7 +103,9 @@ if an occurrence is found in the cell, `false` if not.
"""
function mask!(layer::SimpleSDMResponse{T}, records::GBIF.GBIFRecords) where {T <: Bool}
for record in records
layer[record] = true
if !isnothing(layer[record])
layer[record] = true
end
end
return layer
end
Expand All @@ -107,7 +118,9 @@ the number of occurrences in the cell.
"""
function mask!(layer::SimpleSDMResponse{T}, records::GBIF.GBIFRecords) where {T <: Number}
for record in records
layer[record] = layer[record] + one(T)
if !isnothing(layer[record])
layer[record] = layer[record] + one(T)
end
end
return layer
end
Expand Down
17 changes: 12 additions & 5 deletions src/operations/sliding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ have data.
This function is currently relatively slow. Performance improvements will arrive
at some point.
"""
function slidingwindow(layer::LT, f::FT, d::IT) where {LT <: SimpleSDMLayer, FT <: Function, IT <: Number}
function slidingwindow(layer::LT, f::FT, d::IT; threaded::Bool=Threads.nthreads()>1) where {LT <: SimpleSDMLayer, FT <: Function, IT <: Number}
# We infer the return type from a call to the function on the first three elements
return_type = typeof(f(collect(layer)[1:min(3, length(layer))]))

Expand All @@ -39,13 +39,20 @@ function slidingwindow(layer::LT, f::FT, d::IT) where {LT <: SimpleSDMLayer, FT
# Store latitudes and longitudes
_lat, _lon = latitudes(layer), longitudes(layer)

# Pre-allocation of a vector of pairs containing the pixel and the
# Vector of all positions with a value
filled_positions = CartesianIndices(layer.grid)[findall(!isnothing, layer.grid)]

# We then filter in the occupied positions
for pos in filled_positions
neighbors = filter(p -> haversine((_lon[Tuple(pos)[2]], _lat[Tuple(pos)[1]]), (_lon[Tuple(p)[2]], _lat[Tuple(p)[1]])) < d, filled_positions)
N.grid[pos] = f(layer.grid[neighbors])
if threaded
Threads.@threads for pos in filled_positions
neighbors = filter(p -> haversine((_lon[Tuple(pos)[2]], _lat[Tuple(pos)[1]]), (_lon[Tuple(p)[2]], _lat[Tuple(p)[1]])) < d, filled_positions)
N.grid[pos] = f(layer.grid[neighbors])
end
else
for pos in filled_positions
neighbors = filter(p -> haversine((_lon[Tuple(pos)[2]], _lat[Tuple(pos)[1]]), (_lon[Tuple(p)[2]], _lat[Tuple(p)[1]])) < d, filled_positions)
N.grid[pos] = f(layer.grid[neighbors])
end
end

# And we return the object
Expand Down
3 changes: 3 additions & 0 deletions test/gbif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ kingfisher = GBIF.taxon("Megaceryle alcyon", strict=true)

o = GBIF.occurrences(kingfisher, "hasCoordinate" => "true")

# Array of occurrences is the same thing as occurrences collection
@test temperature[o] == temperature[o.occurrences[1:length(o)]]

# Extract from a single record
for oc in o
@test typeof(temperature[oc]) <: Number
Expand Down