@@ -31,60 +31,64 @@ this will be used for the row labels or will be replaced with the `labels` if pr
3131 display. If not provided, then the column `$(LABEL_COLUMN_NAME) ` in `data` will be
3232 used if it exists. Otherwise, the parameter names will be numeric indices.
3333"""
34- struct SummaryStats{D,N<: AbstractString }
34+ struct SummaryStats{D,L <: Union{Nothing,AbstractVector} , N<: AbstractString }
3535 data:: D
36+ labels:: L
3637 name:: N
37- function SummaryStats (data, name:: N ) where {N<: AbstractString }
38- _coltable = Tables. columntable (data)
39- # define default parameter names if not present, and set as first column
40- if ! haskey (_coltable, LABEL_COLUMN_NAME)
41- data_cols = _coltable
42- labels = Base. OneTo (Tables. rowcount (data))
43- else
44- data_colnames = filter (k -> k != = LABEL_COLUMN_NAME, keys (_coltable))
45- data_cols = NamedTuple {data_colnames} (_coltable)
46- labels = _coltable[LABEL_COLUMN_NAME]
47- end
48- coltable = merge ((; LABEL_COLUMN_NAME => labels), data_cols)
49- return new {typeof(coltable),N} (coltable, name)
50- end
5138end
5239
5340function SummaryStats (
5441 data; labels:: Union{AbstractVector,Nothing} = nothing , name:: AbstractString = " SummaryStats"
5542)
43+ _coltable = Tables. columntable (data)
5644 if labels != = nothing
5745 length (labels) == Tables. rowcount (data) || throw (
5846 DimensionMismatch (
5947 " length $(length (labels)) of `labels` does not match number of rows $(Tables. rowcount (data)) in `data`." ,
6048 ),
6149 )
62- data_with_varnames = merge (
63- Tables. columntable (data), (; LABEL_COLUMN_NAME => labels)
50+ end
51+ if haskey (_coltable, LABEL_COLUMN_NAME)
52+ labels === nothing || throw (
53+ ArgumentError (
54+ " Either `labels` or a column named `$(LABEL_COLUMN_NAME) ` may be provided, but not both." ,
55+ ),
6456 )
65- else
66- data_with_varnames = data
57+ data_colnames = filter (k -> k != = LABEL_COLUMN_NAME, keys (_coltable))
58+ data_cols = NamedTuple {data_colnames} (_coltable)
59+ _labels = _coltable[LABEL_COLUMN_NAME]
60+ return SummaryStats (data_cols, _labels, name)
6761 end
68- return SummaryStats (data_with_varnames , name)
62+ return SummaryStats (_coltable, labels , name)
6963end
7064
7165# forward key interfaces from its parent
7266Base. parent (stats:: SummaryStats ) = getfield (stats, :data )
73- Base. keys (stats:: SummaryStats ) = map (Symbol, Tables. columnnames (stats) )
67+ Base. keys (stats:: SummaryStats ) = Tables. columnnames (stats)
7468Base. haskey (stats:: SummaryStats , nm:: Symbol ) = nm ∈ keys (stats)
75- Base. length (stats:: SummaryStats ) = length (parent (stats))
69+ Base. length (stats:: SummaryStats ) = length (parent (stats)) + 1
7670Base. getindex (stats:: SummaryStats , i:: Union{Int,Symbol} ) = Tables. getcolumn (stats, i)
77- Base. iterate (stats:: SummaryStats , rest... ) = iterate (parent (stats), rest... )
71+ Base. iterate (stats:: SummaryStats ) = (_labels (stats), 2 )
72+ function Base. iterate (stats:: SummaryStats , i:: Int )
73+ state = iterate (parent (stats), i - 1 )
74+ state === nothing && return nothing
75+ return (state[1 ], state[2 ] + 1 )
76+ end
7877function Base. merge (stats:: SummaryStats , other_stats:: SummaryStats... )
7978 isempty (other_stats) && return stats
8079 stats_all = (stats, other_stats... )
8180 stats_last = last (stats_all)
82- return SummaryStats (merge (map (parent, stats_all)... ), stats_last. name)
81+ return SummaryStats (
82+ merge (map (parent, stats_all)... ),
83+ getfield (stats_last, :labels ),
84+ getfield (stats_last, :name ),
85+ )
8386end
8487for f in (:(== ), :isequal )
8588 @eval begin
8689 function Base. $ (f)(stats:: SummaryStats , other_stats:: SummaryStats )
87- return $ (f)(parent (stats), parent (other_stats))
90+ return $ (f)(_labels (stats), _labels (other_stats)) &&
91+ $ (f)(parent (stats), parent (other_stats))
8892 end
8993 end
9094end
@@ -99,30 +103,42 @@ function Base.show(io::IO, mime::MIME"text/html", stats::SummaryStats; kwargs...
99103end
100104
101105function _show (io:: IO , mime:: MIME , stats:: SummaryStats ; kwargs... )
102- nt = parent (stats)
103- data = nt[keys (nt)[2 : end ]]
106+ data = parent (stats)
104107 rhat_formatter = _prettytables_rhat_formatter (data)
105108 extra_formatters = rhat_formatter === nothing ? () : (rhat_formatter,)
106109 return _show_prettytable (
107110 io,
108111 mime,
109112 data;
110113 title= stats. name,
111- row_labels= Tables . getcolumn (stats, LABEL_COLUMN_NAME ),
114+ row_labels= _labels (stats),
112115 extra_formatters,
113116 kwargs... ,
114117 )
115118end
116119
117120# ### Tables interface as column table
118121
122+ _labels (s:: SummaryStats ) = getfield (s, :labels )
123+ _labels (s:: SummaryStats{<:Any,Nothing} ) = eachindex (values (parent (s))... )
124+
119125Tables. istable (:: Type{<:SummaryStats} ) = true
120126Tables. columnaccess (:: Type{<:SummaryStats} ) = true
121127Tables. columns (s:: SummaryStats ) = s
122- Tables. columnnames (s:: SummaryStats ) = Tables. columnnames (parent (s))
123- Tables. getcolumn (stats:: SummaryStats , i:: Int ) = Tables. getcolumn (parent (stats), i)
124- Tables. getcolumn (stats:: SummaryStats , nm:: Symbol ) = Tables. getcolumn (parent (stats), nm)
125- Tables. schema (s:: SummaryStats ) = Tables. schema (parent (s))
128+ Tables. columnnames (s:: SummaryStats ) = (LABEL_COLUMN_NAME, Tables. columnnames (parent (s))... )
129+ function Tables. getcolumn (stats:: SummaryStats , i:: Int )
130+ i == 1 && return _labels (stats)
131+ return Tables. getcolumn (parent (stats), i - 1 )
132+ end
133+ function Tables. getcolumn (stats:: SummaryStats , nm:: Symbol )
134+ nm === LABEL_COLUMN_NAME && return _labels (stats)
135+ return Tables. getcolumn (parent (stats), nm)
136+ end
137+ function Tables. schema (s:: SummaryStats )
138+ labels = _labels (s)
139+ sch = Tables. schema (parent (s))
140+ return Tables. Schema ((LABEL_COLUMN_NAME, sch. names... ), (eltype (labels), sch. types... ))
141+ end
126142
127143IteratorInterfaceExtensions. isiterable (:: SummaryStats ) = true
128144function IteratorInterfaceExtensions. getiterator (s:: SummaryStats )
0 commit comments