Skip to content

Commit

Permalink
Merge pull request #62 from EcoJulia/mdc/normalize_nan_fix
Browse files Browse the repository at this point in the history
Making `normalize` work with NaNs
  • Loading branch information
gottacatchenall authored May 20, 2022
2 parents a4c6381 + fc82097 commit 349e682
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NeutralLandscapes"
uuid = "71847384-8354-4223-ac08-659a5128069f"
authors = ["Timothée Poisot <timothee.poisot@umontreal.ca>", "Michael Krabbe Borregaard <mkborregaard@sund.ku.dk>", "Michael David Catchen <michael.catchen@mail.mcgill.ca>", "Rafael Schouten <rafaelschouten@gmail.com>", "Virgile Baudrot <virgile.baudrot@posteo.net>"]
version = "0.1.0"
version = "0.1.1"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
4 changes: 2 additions & 2 deletions src/updaters/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ and 1. Note that this does not preserve the `rate` parameter for a given
difference between the total maximum and total minimum across all `mats`.
"""
function normalize(mats::Vector{M}) where {M<:AbstractMatrix}
mins, maxs = findmin.(mats), findmax.(mats)
totalmin, totalmax = findmin([x[1] for x in mins])[1], findmax([x[1] for x in maxs])[1]
mins, maxs = [NaNMath.min(x...) for x in mats], [NaNMath.max(x...) for x in mats]
totalmin, totalmax = NaNMath.min(mins...), NaNMath.max(maxs...)
returnmats = copy(mats)
for (i,mat) in enumerate(mats)
returnmats[i] = (mat .- totalmin) ./ (totalmax - totalmin)
Expand Down
18 changes: 18 additions & 0 deletions test/updaters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,30 @@ function testupdaters(model)
@test env != oldenv
end

function testnormalize(model)
updater = model()
env = rand(MidpointDisplacement(0.5), 50, 50)
seq = update(updater, env, 30)
normseq = normalize(seq)
@test length(findall(isnan, normseq[end])) == 0
for m in normseq
@test min(m...) >= 0 && max(m...) <= 1
end


env = [NaN 5 2 1 NaN; 3 4 5 2 1; 6 NaN 0 5 2; NaN NaN 0 4 5]
seq = update(updater, env, 30)
normseq = normalize(seq)
@test length(findall(isnan, normseq[end])) == 5

end

models = [
TemporallyVariableUpdater,
SpatiallyAutocorrelatedUpdater,
SpatiotemporallyAutocorrelatedUpdater
]


testnormalize.(models)
testupdaters.(models)

2 comments on commit 349e682

@gottacatchenall
Copy link
Member Author

Choose a reason for hiding this comment

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

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

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.1.1 -m "<description of version>" 349e68290820cc6855321c5d064ae7f2881829f0
git push origin v0.1.1

Please sign in to comment.