Skip to content

Commit

Permalink
refactor and fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Mar 3, 2021
1 parent fe8ae52 commit cecab92
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
57 changes: 21 additions & 36 deletions src/NeutralLandscapes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,35 @@ using NearestNeighbors: KDTree, knn, nn
using DataStructures: IntDisjointSets, union!, find_root, push!
using Base: @kwdef

"""
All algorithms are descended from the `NeutralLandscapeMaker` type. A new
algorithm must minimally implement and `_landscape!` method for this type.
"""
abstract type NeutralLandscapeMaker end
export NeutralLandscapeMaker

include("landscape.jl")
export rand, rand!
export classify!, classify, blend, label

include(joinpath("algorithms", "nogradient.jl"))
export NoGradient

include(joinpath("algorithms", "planargradient.jl"))
export PlanarGradient

include(joinpath("algorithms", "edgegradient.jl"))
export EdgeGradient
export NeutralLandscapeMaker

include(joinpath("algorithms", "diamondsquare.jl"))
export DiscreteVoronoi
export DiamondSquare, MidpointDisplacement

include(joinpath("algorithms", "wavesurface.jl"))
export WaveSurface

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

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

include(joinpath("algorithms", "nnelement.jl"))
export NearestNeighborElement

include(joinpath("algorithms", "nncluster.jl"))
export NearestNeighborCluster

include(joinpath("algorithms", "perlinnoise.jl"))
export NearestNeighborElement
export NoGradient
export PerlinNoise
export PlanarGradient
export RectangularCluster
export WaveSurface

include(joinpath("algorithms", "discretevoronoi.jl"))
export DiscreteVoronoi

include("landscape.jl")
include("classify.jl")
export classify!, classify, blend, label
include("algorithms/diamondsquare.jl")
include("algorithms/discretevoronoi.jl")
include("algorithms/distancegradient.jl")
include("algorithms/edgegradient.jl")
include("algorithms/nncluster.jl")
include("algorithms/nnelement.jl")
include("algorithms/nogradient.jl")
include("algorithms/perlinnoise.jl")
include("algorithms/planargradient.jl")
include("algorithms/rectangularcluster.jl")
include("algorithms/wavesurface.jl")

end # module
6 changes: 3 additions & 3 deletions src/classify.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
classify!(array, weights[, classifyMask])
classify!(array, weights[, mask])
Classify an array in-place into proportions based upon a list of class weights.
"""
Expand All @@ -20,7 +20,7 @@ end
classify!(array, weights::Real, mask = nothing) = classify!(array, ones(weights), mask)

"""
classify(array, weights[, classifyMask])
classify(array, weights[, mask])
Classify an array into proportions based upon a list of class weights.
"""
Expand Down Expand Up @@ -79,7 +79,7 @@ const _neighborhoods = Dict(


"""
label(mat[, neighborhood])
label(mat[, neighborhood = :rook])
Assign an arbitrary label to all clusters of contiguous matrix elements with the same value.
Returns a matrix of values and the total number of final clusters.
Expand Down
43 changes: 24 additions & 19 deletions src/landscape.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import Random.rand!

"""
_rescale!(mat)
Changes the matrix `mat` so that it is between `0` and `1`.
"""
function _rescale!(mat)
mat .-= NaNMath.minimum(mat)
mat ./= NaNMath.maximum(mat)
end

"""
mask!(array::AbstractArray{<:AbstractFloat}, maskarray::AbstractArray{<:AbstractBool})
NeutralLandscapeMaker
Modifies `array` so that the positions at which `maskarray` is `false` are
replaced by `NaN`.
Abstract supertype that all algorithms are descended from. A new
algorithm must minimally implement a `_landscape!` method for this type.
"""
function mask!(array::AbstractArray{<:Float64}, maskarray::AbstractArray{<:Bool})
(size(array) == size(maskarray)) || throw(DimensionMismatch("The dimensions of array, $(size(array)), and maskarray, $(size(maskarray)), must match. "))
array[.!maskarray] .= NaN
array
end
abstract type NeutralLandscapeMaker end

"""
rand(alg, dims::Tuple{Vararg{Int64,2}}; mask=nothing) where {T <: Integer}
Expand All @@ -46,4 +32,23 @@ function rand!(mat::AbstractArray{<:AbstractFloat,2} where N, alg::T; mask=nothi
_landscape!(mat, alg)
isnothing(mask) || mask!(mat, mask)
_rescale!(mat)
end
end

"""
mask!(array::AbstractArray{<:AbstractFloat}, maskarray::AbstractArray{<:AbstractBool})
Modifies `array` so that the positions at which `maskarray` is `false` are
replaced by `NaN`.
"""
function mask!(array::AbstractArray{<:Float64}, maskarray::AbstractArray{<:Bool})
(size(array) == size(maskarray)) || throw(DimensionMismatch("The dimensions of array, $(size(array)), and maskarray, $(size(maskarray)), must match. "))
array[.!maskarray] .= NaN
array
end


# Changes the matrix `mat` so that it is between `0` and `1`.
function _rescale!(mat)
mat .-= NaNMath.minimum(mat)
mat ./= NaNMath.maximum(mat)
end

0 comments on commit cecab92

Please sign in to comment.