class_docstr: Convenience class to encapsulate TRK file format.
  
  Notes
  -----
  TrackVis (so its file format: TRK) considers the streamline coordinate
  (0,0,0) to be in the corner of the voxel whereas NiBabel's streamlines
  internal representation (Voxel space) assumes (0,0,0) to be in the
  center of the voxel.
  
  Thus, streamlines are shifted by half a voxel on load and are shifted
  back on save.
docstr: Loads streamlines from a filename or file-like object.
  
  Parameters
  ----------
  fileobj : string or file-like object
      If string, a filename; otherwise an open file-like object
      pointing to TRK file (and ready to read from the beginning
      of the TRK header). Note that calling this function
      does not change the file position.
  lazy_load : {False, True}, optional
      If True, load streamlines in a lazy manner i.e. they will not be
      kept in memory. Otherwise, load all streamlines in memory.
  
  Returns
  -------
  trk_file : :class:`TrkFile` object
      Returns an object containing tractogram data and header
      information.
  
  Notes
  -----
  Streamlines of the returned tractogram are assumed to be in *RAS*
  and *mm* space where coordinate (0,0,0) refers to the center of the
  voxel.
docstr_tok: ["Loads","streamlines","from","a","filename","or","file-like","object",".","Parameters","--","--","--","--","--","fileobj",":","string","or","file-like","object","If","string",",","a","filename",";","otherwise","an","open","file-like","object","pointing","to","TRK","file","(","and","ready","to","read","from","the","beginning","of","the","TRK","header",")",".","Note","that","calling","this","function","does","not","change","the","file","position",".","lazy_load",":","{","False",",","True","}",",","optional","If","True",",","load","streamlines","in","a","lazy","manner","i.e",".","they","will","not","be","kept","in","memory",".","Otherwise",",","load","all","streamlines","in","memory",".","Returns","--","--","--","-","trk_file",":",":","class",":","`","TrkFile","`","object","Returns","an","object","containing","tractogram","data","and","header","information",".","Notes","--","--","-","Streamlines","of","the","returned","tractogram","are","assumed","to","be","in","*","RAS","*","and","*","mm","*","space","where","coordinate","(","0,0,0",")","refers","to","the","center","of","the","voxel","."]
code: def load(cls, fileobj, lazy_load=False):
      """Loads streamlines from a filename or file-like object.
  
      Parameters
      ----------
      fileobj : string or file-like object
          If string, a filename; otherwise an open file-like object
          pointing to TRK file (and ready to read from the beginning
          of the TRK header). Note that calling this function
          does not change the file position.
      lazy_load : {False, True}, optional
          If True, load streamlines in a lazy manner i.e. they will not be
          kept in memory. Otherwise, load all streamlines in memory.
  
      Returns
      -------
      trk_file : :class:`TrkFile` object
          Returns an object containing tractogram data and header
          information.
  
      Notes
      -----
      Streamlines of the returned tractogram are assumed to be in *RAS*
      and *mm* space where coordinate (0,0,0) refers to the center of the
      voxel.
      """
      hdr = cls._read_header(fileobj)
  
      # Find scalars and properties name
      data_per_point_slice = {}
      if hdr[Field.NB_SCALARS_PER_POINT] > 0:
          cpt = 0
          for scalar_field in hdr["scalar_name"]:
              scalar_name, nb_scalars = decode_value_from_name(
                  scalar_field
              )
  
              if nb_scalars == 0:
                  continue
  
              slice_obj = slice(cpt, cpt + nb_scalars)
              data_per_point_slice[scalar_name] = slice_obj
              cpt += nb_scalars
  
          if cpt < hdr[Field.NB_SCALARS_PER_POINT]:
              slice_obj = slice(cpt, hdr[Field.NB_SCALARS_PER_POINT])
              data_per_point_slice["scalars"] = slice_obj
  
      data_per_streamline_slice = {}
      if hdr[Field.NB_PROPERTIES_PER_STREAMLINE] > 0:
          cpt = 0
          for property_field in hdr["property_name"]:
              results = decode_value_from_name(property_field)
              property_name, nb_properties = results
  
              if nb_properties == 0:
                  continue
  
              slice_obj = slice(cpt, cpt + nb_properties)
              data_per_streamline_slice[property_name] = slice_obj
              cpt += nb_properties
  
          if cpt < hdr[Field.NB_PROPERTIES_PER_STREAMLINE]:
              slice_obj = slice(
                  cpt, hdr[Field.NB_PROPERTIES_PER_STREAMLINE]
              )
              data_per_streamline_slice["properties"] = slice_obj
  
      if lazy_load:
  
          def _read():
              for pts, scals, props in cls._read(fileobj, hdr):
                  items = data_per_point_slice.items()
                  data_for_points = {k: scals[:, v] for k, v in items}
                  items = data_per_streamline_slice.items()
                  data_for_streamline = {k: props[v] for k, v in items}
                  yield TractogramItem(
                      pts, data_for_streamline, data_for_points
                  )
  
          tractogram = LazyTractogram.from_data_func(_read)
  
      else:
          # Speed up loading by guessing a suitable buffer size.
          with Opener(fileobj) as f:
              old_file_position = f.tell()
              f.seek(0, os.SEEK_END)
              size = f.tell()
              f.seek(old_file_position, os.SEEK_SET)
  
          # Buffer size is in mega bytes.
          mbytes = size // (1024 * 1024)
          sizes = [mbytes, 4, 4]
          if hdr["nb_scalars_per_point"] > 0:
              sizes = [mbytes // 2, mbytes // 2, 4]
  
          trk_reader = cls._read(fileobj, hdr)
          arr_seqs = create_arraysequences_from_generator(
              trk_reader, n=3, buffer_sizes=sizes
          )
          streamlines, scalars, properties = arr_seqs
          properties = np.asarray(properties)  # Actually a 2d array.
          tractogram = Tractogram(streamlines)
  
          for name, slice_ in data_per_point_slice.items():
              tractogram.data_per_point[name] = scalars[:, slice_]
  
          for name, slice_ in data_per_streamline_slice.items():
              tractogram.data_per_streamline[name] = properties[
                  :, slice_
              ]
  
      tractogram.affine_to_rasmm = get_affine_trackvis_to_rasmm(hdr)
      tractogram = tractogram.to_world()
  
      return cls(tractogram, header=hdr)
  
imports: import os
  import string
  import struct
  import warnings
  import numpy
  import nibabel
  from nibabel.openers import Opener
  from nibabel.orientations import aff2axcodes, axcodes2ornt
  from nibabel.volumeutils import endian_codes, native_code, swapped_code
  from .array_sequence import create_arraysequences_from_generator
  from .header import Field
  from .tractogram import LazyTractogram, Tractogram, TractogramItem
  from .tractogram_file import DataError, HeaderError, HeaderWarning, TractogramFile
  from .utils import peek_next
label_desc: Use image node_id 5 for calling the TrkFile obj's underlying member method code with example usage: obj.load(cls, fileobj, lazy_load) and returns: cls