Skip to content

Commit ab20625

Browse files
committed
Add IO methods for LabeledTable
1 parent 775803c commit ab20625

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StatsLearnModels"
22
uuid = "c146b59d-1589-421c-8e09-a22e554fd05c"
3-
authors = ["Elias Carvalho <[email protected]> and contributors"]
43
version = "1.1.1"
4+
authors = ["Elias Carvalho <[email protected]> and contributors"]
55

66
[deps]
77
ColumnSelectors = "9cc86067-7e36-4c61-b350-1ac9833d277f"
@@ -11,7 +11,9 @@ Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
1111
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1212
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
1313
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
14+
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
1415
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
16+
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
1517
TableTransforms = "0d432bfd-3ee1-4ac1-886a-39f05cc69a3e"
1618
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1719

@@ -30,7 +32,9 @@ Distributions = "0.25"
3032
GLM = "1.9"
3133
MLJModelInterface = "1.9"
3234
NearestNeighbors = "0.4"
35+
PrettyTables = "3.0.2"
3336
StatsBase = "0.33, 0.34"
37+
StyledStrings = "1.0"
3438
TableTransforms = "1.15"
3539
Tables = "1.11"
3640
julia = "1.9"

src/StatsLearnModels.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module StatsLearnModels
66

77
using Distances
8+
using PrettyTables
9+
using StyledStrings
810
using DataScienceTraits
911
using StatsBase: mode, mean
1012
using ColumnSelectors: ColumnSelector, selector

src/labeledtable.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,54 @@ Tables.columns(t::LabeledTable) = Tables.columns(t.table)
3535

3636
Tables.columnnames(t::LabeledTable) = Tables.columnnames(t.table)
3737

38+
# -----------
39+
# IO METHODS
40+
# -----------
41+
42+
function Base.summary(io::IO, t::LabeledTable)
43+
name = nameof(typeof(t))
44+
nlab = length(t.labels)
45+
print(io, "$name with $nlab label(s)")
46+
end
47+
48+
Base.show(io::IO, t::LabeledTable) = summary(io, t)
49+
50+
function Base.show(io::IO, ::MIME"text/plain", t::LabeledTable)
51+
pretty_table(io, t; backend=:text, _common_kwargs(t)...)
52+
end
53+
54+
function Base.show(io::IO, ::MIME"text/html", t::LabeledTable)
55+
pretty_table(
56+
io,
57+
t;
58+
backend=:html,
59+
_common_kwargs(t)...,
60+
renderer=:show,
61+
style=HtmlTableStyle(title=["font-size" => "14px"])
62+
)
63+
end
64+
65+
function _common_kwargs(t)
66+
cols = Tables.columns(t)
67+
vars = Tables.columnnames(cols)
68+
69+
labels = map(vars) do var
70+
if var t.labels
71+
styled"{(weight=bold),magenta:$var}"
72+
else
73+
styled"{(weight=bold):$var}"
74+
end
75+
end
76+
77+
(
78+
title=summary(t),
79+
column_labels=collect(labels),
80+
maximum_number_of_rows=10,
81+
new_line_at_end=false,
82+
alignment=:c
83+
)
84+
end
85+
3886
"""
3987
label(table, names)
4088

0 commit comments

Comments
 (0)