Skip to content
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

Merged
merged 1 commit into from
Jun 8, 2020

Conversation

kimikage
Copy link
Collaborator

This fixes #416, but leaves the weight type conversion (#416 (comment)).

Gray{Bool}s do not throw a DomainError due to their type now, although the InexactError is likely to occur. Because of the weight type conversion, the following throws an InexactError.

julia> weighted_color_mean(0.5, Gray{Bool}(1), Gray{Bool}(1))
ERROR: InexactError: Bool(0.5)

The range checking of w1 is done in the type of w1 in order to more intuitive error messages.

Before (master):

julia> weighted_color_mean(-0.001, Gray24(0), Gray24(1))
ERROR: ArgumentError: FixedPointNumbers.Normed{UInt8,8} is an 8-bit type representing 256 values from 0.0 to 1.0; cannot represent -0.001

julia> weighted_color_mean(1.00000005, Gray(0.0f0), Gray(1.0f0)) # Float32(1.00000005) == 1.0f0
Gray{Float32}(0.0f0)

After (this PR):

julia> weighted_color_mean(-0.001, Gray24(0), Gray24(1))
ERROR: DomainError with -0.001:
`w1` must be in [0, 1]

julia> weighted_color_mean(1.00000005, Gray(0.0f0), Gray(1.0f0))
ERROR: DomainError with 1.00000005:
`w1` must be in [0, 1]

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.

@codecov
Copy link

codecov bot commented Apr 28, 2020

Codecov Report

Merging #421 into master will increase coverage by 0.26%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/utilities.jl 97.10% <100.00%> (+3.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6c71c14...5d07301. Read the comment docs.

@kimikage kimikage requested a review from timholy April 30, 2020 16:04
@kimikage kimikage mentioned this pull request May 9, 2020
Copy link
Member

@johnnychen94 johnnychen94 left a 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
Copy link
Member

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.

Suggested change
function _weighted_color_mean(w1::Integer, c1::C, c2::C) where C <: Colorant
function _weighted_color_mean(w1::Integer, c1::Colorant, c2::Colorant)

Copy link
Collaborator Author

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.

Copy link
Member

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?

Copy link
Collaborator Author

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
Copy link
Member

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))

Copy link
Collaborator Author

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.

Copy link
Member

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) 😃

Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

Copy link
Member

@timholy timholy left a 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
Copy link
Member

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.

@kimikage kimikage merged commit 53c2acb into JuliaGraphics:master Jun 8, 2020
@kimikage kimikage deleted the weighted_mean branch June 8, 2020 10:03
@kimikage
Copy link
Collaborator Author

kimikage commented Jun 8, 2020

Thank you for your review despite your busy schedule, @timholy.
Also, thank you for your comment, @johnnychen94.

I opened a new issue for removing the conversion. I will submit a PR later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DomainError needs a val argument.
3 participants