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

Commit

Permalink
Merge pull request #83 from EcoJulia/tp/sources
Browse files Browse the repository at this point in the history
Masking/sliding improvements
  • Loading branch information
tpoisot authored Mar 2, 2021
2 parents 907070f + 3914938 commit d7f4c58
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
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

2 comments on commit d7f4c58

@tpoisot
Copy link
Member Author

@tpoisot tpoisot commented on d7f4c58 Mar 3, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/31172

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.4.7 -m "<description of version>" d7f4c589407ff72451bfa6541910eb3ca104bb4c
git push origin v0.4.7

Please sign in to comment.