From 067d8ad9cc8f5214d69d0ffcd10e78f87d4b3956 Mon Sep 17 00:00:00 2001 From: Art Wild Date: Sun, 29 May 2022 01:12:28 -0400 Subject: [PATCH 1/4] make CoefTable width adjustable --- src/statmodels.jl | 33 ++++++++++++++++++++++++++++++--- test/statmodels.jl | 8 +++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/statmodels.jl b/src/statmodels.jl index 6a6258e96..e88243445 100644 --- a/src/statmodels.jl +++ b/src/statmodels.jl @@ -120,27 +120,54 @@ function show(io::IO, ct::CoefTable) if length(rownms) == 0 rownms = [lpad("[$i]",floor(Integer, log10(nr))+3) for i in 1:nr] end + + if !(get(io, :limit, false)::Bool) + screenheight = screenwidth = typemax(Int) + else + sz = displaysize(io)::Tuple{Int,Int} + screenheight, screenwidth = sz[1] - 4, sz[2] + end + + sepsize = 3 + hdots = " \u2026 " mat = [j == 1 ? NoQuote(rownms[i]) : j-1 == ct.pvalcol ? NoQuote(sprint(show, PValue(cols[j-1][i]))) : j-1 in ct.teststatcol ? TestStat(cols[j-1][i]) : cols[j-1][i] isa AbstractString ? NoQuote(cols[j-1][i]) : cols[j-1][i] for i in 1:nr, j in 1:nc+1] + # Code inspired by print_matrix in Base io = IOContext(io, :compact=>true, :limit=>false) A = Base.alignment(io, mat, 1:size(mat, 1), 1:size(mat, 2), - typemax(Int), typemax(Int), 3) + screenwidth, screenwidth, sepsize) nmswidths = pushfirst!(length.(colnms), 0) A = [nmswidths[i] > sum(A[i]) ? (A[i][1]+nmswidths[i]-sum(A[i]), A[i][2]) : A[i] for i in 1:length(A)] - totwidth = sum(sum.(A)) + 2 * (length(A) - 1) + + # remove columns that do not fit on the screen + maxcols = length(A) + totwidth = sum(sum.(A)) + 2 * (length(A) - 1)+sepsize + if maxcols < nc+1 + ncols = min(nc, maxcols) + mat = mat[:, 1:ncols] + colnms = colnms[1:ncols] + sepsize = length(hdots)+1 + else + sepsize = 0 + end + + # print table + totwidth = sum(sum.(A)) + 2 * (length(A) - 1)+sepsize println(io, repeat('─', totwidth)) print(io, repeat(' ', sum(A[1]))) - for j in 1:length(colnms) + for j in 1:maxcols-1 print(io, " ", lpad(colnms[j], sum(A[j+1]))) end + maxcols < nc+1 && print(io, lpad(hdots, sepsize)) println(io, '\n', repeat('─', totwidth)) for i in 1:size(mat, 1) Base.print_matrix_row(io, mat, A, i, 1:size(mat, 2), " ") + maxcols < nc+1 && print(io, lpad(hdots, sepsize)) i != size(mat, 1) && println(io) end print(io, '\n', repeat('─', totwidth)) diff --git a/test/statmodels.jl b/test/statmodels.jl index 778ea2e39..6e940af7c 100644 --- a/test/statmodels.jl +++ b/test/statmodels.jl @@ -82,6 +82,12 @@ ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4) df = 0.2422083248151139, p = 0.4530583319523316) ] +ct = CoefTable(rand(1,100), ["c$i" for i in 1:100], [], 4) +sct = sprint(show, ct, context=:limit=>false) +@test length(first(split(sct, \'n'))) > 1000 +sct = sprint(show, ct, context=:limit=>true) +@test length(first(split(sct, \'n'))) < 200 + @test sprint(show, PValue(1.0)) == "1.0000" @test sprint(show, PValue(1e-1)) == "0.1000" if VERSION > v"1.6.0-DEV" @@ -190,4 +196,4 @@ end # Defined but not reexported @test StatsBase.params isa Function @test StatsBase.params! isa Function -end \ No newline at end of file +end From 799adde9fb17b8d30038f34b6c449d89a0176f9a Mon Sep 17 00:00:00 2001 From: Art Wild Date: Mon, 30 May 2022 14:28:30 -0400 Subject: [PATCH 2/4] fixed typo --- test/statmodels.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/statmodels.jl b/test/statmodels.jl index 6e940af7c..b14d7af40 100644 --- a/test/statmodels.jl +++ b/test/statmodels.jl @@ -84,9 +84,9 @@ ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4) ct = CoefTable(rand(1,100), ["c$i" for i in 1:100], [], 4) sct = sprint(show, ct, context=:limit=>false) -@test length(first(split(sct, \'n'))) > 1000 +@test length(first(split(sct, '\n'))) > 900 sct = sprint(show, ct, context=:limit=>true) -@test length(first(split(sct, \'n'))) < 200 +@test length(first(split(sct, '\n'))) < 200 @test sprint(show, PValue(1.0)) == "1.0000" @test sprint(show, PValue(1e-1)) == "0.1000" From d0a592cc58c05f90af63174b47a7b00798a2855d Mon Sep 17 00:00:00 2001 From: wildart Date: Sun, 4 Sep 2022 19:02:33 -0400 Subject: [PATCH 3/4] fixed dots alignment and spacing tighter test bounds --- src/statmodels.jl | 18 ++++++++++-------- test/statmodels.jl | 6 +++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/statmodels.jl b/src/statmodels.jl index e88243445..61cd2a63a 100644 --- a/src/statmodels.jl +++ b/src/statmodels.jl @@ -129,7 +129,7 @@ function show(io::IO, ct::CoefTable) end sepsize = 3 - hdots = " \u2026 " + hdots = " … " mat = [j == 1 ? NoQuote(rownms[i]) : j-1 == ct.pvalcol ? NoQuote(sprint(show, PValue(cols[j-1][i]))) : j-1 in ct.teststatcol ? TestStat(cols[j-1][i]) : @@ -146,18 +146,19 @@ function show(io::IO, ct::CoefTable) # remove columns that do not fit on the screen maxcols = length(A) - totwidth = sum(sum.(A)) + 2 * (length(A) - 1)+sepsize if maxcols < nc+1 ncols = min(nc, maxcols) mat = mat[:, 1:ncols] + mat = hcat(mat, [hdots for i in 1:nr]) colnms = colnms[1:ncols] - sepsize = length(hdots)+1 + sepsize = textwidth(hdots)+1 else sepsize = 0 end # print table - totwidth = sum(sum.(A)) + 2 * (length(A) - 1)+sepsize + totwidth = sum(sum, A) + 2 * (maxcols - 1) + sepsize + println(io, repeat('─', totwidth)) print(io, repeat(' ', sum(A[1]))) for j in 1:maxcols-1 @@ -165,10 +166,11 @@ function show(io::IO, ct::CoefTable) end maxcols < nc+1 && print(io, lpad(hdots, sepsize)) println(io, '\n', repeat('─', totwidth)) - for i in 1:size(mat, 1) - Base.print_matrix_row(io, mat, A, i, 1:size(mat, 2), " ") - maxcols < nc+1 && print(io, lpad(hdots, sepsize)) - i != size(mat, 1) && println(io) + m, n =size(mat) + for i in 1:m + Base.print_matrix_row(io, mat, A, i, 1:n, " ") + maxcols < nc+1 && (i+4)%5 == 0 && print(io, lpad(hdots, sepsize)) + i != m && println(io) end print(io, '\n', repeat('─', totwidth)) nothing diff --git a/test/statmodels.jl b/test/statmodels.jl index b14d7af40..23db014a0 100644 --- a/test/statmodels.jl +++ b/test/statmodels.jl @@ -82,11 +82,11 @@ ct = CoefTable(m, ["Estimate", "Stderror", "df", "p"], [], 4) df = 0.2422083248151139, p = 0.4530583319523316) ] -ct = CoefTable(rand(1,100), ["c$i" for i in 1:100], [], 4) +ct = CoefTable(rand(15,100), ["c$i" for i in 1:100], [], 4) sct = sprint(show, ct, context=:limit=>false) -@test length(first(split(sct, '\n'))) > 900 +@test textwidth(first(split(sct, '\n'))) > 900 sct = sprint(show, ct, context=:limit=>true) -@test length(first(split(sct, '\n'))) < 200 +@test textwidth(first(split(sct, '\n'))) <= displaysize()[2] @test sprint(show, PValue(1.0)) == "1.0000" @test sprint(show, PValue(1e-1)) == "0.1000" From 0d7481ac2add5fc254ee8227f0b2496e310500f8 Mon Sep 17 00:00:00 2001 From: Art Date: Sun, 9 Oct 2022 12:16:39 -0400 Subject: [PATCH 4/4] formatting Co-authored-by: Milan Bouchet-Valat --- src/statmodels.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/statmodels.jl b/src/statmodels.jl index 61cd2a63a..570765919 100644 --- a/src/statmodels.jl +++ b/src/statmodels.jl @@ -166,7 +166,7 @@ function show(io::IO, ct::CoefTable) end maxcols < nc+1 && print(io, lpad(hdots, sepsize)) println(io, '\n', repeat('─', totwidth)) - m, n =size(mat) + m, n = size(mat) for i in 1:m Base.print_matrix_row(io, mat, A, i, 1:n, " ") maxcols < nc+1 && (i+4)%5 == 0 && print(io, lpad(hdots, sepsize))