Code Monkey home page Code Monkey logo

Comments (5)

mcabbott avatar mcabbott commented on June 12, 2024

That would be nice to have, but seems tricky to ensure. It does this:

a = wrapdims(rand(2), UniqueVector, x=1:2)
perm = sortperm(a.x)
wrapdims(parent(a)[perm], x=a.x[perm])

and a.x[perm] doesn't know that this is a permutation -- getindex must equally expect e.g. a.x[[1,1,2]].

It might be possible to call sort(a.x) again, and trust that this will produce the same order? UniqueVectors can (and probably should) overload this to preserve the type.

from axiskeys.jl.

takbal avatar takbal commented on June 12, 2024

UniqueVectors also produce an Array after sort(), but the in-place sort!() works. So this should do it for this container:

perm = sortperm(a.x)
newkeys = copy(a.x)
sort!(newkeys)
wrapdims(parent(a)[perm], x=newkeys)

Not sure how general this is for other containers, or if sort! and sortperm is guaranteed to produce the same order.

from axiskeys.jl.

mcabbott avatar mcabbott commented on June 12, 2024

I get an error when I try sort!:

u = UniqueVector([41, 46, 19, 47, 21, 27, 16, 25, 45])
findfirst(isequal(u[5]), u) # fast method
su = sort!(u)  # ArgumentError: cannot set an element that exists elsewhere in UniqueVector
@which sort!(u) # generic one, from Base.Sort

But also, I don't think this can work in general, e.g.

b = wrapdims(rand(3), y = 'c':-1:'a')
sortkeys(b, dims=:y)

from axiskeys.jl.

takbal avatar takbal commented on June 12, 2024

Indeed. UniqueVector seems to have even more fundamental problems, as views or indexing also change the key container type.

A possible solution is to define a function that enforces the key containers:

convert_kc(K::KeyedArray, container_type::Type=UniqueVector)::KeyedArray =
    KeyedArray( NamedDimsArray( parent(parent(K)), dimnames(K)), tuple( [ container_type(x) for x in axiskeys(K) ]...) )

and then call it after each operation that may need accelerated access in its output:

K = convert_kc(sortkeys(K))
K = convert_kc([K1 ; K2])
...

This is not pretty. Maybe KeyedArray could have a static Bool type parameter that, if true, calls this key container conversion at the end of each AxisKeys function that generates a KeyedArray result?

from axiskeys.jl.

mcabbott avatar mcabbott commented on June 12, 2024

Simple views behave well, e.g. @which findfirst(isequal(47), view(u, 2:7)). But for u[2:7] the cost of re-generating the lookup dictionary was felt to be too much, in garrison/UniqueVectors.jl#9. Maybe you could do better, don't re-hash, just update the indices in the dictionary? Although u[inds] isn't always unique...

Base has a function permute! but no permute, which would be the perfect thing to overload here.

A difficulty with a boolean flag is that the function needed to reconstruct a given type isn't obvious from the type. It could cary around this function, though. Still adds a fair bit of complication.

from axiskeys.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.