FastTabular
TableRow
"""
TableRow{M, N}(catcols, contcols, categorydict) <: Block
`Block` for table rows with M categorical and N continuous columns. `data`
is valid if it satisfies the `AbstractRow` interface in Tables.jl, values
present in indices for categorical and continuous columns are consistent,
and `data` is indexable by the elements of `catcols` and `contcols`.
"""
struct
TableRow
{
M
,
N
,
T
}
<:
Block
catcols
::
NTuple
{
M
}
contcols
::
NTuple
{
N
}
categorydict
::
T
end
function
TableRow
(
catcols
,
contcols
,
categorydict
)
TableRow
{
length
(
catcols
)
,
length
(
contcols
)
}
(
catcols
,
contcols
,
categorydict
)
end
function
checkblock
(
block
::
TableRow
,
x
)
columns
=
Tables
.
columnnames
(
x
)
(
all
(
col
->
col
∈
columns
,
(
block
.
catcols
...
,
block
.
contcols
...
)
)
&&
all
(
col
->
haskey
(
block
.
categorydict
,
col
)
&&
(
ismissing
(
x
[
col
]
)
||
x
[
col
]
∈
block
.
categorydict
[
col
]
)
,
block
.
catcols
)
&&
all
(
col
->
ismissing
(
x
[
col
]
)
||
x
[
col
]
isa
Number
,
block
.
contcols
)
)
end
function
mockblock
(
block
::
TableRow
)
cols
=
(
block
.
catcols
...
,
block
.
contcols
...
)
vals
=
map
(
cols
)
do
col
col
in
block
.
catcols
?
rand
(
block
.
categorydict
[
col
]
)
:
rand
(
)
end
return
NamedTuple
(
zip
(
cols
,
vals
)
)
end
"""
setup(TableRow, data[; catcols, contcols])
Create a `TableRow` block from data container `data::TableDataset`. If the
categorical and continuous columns are not specified manually, try to
guess them from the dataset's column types.
"""
function
setup
(
::
Type
{
TableRow
}
,
data
;
catcols
=
nothing
,
contcols
=
nothing
)
catcols_
,
contcols_
=
getcoltypes
(
data
)
catcols
=
isnothing
(
catcols
)
?
catcols_
:
catcols
contcols
=
isnothing
(
contcols
)
?
contcols_
:
contcols
return
TableRow
(
catcols
,
contcols
,
gettransformdict
(
data
,
DataAugmentation
.
Categorify
,
catcols
)
)
end
function
Base
.
show
(
io
::
IO
,
block
::
TableRow
)
print
(
io
,
ShowCase
(
block
,
(
:
catcols
,
:
contcols
)
,
show_params
=
false
,
new_lines
=
true
)
)
end
function
showblock!
(
io
,
::
ShowText
,
block
::
TableRow
,
obs
)
rowdata
=
vcat
(
[
obs
[
col
]
for
col
in
block
.
catcols
]
,
[
obs
[
col
]
for
col
in
block
.
contcols
]
)
rownames
=
[
block
.
catcols
...
,
block
.
contcols
...
]
tabledata
=
hcat
(
rownames
,
rowdata
)
PrettyTables
.
pretty_table
(
io
,
tabledata
;
alignment
=
[
:
r
,
:
l
]
,
highlighters
=
PrettyTables
.
Highlighter
(
(
obs
,
i
,
j
)
->
(
j
==
2
)
,
bold
=
true
)
,
noheader
=
true
,
tf
=
PrettyTables
.
tf_borderless
)
end