code: def save_tractogram(sft, filename, bbox_valid_check=True):
      """Save the stateful tractogram in any format (trk/tck/vtk/vtp/fib/dpy)
  
      Parameters
      ----------
      sft : StatefulTractogram
          The stateful tractogram to save
      filename : string
          Filename with valid extension
      bbox_valid_check : bool
          Verification for negative voxel coordinates or values above the
          volume dimensions. Default is True, to enforce valid file.
  
      Returns
      -------
      output : bool
          True if the saving operation was successful
      """
  
      _, extension = os.path.splitext(filename)
      if extension not in [
          ".trk",
          ".tck",
          ".trx",
          ".vtk",
          ".vtp",
          ".fib",
          ".dpy",
      ]:
          raise TypeError(
              "Output filename is not one of the supported format."
          )
  
      if bbox_valid_check and not sft.is_bbox_in_vox_valid():
          raise ValueError(
              "Bounding box is not valid in voxel space, cannot "
              "load a valid file if some coordinates are invalid.\n"
              "Please set bbox_valid_check to False and then use "
              "the function remove_invalid_streamlines to discard "
              "invalid streamlines."
          )
  
      old_space = deepcopy(sft.space)
      old_origin = deepcopy(sft.origin)
  
      sft.to_rasmm()
      sft.to_center()
  
      timer = time.time()
      if extension in [".trk", ".tck"]:
          tractogram_type = detect_format(filename)
          header = create_tractogram_header(
              tractogram_type, *sft.space_attributes
          )
          new_tractogram = Tractogram(
              sft.streamlines, affine_to_rasmm=np.eye(4)
          )
  
          if extension == ".trk":
              new_tractogram.data_per_point = sft.data_per_point
              new_tractogram.data_per_streamline = (
                  sft.data_per_streamline
              )
  
          fileobj = tractogram_type(new_tractogram, header=header)
          nib.streamlines.save(fileobj, filename)
  
      elif extension in [".vtk", ".vtp", ".fib"]:
          binary = extension in [".vtk", ".fib"]
          save_vtk_streamlines(sft.streamlines, filename, binary=binary)
      elif extension in [".dpy"]:
          dpy_obj = Dpy(filename, mode="w")
          dpy_obj.write_tracks(sft.streamlines)
          dpy_obj.close()
      elif extension in [".trx"]:
          trx = tmm.TrxFile.from_sft(sft)
          tmm.save(trx, filename)
          trx.close()
  
      logging.debug(
          "Save %s with %s streamlines in %s seconds.",
          filename,
          len(sft),
          round(time.time() - timer, 3),
      )
  
      sft.to_space(old_space)
      sft.to_origin(old_origin)
  
      return True
  
file_path: dipy/dipy/io/streamline.py
imports: from copy import deepcopy
  import logging
  import os
  import time
  import nibabel
  from nibabel.streamlines import detect_format
  from nibabel.streamlines.tractogram import Tractogram
  import numpy
  import trx.trx_file_memmap
  from dipy.io.stateful_tractogram import Origin, Space, StatefulTractogram
  from dipy.io.vtk import save_vtk_streamlines, load_vtk_streamlines
  from dipy.io.dpy import Dpy
  from dipy.io.utils import create_tractogram_header, is_header_compatible
label_desc: Use image node_id 1 for calling a global function with example usage: save_tractogram(sft, filename, bbox_valid_check) and returns: True