1- const LABEL_COLUMN_NAME = :label
2-
31const _DEFAULT_SUMMARY_STATS_KIND_DOCSTRING = """
42- `kind::Symbol`: The named collection of summary statistics to be computed:
53 + `:all`: Everything in `:stats` and `:diagnostics`
@@ -17,137 +15,6 @@ const _DEFAULT_SUMMARY_STATS_CI_DOCSTRING = """
1715 interval `<ci>`.
1816"""
1917
20- """
21- struct SummaryStats
22-
23- A container for a column table of values computed by [`summarize`](@ref).
24-
25- This object implements the Tables and TableTraits column table interfaces. It has a custom
26- `show` method.
27-
28- !!! note
29- `SummaryStats` behaves like an `OrderedDict` of columns, where the columns can be
30- accessed using either `Symbol`s or a 1-based integer index. However, this interface
31- is not part of the public API and may change in the future. We recommend using it
32- only interactively.
33-
34- # Constructor
35-
36- SummaryStats(data; name="SummaryStats"[, labels])
37-
38- Construct a `SummaryStats` from tabular `data`.
39-
40- `data` must implement the Tables interface. If it contains a column `$(LABEL_COLUMN_NAME) `,
41- this will be used for the row labels or will be replaced with the `labels` if provided.
42-
43- # Keywords
44-
45- - `name::AbstractString`: The name of the collection of summary statistics, used as the
46- table title in display.
47- - `labels::AbstractVector`: The names of the parameters in `data`, used as row labels in
48- display. If not provided, then the column `$(LABEL_COLUMN_NAME) ` in `data` will be
49- used if it exists. Otherwise, the parameter names will be numeric indices.
50- """
51- struct SummaryStats{D,N<: AbstractString }
52- data:: D
53- name:: N
54- function SummaryStats (data, name:: N ) where {N<: AbstractString }
55- _coltable = Tables. columntable (data)
56- # define default parameter names if not present, and set as first column
57- if ! haskey (_coltable, LABEL_COLUMN_NAME)
58- data_cols = _coltable
59- labels = Base. OneTo (Tables. rowcount (data))
60- else
61- data_colnames = filter (k -> k != = LABEL_COLUMN_NAME, keys (_coltable))
62- data_cols = NamedTuple {data_colnames} (_coltable)
63- labels = _coltable[LABEL_COLUMN_NAME]
64- end
65- coltable = merge ((; LABEL_COLUMN_NAME => labels), data_cols)
66- return new {typeof(coltable),N} (coltable, name)
67- end
68- end
69-
70- function SummaryStats (
71- data; labels:: Union{AbstractVector,Nothing} = nothing , name:: AbstractString = " SummaryStats"
72- )
73- if labels != = nothing
74- length (labels) == Tables. rowcount (data) || throw (
75- DimensionMismatch (
76- " length $(length (labels)) of `labels` does not match number of rows $(Tables. rowcount (data)) in `data`." ,
77- ),
78- )
79- data_with_varnames = merge (
80- Tables. columntable (data), (; LABEL_COLUMN_NAME => labels)
81- )
82- else
83- data_with_varnames = data
84- end
85- return SummaryStats (data_with_varnames, name)
86- end
87-
88- # forward key interfaces from its parent
89- Base. parent (stats:: SummaryStats ) = getfield (stats, :data )
90- Base. keys (stats:: SummaryStats ) = map (Symbol, Tables. columnnames (stats))
91- Base. haskey (stats:: SummaryStats , nm:: Symbol ) = nm ∈ keys (stats)
92- Base. length (stats:: SummaryStats ) = length (parent (stats))
93- Base. getindex (stats:: SummaryStats , i:: Union{Int,Symbol} ) = Tables. getcolumn (stats, i)
94- Base. iterate (stats:: SummaryStats , rest... ) = iterate (parent (stats), rest... )
95- function Base. merge (stats:: SummaryStats , other_stats:: SummaryStats... )
96- isempty (other_stats) && return stats
97- stats_all = (stats, other_stats... )
98- stats_last = last (stats_all)
99- return SummaryStats (merge (map (parent, stats_all)... ), stats_last. name)
100- end
101- for f in (:(== ), :isequal )
102- @eval begin
103- function Base. $ (f)(stats:: SummaryStats , other_stats:: SummaryStats )
104- return $ (f)(parent (stats), parent (other_stats))
105- end
106- end
107- end
108-
109- # ### custom tabular show methods
110-
111- function Base. show (io:: IO , mime:: MIME"text/plain" , stats:: SummaryStats ; kwargs... )
112- return _show (io, mime, stats; kwargs... )
113- end
114- function Base. show (io:: IO , mime:: MIME"text/html" , stats:: SummaryStats ; kwargs... )
115- return _show (io, mime, stats; kwargs... )
116- end
117-
118- function _show (io:: IO , mime:: MIME , stats:: SummaryStats ; kwargs... )
119- nt = parent (stats)
120- data = nt[keys (nt)[2 : end ]]
121- rhat_formatter = _prettytables_rhat_formatter (data)
122- extra_formatters = rhat_formatter === nothing ? () : (rhat_formatter,)
123- return _show_prettytable (
124- io,
125- mime,
126- data;
127- title= stats. name,
128- row_labels= Tables. getcolumn (stats, LABEL_COLUMN_NAME),
129- extra_formatters,
130- kwargs... ,
131- )
132- end
133-
134- # ### Tables interface as column table
135-
136- Tables. istable (:: Type{<:SummaryStats} ) = true
137- Tables. columnaccess (:: Type{<:SummaryStats} ) = true
138- Tables. columns (s:: SummaryStats ) = s
139- Tables. columnnames (s:: SummaryStats ) = Tables. columnnames (parent (s))
140- Tables. getcolumn (stats:: SummaryStats , i:: Int ) = Tables. getcolumn (parent (stats), i)
141- Tables. getcolumn (stats:: SummaryStats , nm:: Symbol ) = Tables. getcolumn (parent (stats), nm)
142- Tables. schema (s:: SummaryStats ) = Tables. schema (parent (s))
143-
144- IteratorInterfaceExtensions. isiterable (:: SummaryStats ) = true
145- function IteratorInterfaceExtensions. getiterator (s:: SummaryStats )
146- return Tables. datavaluerows (Tables. columntable (s))
147- end
148-
149- TableTraits. isiterabletable (:: SummaryStats ) = true
150-
15118"""
15219 summarize(data; kind=:all,kwargs...) -> SummaryStats
15320 summarize(data, stats_funs...; kwargs...) -> SummaryStats
0 commit comments