code: def test_webdataset_coding(ray_start_2_cpus, tmp_path):
      import numpy as np
      import PIL.Image
      import torch
  
      image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
      gray = np.random.randint(0, 255, (100, 100), dtype=np.uint8)
      dstruct = dict(a=[1], b=dict(c=2), d="hello")
      # Note: tensors are supported as numpy format only in strict mode.
      ttensor = torch.tensor([1, 2, 3]).numpy()
  
      sample = {
          "__key__": "foo",
          "jpg": image,
          "gray.png": gray,
          "mp": dstruct,
          "json": dstruct,
          "pt": ttensor,
          "und": b"undecoded",
          "custom": b"nothing",
      }
  
      # write the encoded data using the default encoder
      data = [sample]
      ds = ray.data.from_items(data).repartition(1)
      ds.write_webdataset(path=tmp_path, try_create_dir=True)
  
      # read the encoded data using the default decoder
      paths = glob.glob(f"{tmp_path}/*.tar")
      assert len(paths) == 1
      path = paths[0]
      assert os.path.exists(path)
      ds = ray.data.read_webdataset(
          paths=[str(tmp_path)], parallelism=1
      )
      samples = ds.take(1)
      assert len(samples) == 1
      for sample in samples:
          assert isinstance(sample, dict), sample
          assert sample["__key__"] == "foo"
          assert isinstance(sample["jpg"], np.ndarray)
          assert sample["jpg"].shape == (100, 100, 3)
          assert isinstance(sample["gray.png"], np.ndarray)
          assert sample["gray.png"].shape == (100, 100)
          assert isinstance(sample["mp"], dict)
          assert sample["mp"]["a"] == [1]
          assert sample["mp"]["b"]["c"] == 2
          assert isinstance(sample["json"], dict)
          assert sample["json"]["a"] == [1]
          assert isinstance(sample["pt"], np.ndarray)
          assert sample["pt"].tolist() == [1, 2, 3]
  
      # test the format argument to the default decoder and multiple decoders
      ds = ray.data.read_webdataset(
          paths=[str(tmp_path)],
          parallelism=1,
          decoder=["PIL", custom_decoder],
      )
      samples = ds.take(1)
      assert len(samples) == 1
      for sample in samples:
          assert isinstance(sample, dict), sample
          assert sample["__key__"] == "foo"
          assert isinstance(sample["jpg"], PIL.Image.Image)
          assert isinstance(sample["gray.png"], PIL.Image.Image)
          assert isinstance(sample["und"], bytes)
          assert sample["und"] == b"undecoded"
          assert sample["custom"] == "custom-value"
  
file_path: ray/python/ray/data/tests/test_webdataset.py
imports: import glob
  import io
  import os
  import tarfile
  import pytest
  import ray
  from ray.tests.conftest import *
label_desc: Use image node_id 5 for calling a global function with example usage: test_webdataset_coding(ray_start_2_cpus, tmp_path) without return types