code: def _read_segment_file(
      data, idx, fi, start, stop, raw_extras, filenames, cals, mult
  ):
      """Read a chunk of raw data."""
      n_samps = raw_extras["n_samps"]
      buf_len = int(raw_extras["max_samp"])
      dtype = raw_extras["dtype_np"]
      dtype_byte = raw_extras["dtype_byte"]
      data_offset = raw_extras["data_offset"]
      stim_channel_idxs = raw_extras["stim_channel_idxs"]
      orig_sel = raw_extras["sel"]
      tal_idx = raw_extras.get("tal_idx", np.empty(0, int))
      subtype = raw_extras["subtype"]
      cal = raw_extras["cal"]
      offsets = raw_extras["offsets"]
      gains = raw_extras["units"]
  
      read_sel = np.concatenate([orig_sel[idx], tal_idx])
      tal_data = []
  
      # only try to read the stim channel if it's not None and it's
      # actually one of the requested channels
      idx_arr = (
          np.arange(idx.start, idx.stop)
          if isinstance(idx, slice)
          else idx
      )
  
      # We could read this one EDF block at a time, which would be this:
      ch_offsets = np.cumsum(
          np.concatenate([[0], n_samps]), dtype=np.int64
      )
      block_start_idx, r_lims, d_lims = _blk_read_lims(
          start, stop, buf_len
      )
      # But to speed it up, we really need to read multiple blocks at once,
      # Otherwise we can end up with e.g. 18,181 chunks for a 20 MB file!
      # Let's do ~10 MB chunks:
      n_per = max(10 * 1024 * 1024 // (ch_offsets[-1] * dtype_byte), 1)
      with open(filenames, "rb", buffering=0) as fid:
          # Extract data
          start_offset = (
              data_offset
              + block_start_idx * ch_offsets[-1] * dtype_byte
          )
  
          # first read everything into the `ones` array. For channels with
          # lower sampling frequency, there will be zeros left at the end of the
          # row. Ignore TAL/annotations channel and only store `orig_sel`
          ones = np.zeros(
              (len(orig_sel), data.shape[-1]), dtype=data.dtype
          )
          # save how many samples have already been read per channel
          n_smp_read = [0 for _ in range(len(orig_sel))]
  
          # read data in chunks
          for ai in range(0, len(r_lims), n_per):
              block_offset = ai * ch_offsets[-1] * dtype_byte
              n_read = min(len(r_lims) - ai, n_per)
              fid.seek(start_offset + block_offset, 0)
              # Read and reshape to (n_chunks_read, ch0_ch1_ch2_ch3...)
              many_chunk = _read_ch(
                  fid,
                  subtype,
                  ch_offsets[-1] * n_read,
                  dtype_byte,
                  dtype,
              ).reshape(n_read, -1)
              r_sidx = r_lims[ai][0]
              r_eidx = (
                  buf_len * (n_read - 1) + r_lims[ai + n_read - 1][1]
              )
  
              # loop over selected channels, ci=channel selection
              for ii, ci in enumerate(read_sel):
                  # This now has size (n_chunks_read, n_samp[ci])
                  ch_data = many_chunk[
                      :, ch_offsets[ci] : ch_offsets[ci + 1]
                  ].copy()
  
                  # annotation channel has to be treated separately
                  if ci in tal_idx:
                      tal_data.append(ch_data)
                      continue
  
                  orig_idx = idx_arr[ii]
                  ch_data = ch_data * cal[orig_idx]
                  ch_data += offsets[orig_idx]
                  ch_data *= gains[orig_idx]
  
                  assert ci == orig_sel[orig_idx]
  
                  if n_samps[ci] != buf_len:
                      if orig_idx in stim_channel_idxs:
                          # Stim channel will be interpolated
                          old = np.linspace(0, 1, n_samps[ci] + 1, True)
                          new = np.linspace(0, 1, buf_len, False)
                          ch_data = np.append(
                              ch_data, np.zeros((len(ch_data), 1)), -1
                          )
                          ch_data = interp1d(
                              old, ch_data, kind="zero", axis=-1
                          )(new)
                  elif orig_idx in stim_channel_idxs:
                      ch_data = np.bitwise_and(
                          ch_data.astype(int), 2**17 - 1
                      )
  
                  one_i = ch_data.ravel()[r_sidx:r_eidx]
  
                  # note how many samples have been read
                  smp_read = n_smp_read[orig_idx]
                  ones[
                      orig_idx, smp_read : smp_read + len(one_i)
                  ] = one_i
                  n_smp_read[orig_idx] += len(one_i)
  
          # skip if no data was requested, ie. only annotations were read
          if sum(n_smp_read) > 0:
              # expected number of samples, equals maximum sfreq
              smp_exp = data.shape[-1]
              assert max(n_smp_read) == smp_exp
  
              # resample data after loading all chunks to prevent edge artifacts
              resampled = False
              for i, smp_read in enumerate(n_smp_read):
                  # nothing read, nothing to resample
                  if smp_read == 0:
                      continue
                  # upsample if n_samples is lower than from highest sfreq
                  if smp_read != smp_exp:
                      assert (
                          ones[i, smp_read:] == 0
                      ).all()  # sanity check
                      ones[i, :] = resample(
                          ones[i, :smp_read].astype(np.float64),
                          smp_exp,
                          smp_read,
                          npad=0,
                          axis=-1,
                      )
                      resampled = True
  
              # give warning if we resampled a subselection
              if resampled and raw_extras["nsamples"] != (stop - start):
                  warn(
                      "Loading an EDF with mixed sampling frequencies and "
                      "preload=False will result in edge artifacts. "
                      "It is recommended to use preload=True."
                      "See also https://github.com/mne-tools/mne-python/issues/10635"
                  )
  
              _mult_cal_one(data[:, :], ones, idx, cals, mult)
  
      if len(tal_data) > 1:
          tal_data = np.concatenate([tal.ravel() for tal in tal_data])
          tal_data = tal_data[np.newaxis, :]
      return tal_data
  
imports: import os
  import re
  from datetime import datetime, timedelta, timezone
  import numpy
  from scipy.interpolate import interp1d
  from ..._fiff.constants import FIFF
  from ..._fiff.meas_info import _empty_info, _unique_channel_names
  from ..._fiff.utils import _blk_read_lims, _mult_cal_one
  from ...annotations import Annotations
  from ...filter import resample
  from ...utils import _validate_type, fill_doc, logger, verbose, warn
  from ..base import BaseRaw, _get_scaling
label_desc: Use image node_id 2 for calling a global function with example usage: _read_segment_file(data, idx, fi, start, stop, raw_extras, filenames, cals, mult) and returns: tal_data