Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
b99a8f01
Unverified
Commit
b99a8f01
authored
Feb 19, 2022
by
James Lamb
Committed by
GitHub
Feb 19, 2022
Browse files
[R-package] raise informative errors directly when Booster creation fails (#5014)
parent
2f27d4b2
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
80 deletions
+131
-80
R-package/R/lgb.Booster.R
R-package/R/lgb.Booster.R
+61
-79
R-package/tests/testthat/test_lgb.Booster.R
R-package/tests/testthat/test_lgb.Booster.R
+70
-1
No files found.
R-package/R/lgb.Booster.R
View file @
b99a8f01
...
@@ -26,15 +26,10 @@ Booster <- R6::R6Class(
...
@@ -26,15 +26,10 @@ Booster <- R6::R6Class(
modelfile
=
NULL
,
modelfile
=
NULL
,
model_str
=
NULL
)
{
model_str
=
NULL
)
{
# Create parameters and handle
handle
<-
NULL
handle
<-
NULL
# Attempts to create a handle for the dataset
try
({
# Check if training dataset is not null
if
(
!
is.null
(
train_set
))
{
if
(
!
is.null
(
train_set
))
{
# Check if training dataset is lgb.Dataset or not
if
(
!
lgb.is.Dataset
(
train_set
))
{
if
(
!
lgb.is.Dataset
(
train_set
))
{
stop
(
"lgb.Booster: Can only use lgb.Dataset as training data"
)
stop
(
"lgb.Booster: Can only use lgb.Dataset as training data"
)
}
}
...
@@ -54,7 +49,6 @@ Booster <- R6::R6Class(
...
@@ -54,7 +49,6 @@ Booster <- R6::R6Class(
private
$
num_dataset
<-
1L
private
$
num_dataset
<-
1L
private
$
init_predictor
<-
train_set
$
.__enclos_env__
$
private
$
predictor
private
$
init_predictor
<-
train_set
$
.__enclos_env__
$
private
$
predictor
# Check if predictor is existing
if
(
!
is.null
(
private
$
init_predictor
))
{
if
(
!
is.null
(
private
$
init_predictor
))
{
# Merge booster
# Merge booster
...
@@ -107,16 +101,6 @@ Booster <- R6::R6Class(
...
@@ -107,16 +101,6 @@ Booster <- R6::R6Class(
}
}
})
# Check whether the handle was created properly if it was not stopped earlier by a stop call
if
(
isTRUE
(
lgb.is.null.handle
(
x
=
handle
)))
{
stop
(
"lgb.Booster: cannot create Booster handle"
)
}
else
{
# Create class
class
(
handle
)
<-
"lgb.Booster.handle"
class
(
handle
)
<-
"lgb.Booster.handle"
private
$
handle
<-
handle
private
$
handle
<-
handle
private
$
num_class
<-
1L
private
$
num_class
<-
1L
...
@@ -126,8 +110,6 @@ Booster <- R6::R6Class(
...
@@ -126,8 +110,6 @@ Booster <- R6::R6Class(
,
private
$
num_class
,
private
$
num_class
)
)
}
self
$
params
<-
params
self
$
params
<-
params
return
(
invisible
(
NULL
))
return
(
invisible
(
NULL
))
...
...
R-package/tests/testthat/test_lgb.Booster.R
View file @
b99a8f01
...
@@ -947,7 +947,76 @@ test_that("Booster$new() using a Dataset with a null handle should raise an info
...
@@ -947,7 +947,76 @@ test_that("Booster$new() using a Dataset with a null handle should raise an info
verbose
=
VERBOSITY
verbose
=
VERBOSITY
)
)
)
)
},
regexp
=
"lgb.Booster: cannot create Booster handle"
)
},
regexp
=
"Attempting to create a Dataset without any raw data"
)
})
test_that
(
"Booster$new() raises informative errors for malformed inputs"
,
{
data
(
agaricus.train
,
package
=
"lightgbm"
)
train
<-
agaricus.train
dtrain
<-
lgb.Dataset
(
train
$
data
,
label
=
train
$
label
)
# no inputs
expect_error
({
Booster
$
new
()
},
regexp
=
"lgb.Booster: Need at least either training dataset, model file, or model_str"
)
# unrecognized objective
expect_error
({
Booster
$
new
(
params
=
list
(
objective
=
"not_a_real_objective"
)
,
train_set
=
dtrain
)
},
regexp
=
"Unknown objective type name: not_a_real_objective"
)
# train_set is not a Dataset
expect_error
({
Booster
$
new
(
train_set
=
data.table
::
data.table
(
rnorm
(
1L
:
10L
))
)
},
regexp
=
"lgb.Booster: Can only use lgb.Dataset as training data"
)
# model file isn't a string
expect_error
({
Booster
$
new
(
modelfile
=
list
()
)
},
regexp
=
"lgb.Booster: Can only use a string as model file path"
)
# model file doesn't exist
expect_error
({
Booster
$
new
(
params
=
list
()
,
modelfile
=
"file-that-does-not-exist.model"
)
},
regexp
=
"Could not open file-that-does-not-exist.model"
)
# model file doesn't contain a valid LightGBM model
model_file
<-
tempfile
(
fileext
=
".model"
)
writeLines
(
text
=
c
(
"make"
,
"good"
,
"predictions"
)
,
con
=
model_file
)
expect_error
({
Booster
$
new
(
params
=
list
()
,
modelfile
=
model_file
)
},
regexp
=
"Unknown model format or submodel type in model file"
)
# malformed model string
expect_error
({
Booster
$
new
(
params
=
list
()
,
model_str
=
"a\nb\n"
)
},
regexp
=
"Model file doesn't specify the number of classes"
)
# model string isn't character or raw
expect_error
({
Booster
$
new
(
model_str
=
numeric
()
)
},
regexp
=
"lgb.Booster: Can only use a character/raw vector as model_str"
)
})
})
# this is almost identical to the test above it, but for lgb.cv(). A lot of code
# this is almost identical to the test above it, but for lgb.cv(). A lot of code
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment