-
Notifications
You must be signed in to change notification settings - Fork 1
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 LinearCombination transform #8
Conversation
I think e.g. df = DataFrame(
:time = Day(1):Day(1):Day(5),
:a = [1, 2, 3, 4, 5],
:b = [1, 1, 2, 3, 5],
)
lc = LinearCombination([1, 5])
Transforms.apply(df, lc; cols=[:a, :b]) similarly for |
If it's selecting a subset of columns, then |
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.
We should handle dims
/cols
as Glenn explained, but otherwise this looks good to me.
I agree using |
Codecov Report
@@ Coverage Diff @@
## main #8 +/- ##
========================================
Coverage ? 100.00%
========================================
Files ? 5
Lines ? 38
Branches ? 0
========================================
Hits ? 38
Misses ? 0
Partials ? 0 Continue to review full report at Codecov.
|
I have implemented |
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.
Looks good to me. I don't think there is a use for dims
as long as this transform is only applied on columns.
Sorry, that's dead right, in my first response I completely confused But I think there is still a place for e.g. consider just taking a julia> A = [1 2 3; 4 5 6; 7 8 9];
julia> sum(A, dims=2)
3×1 Array{Int64,2}:
6
15
24
julia> sum(A, dims=1)
1×3 Array{Int64,2}:
12 15 18 I think all that needs to change is to change This means we would also have to change |
d = Colon() | ||
@test_throws ArgumentError Transforms.apply(A, lc; dims=d) | ||
end | ||
|
||
@testset "dims = 1" begin |
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.
does this also work if passing in the axis key? e.g. foo
, bar
?
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 checked it with Power
on AxisKeys so it should work
https://github.com/invenia/Transforms.jl/blob/main/test/power.jl#L77
but I forgot to do the same with AxisArrays
https://github.com/invenia/Transforms.jl/blob/main/test/power.jl#L65
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 doubt it
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.
How do you do the same with AxisArrays though? If I use (Colon(), :foo, :bar)
like for KeyedArray in test/power.jl
I get
dims = foo: Error During Test at /Users/bencottier/JuliaEnvs/Transform.jl/test/power.jl:65
Got exception outside of a @test
MethodError: no method matching length(::Symbol)
Closest candidates are:
length(!Matched::Base.Iterators.Flatten{Tuple{}}) at iterators.jl:1061
length(!Matched::Base.MethodList) at reflection.jl:872
length(!Matched::DataStructures.EnumerateAll) at /Users/bencottier/.julia/packages/DataStructures/ixwFs/src/multi_dict.jl:96
...
Stacktrace:
[1] #eachslice#196 at ./abstractarraymath.jl:496 [inlined]
[2] apply!(::AxisArray{Int64,2,Array{Int64,2},Tuple{Axis{:foo,Array{String,1}},Axis{:bar,Array{String,1}}}}, ::Power; dims::Symbol, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/bencottier/JuliaEnvs/Transform.jl/src/transformers.jl:57
[3] apply(::AxisArray{Int64,2,Array{Int64,2},Tuple{Axis{:foo,Array{String,1}},Axis{:bar,Array{String,1}}}}, ::Power; kwargs::Base.Iterators.Pairs{Symbol,Symbol,Tuple{Symbol},NamedTuple{(:dims,),Tuple{Symbol}}}) at /Users/bencottier/JuliaEnvs/Transform.jl/src/transformers.jl:64
[4] macro expansion at /Users/bencottier/JuliaEnvs/Transform.jl/test/power.jl:66 [inlined]
[5] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1190 [inlined]
[6] macro expansion at /Users/bencottier/JuliaEnvs/Transform.jl/test/power.jl:65 [inlined]
[7] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1115 [inlined]
[8] macro expansion at /Users/bencottier/JuliaEnvs/Transform.jl/test/power.jl:62 [inlined]
[9] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1115 [inlined]
[10] top-level scope at /Users/bencottier/JuliaEnvs/Transform.jl/test/power.jl:3
[11] include(::String) at ./client.jl:457
[12] top-level scope at /Users/bencottier/JuliaEnvs/Transform.jl/test/runtests.jl:12
[13] top-level scope at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1115
[14] top-level scope at /Users/bencottier/JuliaEnvs/Transform.jl/test/runtests.jl:11
[15] include(::String) at ./client.jl:457
[16] top-level scope at none:6
[17] eval(::Module, ::Any) at ./boot.jl:331
[18] exec_options(::Base.JLOptions) at ./client.jl:272
[19] _start() at ./client.jl:506
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.
it looks like eachslice
doesn't work on AxisArrays maybe. Nevermind it was only a passing thought.
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.
yeah it doesn't work without some extra voodoo
julia> eachslice(A, dims=axisdim(A, Axis{:foo}))
Base.Generator{Base.OneTo{Int64},Base.var"#199#202"{AxisArray{Int64,2,Array{Int64,2},Tuple{Axis{:foo,Array{String,1}},Axis{:bar,Array{String,1}}}},Tuple{},Tuple{Colon}}}(Base.var"#199#202"{AxisArray{Int64,2,Array{Int64,2},Tuple{Axis{:foo,Array{String,1}},Axis{:bar,Array{String,1}}}},Tuple{},Tuple{Colon}}([1 2 3; 4 5 6], (), (Colon(),)), Base.OneTo(2))
And it's been on their todo list for 5 years 🙄
JuliaArrays/AxisArrays.jl#7
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 can make this work if we want to, by checking if it's an AxisArray. I might prefer to do this in a follow up MR just to keep diffs smaller.
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.
Closes #5
Note that I couldn't get a linear combination using Statistics.mean to work. A lot of operations were returning NaNs. Also, that mean function expects a whole matrix of weights and not just the weights for each column. For these reasons, I opted not to define the CustomTransform instead of doing this explicitly.
From the issue description, I am unclear what the role of
dims
kwarg is or how it should work.