code: def test_split_dataset_class_balanced(self):
      benchmark = get_fast_benchmark(n_samples_per_class=1000)
      exp = benchmark.train_stream[0]
      num_classes = len(exp.classes_in_this_experience)
  
      train_d, valid_d = split_validation_class_balanced(
          0.5, exp.dataset
      )
      assert abs(len(train_d) - len(valid_d)) <= num_classes
      for cid in exp.classes_in_this_experience:
          train_cnt = (torch.as_tensor(train_d.targets) == cid).sum()
          valid_cnt = (torch.as_tensor(valid_d.targets) == cid).sum()
          # print(train_cnt, valid_cnt)
          assert abs(train_cnt - valid_cnt) <= 1
  
      ratio = 0.123
      len_data = len(exp.dataset)
      train_d, valid_d = split_validation_class_balanced(
          ratio, exp.dataset
      )
      assert_almost_equal(len(valid_d) / len_data, ratio, decimal=2)
      for cid in exp.classes_in_this_experience:
          data_cnt = (torch.as_tensor(exp.dataset.targets) == cid).sum()
          valid_cnt = (torch.as_tensor(valid_d.targets) == cid).sum()
          assert_almost_equal(valid_cnt / data_cnt, ratio, decimal=2)
  
imports: import unittest
  import torch
  from numpy.testing import assert_almost_equal
  from torch.utils.data import TensorDataset, DataLoader
  from avalanche.benchmarks import benchmark_from_datasets, benchmark_with_validation_stream, CLScenario, CLStream, split_validation_random, task_incremental_benchmark
  from avalanche.benchmarks.scenarios.dataset_scenario import DatasetExperience, split_validation_class_balanced
  from avalanche.benchmarks.utils import AvalancheDataset
  from tests.unit_tests_utils import dummy_tensor_dataset, get_fast_benchmark, DummyImageDataset
label_desc: Use image node_id 2 for calling the DatasetSplitterTest obj's underlying member method code with example usage: obj.test_split_dataset_class_balanced() without return types