code: def apply_affine_to_boxes(
      boxes: NdarrayTensor, affine: NdarrayOrTensor
  ) -> NdarrayTensor:
      """
      This function applies affine matrices to the boxes
  
      Args:
          boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be StandardMode
          affine: affine matrix to be applied to the box coordinates, sized (spatial_dims+1,spatial_dims+1)
  
      Returns:
          returned affine transformed boxes, with same data type as ``boxes``, does not share memory with ``boxes``
      """
  
      # convert numpy to tensor if needed
      boxes_t, *_ = convert_data_type(boxes, torch.Tensor)
  
      # some operation does not support torch.float16
      # convert to float32
  
      boxes_t = boxes_t.to(dtype=COMPUTE_DTYPE)
      affine_t, *_ = convert_to_dst_type(src=affine, dst=boxes_t)
  
      spatial_dims = get_spatial_dims(boxes=boxes_t)
  
      # affine transform left top and bottom right points
      # might flipped, thus lt may not be left top any more
      lt: torch.Tensor = _apply_affine_to_points(
          boxes_t[:, :spatial_dims], affine_t, include_shift=True
      )
      rb: torch.Tensor = _apply_affine_to_points(
          boxes_t[:, spatial_dims:], affine_t, include_shift=True
      )
  
      # make sure lt_new is left top, and rb_new is bottom right
      lt_new, _ = torch.min(torch.stack([lt, rb], dim=2), dim=2)
      rb_new, _ = torch.max(torch.stack([lt, rb], dim=2), dim=2)
  
      boxes_t_affine = torch.cat([lt_new, rb_new], dim=1)
  
      # convert tensor back to numpy if needed
      boxes_affine: NdarrayOrTensor
      boxes_affine, *_ = convert_to_dst_type(
          src=boxes_t_affine, dst=boxes
      )
      return boxes_affine
  
imports: from __future__ import annotations
  from collections.abc import Sequence
  from copy import deepcopy
  import numpy
  import torch
  from monai.config.type_definitions import DtypeLike, NdarrayOrTensor, NdarrayTensor
  from monai.data.box_utils import COMPUTE_DTYPE, TO_REMOVE, get_spatial_dims
  from monai.transforms import Resize
  from monai.transforms.utils import create_scale
  from monai.utils import look_up_option
  from monai.utils.misc import ensure_tuple, ensure_tuple_rep
  from monai.utils.type_conversion import convert_data_type, convert_to_dst_type
label_desc: Use image node_id 2 for calling a global function with example usage: apply_affine_to_boxes(boxes, affine) and returns: boxes_affine