Skip to content

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

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

Merged
merged 5 commits into from
Mar 24, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: julia-actions/cache@v1
env:
cache-name: cache-artifacts
with:
Expand Down
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::Tuple{<:Any,Vararg{<: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
7 changes: 6 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 Expand Up @@ -197,6 +199,9 @@ end
v = categorical(collect("asdfasdf"))
tt = TypedTables.Table(v=v, w=v)
@test selectcols(tt, :w) == v

X = (; x1=ones(3), x2=ones(3), x3=ones(3));
selectcols(X, MLJBase.schema(X).names)
end

# https://github.com/JuliaAI/MLJBase.jl/issues/784
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