Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/distance gradient #13

Merged
merged 14 commits into from
Feb 14, 2021
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.0.1"

[deps]
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Expand Down
Binary file modified docs/src/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion docs/src/gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ heatmap(rand(EdgeGradient(), (45, 45)))
heatmap(rand(WaveSurface(), (45, 45)))
```

## Distance gradient

```@example gallery
sources = unique(rand(1:2025, 15))
heatmap(rand(DistanceGradient(sources), (45, 45)))
```

## Rectangular cluster

```@example gallery
heatmap(rand(RectangularCluster(), (45, 45)))
```
```
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ NoGradient
PlanarGradient
EdgeGradient
WaveSurface
DistanceGradient
RectangularCluster
```

Expand Down
4 changes: 4 additions & 0 deletions src/NeutralLandscapes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module NeutralLandscapes

import NaNMath
using Random: rand!
using NearestNeighbors

abstract type NeutralLandscapeMaker end
export NeutralLandscapeMaker
Expand All @@ -21,6 +22,9 @@ export EdgeGradient
include(joinpath("algorithms", "wavesurface.jl"))
export WaveSurface

include(joinpath("algorithms", "distancegradient.jl"))
export DistanceGradient

include(joinpath("algorithms", "rectangularcluster.jl"))
export RectangularCluster

Expand Down
23 changes: 23 additions & 0 deletions src/algorithms/distancegradient.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
DistanceGradient

The `sources` field is the *linear* indices of the matrix, from which the
distance must be calculated.
"""
struct DistanceGradient <: NeutralLandscapeMaker
sources::Vector{Integer}
end

DistanceGradient() = DistanceGradient([1])

function _landscape!(mat, alg::DistanceGradient)
@assert maximum(alg.sources) <= length(mat)
@assert minimum(alg.sources) > 0
coordinates = Matrix{Float64}(undef, (2, prod(size(mat))))
for (i, p) in enumerate(Iterators.product(axes(mat)...))
coordinates[1:2, i] .= p
end
tree = KDTree(coordinates[:,alg.sources])
mat[:] .= nn(tree, coordinates)[2]
return mat
end
2 changes: 1 addition & 1 deletion src/algorithms/nogradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ This type is used to generate a random landscape with no gradients
struct NoGradient <: NeutralLandscapeMaker
end

_landscape!(mat, ::NoGradient) where {IT <: Integer} = rand!(mat)
_landscape!(mat, ::NoGradient) = rand!(mat)