test_dataset.R 19.9 KB
Newer Older
1
2
3
4
data(agaricus.train, package = "lightgbm")
train_data <- agaricus.train$data[seq_len(1000L), ]
train_label <- agaricus.train$label[seq_len(1000L)]

5
6
7
data(agaricus.test, package = "lightgbm")
test_data <- agaricus.test$data[1L:100L, ]
test_label <- agaricus.test$label[1L:100L]
Guolin Ke's avatar
Guolin Ke committed
8
9
10

test_that("lgb.Dataset: basic construction, saving, loading", {
  # from sparse matrix
11
12
13
14
  dtest1 <- lgb.Dataset(
    test_data
    , label = test_label
    , params = list(
15
      verbose = .LGB_VERBOSITY
16
17
    )
  )
18
  # from dense matrix
19
  dtest2 <- lgb.Dataset(as.matrix(test_data), label = test_label)
20
  expect_equal(get_field(dtest1, "label"), get_field(dtest2, "label"))
21

Guolin Ke's avatar
Guolin Ke committed
22
  # save to a local file
23
  tmp_file <- tempfile("lgb.Dataset_")
Guolin Ke's avatar
Guolin Ke committed
24
25
  lgb.Dataset.save(dtest1, tmp_file)
  # read from a local file
26
27
28
  dtest3 <- lgb.Dataset(
    tmp_file
    , params = list(
29
      verbose = .LGB_VERBOSITY
30
31
    )
  )
Guolin Ke's avatar
Guolin Ke committed
32
33
  lgb.Dataset.construct(dtest3)
  unlink(tmp_file)
34
  expect_equal(get_field(dtest1, "label"), get_field(dtest3, "label"))
Guolin Ke's avatar
Guolin Ke committed
35
36
})

37
38
39
40
41
42
43
44
45
46
47
48
test_that("lgb.Dataset: get_field & set_field", {
  dtest <- lgb.Dataset(test_data)
  dtest$construct()

  set_field(dtest, "label", test_label)
  labels <- get_field(dtest, "label")
  expect_equal(test_label, get_field(dtest, "label"))

  expect_true(length(get_field(dtest, "weight")) == 0L)
  expect_true(length(get_field(dtest, "init_score")) == 0L)

  # any other label should error
49
50
51
52
53
  expect_error(
    set_field(dtest, "asdf", test_label)
    , regexp = "Dataset$set_field(): field_name must be one of the following: 'label', 'weight', 'init_score', 'group'"  # nolint: line_length.
    , fixed = TRUE
  )
54
55
})

Guolin Ke's avatar
Guolin Ke committed
56
test_that("lgb.Dataset: slice, dim", {
57
  dtest <- lgb.Dataset(test_data, label = test_label)
Guolin Ke's avatar
Guolin Ke committed
58
59
  lgb.Dataset.construct(dtest)
  expect_equal(dim(dtest), dim(test_data))
60
  dsub1 <- lgb.slice.Dataset(dtest, seq_len(42L))
Guolin Ke's avatar
Guolin Ke committed
61
  lgb.Dataset.construct(dsub1)
62
  expect_equal(nrow(dsub1), 42L)
Guolin Ke's avatar
Guolin Ke committed
63
64
65
  expect_equal(ncol(dsub1), ncol(test_data))
})

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
test_that("Dataset$set_reference() on a constructed Dataset fails if raw data has been freed", {
  dtrain <- lgb.Dataset(train_data, label = train_label)
  dtrain$construct()
  dtest <- lgb.Dataset(test_data, label = test_label)
  dtest$construct()
  expect_error({
    dtest$set_reference(dtrain)
  }, regexp = "cannot set reference after freeing raw data")
})

test_that("Dataset$set_reference() fails if reference is not a Dataset", {
  dtrain <- lgb.Dataset(
    train_data
    , label = train_label
    , free_raw_data = FALSE
  )
  expect_error({
    dtrain$set_reference(reference = data.frame(x = rnorm(10L)))
  }, regexp = "Can only use lgb.Dataset as a reference")

  # passing NULL when the Dataset already has a reference raises an error
  dtest <- lgb.Dataset(
    test_data
    , label = test_label
    , free_raw_data = FALSE
  )
  dtrain$set_reference(dtest)
  expect_error({
    dtrain$set_reference(reference = NULL)
  }, regexp = "Can only use lgb.Dataset as a reference")
})

test_that("Dataset$set_reference() setting reference to the same Dataset has no side effects", {
  dtrain <- lgb.Dataset(
    train_data
    , label = train_label
    , free_raw_data = FALSE
    , categorical_feature = c(2L, 3L)
  )
  dtrain$construct()

  cat_features_before <- dtrain$.__enclos_env__$private$categorical_feature
  colnames_before <- dtrain$get_colnames()
  predictor_before <- dtrain$.__enclos_env__$private$predictor

  dtrain$set_reference(dtrain)
  expect_identical(
    cat_features_before
    , dtrain$.__enclos_env__$private$categorical_feature
  )
  expect_identical(
    colnames_before
    , dtrain$get_colnames()
  )
  expect_identical(
    predictor_before
    , dtrain$.__enclos_env__$private$predictor
  )
})

test_that("Dataset$set_reference() updates categorical_feature, colnames, and predictor", {
  dtrain <- lgb.Dataset(
    train_data
    , label = train_label
    , free_raw_data = FALSE
    , categorical_feature = c(2L, 3L)
  )
  dtrain$construct()
  bst <- Booster$new(
    train_set = dtrain
136
    , params = list(verbose = -1L, num_threads = .LGB_MAX_THREADS)
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  )
  dtrain$.__enclos_env__$private$predictor <- bst$to_predictor()

  test_original_feature_names <- paste0("feature_col_", seq_len(ncol(test_data)))
  dtest <- lgb.Dataset(
    test_data
    , label = test_label
    , free_raw_data = FALSE
    , colnames = test_original_feature_names
  )
  dtest$construct()

  # at this point, dtest should not have categorical_feature
  expect_null(dtest$.__enclos_env__$private$predictor)
  expect_null(dtest$.__enclos_env__$private$categorical_feature)
  expect_identical(
    dtest$get_colnames()
    , test_original_feature_names
  )

  dtest$set_reference(dtrain)

  # after setting reference to dtrain, those attributes should have dtrain's values
160
161
162
163
  expect_true(methods::is(
    dtest$.__enclos_env__$private$predictor
    , "lgb.Predictor"
  ))
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  expect_identical(
    dtest$.__enclos_env__$private$predictor$.__enclos_env__$private$handle
    , dtrain$.__enclos_env__$private$predictor$.__enclos_env__$private$handle
  )
  expect_identical(
    dtest$.__enclos_env__$private$categorical_feature
    , dtrain$.__enclos_env__$private$categorical_feature
  )
  expect_identical(
    dtest$get_colnames()
    , dtrain$get_colnames()
  )
  expect_false(
    identical(dtest$get_colnames(), test_original_feature_names)
  )
})

Guolin Ke's avatar
Guolin Ke committed
181
test_that("lgb.Dataset: colnames", {
182
  dtest <- lgb.Dataset(test_data, label = test_label)
Guolin Ke's avatar
Guolin Ke committed
183
184
185
  expect_equal(colnames(dtest), colnames(test_data))
  lgb.Dataset.construct(dtest)
  expect_equal(colnames(dtest), colnames(test_data))
186
187
  expect_error({
    colnames(dtest) <- "asdf"
188
  }, regexp = "can't assign '1' colnames to an lgb.Dataset with '126' columns")
189
  new_names <- make.names(seq_len(ncol(test_data)))
190
191
192
  expect_silent({
    colnames(dtest) <- new_names
  })
Guolin Ke's avatar
Guolin Ke committed
193
194
195
196
  expect_equal(colnames(dtest), new_names)
})

test_that("lgb.Dataset: nrow is correct for a very sparse matrix", {
197
198
  nr <- 1000L
  x <- Matrix::rsparsematrix(nr, 100L, density = 0.0005)
Guolin Ke's avatar
Guolin Ke committed
199
200
201
202
203
  # we want it very sparse, so that last rows are empty
  expect_lt(max(x@i), nr)
  dtest <- lgb.Dataset(x)
  expect_equal(dim(dtest), dim(x))
})
204
205

test_that("lgb.Dataset: Dataset should be able to construct from matrix and return non-null handle", {
206
  rawData <- matrix(runif(1000L), ncol = 10L)
207
  ref_handle <- NULL
208
  handle <- .Call(
209
    LGBM_DatasetCreateFromMat_R
210
211
212
    , rawData
    , nrow(rawData)
    , ncol(rawData)
213
    , lightgbm:::.params2str(params = list())
214
215
    , ref_handle
  )
216
  expect_true(methods::is(handle, "externalptr"))
217
  expect_false(is.null(handle))
218
  .Call(LGBM_DatasetFree_R, handle)
219
  handle <- NULL
220
})
221

222
test_that("cpp errors should be raised as proper R errors", {
223
  testthat::skip_if(
224
225
    Sys.getenv("COMPILER", "") == "MSVC"
    , message = "Skipping on Visual Studio"
226
  )
227
228
229
230
231
232
233
234
  data(agaricus.train, package = "lightgbm")
  train <- agaricus.train
  dtrain <- lgb.Dataset(
    train$data
    , label = train$label
    , init_score = seq_len(10L)
  )
  expect_error({
235
236
237
    capture.output({
      dtrain$construct()
    }, type = "message")
238
239
240
  }, regexp = "Initial score size doesn't match data size")
})

241
242
243
244
245
246
247
248
249
250
251
252
253
test_that("lgb.Dataset$set_field() should convert 'group' to integer", {
  ds <- lgb.Dataset(
    data = matrix(rnorm(100L), nrow = 50L, ncol = 2L)
    , label = sample(c(0L, 1L), size = 50L, replace = TRUE)
  )
  ds$construct()
  current_group <- ds$get_field("group")
  expect_null(current_group)
  group_as_numeric <- rep(25.0, 2L)
  ds$set_field("group", group_as_numeric)
  expect_identical(ds$get_field("group"), as.integer(group_as_numeric))
})

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
test_that("lgb.Dataset should throw an error if 'reference' is provided but of the wrong format", {
  data(agaricus.test, package = "lightgbm")
  test_data <- agaricus.test$data[1L:100L, ]
  test_label <- agaricus.test$label[1L:100L]
  # Try to trick lgb.Dataset() into accepting bad input
  expect_error({
    dtest <- lgb.Dataset(
      data = test_data
      , label = test_label
      , reference = data.frame(x = seq_len(10L), y = seq_len(10L))
    )
  }, regexp = "reference must be a")
})

test_that("Dataset$new() should throw an error if 'predictor' is provided but of the wrong format", {
  data(agaricus.test, package = "lightgbm")
  test_data <- agaricus.test$data[1L:100L, ]
  test_label <- agaricus.test$label[1L:100L]
  expect_error({
    dtest <- Dataset$new(
      data = test_data
      , label = test_label
      , predictor = data.frame(x = seq_len(10L), y = seq_len(10L))
    )
  }, regexp = "predictor must be a", fixed = TRUE)
})
280
281
282
283
284
285
286
287
288
289
290
291
292
293

test_that("Dataset$get_params() successfully returns parameters if you passed them", {
  # note that this list uses one "main" parameter (feature_pre_filter) and one that
  # is an alias (is_sparse), to check that aliases are handled correctly
  params <- list(
    "feature_pre_filter" = TRUE
    , "is_sparse" = FALSE
  )
  ds <- lgb.Dataset(
    test_data
    , label = test_label
    , params = params
  )
  returned_params <- ds$get_params()
294
  expect_identical(class(returned_params), "list")
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  expect_identical(length(params), length(returned_params))
  expect_identical(sort(names(params)), sort(names(returned_params)))
  for (param_name in names(params)) {
    expect_identical(params[[param_name]], returned_params[[param_name]])
  }
})

test_that("Dataset$get_params() ignores irrelevant parameters", {
  params <- list(
    "feature_pre_filter" = TRUE
    , "is_sparse" = FALSE
    , "nonsense_parameter" = c(1.0, 2.0, 5.0)
  )
  ds <- lgb.Dataset(
    test_data
    , label = test_label
    , params = params
  )
  returned_params <- ds$get_params()
  expect_false("nonsense_parameter" %in% names(returned_params))
})

test_that("Dataset$update_parameters() does nothing for empty inputs", {
  ds <- lgb.Dataset(
    test_data
    , label = test_label
  )
  initial_params <- ds$get_params()
  expect_identical(initial_params, list())

  # update_params() should return "self" so it can be chained
  res <- ds$update_params(
    params = list()
  )
329
  expect_true(.is_Dataset(res))
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

  new_params <- ds$get_params()
  expect_identical(new_params, initial_params)
})

test_that("Dataset$update_params() works correctly for recognized Dataset parameters", {
  ds <- lgb.Dataset(
    test_data
    , label = test_label
  )
  initial_params <- ds$get_params()
  expect_identical(initial_params, list())

  new_params <- list(
    "data_random_seed" = 708L
    , "enable_bundle" = FALSE
  )
  res <- ds$update_params(
    params = new_params
  )
350
  expect_true(.is_Dataset(res))
351
352
353
354
355
356

  updated_params <- ds$get_params()
  for (param_name in names(new_params)) {
    expect_identical(new_params[[param_name]], updated_params[[param_name]])
  }
})
357

358
test_that("Dataset's finalizer should not fail on an already-finalized Dataset", {
359
360
361
362
  dtest <- lgb.Dataset(
    data = test_data
    , label = test_label
  )
363
  expect_true(.is_null_handle(dtest$.__enclos_env__$private$handle))
364
365

  dtest$construct()
366
  expect_false(.is_null_handle(dtest$.__enclos_env__$private$handle))
367

368
  dtest$.__enclos_env__$private$finalize()
369
  expect_true(.is_null_handle(dtest$.__enclos_env__$private$handle))
370
371

  # calling finalize() a second time shouldn't cause any issues
372
  dtest$.__enclos_env__$private$finalize()
373
  expect_true(.is_null_handle(dtest$.__enclos_env__$private$handle))
374
375
})

376
377
378
379
test_that("lgb.Dataset: should be able to run lgb.train() immediately after using lgb.Dataset() on a file", {
  dtest <- lgb.Dataset(
    data = test_data
    , label = test_label
380
    , params = list(
381
      verbose = .LGB_VERBOSITY
382
    )
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  )
  tmp_file <- tempfile(pattern = "lgb.Dataset_")
  lgb.Dataset.save(
    dataset = dtest
    , fname = tmp_file
  )

  # read from a local file
  dtest_read_in <- lgb.Dataset(data = tmp_file)

  param <- list(
    objective = "binary"
    , metric = "binary_logloss"
    , num_leaves = 5L
    , learning_rate = 1.0
398
    , verbose = .LGB_VERBOSITY
399
    , num_threads = .LGB_MAX_THREADS
400
401
402
403
404
405
406
407
  )

  # should be able to train right away
  bst <- lgb.train(
    params = param
    , data = dtest_read_in
  )

408
  expect_true(.is_Booster(x = bst))
409
410
411
412
413
414
})

test_that("lgb.Dataset: should be able to run lgb.cv() immediately after using lgb.Dataset() on a file", {
  dtest <- lgb.Dataset(
    data = test_data
    , label = test_label
415
    , params = list(
416
      verbosity = .LGB_VERBOSITY
417
    )
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  )
  tmp_file <- tempfile(pattern = "lgb.Dataset_")
  lgb.Dataset.save(
    dataset = dtest
    , fname = tmp_file
  )

  # read from a local file
  dtest_read_in <- lgb.Dataset(data = tmp_file)

  param <- list(
    objective = "binary"
    , metric = "binary_logloss"
    , num_leaves = 5L
    , learning_rate = 1.0
433
    , num_iterations = 5L
434
    , verbosity = .LGB_VERBOSITY
435
    , num_threads = .LGB_MAX_THREADS
436
437
438
439
440
441
442
443
  )

  # should be able to train right away
  bst <- lgb.cv(
    params = param
    , data = dtest_read_in
  )

444
  expect_true(methods::is(bst, "lgb.CVBooster"))
445
})
446

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
test_that("lgb.Dataset: should be able to be used in lgb.cv() when constructed with categorical feature indices", {
  data("mtcars")
  y <- mtcars$mpg
  x <- as.matrix(mtcars[, -1L])
  categorical_feature <- which(names(mtcars) %in% c("cyl", "vs", "am", "gear", "carb")) - 1L
  dtrain <- lgb.Dataset(
    data = x
    , label = y
    , categorical_feature = categorical_feature
    , free_raw_data = TRUE
    , params = list(num_threads = .LGB_MAX_THREADS)
  )
  # constructing the Dataset frees the raw data
  dtrain$construct()
  params <- list(
    objective = "regression"
    , num_leaves = 2L
    , verbose = .LGB_VERBOSITY
    , num_threads = .LGB_MAX_THREADS
  )
  # cv should reuse the same categorical features without checking the indices
  bst <- lgb.cv(params = params, data = dtrain, stratified = FALSE, nrounds = 1L)
  expect_equal(
    unlist(bst$boosters[[1L]]$booster$params$categorical_feature)
    , categorical_feature - 1L  # 0-based
  )
})


476
477
478
479
test_that("lgb.Dataset: should be able to use and retrieve long feature names", {
  # set one feature to a value longer than the default buffer size used
  # in LGBM_DatasetGetFeatureNames_R
  feature_names <- names(iris)
480
  long_name <- strrep("a", 1000L)
481
482
483
484
485
486
487
488
489
490
491
492
  feature_names[1L] <- long_name
  names(iris) <- feature_names
  # check that feature name survived the trip from R to C++ and back
  dtrain <- lgb.Dataset(
    data = as.matrix(iris[, -5L])
    , label = as.numeric(iris$Species) - 1L
  )
  dtrain$construct()
  col_names <- dtrain$get_colnames()
  expect_equal(col_names[1L], long_name)
  expect_equal(nchar(col_names[1L]), 1000L)
})
493
494
495
496
497
498
499
500
501
502
503
504
505
506

test_that("lgb.Dataset: should be able to create a Dataset from a text file with a header", {
  train_file <- tempfile(pattern = "train_", fileext = ".csv")
  write.table(
    data.frame(y = rnorm(100L), x1 = rnorm(100L), x2 = rnorm(100L))
    , file = train_file
    , sep = ","
    , col.names = TRUE
    , row.names = FALSE
    , quote = FALSE
  )

  dtrain <- lgb.Dataset(
    data = train_file
507
508
    , params = list(
      header = TRUE
509
      , verbosity = .LGB_VERBOSITY
510
    )
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  )
  dtrain$construct()
  expect_identical(dtrain$get_colnames(), c("x1", "x2"))
  expect_identical(dtrain$get_params(), list(header = TRUE))
  expect_identical(dtrain$dim(), c(100L, 2L))
})

test_that("lgb.Dataset: should be able to create a Dataset from a text file without a header", {
  train_file <- tempfile(pattern = "train_", fileext = ".csv")
  write.table(
    data.frame(y = rnorm(100L), x1 = rnorm(100L), x2 = rnorm(100L))
    , file = train_file
    , sep = ","
    , col.names = FALSE
    , row.names = FALSE
    , quote = FALSE
  )

  dtrain <- lgb.Dataset(
    data = train_file
531
532
    , params = list(
      header = FALSE
533
      , verbosity = .LGB_VERBOSITY
534
    )
535
536
537
538
539
540
  )
  dtrain$construct()
  expect_identical(dtrain$get_colnames(), c("Column_0", "Column_1"))
  expect_identical(dtrain$get_params(), list(header = FALSE))
  expect_identical(dtrain$dim(), c(100L, 2L))
})
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

test_that("Dataset: method calls on a Dataset with a null handle should raise an informative error and not segfault", {
  data(agaricus.train, package = "lightgbm")
  train <- agaricus.train
  dtrain <- lgb.Dataset(train$data, label = train$label)
  dtrain$construct()
  dvalid <- dtrain$create_valid(
    data = train$data[seq_len(100L), ]
    , label = train$label[seq_len(100L)]
  )
  dvalid$construct()
  tmp_file <- tempfile(fileext = ".rds")
  saveRDS(dtrain, tmp_file)
  rm(dtrain)
  dtrain <- readRDS(tmp_file)
  expect_error({
    dtrain$construct()
  }, regexp = "Attempting to create a Dataset without any raw data")
  expect_error({
    dtrain$dim()
  }, regexp = "cannot get dimensions before dataset has been constructed")
  expect_error({
    dtrain$get_colnames()
  }, regexp = "cannot get column names before dataset has been constructed")
565
566
567
  expect_error({
    dtrain$get_feature_num_bin(1L)
  }, regexp = "Cannot get number of bins in feature before constructing Dataset.")
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  expect_error({
    dtrain$save_binary(fname = tempfile(fileext = ".bin"))
  }, regexp = "Attempting to create a Dataset without any raw data")
  expect_error({
    dtrain$set_categorical_feature(categorical_feature = 1L)
  }, regexp = "cannot set categorical feature after freeing raw data")
  expect_error({
    dtrain$set_reference(reference = dvalid)
  }, regexp = "cannot set reference after freeing raw data")

  tmp_valid_file <- tempfile(fileext = ".rds")
  saveRDS(dvalid, tmp_valid_file)
  rm(dvalid)
  dvalid <- readRDS(tmp_valid_file)
  dtrain <- lgb.Dataset(
    train$data
    , label = train$label
    , free_raw_data = FALSE
  )
  dtrain$construct()
  expect_error({
    dtrain$set_reference(reference = dvalid)
  }, regexp = "cannot get column names before dataset has been constructed")
})
592
593
594
595
596
597
598
599

test_that("lgb.Dataset$get_feature_num_bin() works", {
  raw_df <- data.frame(
    all_random = runif(100L)
    , two_vals = rep(c(1.0, 2.0), 50L)
    , three_vals = c(rep(c(0.0, 1.0, 2.0), 33L), 0.0)
    , two_vals_plus_missing = c(rep(c(1.0, 2.0), 49L), NA_real_, NA_real_)
    , all_zero = rep(0.0, 100L)
600
    , categorical = sample.int(2L, 100L, replace = TRUE)
601
  )
602
  n_features <- ncol(raw_df)
603
604
  raw_mat <- data.matrix(raw_df)
  min_data_in_bin <- 2L
605
606
607
608
609
  ds <- lgb.Dataset(
    raw_mat
    , params = list(min_data_in_bin = min_data_in_bin)
    , categorical_feature = n_features
  )
610
611
612
613
614
615
616
  ds$construct()
  expected_num_bins <- c(
    100L %/% min_data_in_bin + 1L  # extra bin for zero
    , 3L  # 0, 1, 2
    , 3L  # 0, 1, 2
    , 4L  # 0, 1, 2 + NA
    , 0L  # unused
617
    , 3L  # 1, 2 + NA
618
  )
619
  actual_num_bins <- sapply(1L:n_features, ds$get_feature_num_bin)
620
  expect_identical(actual_num_bins, expected_num_bins)
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
  # test using defined feature names
  bins_by_name <- sapply(colnames(raw_mat), ds$get_feature_num_bin)
  expect_identical(unname(bins_by_name), expected_num_bins)
  # test using default feature names
  no_names_mat <- raw_mat
  colnames(no_names_mat) <- NULL
  ds_no_names <- lgb.Dataset(
    no_names_mat
    , params = list(min_data_in_bin = min_data_in_bin)
    , categorical_feature = n_features
  )
  ds_no_names$construct()
  default_names <- lapply(
    X = seq(1L, ncol(raw_mat))
    , FUN = function(i) {
      sprintf("Column_%d", i - 1L)
    }
  )
  bins_by_default_name <- sapply(default_names, ds_no_names$get_feature_num_bin)
  expect_identical(bins_by_default_name, expected_num_bins)
641
})
642
643
644
645
646
647
648

test_that("lgb.Dataset can be constructed with categorical features and without colnames", {
  # check that dataset can be constructed
  raw_mat <- matrix(rep(c(0L, 1L), 50L), ncol = 1L)
  ds <- lgb.Dataset(raw_mat, categorical_feature = 1L)$construct()
  sparse_mat <- as(raw_mat, "dgCMatrix")
  ds2 <- lgb.Dataset(sparse_mat, categorical_feature = 1L)$construct()
649
650
651
  # check that the column names are the default ones
  expect_equal(ds$.__enclos_env__$private$colnames, "Column_0")
  expect_equal(ds2$.__enclos_env__$private$colnames, "Column_0")
652
653
654
655
656
  # check for error when index is greater than the number of columns
  expect_error({
    lgb.Dataset(raw_mat, categorical_feature = 2L)$construct()
  }, regexp = "supplied a too large value in categorical_feature: 2 but only 1 features")
})
657
658
659
660
661
662
663
664
665

test_that("lgb.Dataset.slice fails with a categorical feature index greater than the number of features", {
  data <- matrix(runif(100L), nrow = 50L, ncol = 2L)
  ds <- lgb.Dataset(data = data, categorical_feature = 3L)
  subset <- ds$slice(1L:20L)
  expect_error({
    subset$construct()
  }, regexp = "supplied a too large value in categorical_feature: 3 but only 2 features")
})