JuliaDynamics/ComplexityMeasures.jl

Add generic histogram method for `Vector{<:Any}`, so we can use `counts(::UniqueElements, x)` on any data

Offen

#349 geöffnet am 21.12.2023

 (10 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Julia (19 Forks)auto 404
enhancementgood first issue

Repository-Metriken

Stars
 (79 Sterne)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

Currently, we can do

julia> x = rand([1, 4, 6, 7, 8], 1000)

julia> counts(x)
 Counts{Int64,1} over 5 outcomes
 1  199
 4  215
 6  197
 7  180
 8  209

But this only works if x is sortable, because fasthist! uses sorting internally. We should have a generic (probably much slower) fallback that enables things like this to work too:

julia> y = ['a', (1, 2, 3,), 25];

julia> counts(y)
ERROR: MethodError: no method matching isless(::Tuple{Int64, Int64, Int64}, ::Char)

Closest candidates are:
  isless(::Char, ::Char)
   @ Base char.jl:214
  isless(::AbstractChar, ::AbstractChar)
   @ Base char.jl:221
  isless(::Tuple, ::Tuple{})
   @ Base tuple.jl:532
  ...

Stacktrace:
  [1] lt(o::Base.Order.ForwardOrdering, a::Tuple{Int64, Int64, Int64}, b::Char)
    @ Base.Order ./ordering.jl:117
  [2] _sort!(v::Vector{Any}, #unused#::Base.Sort.InsertionSortAlg, o::Base.Order.ForwardOrdering, kw::NamedTuple{(:scratch, :lo, :hi), Tuple{Nothing, Int64, Int64}})
    @ Base.Sort ./sort.jl:748
  [3] _sort!
    @ ./sort.jl:713 [inlined]
  [4] _sort!
    @ ./sort.jl:660 [inlined]
  [5] _sort!
    @ ./sort.jl:596 [inlined]
  [6] #sort!#28
    @ ./sort.jl:1374 [inlined]
  [7] sort!
    @ ./sort.jl:1367 [inlined]
  [8] fasthist!(x::Vector{Any})
    @ ComplexityMeasures ~/Documents/Repos/ComplexityMeasures.jl/src/encoding_implementations/fasthist.jl:21
  [9] counts_and_outcomes
    @ ~/Documents/Repos/ComplexityMeasures.jl/src/core/counts.jl:99 [inlined]
 [10] counts(x::Vector{Any})
    @ ComplexityMeasures ~/Documents/Repos/ComplexityMeasures.jl/src/core/counts.jl:121
 [11] top-level scope
    @ REPL[42]:1

Contributor Guide