code: def as_volume_img(obj, copy=True, squeeze=True, world_space=None):
      """Convert the input to a VolumeImg.
  
      Parameters
      ----------
      obj : filename, pynifti or brifti object, or volume dataset.
          Input object, in any form that can be converted to a
          VolumeImg. This includes Nifti filenames, pynifti or brifti
          objects, or other volumetric dataset objects.
      copy: boolean, optional
          If copy is True, the data and affine arrays are copied,
          elsewhere a view is taken.
      squeeze: boolean, optional
          If squeeze is True, the data array is squeeze on for
          dimensions above 3.
      world_space: string or None, optional
          An optional specification of the world space, to override
          that given by the image.
  
      Returns
      -------
      volume_img: VolumeImg object
          A VolumeImg object containing the data. The metadata is
          kept as much as possible in the metadata attribute.
  
      Notes
      ------
      The world space might not be correctly defined by the input
      object (in particular, when loading data from disk). In this
      case, you can correct it manually using the world_space keyword
      argument.
  
      For pynifti objects, the data is transposed.
      """
      if hasattr(obj, "as_volume_img"):
          obj = obj.as_volume_img(copy=copy)
          if copy:
              obj = obj.__copy__()
          return obj
  
      elif isinstance(obj, str):
          if not os.path.exists(obj):
              raise ValueError(f"The file '{obj}' cannot be found")
          obj = nib.load(obj)
          copy = False
  
      if isinstance(obj, SpatialImage):
          data = obj.get_fdata()
          affine = get_affine(obj)
          header = dict(get_header(obj))
          fname = obj.file_map["image"].filename
          if fname:
              header["filename"] = fname
      elif (
          hasattr(obj, "data")
          and hasattr(obj, "sform")
          and hasattr(obj, "getVolumeExtent")
      ):
          # Duck-types to a pynifti object
          data = obj.data.T
          affine = obj.sform
          header = obj.header
          filename = obj.getFilename()
          if filename != "":
              header["filename"] = filename
      else:
          raise ValueError(
              "Invalid type ({}) passed in: cannot convert {} to "
              "VolumeImg".format(type(obj), obj)
          )
  
      if world_space is None and header.get("sform_code", 0) == 4:
          world_space = "mni152"
  
      data = np.asanyarray(data)
      affine = np.asanyarray(affine)
      if copy:
          data = data.copy()
          affine = affine.copy()
  
      if squeeze:
          # Squeeze the dimensions above 3
          shape = [
              val
              for index, val in enumerate(data.shape)
              if val != 1 or index < 3
          ]
          data = np.reshape(data, shape)
  
      return VolumeImg(data, affine, world_space, metadata=header)
  
file_path: nipy/nipy/labs/datasets/converters.py
imports: import os
  import nibabel
  import numpy
  from nibabel.spatialimages import SpatialImage
  from nipy.io.nibcompat import get_affine, get_header
  from .volumes.volume_img import VolumeImg
label_desc: Use image node_id 1 for calling a global function with example usage: as_volume_img(obj, copy, squeeze, world_space) and returns: VolumeImg, obj