-
Notifications
You must be signed in to change notification settings - Fork 2
introduce sumfinite
, enhancement to *finite
, rename minfinite
/maxfinite
#17
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f355451
Enhancements to `meanfinite` and `varfinite`, add `sumfinite`
johnnychen94 f475ba2
introduce test util `generate_test_types`
johnnychen94 1cbf046
enhance `minfinite` and `maxfinite`
johnnychen94 212d276
remove direct test dependency ColorVectorSpace
johnnychen94 e8d72e8
add an abs2 compatibility note in the docs of varfinite
johnnychen94 a4111bf
remove unused codes
johnnychen94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,134 @@ | ||
using ImageBase | ||
using ImageBase: varmult | ||
using Statistics | ||
using Test | ||
|
||
@testset "Reductions" begin | ||
_abs(x::Colorant) = mapreducec(abs, +, 0, x) | ||
|
||
A = rand(5,5,3) | ||
img = colorview(RGB, PermutedDimsArray(A, (3,1,2))) | ||
s12 = sum(img, dims=(1,2)) | ||
@test eltype(s12) <: RGB | ||
|
||
A = [NaN, 1, 2, 3] | ||
@test meanfinite(A, dims=1) ≈ [2] | ||
@test varfinite(A, dims=1) ≈ [1] | ||
|
||
A = [NaN NaN 1; | ||
1 2 3] | ||
vf = varfinite(A, dims=2) | ||
@test isnan(vf[1]) | ||
|
||
A = [NaN 1 2 3; | ||
NaN 6 5 4] | ||
mf = meanfinite(A, dims=1) | ||
vf = varfinite(A, dims=1) | ||
@test isnan(mf[1]) | ||
@test mf[2:end] ≈ [3.5,3.5,3.5] | ||
@test isnan(vf[1]) | ||
@test vf[2:end] ≈ [12.5,4.5,0.5] | ||
|
||
@test meanfinite(A, dims=2) ≈ reshape([2, 5], 2, 1) | ||
@test varfinite(A, dims=2) ≈ reshape([1, 1], 2, 1) | ||
|
||
@test meanfinite(A, dims=(1,2)) ≈ [3.5] | ||
@test varfinite(A, dims=(1,2)) ≈ [3.5] | ||
|
||
@test minfinite(A) == 1 | ||
@test maxfinite(A) == 6 | ||
@test maxabsfinite(A) == 6 | ||
A = rand(10:20, 5, 5) | ||
@test minfinite(A) == minimum(A) | ||
@test maxfinite(A) == maximum(A) | ||
A = reinterpret(N0f8, rand(0x00:0xff, 5, 5)) | ||
@test minfinite(A) == minimum(A) | ||
@test maxfinite(A) == maximum(A) | ||
A = rand(Float32,3,5,5) | ||
img = colorview(RGB, A) | ||
dc = meanfinite(img, dims=1)-reshape(reinterpretc(RGB{Float32}, mean(A, dims=2)), (1,5)) | ||
@test maximum(map(_abs, dc)) < 1e-6 | ||
dc = minfinite(img)-RGB{Float32}(minimum(A, dims=(2,3))...) | ||
@test _abs(dc) < 1e-6 | ||
dc = maxfinite(img)-RGB{Float32}(maximum(A, dims=(2,3))...) | ||
@test _abs(dc) < 1e-6 | ||
@testset "sumfinite, meanfinite, varfinite" begin | ||
for T in generate_test_types([N0f8, Float32], [Gray, RGB]) | ||
A = rand(T, 5, 5) | ||
s12 = sum(A, dims=(1,2)) | ||
@test eltype(s12) <: Union{T, float(T), float64(T)} | ||
|
||
@test sumfinite(A) ≈ sum(A) | ||
@test sumfinite(A, dims=1) ≈ sum(A, dims=1) | ||
@test sumfinite(A, dims=(1, 2)) ≈ sum(A, dims=(1, 2)) | ||
|
||
@test meanfinite(A) ≈ mean(A) | ||
@test meanfinite(A, dims=1) ≈ mean(A, dims=1) | ||
@test meanfinite(A, dims=(1, 2)) ≈ mean(A, dims=(1, 2)) | ||
|
||
@test varfinite(A) ≈ varmult(⋅, A) | ||
@test varfinite(A, dims=1) ≈ varmult(⋅, A, dims=1) | ||
@test varfinite(A, dims=(1, 2)) ≈ varmult(⋅, A, dims=(1, 2)) | ||
|
||
# test NaN/Inf | ||
if eltype(T) != N0f8 | ||
A = rand(T, 5, 5) .- 0.5 .* oneunit(T) | ||
A[1] = Inf | ||
@test sum(A) ≈ A[1] | ||
@test sum(abs, A) ≈ A[1] | ||
@test sumfinite(A) ≈ sum(A[2:end]) | ||
@test sumfinite(abs, A) ≈ sum(abs, A[2:end]) | ||
A[1] = NaN | ||
@test isnan(sum(A)) | ||
@test isnan(sum(abs, A)) | ||
@test sumfinite(A) ≈ sum(A[2:end]) | ||
@test sumfinite(abs, A) ≈ sum(abs, A[2:end]) | ||
|
||
A = rand(T, 5, 5) .- 0.5 .* oneunit(T) | ||
A[1] = Inf | ||
@test mean(A) ≈ A[1] | ||
@test mean(abs, A) ≈ A[1] | ||
@test meanfinite(A) ≈ mean(A[2:end]) | ||
@test meanfinite(abs, A) ≈ mean(abs, A[2:end]) | ||
A[1] = NaN | ||
@test isnan(mean(A)) | ||
@test isnan(mean(abs, A)) | ||
@test meanfinite(A) ≈ mean(A[2:end]) | ||
@test meanfinite(abs, A) ≈ mean(abs, A[2:end]) | ||
|
||
A = rand(T, 5, 5) | ||
A[1] = Inf | ||
@test isnan(varmult(⋅, A)) | ||
@test varfinite(A) ≈ varmult(⋅, A[2:end]) | ||
A[1] = NaN | ||
@test isnan(varmult(⋅, A)) | ||
@test varfinite(A) ≈ varmult(⋅, A[2:end]) | ||
end | ||
end | ||
|
||
A = [NaN, 1, 2, 3] | ||
@test meanfinite(A, dims=1) ≈ [2] | ||
@test varfinite(A, dims=1) ≈ [1] | ||
|
||
A = [NaN NaN 1; | ||
1 2 3] | ||
vf = varfinite(A, dims=2) | ||
@test isnan(vf[1]) | ||
|
||
A = [NaN 1 2 3; | ||
NaN 6 5 4] | ||
mf = meanfinite(A, dims=1) | ||
vf = varfinite(A, dims=1) | ||
@test isnan(mf[1]) | ||
@test mf[2:end] ≈ [3.5,3.5,3.5] | ||
@test isnan(vf[1]) | ||
@test vf[2:end] ≈ [12.5,4.5,0.5] | ||
|
||
@test meanfinite(A, dims=2) ≈ reshape([2, 5], 2, 1) | ||
@test varfinite(A, dims=2) ≈ reshape([1, 1], 2, 1) | ||
|
||
@test meanfinite(A, dims=(1,2)) ≈ [3.5] | ||
@test varfinite(A, dims=(1,2)) ≈ [3.5] | ||
|
||
# Ensure we're consistant with our decision to `abs2` in ColorVectorSpace | ||
# See also: https://github.com/JuliaGraphics/ColorVectorSpace.jl/blob/master/README.md#abs-and-abs2 | ||
A = rand(Gray, 4, 4) | ||
@test varfinite(A) ≈ varfinite(RGB.(A)) | ||
A[1] = Inf | ||
@test varfinite(A) ≈ varfinite(RGB.(A)) | ||
A[1] = NaN | ||
@test varfinite(A) ≈ varfinite(RGB.(A)) | ||
end | ||
|
||
@testset "minfinite, maxfinite, maxabsfinite" begin | ||
for T in generate_test_types([N0f8, Float32], [Gray, ]) | ||
A = rand(T, 5, 5) | ||
@test @inferred(minimum_finite(A)) == minimum(A) | ||
@test @inferred(maximum_finite(A)) == maximum(A) | ||
@test minimum_finite(A; dims=1) == minimum(A; dims=1) | ||
@test maximum_finite(A; dims=1) == maximum(A; dims=1) | ||
@test_broken @inferred maximum_finite(A; dims=1) | ||
@test_broken @inferred minimum_finite(A; dims=1) | ||
|
||
@test maximum_finite(abs2, A) == maximum(abs2, A) | ||
@test_broken @inferred maximum(abs2, A) | ||
@test_broken @inferred minimum(abs2, A) | ||
|
||
if eltype(T) != N0f8 | ||
A = rand(T, 5, 5) .- 0.5 * rand(T, 5, 5) | ||
A[1] = Inf | ||
|
||
@test @inferred(minimum_finite(A)) == minimum(A[2:end]) | ||
@test @inferred(maximum_finite(A)) == maximum(A[2:end]) | ||
@test minimum_finite(abs, A) == minimum(abs, A[2:end]) | ||
@test maximum_finite(abs2, A) == maximum(abs2, A[2:end]) | ||
end | ||
end | ||
|
||
# minimum_finite and maximum_finite for RGB are processed per channel | ||
A = rand(RGB{Float32}, 5, 5) | ||
@test minimum_finite(A) == RGB(minimum(channelview(A), dims=(2, 3))...) | ||
@test maximum_finite(A) == RGB(maximum(channelview(A), dims=(2, 3))...) | ||
|
||
# Container of abstract type | ||
A = Any[1, 2, Gray(0.3)] | ||
@test minimum_finite(A) == 0.3 | ||
@test maximum_finite(A) == 2.0 | ||
@test Base.return_types(minimum_finite, (typeof(A),)) == Base.return_types(minimum, (typeof(A), )) | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# AbstractGray and Color3 tests should be generated seperately | ||
function generate_test_types(number_types::AbstractArray{<:DataType}, color_types::AbstractArray{<:UnionAll}) | ||
test_types = map(Iterators.product(number_types, color_types)) do T | ||
try | ||
T[2]{T[1]} | ||
catch err | ||
!isa(err, TypeError) && rethrow(err) | ||
end | ||
end | ||
test_types = filter(x->x != false, test_types) | ||
if isempty(filter(x->x<:Color3, test_types)) | ||
test_types = [number_types..., test_types...] | ||
end | ||
test_types | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make this
varmult_finite
instead?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good suggestion, let me open an issue for it first and then fix it in a separate PR; this PR is already quite big actually.
Edit: issue opened #18