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

Allow generators and iterators #194

Merged
merged 26 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
10c5c2b
Allow generators and iterators in evaluate
dkarrasch Dec 5, 2020
d3dd6e4
fix test
dkarrasch Dec 5, 2020
27699ff
fix one type-thing
dkarrasch Dec 5, 2020
e127fb4
include result_type proposal, add hamming tests
dkarrasch Dec 5, 2020
948291d
include renyi_divergence, haversine, bregman
dkarrasch Dec 6, 2020
243b7b0
include bhattacharyya / hellinger
dkarrasch Dec 6, 2020
8101bb3
Update test/test_dists.jl
dkarrasch Dec 8, 2020
8f44a30
include some review comments
dkarrasch Dec 8, 2020
c055d4d
relax parameter types
dkarrasch Dec 8, 2020
0227942
clean up UnionMetric evaluate
dkarrasch Dec 8, 2020
9b34ed9
include iterator-based pair- and colwise
dkarrasch Dec 12, 2020
85cdb1b
simplify/optimize pairwise
dkarrasch Dec 12, 2020
46a91c2
include generic result_type tests
dkarrasch Dec 13, 2020
8fb5108
Revert "clean up UnionMetric evaluate"
dkarrasch Dec 13, 2020
5d04ff0
minor UnionMetric edits
dkarrasch Dec 14, 2020
27994eb
include comments from code review
dkarrasch Dec 14, 2020
6e4c09b
add colwise & pairwise docstrings
dkarrasch Dec 14, 2020
5b096d4
Apply suggestions from code review
dkarrasch Dec 15, 2020
518fd3d
simplify _eltype, add a note to colwise docstring
dkarrasch Dec 15, 2020
18f17af
fix typo
dkarrasch Dec 15, 2020
8640d36
transpose -> permutedims
dkarrasch Dec 15, 2020
5be0415
handle CartesianIndex
dkarrasch Dec 15, 2020
4333bda
increase code coverage
dkarrasch Dec 15, 2020
8219ad0
fix docstrings
dkarrasch Dec 16, 2020
5402ed8
Revert "handle CartesianIndex"
dkarrasch Dec 16, 2020
1350d47
rm redundant tests
dkarrasch Dec 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ end

# breaks the implementation into eval_start, eval_op, eval_reduce and eval_end

@inline _zip(a, b) = zip(a, b)
@inline _zip(a::CartesianIndex, b::CartesianIndex) = zip(a.I, b.I)
@inline _zip(a, b, p) = zip(a, b, p)
@inline _zip(a::CartesianIndex, b::CartesianIndex, p) = zip(a.I, b.I, p)

Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, ::Nothing)
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
@boundscheck if length(a) != length(b)
throw(DimensionMismatch("first collection has length $(length(a)) which does not match the length of the second, $(length(b))."))
Expand All @@ -234,7 +239,7 @@ Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, ::Nothing)
return zero(result_type(d, a, b))
end
s = eval_start(d, a, b)
@inbounds for (ai, bi) in zip(a, b)
@inbounds for (ai, bi) in _zip(a, b)
s = eval_reduce(d, s, eval_op(d, ai, bi))
end
return eval_end(d, s)
Expand Down Expand Up @@ -274,7 +279,7 @@ Base.@propagate_inbounds function _evaluate(d::UnionMetrics, a, b, p)
return zero(result_type(d, a, b))
end
s = eval_start(d, a, b)
@inbounds for (ai, bi, pi) in zip(a, b, p)
@inbounds for (ai, bi, pi) in _zip(a, b, p)
s = eval_reduce(d, s, eval_op(d, ai, bi, pi))
end
return eval_end(d, s)
Expand Down
8 changes: 8 additions & 0 deletions test/test_dists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@ end
@test bregman(F, ∇, p, q) ≈ ISdist(p, q)
end

@testset "CartesianIndex" begin
A = reshape(collect(1:9), 3, 3)
inds1 = findall(iseven, A)
inds2 = findall(isodd, A)
@test sum(pairwise(SqEuclidean(), inds1, inds2)) == 52
@test euclidean(inds1[1], inds1[1]) === 0.0
end
Copy link
Member

Choose a reason for hiding this comment

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

Does this cover a real use case? I find it surprising that somebody would want to do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

There was a feature request in #177. But we could remove it for now and clarify there what the actual intention and the use are.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I'd leave it out until we have a clearer view of why that would be useful. The fact that CartesianIndex isn't iterable seems to indicate that it's not intended to be used that way, and it would be absurd to have packages work around that everywhere...


#=
@testset "zero allocation colwise!" begin
d = Euclidean()
Expand Down