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

eachslice + collect #73

Closed
wants to merge 3 commits into from
Closed

eachslice + collect #73

wants to merge 3 commits into from

Conversation

mcabbott
Copy link
Collaborator

I believe this closes #59 and #34.

I ‘m not sure why write a custom generator for eachslice. This PR changes it to just looks up dims, then run the same code as base:

julia> ab = NamedDimsArray((1:3) .+ zeros(Int,3)', (:a, :b));

julia> @btime eachslice($ab, dims=:b); # master: 319.215 ns (5 allocations: 112 bytes)
  220.690 ns (3 allocations: 64 bytes) 

julia> @btime eachslice($(parent(ab)), dims=2);
  216.897 ns (3 allocations: 64 bytes)

julia> @btime [sum(x)^2 for x in eachslice($ab, dims=:b)]; # master: 14.636 μs (40 allocations: 1.73 KiB)
  383.851 ns (8 allocations: 336 bytes)

Still much slower than eachcol, for which there is no special code at all:

julia> NamedDims.names(first(eachcol(ab)))
(:a,)

julia> @btime eachcol($ab);
  12.992 ns (2 allocations: 48 bytes)

julia> @btime [sum(x)^2 for x in eachcol($ab)];
  76.208 ns (7 allocations: 320 bytes)

It also adds the simplest possible overload of collect so that simple comprehensions will keep names. Doesn’t work on generators of generators, nor products:

julia> [x^2 for x in ab]
3×3 NamedDimsArray{(:a, :b),Int64,2,Array{Int64,2}}:
 1  1  1
 4  4  4
 9  9  9

julia> NamedDims.names([sum(x)^2 for x in eachcol(ab)]) # doesn't work yet
(:_,)

@codecov
Copy link

codecov bot commented Oct 10, 2019

Codecov Report

Merging #73 into master will decrease coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #73      +/-   ##
==========================================
- Coverage   86.63%   86.53%   -0.11%     
==========================================
  Files           8        8              
  Lines         247      260      +13     
==========================================
+ Hits          214      225      +11     
- Misses         33       35       +2
Impacted Files Coverage Δ
src/functions.jl 97.67% <100%> (+0.37%) ⬆️
src/name_core.jl 90.24% <0%> (-1.76%) ⬇️

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 26e974b...97d070d. Read the comment docs.

Copy link
Contributor

@nickrobinson251 nickrobinson251 left a comment

Choose a reason for hiding this comment

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

thanks!

src/functions.jl Outdated Show resolved Hide resolved
else
eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2))
eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1))
# every line identical to Base, but no _eachslice(A, dims) to disatch on.
Copy link
Contributor

Choose a reason for hiding this comment

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

@oxinabox previously objected to copying Base code here, so I'll defer to him on 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 was wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Other approaches are possible, I see that Compat.jl does have eachslice etc. I don't know what you think of depending on that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that Compat.jl does have eachslice etc. I don't know what you think of depending on that.

Now that Compat.jl is for Julia v1.0+ I'd be happy to use it, rather than copy code here :)

@test names(first(eachrow(nda))) == (:y,)

@test_broken names(collect(eachcol(nda))) == (:y,)
@test_broken names(collect(eachrow(nda))) == (:y,)
Copy link
Contributor

Choose a reason for hiding this comment

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

why are these test_broken?
If there's a good reason, let's open an issue to fix them, and comment a link to that issue here

Copy link
Member

@oxinabox oxinabox Oct 11, 2019

Choose a reason for hiding this comment

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

This doesn't seem like it is broken at all.
that is a normal vector of named vectors.
I think I would find it surprising if it wasn't.

Copy link
Collaborator Author

@mcabbott mcabbott Oct 11, 2019

Choose a reason for hiding this comment

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

I think the name of the generator dimension would ideally be passed along, as is now done for [f(x) for x in nda]. But I couldn't see a way to make this work without JuliaLang/julia#32310 .

test/functions.jl Outdated Show resolved Hide resolved
@oxinabox
Copy link
Member

It also adds the simplest possible overload of collect so that simple comprehensions will keep names. Doesn’t work on generators of generators, nor products:

Can this be in a seperare PR, it is a more contentious feature that IDK how I feel about right now

mcabbott and others added 2 commits October 11, 2019 11:37
@mcabbott
Copy link
Collaborator Author

mcabbott commented Oct 11, 2019

Perhaps we should have an issue for how many more general generators can or should be handled? Making [sqrt(x) for x in ab] behave like map(sqrt, ab) seemed straightforward.
Edit: perhaps more general maps, like map(+, ab, rand(3,3)), which also currently forgets names, fall in the same basket.

@mcabbott
Copy link
Collaborator Author

Now that Compat.jl is for Julia v1.0+ I'd be happy to use it, rather than copy code here :)

OK then perhaps I close this, and leave you to make a Compat PR.

it is a more contentious feature ...

If you believe map & collected generators ought not to simply propagate names along, then perhaps open a discussion issue?

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.

Support eachslice for Julia 1.0
3 participants