Skip to content

Commit 24c21a9

Browse files
authored
Adjust show method signature for CoefTable to include MIME"text/plain" (#986)
1 parent 7388a8e commit 24c21a9

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

src/statmodels.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,3 @@
1-
2-
## coefficient tables with specialized show method
3-
4-
mutable struct CoefTable
5-
cols::Vector
6-
colnms::Vector
7-
rownms::Vector
8-
pvalcol::Int
9-
teststatcol::Int
10-
function CoefTable(cols::Vector,colnms::Vector,rownms::Vector,
11-
pvalcol::Int=0,teststatcol::Int=0)
12-
nc = length(cols)
13-
nrs = map(length,cols)
14-
nr = nrs[1]
15-
length(colnms) in [0,nc] || throw(ArgumentError("colnms should have length 0 or $nc"))
16-
length(rownms) in [0,nr] || throw(ArgumentError("rownms should have length 0 or $nr"))
17-
all(nrs .== nr) || throw(ArgumentError("Elements of cols should have equal lengths, but got $nrs"))
18-
pvalcol in 0:nc || throw(ArgumentError("pvalcol should be between 0 and $nc"))
19-
teststatcol in 0:nc || throw(ArgumentError("teststatcol should be between 0 and $nc"))
20-
new(cols,colnms,rownms,pvalcol,teststatcol)
21-
end
22-
23-
function CoefTable(mat::Matrix,colnms::Vector,rownms::Vector,
24-
pvalcol::Int=0,teststatcol::Int=0)
25-
nc = size(mat,2)
26-
cols = Any[mat[:, i] for i in 1:nc]
27-
CoefTable(cols,colnms,rownms,pvalcol,teststatcol)
28-
end
29-
end
30-
31-
Base.length(ct::CoefTable) = length(ct.cols[1])
32-
function Base.eltype(ct::CoefTable)
33-
names = isempty(ct.rownms) ?
34-
tuple(Symbol.(ct.colnms)...) :
35-
tuple(Symbol("Name"), Symbol.(ct.colnms)...)
36-
types = isempty(ct.rownms) ?
37-
Tuple{eltype.(ct.cols)...} :
38-
Tuple{eltype(ct.rownms), eltype.(ct.cols)...}
39-
NamedTuple{names, types}
40-
end
41-
42-
function Base.iterate(ct::CoefTable, i::Integer=1)
43-
if i in 1:length(ct)
44-
cols = getindex.(ct.cols, Ref(i))
45-
nt = isempty(ct.rownms) ?
46-
eltype(ct)(tuple(cols...)) :
47-
eltype(ct)(tuple(ct.rownms[i], cols...))
48-
(nt, i+1)
49-
else
50-
nothing
51-
end
52-
end
53-
541
"""
552
Show a p-value using 6 characters, either using the standard 0.XXXX
563
representation or as <Xe-YY.
@@ -113,7 +60,60 @@ end
11360

11461
show(io::IO, n::NoQuote) = print(io, n.s)
11562

116-
function show(io::IO, ct::CoefTable)
63+
64+
## coefficient tables with specialized show method
65+
66+
mutable struct CoefTable
67+
cols::Vector
68+
colnms::Vector
69+
rownms::Vector
70+
pvalcol::Int
71+
teststatcol::Int
72+
function CoefTable(cols::Vector,colnms::Vector,rownms::Vector,
73+
pvalcol::Int=0,teststatcol::Int=0)
74+
nc = length(cols)
75+
nrs = map(length,cols)
76+
nr = nrs[1]
77+
length(colnms) in [0,nc] || throw(ArgumentError("colnms should have length 0 or $nc"))
78+
length(rownms) in [0,nr] || throw(ArgumentError("rownms should have length 0 or $nr"))
79+
all(nrs .== nr) || throw(ArgumentError("Elements of cols should have equal lengths, but got $nrs"))
80+
pvalcol in 0:nc || throw(ArgumentError("pvalcol should be between 0 and $nc"))
81+
teststatcol in 0:nc || throw(ArgumentError("teststatcol should be between 0 and $nc"))
82+
new(cols,colnms,rownms,pvalcol,teststatcol)
83+
end
84+
85+
function CoefTable(mat::Matrix,colnms::Vector,rownms::Vector,
86+
pvalcol::Int=0,teststatcol::Int=0)
87+
nc = size(mat,2)
88+
cols = Any[mat[:, i] for i in 1:nc]
89+
CoefTable(cols,colnms,rownms,pvalcol,teststatcol)
90+
end
91+
end
92+
93+
Base.length(ct::CoefTable) = length(ct.cols[1])
94+
function Base.eltype(ct::CoefTable)
95+
names = isempty(ct.rownms) ?
96+
tuple(Symbol.(ct.colnms)...) :
97+
tuple(Symbol("Name"), Symbol.(ct.colnms)...)
98+
types = isempty(ct.rownms) ?
99+
Tuple{eltype.(ct.cols)...} :
100+
Tuple{eltype(ct.rownms), eltype.(ct.cols)...}
101+
NamedTuple{names, types}
102+
end
103+
104+
function Base.iterate(ct::CoefTable, i::Integer=1)
105+
if i in 1:length(ct)
106+
cols = getindex.(ct.cols, Ref(i))
107+
nt = isempty(ct.rownms) ?
108+
eltype(ct)(tuple(cols...)) :
109+
eltype(ct)(tuple(ct.rownms[i], cols...))
110+
(nt, i+1)
111+
else
112+
nothing
113+
end
114+
end
115+
116+
function show(io::IO, ::MIME"text/plain", ct::CoefTable)
117117
cols = ct.cols; rownms = ct.rownms; colnms = ct.colnms;
118118
nc = length(cols)
119119
nr = length(cols[1])

test/statmodels.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ct_noname = CoefTable(Any[v1, v2, v3, v4, v5],
1414
["Estimate", "Comments", "df", "t", "p"],
1515
[], 5, 4)
1616

17-
@test sprint(show, ct) == """
17+
@test sprint(show, "text/plain", ct) == """
1818
───────────────────────────────────────────────
1919
Estimate Comments df t p
2020
───────────────────────────────────────────────
@@ -23,7 +23,7 @@ x2 -23.14 Great 56 0.13 0.3467
2323
x3 1.56734e-13 Bad 2 0.00 <1e-15
2424
───────────────────────────────────────────────"""
2525

26-
@test sprint(show, ct_noname) == """
26+
@test sprint(show, "text/plain", ct_noname) == """
2727
────────────────────────────────────────────────
2828
Estimate Comments df t p
2929
────────────────────────────────────────────────
@@ -61,7 +61,7 @@ m = [0.11258244478647295 0.05664544616214151 0.38181274408522614 0.8197779704
6161
0.36831406658084287 0.12078054506961555 0.8151038332483567 0.6699313951612162
6262
0.3444540231363058 0.17957407667101322 0.2422083248151139 0.4530583319523316]
6363
ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4)
64-
@test sprint(show, ct) == """
64+
@test sprint(show, "text/plain", ct) == """
6565
──────────────────────────────────────────
6666
Estimate Stderror df p
6767
──────────────────────────────────────────

0 commit comments

Comments
 (0)