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

Support rekeying a KeyedArray #63

Merged
merged 4 commits into from
Oct 25, 2022
Merged

Support rekeying a KeyedArray #63

merged 4 commits into from
Oct 25, 2022

Conversation

rofinn
Copy link
Collaborator

@rofinn rofinn commented Apr 23, 2021

I decided to go with pairs vs kwargs because:

  1. I wanted to support unnamed dims
  2. The nested pair syntax of oldname => newname => newvalues seemed more intuitive than mixing pairs and kwargs.

Closes #60

@rofinn
Copy link
Collaborator Author

rofinn commented Apr 23, 2021

Nightly error seems unrelated?

@glennmoy
Copy link
Contributor

Nightly error seems unrelated?

nightly failed on my PR due to #57

src/names.jl Show resolved Hide resolved
src/names.jl Outdated
can also pass `dimname => newkey`, or even `oldname => newname => newkey` to both `rename`
and `rekey` the specified dimension.
"""
rekey(x::AbstractArray, k2::Tuple) = KeyedArray(keyless(x), k2)
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if this method ought to be restricted to types which have axis keys?

Mostly because it seems a little odd verbally, and redundant to provide a constructor. But, on the other hand, maybe it's useful not to have to care?

Precedent:

julia> rename(rand(3), (:x,))
ERROR: MethodError: no method matching rename(::Vector{Float64}, ::Tuple{Symbol})
Closest candidates are:
  rename(::NamedDimsArray, ::Any) at /Users/me/.julia/dev/NamedDims/src/name_operations.jl:14

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I mostly did this to avoid needing the awkward rekey(A::Union{NdaKa{L,T,N}, KaNda{L,T,N}}, k2::Tuple) where {L, T, N} syntax. I assume there isn't a better solution? I suppose if this code is being used more than read then it doesn't really matter, but it's really hard for my brain to parse that Union type.

Copy link
Owner

Choose a reason for hiding this comment

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

Could be just A::Union{KeyedArray, NdaKa} I think?

Copy link
Owner

Choose a reason for hiding this comment

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

Unless you wanted to split things so as to preserve the order of wrappers, with a separate A:: NdaKa method which unwraps the names & puts them back on afterwards.

src/names.jl Outdated Show resolved Hide resolved
src/names.jl Outdated Show resolved Hide resolved
@mcabbott
Copy link
Owner

mcabbott commented May 6, 2021

Failure in 1.6 is

selectors: Error During Test at /home/runner/work/AxisKeys.jl/AxisKeys.jl/test/_basic.jl:106
128
  Test threw exception
129
  Expression: [V4[Near(x)] for x = xs] == [V5[Near(x)] for x = xs]
130
  BoundsError: attempt to access 10-element StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}} at index [0]
131
  Stacktrace:
132
    [1] throw_boundserror(A::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}, I::Tuple{Int64})
133
      @ Base ./abstractarray.jl:651
134

Seems like that should be unrelated... I should take a look at some point.

@mzgubic
Copy link
Contributor

mzgubic commented Jun 8, 2022

@mcabbott this would be nice to have, any chance you will have time to review at some point?

src/names.jl Outdated Show resolved Hide resolved
src/names.jl Outdated
Comment on lines 164 to 165
can also pass `dimname => newkey`, or even `oldname => newname => newkey` to both `rename`
and `rekey` the specified dimension.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is rekeying and renaming at some time a common operation?
Mightn't it be clearer to seperate it into two operations?

I would feel more comfortable with this PR if it only rekeyed.
And then we could do a follow up PR with the rekey + rename, that could be reveiwed can considered seperately

Copy link
Collaborator Author

@rofinn rofinn Oct 25, 2022

Choose a reason for hiding this comment

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

That's a good point. Also, should something like rename be defined for NamedDimsArrays first? I like the syntax for doing both at the same time, but I agree that could be a separate PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe there is a rename in NamedDims.jl

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are we still okay with the pairs syntax or do we want to use kwargs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, I've just dropped the rekey and rename method for now. I think that kind of functionality is only common in the DataFrames ecosystem (combine).

Copy link
Collaborator

@oxinabox oxinabox left a comment

Choose a reason for hiding this comment

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

Approved subject to splitting the rename+rekey into a seperate PR

@rofinn rofinn changed the title Support various kinds of rekeying/renaming keys Support rekeying a KeyedArray Oct 25, 2022
@rofinn
Copy link
Collaborator Author

rofinn commented Oct 25, 2022

Will likely fail CI with the new CanonicalIndexError on 1.8.

@rofinn rofinn merged commit a96b5b5 into mcabbott:master Oct 25, 2022
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.

Add rekey method
5 participants