Skip to content

Commit ded0336

Browse files
committed
[ML][Python] Add test for invalid sampling ratio
1 parent 5318d8f commit ded0336

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

bindings/pyroot/pythonizations/test/ml_dataloader.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4683,8 +4683,37 @@ def test(size_of_batch, num_of_entries_major, num_of_entries_minor, sampling_rat
46834683
self.teardown_file(file_name2)
46844684
raise
46854685

4686+
def test_raises(size_of_batch, num_of_entries_major, num_of_entries_minor, sampling_ratio):
4687+
define_rdf_major(num_of_entries_major, file_name1)
4688+
define_rdf_minor(num_of_entries_minor, file_name2)
4689+
4690+
df1 = ROOT.RDataFrame(tree_name, file_name1)
4691+
df2 = ROOT.RDataFrame(tree_name, file_name2)
4692+
4693+
with self.assertRaisesRegex(
4694+
Exception, r"The sampling_ratio is too low: not enough entries in the majority class to sample from."
4695+
):
4696+
ROOT.Experimental.ML.CreateNumPyGenerators(
4697+
[df1, df2],
4698+
batch_size=size_of_batch,
4699+
target=["b3", "b5"],
4700+
weights="b1",
4701+
validation_split=0.3,
4702+
shuffle=False,
4703+
drop_remainder=False,
4704+
load_eager=True,
4705+
sampling_type="undersampling",
4706+
sampling_ratio=sampling_ratio,
4707+
replacement=False,
4708+
)
4709+
4710+
# test the functionality with a proper sampling ratio
46864711
test(batch_size, entries_in_rdf_major, entries_in_rdf_minor, sampling_ratio)
46874712

4713+
bad_sampling_ratio = round(max(min_allowed_sampling_ratio - 0.01, 0.01), 2)
4714+
# test that an error is raised when the sampling ratio is too low
4715+
test_raises(batch_size, entries_in_rdf_major, entries_in_rdf_minor, bad_sampling_ratio)
4716+
46884717
def test14_big_data_replacement_true(self):
46894718
file_name1 = "big_data_major.root"
46904719
file_name2 = "big_data_minor.root"

0 commit comments

Comments
 (0)