diff --git a/src/NeutralLandscapes.jl b/src/NeutralLandscapes.jl index 3760f84..bb8a7d6 100644 --- a/src/NeutralLandscapes.jl +++ b/src/NeutralLandscapes.jl @@ -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 diff --git a/src/classify.jl b/src/classify.jl index 266e577..4f28d99 100644 --- a/src/classify.jl +++ b/src/classify.jl @@ -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. """ @@ -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. """ @@ -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. diff --git a/src/landscape.jl b/src/landscape.jl index 268756b..f1b4a87 100644 --- a/src/landscape.jl +++ b/src/landscape.jl @@ -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} @@ -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 \ No newline at end of file +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