code: def test_directory_iterator(all_test_images, tmpdir):
      num_classes = 2
  
      # create folders and subfolders
      paths = []
      for cl in range(num_classes):
          class_directory = "class-{}".format(cl)
          classpaths = [
              class_directory,
              os.path.join(class_directory, "subfolder-1"),
              os.path.join(class_directory, "subfolder-2"),
              os.path.join(
                  class_directory, "subfolder-1", "sub-subfolder"
              ),
          ]
          for path in classpaths:
              tmpdir.join(path).mkdir()
          paths.append(classpaths)
  
      # save the images in the paths
      count = 0
      filenames = []
      for test_images in all_test_images:
          for im in test_images:
              # rotate image class
              im_class = count % num_classes
              # rotate subfolders
              classpaths = paths[im_class]
              filename = os.path.join(
                  classpaths[count % len(classpaths)],
                  "image-{}.png".format(count),
              )
              filenames.append(filename)
              im.save(str(tmpdir / filename))
              count += 1
  
      # create iterator
      generator = image_data_generator.ImageDataGenerator()
      dir_iterator = generator.flow_from_directory(str(tmpdir))
  
      # check number of classes and images
      assert len(dir_iterator.class_indices) == num_classes
      assert len(dir_iterator.classes) == count
      assert set(dir_iterator.filenames) == set(filenames)
  
      # Test invalid use cases
      with pytest.raises(ValueError):
          generator.flow_from_directory(str(tmpdir), color_mode="cmyk")
      with pytest.raises(ValueError):
          generator.flow_from_directory(
              str(tmpdir), class_mode="output"
          )
  
      def preprocessing_function(x):
          """This will fail if not provided by a Numpy array.
          Note: This is made to enforce backward compatibility.
          """
  
          assert x.shape == (26, 26, 3)
          assert type(x) is np.ndarray
  
          return np.zeros_like(x)
  
      # Test usage as Sequence
      generator = image_data_generator.ImageDataGenerator(
          preprocessing_function=preprocessing_function
      )
      dir_seq = generator.flow_from_directory(
          str(tmpdir),
          target_size=(26, 26),
          color_mode="rgb",
          batch_size=3,
          class_mode="categorical",
      )
      assert len(dir_seq) == np.ceil(count / 3.0)
      x1, y1 = dir_seq[1]
      assert x1.shape == (3, 26, 26, 3)
      assert y1.shape == (3, num_classes)
      x1, y1 = dir_seq[5]
      assert (x1 == 0).all()
  
      with pytest.raises(ValueError):
          x1, y1 = dir_seq[14]
  
file_path: keras-preprocessing/tests/image/directory_iterator_test.py
imports: import os
  import shutil
  import tempfile
  import numpy
  import pytest
  from PIL import Image
  from keras_preprocessing.image import image_data_generator
label_desc: Use image node_id 2 for calling a global function with example usage: test_directory_iterator(all_test_images, tmpdir) and returns: np