`boxcox` in `UnivariateBoxCoxTransformer` throws the type `DomainError` instead of a `DomainError` instance

Aperta Adatta ai principianti
#119 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
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
julia

Direzione di ricerca

Leggi src/transformers/other_transformers/univariate_boxcox_transformer.jl, in particolare l’helper boxcox alle righe 26–33, e controlla test/transformers/other_transformers/univariate_boxcox_transformer.jl. Aggiungi test per gli input di trasformazione non validi sia nel caso generale sia nel caso lambda == 0, quindi esegui quel file di test. Il lavoro è completo quando questi percorsi generano istanze di DomainError con dettagli utili sul valore in questione.

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

Descrizione

The internal boxcox helper used by UnivariateBoxCoxTransformer does throw(DomainError) when c + x is out of range:

https://github.com/JuliaAI/MLJTransforms.jl/blob/d3e3db364f30bef397eddeae7f30a5e7967757fc/src/transformers/other_transformers/univariate_boxcox_transformer.jl#L26-L33

function boxcox(lambda, c, x::Real)
    c + x >= 0 || throw(DomainError)
    if lambda == 0.0
        c + x > 0 || throw(DomainError)
        return log(c + x)
    end
    return ((c + x)^lambda - 1)/lambda
end

That throws the DataType DomainError itself, not an exception object. So when you fit on positive data and later call transform on new data with a negative value, the error you get is not actually a DomainError. catch e; e isa DomainError is false, @test_throws DomainError fails, and the message is just DomainError with nothing about which value was bad.

Minimal example:

using MLJBase, MLJTransforms, Test
import MLJTransforms as MT

x = [1.0, 2.0, 3.0, 4.0, 5.0]
mach = machine(UnivariateBoxCoxTransformer(), x)
fit!(mach, verbosity=0)

try
    transform(mach, [-1.0, 2.0])
catch e
    @show e typeof(e) (e isa DomainError) (e === DomainError)
    print("showerror: "); showerror(stdout, e); println()
end

# lambda == 0 branch, same thing
try
    MT.boxcox(0.0, 0.0, 0.0)
catch e
    @show e typeof(e) (e isa DomainError)
end

# for comparison, what Base throws
try
    log(-1.0)
catch e
    @show typeof(e) (e isa DomainError)
end

@test_throws DomainError transform(mach, [-1.0])

Output:

fitted_params(mach) = (λ = 0.98, c = 0.0)
e = DomainError
typeof(e) = DataType
e isa DomainError = false
e === DomainError = true
showerror: DomainError
e = DomainError
typeof(e) = DataType
e isa DomainError = false
typeof(e) = DomainError
e isa DomainError = true

test_throws: Test Failed at boxcox.jl:35
  Expression: transform(mach, [-1.0])
    Expected: DomainError
      Thrown: DataType
      DomainError
      Stacktrace:
        [1] boxcox
          @ src/transformers/other_transformers/univariate_boxcox_transformer.jl:27 [inlined]
        ...
        [5] boxcox(lambda::Float64, c::Float64, v::Vector{Float64})
          @ MLJTransforms src/transformers/other_transformers/univariate_boxcox_transformer.jl:35
        [6] transform
          @ src/transformers/other_transformers/univariate_boxcox_transformer.jl:70 [inlined]

Expected: an actual DomainError instance with the offending value and a short hint, e.g.

c + x >= 0 || throw(DomainError(x, "Box-Cox transformation requires x + c >= 0 (c = $c)."))

and the same for the lambda == 0 branch (x + c > 0).

Actual: the type DomainError is thrown, so it doesn't match DomainError in catch/@test_throws and the message carries no information.

The existing test in test/transformers/other_transformers/univariate_boxcox_transformer.jl only covers the round trip, so nothing exercises this path right now. A @test_throws DomainError for a negative input at transform time (and for 0.0 when λ == 0) would cover it.

Versions: Julia 1.13.4, MLJTransforms 0.2.4 (dev at d3e3db3), MLJBase 1.15.4.

Lingua principale
Julia
Stelle
1
Fork
5
Merge medio
6h 44m
PR unite (30g)
3

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.