ContrastEncoder: missing buildmatrix with vector-valued mode gives "Nothing is not callable" instead of BUILDFUNC_MUST_BE_SPECIFIED

Aperta Adatta ai principianti
#108 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
90/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
julia

Direzione di ricerca

Inizia leggendo src/encoders/contrast_encoder/contrast_encoder.jl intorno a contrast_encoder_fit, in particolare la validazione della modalità e la chiamata a buildmatrix. Esegui i casi di riproduzione, quindi aggiorna i test esistenti in test/encoders/contrast_encoder.jl per coprire le modalità vettoriali contenenti :contrast o :hypothesis e le modalità AbstractVector{Symbol} diverse da Vector. Il lavoro è completato quando questi casi generano il previsto ArgumentError anziché MethodError o un errore di modalità non supportata, mantenendo coperti i comportamenti esistenti per i valori scalari.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When mode is given as a vector, the buildmatrix check in contrast_encoder_fit never runs, so forgetting buildmatrix for a :contrast / :hypothesis feature gives a confusing MethodError instead of the intended BUILDFUNC_MUST_BE_SPECIFIED message.

The check is:

https://github.com/JuliaAI/MLJTransforms.jl/blob/d3e3db364f30bef397eddeae7f30a5e7967757fc/src/encoders/contrast_encoder/contrast_encoder.jl#L97-L100

if mode in (:contrast, :hypothesis)
    buildmatrix === nothing && throw(ArgumentError(BUILDFUNC_MUST_BE_SPECIFIED))
end

For mode = [:contrast, :dummy] this is [:contrast, :dummy] in (:contrast, :hypothesis), which is false, so we fall through to buildmatrix(name, k) with buildmatrix === nothing at L109/L113.

Reproduction
using MLJTransforms, MLJBase, CategoricalArrays
X = (A = categorical(["a", "b", "c", "a"]), B = categorical(["x", "y", "x", "y"]))

# scalar mode: helpful error
try
    MLJTransforms.contrast_encoder_fit(X, [:A]; mode = :contrast, ignore = false)
catch e
    println("scalar mode  -> ", sprint(showerror, e))
end

# vector mode: check is skipped
try
    MLJTransforms.contrast_encoder_fit(X, [:A, :B]; mode = [:contrast, :dummy], ignore = false)
catch e
    println("vector mode  -> ", sprint(showerror, e))
end

# same through the MLJ interface
enc = ContrastEncoder(features = [:A, :B], mode = [:hypothesis, :dummy], ignore = false)
mach = machine(enc, X)
try
    fit!(mach, verbosity = 0)
catch e
    println("machine      -> ", sprint(showerror, e)[1:min(end, 300)])
end

Output:

scalar mode  -> ArgumentError: In ContrastEncoder with mode=:contrast or mode=:hypothesis, the `buildmatrix` argument must be specified.
vector mode  -> MethodError: objects of type Nothing are not callable.
In case you did not try calling it explicitly, check if a Nothing has been passed as an argument to a method that expects a callable instead.
The object of type `Nothing` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
┌ Error: Problem fitting the machine machine(ContrastEncoder(features = [:A, :B], …), …). 
└ @ MLJBase ~/.julia/packages/MLJBase/lZsbe/src/machines.jl:758
[ Info: Running type checks... 
[ Info: Type checks okay. 
machine      -> MethodError: objects of type Nothing are not callable.
In case you did not try calling it explicitly, check if a Nothing has been passed as an argument to a method that expects a callable instead.
The object of type `Nothing` exists, but no method is defined for this combination of argument types wh

Expected: the vector case throws the same ArgumentError(BUILDFUNC_MUST_BE_SPECIFIED) as the scalar case.
Actual: MethodError: objects of type Nothing are not callable.

Related: non-Vector mode vectors

A nearby detail in the same block: the keyword is typed mode::Union{Symbol, AbstractVector{Symbol}} (and the struct allows AbstractVector{Symbol}), but the vector branch at L90 tests mode isa Vector{Symbol}. So any other AbstractVector{Symbol} skips the vector handling and the whole vector ends up treated as one mode:

modes = view([:sum, :dummy, :helmert], 1:2)   # AbstractVector{Symbol}, not a Vector
MLJTransforms.contrast_encoder_fit(X, [:A, :B]; mode = modes, ignore = false)
ArgumentError: Mode [:sum, :dummy] is not supported.
Possible fix

Something like:

mode_is_vector = mode isa AbstractVector{Symbol}
...
modes = mode_is_vector ? mode : (mode,)
if any(in((:contrast, :hypothesis)), modes)
    buildmatrix === nothing && throw(ArgumentError(BUILDFUNC_MUST_BE_SPECIFIED))
end

plus a test next to the existing BUILDFUNC_MUST_BE_SPECIFIED one in test/encoders/contrast_encoder.jl (which currently only covers the scalar mode = :contrast case). Happy to open a PR if this sounds right.

Versions
  • MLJTransforms dev at d3e3db3 (v0.2.4)
  • Julia 1.13.3, MLJBase from the registry, Linux x86_64
Lingua principale
Julia
Stelle
1
Fork
5
Merge medio
1g 20m
PR unite (30g)
7

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaAI/MLJTransforms.jl

Tutte le issue di JuliaAI/MLJTransforms.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.