Skip to content

Commit 241d184

Browse files
committed
factor out new logging for better maintenance
1 parent 687dddd commit 241d184

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/MLJDecisionTreeInterface.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ function MMI.fit(
136136
return (forest, classes_seen, integers_seen), cache, report
137137
end
138138

139+
info_recomputing(n) = "Detected a change to hyperparameters " *
140+
"not restricted to `n_trees`. Recomputing all $n trees trees. "
141+
info_dropping(n) = "Dropping $n trees from the ensemble. "
142+
info_adding(n) = "Adding $n trees to the ensemble. "
143+
139144
function MMI.update(
140145
model::RandomForestClassifier,
141146
verbosity::Int,
@@ -150,8 +155,7 @@ function MMI.update(
150155
only_iterations_have_changed = MMI.is_same_except(model, old_model, :n_trees)
151156

152157
if !only_iterations_have_changed
153-
verbosity > 0 && @info "Detected a change to hyperparameters " *
154-
"not restricted to `n_trees`. Recomputing $n_trees trees. "
158+
verbosity > 0 && @info info_recomputing(model.n_trees)
155159
return MMI.fit(
156160
model,
157161
verbosity,
@@ -166,10 +170,10 @@ function MMI.update(
166170
Δn_trees = model.n_trees - old_model.n_trees
167171
# if `n_trees` drops, then tuncate, otherwise compute more trees
168172
if Δn_trees < 0
169-
verbosity > 0 && @info "Dropping $(-Δn_trees) trees from the forest. "
173+
verbosity > 0 && @info info_dropping(-Δn_trees)
170174
forest = old_forest[1:model.n_trees]
171175
else
172-
verbosity > 0 && @info "Adding $Δn_trees trees to the forest. "
176+
verbosity > 0 && @info info_adding(Δn_trees)
173177
forest = DT.build_forest(
174178
old_forest,
175179
yplain, Xmatrix,
@@ -340,8 +344,7 @@ function MMI.update(
340344
only_iterations_have_changed = MMI.is_same_except(model, old_model, :n_trees)
341345

342346
if !only_iterations_have_changed
343-
verbosity > 0 && @info "Detected a change to hyperparameters " *
344-
"not restricted to `n_trees`. Recomputing $n_trees trees. "
347+
verbosity > 0 && @info info_recomputing(model.n_trees)
345348
return MMI.fit(
346349
model,
347350
verbosity,
@@ -355,10 +358,10 @@ function MMI.update(
355358

356359
# if `n_trees` drops, then tuncate, otherwise compute more trees
357360
if Δn_trees < 0
358-
verbosity > 0 && @info "Dropping $(-Δn_trees) trees from the forest. "
361+
verbosity > 0 && @info info_dropping(-Δn_trees)
359362
forest = old_forest[1:model.n_trees]
360363
else
361-
verbosity > 0 && @info "Adding $Δn_trees trees to the forest. "
364+
verbosity > 0 && @info info_adding(Δn_trees)
362365
forest = DT.build_forest(
363366
old_forest,
364367
y,

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ end
286286
mach.model = $M(n_trees=7, rng = stable_rng())
287287
@test_logs(
288288
(:info, r""),
289-
(:info, r"Adding 3 trees"),
289+
(:info, MLJDecisionTreeInterface.info_adding(3)),
290290
fit!(mach, verbosity=1),
291291
)
292292

293293
# decrease n_trees:
294294
mach.model = $M(n_trees=5, rng = stable_rng())
295295
@test_logs(
296296
(:info, r""),
297-
(:info, r"Dropping 2 trees"),
297+
(:info, MLJDecisionTreeInterface.info_dropping(2)),
298298
fit!(mach, verbosity=1),
299299
)
300300
forest1_5 = fitted_params(mach).forest
@@ -304,7 +304,7 @@ end
304304
mach.model = $M(n_trees=5, rng = stable_rng(), max_depth=1)
305305
@test_logs(
306306
(:info, r""),
307-
(:info, r"Detected"),
307+
(:info, MLJDecisionTreeInterface.info_recomputing(5)),
308308
fit!(mach, verbosity=1),
309309
)
310310
forest1_5_again = fitted_params(mach).forest

0 commit comments

Comments
 (0)