-
Notifications
You must be signed in to change notification settings - Fork 48
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
Fix error types and conditions in weighted_color_mean
#421
Conversation
Codecov Report
@@ Coverage Diff @@
## master #421 +/- ##
==========================================
+ Coverage 81.70% 81.97% +0.26%
==========================================
Files 10 10
Lines 880 882 +2
==========================================
+ Hits 719 723 +4
+ Misses 161 159 -2
Continue to review full report at Codecov.
|
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 improvement of the test set looks great!
weight2 = oneunit(weight1) - weight1 | ||
mapc((x, y)->convert(T, muladd(weight1, x, weight2 * y)), c1, c2) | ||
end | ||
function _weighted_color_mean(w1::Integer, c1::C, c2::C) where C <: Colorant |
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.
Any reason not allowing different types of c1
and c2
, since it only requires w1
to be 0
or 1
.
function _weighted_color_mean(w1::Integer, c1::C, c2::C) where C <: Colorant | |
function _weighted_color_mean(w1::Integer, c1::Colorant, c2::Colorant) |
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.
Any reason allowing different types of c1
and c2
? They cannot be optimized, i.e. can throw errors.
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 don't get it, why would it throw errors? Is that because c1
and c2
are not promoted to a common type?
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.
Is that because c1 and c2 are not promoted to a common type?
Yes. Of course, no error should be thrown for colors in the same color space, but this optimization is not available.
julia> weighted_color_mean(1, Gray{Float32}(0), Gray{Float64}(1))
Gray{Float64}(0.0)
mapc((x,y)->weight1*x+weight2*y, c1, c2) | ||
@fastmath min(w1, oneunit(w1) - w1) >= zero(w1) || throw(DomainError(w1, "`w1` must be in [0, 1]")) | ||
T = promote_type(eltype(c1), eltype(c2)) | ||
weight1 = convert(T, w1) # TODO: Consider the need for this |
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 guess we should make the following work without throwing error?
weighted_color_mean(0.5, Gray{Bool}(0), Gray{Bool}(0))
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 noticed this issue when tackling the problem with Gray{Bool}
, but I think RGB{N0f8}
is more important for people involved in image processing.
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 mean this could be a good reason to remove convert(T, w1)
😃
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 agree with you. However, I don't understand the purpose of the conversion, so I can't come to a conclusion. Whatever the reason, if we remove it, it should be in v0.13.
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'd agree it can be removed. That seems like a pretty minor breakage (in accordance with the viewpoint that "types are an implementation detail, values are what matter"), so I'd be OK either with v0.12 or v0.13.
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 removal requires some modifications of tests, so I think it's better to make a separate PR. If we make the two steps anyway, it's a bit safer to do this in v0.12 and the removal in v0.13.
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.
LGTM, sorry I forgot about this!
mapc((x,y)->weight1*x+weight2*y, c1, c2) | ||
@fastmath min(w1, oneunit(w1) - w1) >= zero(w1) || throw(DomainError(w1, "`w1` must be in [0, 1]")) | ||
T = promote_type(eltype(c1), eltype(c2)) | ||
weight1 = convert(T, w1) # TODO: Consider the need for this |
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'd agree it can be removed. That seems like a pretty minor breakage (in accordance with the viewpoint that "types are an implementation detail, values are what matter"), so I'd be OK either with v0.12 or v0.13.
Thank you for your review despite your busy schedule, @timholy. I opened a new issue for removing the conversion. I will submit a PR later. |
This fixes #416, but leaves the weight type conversion (#416 (comment)).
Gray{Bool}
s do not throw aDomainError
due to their type now, although theInexactError
is likely to occur. Because of the weight type conversion, the following throws anInexactError
.The range checking of
w1
is done in the type ofw1
in order to more intuitive error messages.Before (master):
After (this PR):
I'm not sure wheter this is a breaking change. If this is a breaking change, I think it is better to remove the weight type conversion in this PR.