docstr: Create a binary chunk from all the binarized items.
docstr_tok: ["Create","a","binary","chunk","from","all","the","binarized","items","."]
code: def _create_chunk(
      self, filename: str, on_done: bool = False
  ) -> bytes:
      """Create a binary chunk from all the binarized items."""
      items = []
  
      if on_done:
          indices = sorted(self._serialized_items.keys())
          for i in range(len(indices) - 1):
              assert indices[i] == indices[i + 1] - 1, indices
          items = [
              self._serialized_items.pop(index) for index in indices
          ]
      else:
          assert self._max_index is not None, (
              self._max_index,
              self._min_index,
          )
          assert self._min_index is not None, (
              self._max_index,
              self._min_index,
          )
          if self._max_index == self._min_index:
              # A single item is larger than the target chunk size; allow the chunk to be bigger than the target size
              items.append(self._serialized_items.pop(self._max_index))
          items.extend(
              self._serialized_items.pop(index)
              for index in range(self._min_index, self._max_index)
          )
  
      if len(items) == 0:
          raise RuntimeError(
              "The items shouldn't have an empty length. Something went wrong."
              f" Found {self._pretty_serialized_items()} with boundaries: {self._min_index}, {self._max_index}."
          )
  
      num_items = np.uint32(len(items))
      sizes = list(map(len, items))
      offsets = np.array([0] + sizes).cumsum().astype(np.uint32)
      offsets += len(num_items.tobytes()) + len(offsets.tobytes())
      sample_data = b"".join([item.data for item in items])
      data = num_items.tobytes() + offsets.tobytes() + sample_data
  
      current_chunk_bytes = sum([item.bytes for item in items])
  
      if self._chunk_bytes and current_chunk_bytes > self._chunk_bytes:
          warnings.warn(
              f"An item was larger than the target chunk size ({_human_readable_bytes(self._chunk_bytes)})."
              f" The current chunk will be {_human_readable_bytes(current_chunk_bytes)} in size.",
              UserWarning,
          )
  
      if self._chunk_size:
          assert num_items.item() <= self._chunk_size
  
      dim: Optional[int] = None
      if items[0].dim:
          dim = sum(
              [
                  item.dim if item.dim is not None else 0
                  for item in items
              ]
          )
  
      chunk_info = {
          "chunk_bytes": current_chunk_bytes,
          "chunk_size": num_items.item(),
          "filename": filename,
          "dim": dim,
      }
  
      self._chunks_info.append(chunk_info)
  
      return data
  
imports: import json
  import os
  import warnings
  from dataclasses import dataclass
  from time import sleep
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from lightning.data.streaming.compression import _COMPRESSORS, Compressor
  from lightning.data.streaming.constants import _INDEX_FILENAME, _TORCH_GREATER_EQUAL_2_1_0
  from lightning.data.streaming.serializers import Serializer, _get_serializers
  from lightning.data.utilities.env import _DistributedEnv, _WorkerEnv
  from lightning.data.utilities.format import _convert_bytes_to_int, _human_readable_bytes
label_desc: Use image node_id 7 for calling the BinaryWriter obj's underlying member method code with example usage: obj._create_chunk(filename, on_done) and returns: data