code: def as_ndarray(arr, copy=False, dtype=None, order="K"):
      """Convert to numpy.ndarray starting with an arbitrary array, .
  
      In the case of a memmap array, a copy is automatically made to break the
      link with the underlying file (whatever the value of the "copy" keyword).
  
      The purpose of this function is mainly to get rid of memmap objects, but
      it can be used for other purposes. In particular, combining copying and
      casting can lead to performance improvements in some cases, by avoiding
      unnecessary copies.
  
      If not specified, input array order is preserved, in all cases, even when
      a copy is requested.
  
      Caveat: this function does not copy during bool to/from 1-byte dtype
      conversions. This can lead to some surprising results in some rare cases.
  
      Example:
  
      >>> import numpy
      >>> a = numpy.asarray([0, 1, 2], dtype=numpy.int8)
      >>> b = as_ndarray(a, dtype=bool)
      >>> b
      array([False,  True,  True])
      >>> c = as_ndarray(b, dtype=numpy.int8)
      >>> c
      array([0, 1, 2], dtype=int8)
  
      The usually expected result for the last line would be array([0, 1, 1])
      because True evaluates to 1. Since there is no copy made here, the original
      array is recovered.
  
      Parameters
      ----------
      arr: array-like
          input array. Any value accepted by numpy.asarray is valid.
  
      copy: bool
          if True, force a copy of the array. Always True when arr is a memmap.
  
      dtype: any numpy dtype
          dtype of the returned array. Performing copy and type conversion at the
          same time can in some cases avoid an additional copy.
  
      order: :obj:`str`, default='K'
          gives the order of the returned array.
          Valid values are: "C", "F", "A", "K", None.
          See ndarray.copy() for more information.
  
      Returns
      -------
      ret: numpy.ndarray
          Numpy array containing the same data as arr, always of class
          numpy.ndarray, and with no link to any underlying file.
      """
      # This function should work on numpy 1.3
      # in this version, astype() and copy() have no "order" keyword.
      # and asarray() does not accept the "K" and "A" values for order.
  
      # numpy.asarray never copies a subclass of numpy.ndarray (even for
      #     memmaps) when dtype is unchanged.
      # .astype() always copies
  
      if order not in ("C", "F", "A", "K", None):
          raise ValueError(f"Invalid value for 'order': {str(order)}")
  
      if isinstance(arr, np.memmap):
          if dtype is None:
              if order in ("K", "A", None):
                  ret = np.array(np.asarray(arr), copy=True)
              else:
                  ret = np.array(
                      np.asarray(arr), copy=True, order=order
                  )
          else:
              if order in ("K", "A", None):
                  # always copy (even when dtype does not change)
                  ret = np.asarray(arr).astype(dtype)
              else:
                  # First load data from disk without changing order
                  # Changing order while reading through a memmap is incredibly
                  # inefficient.
                  ret = np.array(arr, copy=True)
                  ret = _asarray(ret, dtype=dtype, order=order)
  
      elif isinstance(arr, np.ndarray):
          ret = _asarray(arr, dtype=dtype, order=order)
          # In the present cas, np.may_share_memory result is always reliable.
          if np.may_share_memory(ret, arr) and copy:
              # order-preserving copy
              if ret.flags["F_CONTIGUOUS"]:
                  ret = ret.T.copy().T
              else:
                  ret = ret.copy()
  
      elif isinstance(arr, (list, tuple)):
          if order in ("A", "K"):
              ret = np.asarray(arr, dtype=dtype)
          else:
              ret = np.asarray(arr, dtype=dtype, order=order)
  
      else:
          raise ValueError(f"Type not handled: {arr.__class__}")
  
      return ret
  
imports: import csv
  import numpy
  from .helpers import stringify_path
label_desc: Use image node_id 2 for calling a global function with example usage: as_ndarray(arr, copy, dtype, order) and returns: ret