class_docstr: RawNeuralynx class.
docstr: Read a chunk of raw data.
docstr_tok: ["Read","a","chunk","of","raw","data","."]
code: def _read_segment_file(self, data, idx, fi, start, stop, cals, mult):
      """Read a chunk of raw data."""
      from neo import Segment
      from neo.io import NeuralynxIO
  
      # quantities is a dependency of neo so we are guaranteed it exists
      from quantities import Hz
  
      nlx_reader = NeuralynxIO(
          dirname=self._filenames[fi],
          exclude_filename=self._raw_extras[0]["exclude_fnames"],
      )
      neo_block = nlx_reader.read(lazy=True)
  
      # check that every segment has 1 associated neo.AnalogSignal() object
      # (not sure what multiple analogsignals per neo.Segment would mean)
      assert sum(
          [
              len(segment.analogsignals)
              for segment in neo_block[0].segments
          ]
      ) == len(neo_block[0].segments)
  
      segment_sizes = self._raw_extras[fi]["segment_sizes"]
  
      # construct a (n_segments, 2) array of the first and last
      # sample index for each segment relative to the start of the recording
      seg_starts = [0]  # first chunk starts at sample 0
      seg_stops = [segment_sizes[0] - 1]
      for i in range(1, len(segment_sizes)):
          ons_new = (
              seg_stops[i - 1] + 1
          )  # current chunk starts one sample after the previous one
          seg_starts.append(ons_new)
          off_new = (
              seg_stops[i - 1] + segment_sizes[i]
          )  # the last sample is len(chunk) samples after the previous ended
          seg_stops.append(off_new)
  
      start_stop_samples = np.stack(
          [np.array(seg_starts), np.array(seg_stops)]
      ).T
  
      first_seg = self._raw_extras[0]["smp2seg"][
          start
      ]  # segment containing start sample
      last_seg = self._raw_extras[0]["smp2seg"][
          stop - 1
      ]  # segment containing stop sample
  
      # select all segments between the one that contains the start sample
      # and the one that contains the stop sample
      sel_samples_global = start_stop_samples[
          first_seg : last_seg + 1, :
      ]
  
      # express end samples relative to segment onsets
      # to be used for slicing the arrays below
      sel_samples_local = sel_samples_global.copy()
      sel_samples_local[0:-1, 1] = (
          sel_samples_global[0:-1, 1] - sel_samples_global[0:-1, 0]
      )
      sel_samples_local[
          1::, 0
      ] = 0  # now set the start sample for all segments after the first to 0
  
      sel_samples_local[0, 0] = (
          start - sel_samples_global[0, 0]
      )  # express start sample relative to segment onset
      sel_samples_local[-1, -1] = (stop - 1) - sel_samples_global[
          -1, 0
      ]  # express stop sample relative to segment onset
  
      # array containing Segments
      segments_arr = np.array(neo_block[0].segments, dtype=object)
  
      # if gaps were detected, correctly insert gap Segments in between valid Segments
      gap_samples = self._raw_extras[fi]["seg_gap_dict"]["gap_n_samps"]
      gap_segments = [
          Segment(f"gap-{i}") for i in range(len(gap_samples))
      ]
  
      # create AnalogSignal objects representing gap data filled with 0's
      sfreq = nlx_reader.get_signal_sampling_rate()
      n_chans = (
          np.arange(idx.start, idx.stop, idx.step).size
          if type(idx) is slice
          else len(
              idx
          )  # idx can be a slice or an np.array so check both
      )
  
      for seg, n in zip(gap_segments, gap_samples):
          asig = AnalogSignalGap(
              signal=np.zeros((n, n_chans)),
              units="uV",
              sampling_rate=sfreq * Hz,
          )
          seg.analogsignals.append(asig)
  
      n_total_segments = len(neo_block[0].segments + gap_segments)
      segments_arr = np.zeros((n_total_segments,), dtype=object)
  
      # insert inferred gap segments at the right place in between valid segments
      isgap = self._raw_extras[0]["seg_gap_dict"]["isgap"]
      segments_arr[~isgap] = neo_block[0].segments
      segments_arr[isgap] = gap_segments
  
      # now load data from selected segments/channels via
      # neo.Segment.AnalogSignal.load() or AnalogSignalGap.load()
      all_data = np.concatenate(
          [
              signal.load(channel_indexes=idx).magnitude[
                  samples[0] : samples[-1] + 1, :
              ]
              for seg, samples in zip(
                  segments_arr[first_seg : last_seg + 1],
                  sel_samples_local,
              )
              for signal in seg.analogsignals
          ]
      ).T
  
      all_data *= 1e-6  # Convert uV to V
      n_channels = len(nlx_reader.header["signal_channels"]["name"])
      block = np.zeros((n_channels, stop - start), dtype=data.dtype)
      block[idx] = all_data  # shape = (n_channels, n_samples)
  
      # Then store the result where it needs to go
      _mult_cal_one(data, block, idx, cals, mult)
  
file_path: mne-python/mne/io/neuralynx/neuralynx.py
imports: import glob
  import os
  import numpy
  from ..._fiff.meas_info import create_info
  from ..._fiff.utils import _mult_cal_one
  from ...annotations import Annotations
  from ...utils import _check_fname, _soft_import, fill_doc, logger, verbose
  from ..base import BaseRaw
label_desc: Use image node_id 2 for calling the RawNeuralynx obj's underlying member method code with example usage: obj._read_segment_file(data, idx, fi, start, stop, cals, mult) without return types