code: def _read_matrix(fid, tag, shape, rlims):
      """Read a matrix (dense or sparse) tag."""
      # This should be easy to implement (see _frombuffer_rows)
      # if we need it, but for now, it's not...
      if shape is not None or rlims is not None:
          raise ValueError(
              "Row reading not implemented for matrices yet"
          )
  
      matrix_coding, matrix_type, bit, dtype = _matrix_info(tag)
  
      pos = tag.pos + 16
      fid.seek(pos + tag.size - 4, 0)
      if matrix_coding == "dense":
          # Find dimensions and return to the beginning of tag data
          ndim = int(np.frombuffer(fid.read(4), dtype=">i4").item())
          fid.seek(-(ndim + 1) * 4, 1)
          dims = np.frombuffer(fid.read(4 * ndim), dtype=">i4")[::-1]
          #
          # Back to where the data start
          #
          fid.seek(pos, 0)
  
          if ndim > 3:
              raise Exception(
                  "Only 2 or 3-dimensional matrices are supported at this time"
              )
  
          data = fid.read(int(bit * dims.prod()))
          data = np.frombuffer(data, dtype=dtype)
          # Note: we need the non-conjugate transpose here
          if matrix_type == FIFF.FIFFT_COMPLEX_FLOAT:
              data = data.view(">c8")
          elif matrix_type == FIFF.FIFFT_COMPLEX_DOUBLE:
              data = data.view(">c16")
          data.shape = dims
      else:
          # Find dimensions and return to the beginning of tag data
          ndim = int(np.frombuffer(fid.read(4), dtype=">i4").item())
          fid.seek(-(ndim + 2) * 4, 1)
          dims = np.frombuffer(fid.read(4 * (ndim + 1)), dtype=">i4")
          if ndim != 2:
              raise Exception(
                  "Only two-dimensional matrices are supported at this time"
              )
  
          # Back to where the data start
          fid.seek(pos, 0)
          nnz = int(dims[0])
          nrow = int(dims[1])
          ncol = int(dims[2])
          # We need to make a copy so that we can own the data, otherwise we get:
          #     _sparsetools.csr_sort_indices(len(self.indptr) - 1, self.indptr,
          # E   ValueError: WRITEBACKIFCOPY base is read-only
          data = np.frombuffer(fid.read(bit * nnz), dtype=dtype).copy()
          shape = (dims[1], dims[2])
          if matrix_coding == "sparse CCS":
              tmp_indices = fid.read(4 * nnz)
              indices = np.frombuffer(tmp_indices, dtype=">i4")
              tmp_ptr = fid.read(4 * (ncol + 1))
              indptr = np.frombuffer(tmp_ptr, dtype=">i4")
              if indptr[-1] > len(indices) or np.any(indptr < 0):
                  # There was a bug in MNE-C that caused some data to be
                  # stored without byte swapping
                  indices = np.concatenate(
                      (
                          np.frombuffer(
                              tmp_indices[: 4 * (nrow + 1)], dtype=">i4"
                          ),
                          np.frombuffer(
                              tmp_indices[4 * (nrow + 1) :], dtype="<i4"
                          ),
                      )
                  )
                  indptr = np.frombuffer(tmp_ptr, dtype="<i4")
              data = csc_matrix((data, indices, indptr), shape=shape)
          else:
              assert matrix_coding == "sparse RCS", matrix_coding
              tmp_indices = fid.read(4 * nnz)
              indices = np.frombuffer(tmp_indices, dtype=">i4")
              tmp_ptr = fid.read(4 * (nrow + 1))
              indptr = np.frombuffer(tmp_ptr, dtype=">i4")
              if indptr[-1] > len(indices) or np.any(indptr < 0):
                  # There was a bug in MNE-C that caused some data to be
                  # stored without byte swapping
                  indices = np.concatenate(
                      (
                          np.frombuffer(
                              tmp_indices[: 4 * (ncol + 1)], dtype=">i4"
                          ),
                          np.frombuffer(
                              tmp_indices[4 * (ncol + 1) :], dtype="<i4"
                          ),
                      )
                  )
                  indptr = np.frombuffer(tmp_ptr, dtype="<i4")
              data = csr_matrix((data, indices, indptr), shape=shape)
      return data
  
imports: import html
  import re
  import struct
  from dataclasses import dataclass
  from functools import partial
  from typing import Any
  import numpy
  from scipy.sparse import csc_matrix, csr_matrix
  from ..utils import _check_option, warn
  from ..utils.numerics import _julian_to_cal
  from .constants import FIFF, _ch_coil_type_named, _ch_kind_named, _ch_unit_mul_named, _ch_unit_named, _dig_cardinal_named, _dig_kind_named
label_desc: Use image node_id 6 for calling a global function with example usage: _read_matrix(fid, tag, shape, rlims) and returns: data