Skip to content
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

Fix MLJ Serialization, add test for single class classifiers, fix single class classifiers predict #35

Merged
merged 3 commits into from
Jun 18, 2024
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 Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CatBoost"
uuid = "e2e10f9a-a85d-4fa9-b6b2-639a32100a12"
authors = ["Beacon Biosignals, Inc."]
version = "0.3.4"
version = "0.3.5"

[deps]
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
Expand Down
9 changes: 5 additions & 4 deletions src/mlj_catboostclassifier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ MMI.reports_feature_importances(::Type{<:CatBoostClassifier}) = true
function MMI.predict(mlj_model::CatBoostClassifier, fitresult, X_pool)
if fitresult[1] === nothing
# Always predict the single class
n = nrow(X_pool)
n = pyconvert(Int, X_pool.shape[0])
classes = [fitresult.single_class]
probs = ones(n, 1)
return MMI.UnivariateFinite(classes, probs; pool=fitresult.y_first)
pool = MMI.categorical([fitresult.y_first])
return MMI.UnivariateFinite(classes, probs; pool=pool)
end

model, y_first = fitresult
Expand All @@ -116,8 +117,8 @@ end
function MMI.predict_mode(mlj_model::CatBoostClassifier, fitresult, X_pool)
if fitresult[1] === nothing
# Return probability 1 for the single class
n = nrow(X_pool)
return hcat(ones(n), zeros(n))
n = pyconvert(Int, X_pool.shape[0])
return fill(fitresult.y_first, n)
end

model, y_first = fitresult
Expand Down
64 changes: 51 additions & 13 deletions src/mlj_serialization.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# Taken from https://github.com/JuliaAI/MLJXGBoostInterface.jl
# It is likely also not the optimal method for serializing models, but it works

"""
_persistent(booster)
_persistent(model::CatBoostModels, fitresult)

Private method.

Return a persistent (ie, Julia-serializable) representation of the
CatBoost.jl model `booster`.
CatBoost.jl model `fitresult`.

Restore the model with [`booster`](@ref)
Restore the model with [`fitresult`](@ref)
"""
function _persistent(booster)
function _persistent(::CatBoostRegressor, fitresult)

Check warning on line 14 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L14

Added line #L14 was not covered by tests
ctb_file, io = mktemp()
close(io)

booster.save_model(ctb_file)
fitresult.save_model(ctb_file)

Check warning on line 18 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L18

Added line #L18 was not covered by tests
persistent_booster = read(ctb_file)
rm(ctb_file)
return persistent_booster
end
function _persistent(::CatBoostClassifier, fitresult)
model, y_first = fitresult
if model === nothing

Check warning on line 25 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L23-L25

Added lines #L23 - L25 were not covered by tests
# Case 1: Single unique class
return (nothing, fitresult.single_class, y_first)

Check warning on line 27 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L27

Added line #L27 was not covered by tests
else
# Case 2: Multiple unique classes
ctb_file, io = mktemp()
close(io)

Check warning on line 31 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L30-L31

Added lines #L30 - L31 were not covered by tests

model.save_model(ctb_file)
persistent_booster = read(ctb_file)
rm(ctb_file)
return (persistent_booster, y_first)

Check warning on line 36 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L33-L36

Added lines #L33 - L36 were not covered by tests
end
end

"""
_booster(persistent)
Expand All @@ -28,24 +45,45 @@
Return the CatBoost.jl model which has `persistent` as its persistent
(Julia-serializable) representation. See [`persistent`](@ref) method.
"""
function _booster(persistent)
function _booster(::CatBoostRegressor, persistent)
ctb_file, io = mktemp()
write(io, persistent)
close(io)

Check warning on line 51 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L48-L51

Added lines #L48 - L51 were not covered by tests

booster = catboost.CatBoostRegressor().load_model(ctb_file)

Check warning on line 53 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L53

Added line #L53 was not covered by tests

rm(ctb_file)

Check warning on line 55 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L55

Added line #L55 was not covered by tests

return booster

Check warning on line 57 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L57

Added line #L57 was not covered by tests
end
function _booster(::CatBoostClassifier, persistent)

Check warning on line 59 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L59

Added line #L59 was not covered by tests
ctb_file, io = mktemp()
write(io, persistent)
close(io)

booster = catboost.CatBoost().load_model(ctb_file)
booster = catboost.CatBoostClassifier().load_model(ctb_file)

Check warning on line 64 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L64

Added line #L64 was not covered by tests

rm(ctb_file)

return booster
end

function MMI.save(::CatBoostModels, fr; kw...)
(booster, a_target_element) = fr
return (_persistent(booster), a_target_element)
function MMI.save(model::CatBoostModels, fitresult; kwargs...)
return _persistent(model, fitresult)

Check warning on line 72 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L71-L72

Added lines #L71 - L72 were not covered by tests
end

function MMI.restore(model::CatBoostRegressor, serializable_fitresult)
return _booster(model, serializable_fitresult)

Check warning on line 76 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L75-L76

Added lines #L75 - L76 were not covered by tests
end

function MMI.restore(::CatBoostModels, fr)
(persistent, a_target_element) = fr
return (_booster(persistent), a_target_element)
function MMI.restore(model::CatBoostClassifier, serializable_fitresult)
if serializable_fitresult[1] === nothing

Check warning on line 80 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L79-L80

Added lines #L79 - L80 were not covered by tests
# Case 1: Single unique class
return (model=nothing, single_class=serializable_fitresult[2],

Check warning on line 82 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L82

Added line #L82 was not covered by tests
y_first=serializable_fitresult[3])
else
# Case 2: Multiple unique classes
persistent_booster, y_first = serializable_fitresult
return (_booster(model, persistent_booster), y_first)

Check warning on line 87 in src/mlj_serialization.jl

View check run for this annotation

Codecov / codecov/patch

src/mlj_serialization.jl#L86-L87

Added lines #L86 - L87 were not covered by tests
end
end
21 changes: 19 additions & 2 deletions test/mlj_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@
preds = MLJBase.predict(mach, X)
probs = MLJBase.predict_mode(mach, X)

serializable_fitresult = MLJBase.save(mach, mach.fitresult)
serializable_fitresult = MLJBase.save(mach, mach)
restored_fitresult = MLJBase.restore(mach, serializable_fitresult)
end

@testset "CatBoostClassifier - single class" begin
X = (; a=[1, 4, 5, 6], b=[4, 5, 6, 7])
y = [0, 0, 0, 0]

# MLJ Interface
model = CatBoostClassifier(; iterations=5)
mach = machine(model, X, y)
MLJBase.fit!(mach)
preds = MLJBase.predict(mach, X)
println(preds)
probs = MLJBase.predict_mode(mach, X)
println(probs)

serializable_fitresult = MLJBase.save(mach, mach)
restored_fitresult = MLJBase.restore(mach, serializable_fitresult)
end

Expand All @@ -36,7 +53,7 @@
MLJBase.fit!(mach)
preds = MLJBase.predict(mach, X)

serializable_fitresult = MLJBase.save(mach, mach.fitresult)
serializable_fitresult = MLJBase.save(mach, mach)
restored_fitresult = MLJBase.restore(mach, serializable_fitresult)
end

Expand Down
Loading