-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add handling of an empty iterator for mean and var #29033
Changes from 3 commits
4f9028c
40b2ea7
9e0b0ba
0487d17
bff28ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ end | |
end | ||
|
||
@testset "mean" begin | ||
@test_throws ArgumentError mean(()) | ||
@test_throws MethodError mean(()) | ||
@test mean((1,2,3)) === 2. | ||
@test mean([0]) === 0. | ||
@test mean([1.]) === 1. | ||
|
@@ -86,6 +86,21 @@ end | |
@test ismissing(mean([missing, NaN])) | ||
@test isequal(mean([missing 1.0; 2.0 3.0], dims=1), [missing 2.0]) | ||
@test mean(skipmissing([1, missing, 2])) === 1.5 | ||
@test isequal(mean(Complex{Float64}[]), NaN+NaN*im) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also make sense to test that an empty BTW, you could probably use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is in muddy waters :) You cannot use
and this is exactly the case here, that is why I had to use I will add the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I must be missing something: you really expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The situation is more complex (and that is why I have used this as a test):
also you have:
For the same reason that we have different |
||
@test mean(Complex{Float64}[]) isa Complex{Float64} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not also test the value? Same in other places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The value is tested above in line 89 (unless you meant something else). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, this one is, I originally spotted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All cleaned up now (hopefully 😄, as you have a keener eye for details 👍) |
||
@test isequal(mean(skipmissing(Complex{Float64}[])), NaN+NaN*im) | ||
@test mean(skipmissing(Complex{Float64}[])) isa Complex{Float64} | ||
@test isequal(mean(abs, Complex{Float64}[]), NaN) | ||
@test mean(abs, Complex{Float64}[]) isa Float64 | ||
@test isequal(mean(abs, skipmissing(Complex{Float64}[])), NaN) | ||
@test mean(abs, skipmissing(Complex{Float64}[])) isa Float64 | ||
@test isequal(mean(Int[]), NaN) | ||
@test mean(Int[]) isa Float64 | ||
@test isequal(mean(skipmissing(Int[])), NaN) | ||
@test mean(skipmissing(Int[])) isa Float64 | ||
@test_throws MethodError mean([]) | ||
@test_throws MethodError mean(skipmissing([])) | ||
@test_throws ArgumentError mean((1 for i in 2:1)) | ||
|
||
# Check that small types are accumulated using wider type | ||
for T in (Int8, UInt8) | ||
|
@@ -109,10 +124,10 @@ end | |
@testset "var & std" begin | ||
# edge case: empty vector | ||
# iterable; this has to throw for type stability | ||
@test_throws ArgumentError var(()) | ||
@test_throws ArgumentError var((); corrected=false) | ||
@test_throws ArgumentError var((); mean=2) | ||
@test_throws ArgumentError var((); mean=2, corrected=false) | ||
@test_throws MethodError var(()) | ||
@test_throws MethodError var((); corrected=false) | ||
@test_throws MethodError var((); mean=2) | ||
@test_throws MethodError var((); mean=2, corrected=false) | ||
# reduction | ||
@test isnan(var(Int[])) | ||
@test isnan(var(Int[]; corrected=false)) | ||
|
@@ -245,6 +260,18 @@ end | |
@test ismissing(f([missing, NaN], missing)) | ||
@test f(skipmissing([1, missing, 2]), 0) === f([1, 2], 0) | ||
end | ||
|
||
@test isequal(var(Complex{Float64}[]), NaN) | ||
@test var(Complex{Float64}[]) isa Float64 | ||
@test isequal(var(skipmissing(Complex{Float64}[])), NaN) | ||
@test var(skipmissing(Complex{Float64}[])) isa Float64 | ||
@test_throws MethodError var([]) | ||
@test_throws MethodError var(skipmissing([])) | ||
@test_throws MethodError var((1 for i in 2:1)) | ||
@test isequal(var(Int[]), NaN) | ||
@test var(Int[]) isa Float64 | ||
@test isequal(var(skipmissing(Int[])), NaN) | ||
@test var(skipmissing(Int[])) isa Float64 | ||
end | ||
|
||
function safe_cov(x, y, zm::Bool, cr::Bool) | ||
|
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.
So this throws an error when calling
zero
ifeltype
returnsAny
. Maybe bettercallthrow a more explicit error similar to the existing one?Could use
oftype
.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.
I would leave it as is, because this is how
var
forAbstractArray
is implemented and we want to be non-breaking (so it should have 100% the same behavior - the same outputs and the same errors - so using array or iterator is transparent).For sure in Julia 2.0 approach we could consider cleaning it up in both places.
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.
I just mean that instead of letting
zero
throw the error, it would be clearer to checkT === Any
and throw a more explicit error.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.
The problem is that
T
does not have to beAny
. It can be an arbitrary type that is or is not a subtype ofNumber
. And upfront we do not know if this type:zero
abs2
definedNaN
Any of those may or may not be true so the only way to be consistent is to repeat the same code that is used for a version of
var
defined for arrays (otherwise I will be able to create a situation where array and iteratorvar
differs).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.
OK. We could still print a nice error in the most common case, but then we'd lose consistency.
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.
This is the point for now. As discussed in Julia 2.0 we can probably clean it up in both methods.
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.
personally, would use
oftype
as @nalimilan suggested for clarity