Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selectcols to have tuples as "indices" argument #992

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/interface/data_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function MMI.selectcols(::FI, ::Val{:table}, X, c::Union{Symbol, Integer})
return Tables.getcolumn(cols, c)
end

function MMI.selectcols(::FI, ::Val{:table}, X, c::Union{Colon, AbstractArray})
function MMI.selectcols(::FI, ::Val{:table}, X, c)
if isdataframe(X)
return X[!, c]
else
Expand All @@ -115,18 +115,28 @@ end
# utils for `select`*

# project named tuple onto a tuple with only specified `labels` or indices:
function project(t::NamedTuple, labels::AbstractArray{Symbol})
function project(t::NamedTuple, labels::Union{AbstractArray{Symbol},NTuple{<:Any,Symbol}})
return NamedTuple{tuple(labels...)}(t)
end

project(t::NamedTuple, label::Colon) = t
project(t::NamedTuple, label::Symbol) = project(t, [label,])
project(t::NamedTuple, i::Integer) = project(t, [i,])

function project(t::NamedTuple, indices::AbstractArray{<:Integer})
function project(
t::NamedTuple,
indices::AbstractArray{<:Integer},
)
return NamedTuple{tuple(keys(t)[indices]...)}(tuple([t[i] for i in indices]...))
end

function project(
t::NamedTuple,
indices::NTuple{<:Any,<:Integer},
)
return NamedTuple{tuple(keys(t)[[indices...]]...)}(tuple([t[i] for i in indices]...))
end

# utils for selectrows
typename(X) = split(string(supertype(typeof(X))), '.')[end]
isdataframe(X) = typename(X) == "AbstractDataFrame"
Expand Down
4 changes: 3 additions & 1 deletion test/interface/data_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ end
s = schema(tt)
@test nrows(tt) == N

@test selectcols(tt, 4:6) ==
@test selectcols(tt, 4:6) == selectcols(tt, (4, 5, 6)) ==
selectcols(tt, (:x4, :x5, :z)) ==
selectcols(tt, [:x4, :x5, :z]) ==
selectcols(TypedTables.Table(x4=tt.x4, x5=tt.x5, z=tt.z), :)
@test selectcols(tt, [:x1, :z]) ==
selectcols(TypedTables.Table(x1=tt.x1, z=tt.z), :)
Expand Down
6 changes: 4 additions & 2 deletions test/resampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ end
measures=[LogLoss(), BrierScore()], verbosity=0)
end

docstring_text = @doc(PerformanceEvaluation) |> string

@testset "reported fields in documentation" begin
# Using `evaluate` to obtain a `PerformanceEvaluation` object.
clf = ConstantClassifier()
Expand All @@ -875,11 +877,11 @@ end
cols = ["measure", "operation", "measurement", "1.96*SE", "per_fold"]
@test all(contains.(show_text, cols))
print(show_text)
docstring_text = string(@doc(PerformanceEvaluation))
for fieldname in fieldnames(PerformanceEvaluation)
@test contains(show_text, string(fieldname))
# string(text::Markdown.MD) converts `-` list items to `*`.
@test contains(docstring_text, " * `$fieldname`")
@test contains(docstring_text, "* `$fieldname`") ||
contains(docstring_text, "- `$fieldname`")
end

measures = [LogLoss(), Accuracy()]
Expand Down
Loading