I’m working on something I’m currently calling a TupleVector for representing an MCMC chain where each sample is a NamedTuple. Internally the structure is something like
julia> unwrap(tv)
(a = [1, 3, 4, 5], b = [[0.36932704908184055, 0.38928341185135773, 0.531478384195232], [1.1206308130858036, 0.186852988457207, -0.7630326680035839], [-0.36521261611586725, -1.2910950683384825, -1.3507690793756433], [1.8710082871859233, 1.8902622784110135, 0.542632944187108]])
But instead of 4 samples, it’s typically hundreds or thousands.
I have this set up so it works like an Array and also like a NamedTuple, so
julia> tv[1]
(a = 1, b = [0.36932704908184055, 0.38928341185135773, 0.531478384195232])
julia> tv.a
4-element ElasticVector{Int64, 0, Vector{Int64}}:
1
3
4
5
julia> tv.b
4-element ArrayOfSimilarArrays{Float64, 1, 1, 2, ElasticMatrix{Float64, 1, Vector{Float64}}}:
[0.36932704908184055, 0.38928341185135773, 0.531478384195232]
[1.1206308130858036, 0.186852988457207, -0.7630326680035839]
[-0.36521261611586725, -1.2910950683384825, -1.3507690793756433]
[1.8710082871859233, 1.8902622784110135, 0.542632944187108]
So far, so good. Rather than show such a long list, I want to be able to summarize it, and also not show so many useless decimal places. So I have a summarize function to turn each column into one of these:
struct RealSummary <: Summary
μ :: Float64
σ :: Float64
end
function Base.show(io::IO, s::RealSummary)
io = IOContext(io, :compact => true)
print(io, s.μ, " ± ", s.σ)
end
So now the whole thing looks like this:
julia> tv
4-element TupleVector with schema (a = Int64, b = Vector{Float64})
(a = 3.25 ± 1.70783, b = NestedTuples.RealSummary[0.43003 ± 0.088422 0.181484 ± 0.941843 -1.00236 ± 0.552591 1.43463 ± 0.772556])
It’s getting there, but still not great:
- Things like
3.25 ± 1.70783are silly, still way too many decimal places - The
NestedTuples.RealSummaryneeds to go - The array is hard to read, especially with no commas
Looking at the show methods for arrays, it seems like really fine-tuning this would require copying and pasting a lot of code. I was hoping there might be something more easily composable where I could try things out, swap out components, etc. For example, I’d really like to avoid working through all the logic about when to have the REPL show ... and leave out some array entries, that kind of thing.
Any suggestions for a better way to go about this?