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

オープン 初心者向け
#119 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
julia

調査の方向性

src/transformers/other_transformers/univariate_boxcox_transformer.jlを読み、特に26〜33行目のboxcoxヘルパーを確認し、test/transformers/other_transformers/univariate_boxcox_transformer.jlを調べてください。一般の場合とlambda == 0の場合の両方について、無効な変換入力を対象とするテストを追加し、そのテストファイルを実行してください。完了の条件は、これらの経路が問題の値について有用な詳細を含むDomainErrorインスタンスをスローすることです。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Julia
スター
1
フォーク
5
平均マージ
6時間 44分
マージ済み PR(30日)
3

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JuliaAI/MLJTransforms.jl のほかの issue

JuliaAI/MLJTransforms.jl の issue をすべて見る

似ている issue

Julia の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。