code: def test_categorical_encoder(device):
      from speechbrain.dataio.encoder import CategoricalEncoder
  
      encoder = CategoricalEncoder()
      encoder.expect_len(4)
      encoder.update_from_iterable("abcd")
      integers = encoder.encode_sequence("dcba")
      assert all(isinstance(i, int) for i in integers)
      assert encoder.is_continuous()
      with pytest.raises(KeyError):
          encoder.add_label("a")
      # Does NOT raise:
      encoder.ensure_label("a")
      with pytest.raises(KeyError):
          encoder.insert_label("a", -3)
      encoder.enforce_label("a", -3)
      assert encoder.encode_label("a") == -3
      assert not encoder.is_continuous()
  
      # Decoding:
      import torch
  
      encoder = CategoricalEncoder()
      encoder.expect_len(4)
      encoder.update_from_iterable("abcd")
      result = encoder.decode_torch(
          torch.tensor([[0, 0], [1, 1], [2, 2], [3, 3]], device=device)
      )
      assert result == [["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"]]
      result = encoder.decode_ndim([[0, 0], [1, 1], [2, 2], [3, 3]])
      assert result == [["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"]]
      result = encoder.decode_ndim(
          torch.tensor([[0, 0], [1, 1], [2, 2], [3, 3]])
      )
      assert result == [["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"]]
      result = encoder.decode_ndim(
          [[[[[0, 0], [1, 1], [2, 2], [3, 3]]]]]
      )
      assert result == [
          [[[["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"]]]]
      ]
      result = encoder.decode_torch(
          torch.tensor(
              [[[[[0, 0], [1, 1], [2, 2], [3, 3]]]]], device=device
          )
      )
      assert result == [
          [[[["a", "a"], ["b", "b"], ["c", "c"], ["d", "d"]]]]
      ]
      result = encoder.decode_ndim([[0, 0], [1], [2, 2, 2], []])
      assert result == [["a", "a"], ["b"], ["c", "c", "c"], []]
  
      encoder = CategoricalEncoder()
      encoder.expect_len(3)
      encoder.limited_labelset_from_iterable(
          "aabbbcccd", n_most_common=3
      )
      encoder.encode_sequence("abc")
      with pytest.raises(KeyError):
          encoder.encode_label("d")
      encoder = CategoricalEncoder()
      encoder.expect_len(2)
      encoder.limited_labelset_from_iterable("aabbbcccd", min_count=3)
      encoder.encode_sequence("cbcb")
      with pytest.raises(KeyError):
          encoder.encode_label("a")
      with pytest.raises(KeyError):
          encoder.encode_label("d")
      encoder = CategoricalEncoder()
      encoder.expect_len(2)
      encoder.limited_labelset_from_iterable(
          "aabbbcccd", n_most_common=3, min_count=3
      )
      encoder.encode_sequence("cbcb")
      with pytest.raises(KeyError):
          encoder.encode_label("a")
      with pytest.raises(KeyError):
          encoder.encode_label("d")
  
      encoder = CategoricalEncoder(unk_label="<unk>")
      encoder.expect_len(4)
      encoder.update_from_iterable("abc")
      assert encoder.encode_label("a") == 1
      assert encoder.encode_label("d") == 0
      assert encoder.decode_ndim(encoder.encode_label("d")) == "<unk>"
  
imports: import pytest
label_desc: Use image node_id 1 for calling a global function with example usage: test_categorical_encoder(device) without return types