JuliaGaussianProcesses / JuliaGaussianProcesses/ParameterHandling.jl

default behavior for flatten/unflatten with any type

Open
#75 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
74
Forks
10
PR merge metrics
No merged PRs in 30d

Description

Hi,
I'm using ParameterHandling only for the flatten/unflatten tools. In my case I need to carry extra information within my tuple stored in different type that do not need to be flatten but I need to recover it in my restructured NamedTuple
Here is an example

struct MyType
       a::Float64
       b::Int
end

mynametuple = (;a=rand(2),b=randn(6),m = MyType(1,2))

If I try to flatten this tuple I get an error:

flatvector, restructure = ParameterHandling.flatten( mynametuple)
ERROR: MethodError: no method matching flatten(::Type{Float64}, ::MyType)

Closest candidates are:
  flatten(::Type{T}, ::Tuple{}) where T<:Real
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:92
  flatten(::Type{T}, ::Nothing) where T<:Real
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:22
  flatten(::Type{T}, ::Integer) where T<:Real
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:28
  ...

Stacktrace:
 [1] flatten(::Type{Float64}, x::Tuple{MyType})
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:82
 [2] flatten(::Type{Float64}, x::Tuple{Vector{Float64}, MyType})
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:83
 [3] flatten(::Type{Float64}, x::Tuple{Vector{Float64}, Vector{Float64}, MyType})
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:83
 [4] flatten(::Type{Float64}, x::@NamedTuple{a::Vector{Float64}, b::Vector{Float64}, m::MyType})
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:99
 [5] flatten(x::@NamedTuple{a::Vector{Float64}, b::Vector{Float64}, m::MyType})
   @ ParameterHandling ~/.julia/packages/ParameterHandling/WkHof/src/flatten.jl:20
 [6] top-level scope
   @ REPL[14]:1

However, with this simple overloading of ParameterHandling.flatten as a default behaviour

function ParameterHandling.flatten(::Type{T}, x) where {T<:Real}
           v = T[]
           unflatten_to_Any(::Vector{T}) = x
           return v, unflatten_to_Any
 end

I get:

julia> flatvector, restructure = ParameterHandling.flatten( mynametuple)
([0.8409925019315632, 0.2819056254106582, 1.598581283953101, 0.3921407396316173, -1.6414335944803775, 1.0503960727007877, -1.183085803897985, -2.3827810941365573], ParameterHandling.var"#unflatten_to_NamedTuple#15"{Float64, @NamedTuple{a::Vector{Float64}, b::Vector{Float64}, m::MyType}, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_empty_Tuple#14"{Float64, Tuple{}}, var"#unflatten_to_Any#6"{Float64, MyType}}, ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}}, ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}}}((a = [0.8409925019315632, 0.2819056254106582], b = [1.598581283953101, 0.3921407396316173, -1.6414335944803775, 1.0503960727007877, -1.183085803897985, -2.3827810941365573], m = MyType(1.0, 2)), ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_empty_Tuple#14"{Float64, Tuple{}}, var"#unflatten_to_Any#6"{Float64, MyType}}, ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}}, ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}}(6, 2, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_empty_Tuple#14"{Float64, Tuple{}}, var"#unflatten_to_Any#6"{Float64, MyType}}, ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}}(0, 6, ParameterHandling.var"#unflatten_to_Tuple#13"{Float64, Int64, Int64, ParameterHandling.var"#unflatten_to_empty_Tuple#14"{Float64, Tuple{}}, var"#unflatten_to_Any#6"{Float64, MyType}}(0, 0, ParameterHandling.var"#unflatten_to_empty_Tuple#14"{Float64, Tuple{}}(()), var"#unflatten_to_Any#6"{Float64, MyType}(MyType(1.0, 2))), ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}()), ParameterHandling.var"#unflatten_to_Vector#4"{Float64, Float64}())))

julia> restructure(flatvector)
(a = [0.8409925019315632, 0.2819056254106582], b = [1.598581283953101, 0.3921407396316173, -1.6414335944803775, 1.0503960727007877, -1.183085803897985, -2.3827810941365573], m = MyType(1.0, 2))

Is there any reason for not implementing such default behavior? Am I missing something?
Should I make a PR?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/flatten.jl, especially the methods around the reported stack-trace locations, and reproduce the NamedTuple example with a custom MyType. Check how flatten and the returned restructure function handle unsupported values. Done means a default behavior for such values is defined and the example can flatten and reconstruct the original MyType without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.