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

Open Beginner friendly
#119 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
julia

Research direction

Read src/transformers/other_transformers/univariate_boxcox_transformer.jl, especially the boxcox helper at lines 26–33, and inspect test/transformers/other_transformers/univariate_boxcox_transformer.jl. Add coverage for invalid transform inputs in both the general and lambda == 0 cases, then run that test file. Done means these paths throw DomainError instances with useful details about the offending value.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Julia
Stars
1
Forks
5
Avg merge
6h 44m
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaAI/MLJTransforms.jl

All issues in JuliaAI/MLJTransforms.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.