code: def write_sparse_matrix(
      data: sp.csc_matrix | sp.csr_matrix,
      filepath: types.PathLike,
      *,
      compressed: bool = True,
      make_dirs: bool = False,
  ) -> None:
      """
      Write sparse matrix ``data`` to disk at ``filepath``, optionally compressed,
      into a single ``.npz`` file.
  
      Args:
          data
          filepath: Path to file on disk to which data will be written. If ``filepath``
              does not end in ``.npz``, that extension is automatically appended to the name.
          compressed: If True, save arrays into a single file in compressed numpy binary format.
          make_dirs: If True, automatically create (sub)directories
              if not already present in order to write ``filepath``.
  
      See Also:
          https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.io.html#numpy-binary-files-npy-npz
      """
      if not isinstance(data, (sp.csc_matrix, sp.csr_matrix)):
          raise TypeError(
              errors.type_invalid_msg(
                  "data",
                  type(data),
                  Union[sp.csc_matrix, sp.csr_matrix],
              )
          )
      filepath = utils.to_path(filepath).resolve()
      if make_dirs is True:
          io_utils._make_dirs(filepath, "w")
      if compressed is True:
          np.savez_compressed(
              str(filepath),
              data=data.data,
              indices=data.indices,
              indptr=data.indptr,
              shape=data.shape,
          )
      else:
          np.savez(
              str(filepath),
              data=data.data,
              indices=data.indices,
              indptr=data.indptr,
              shape=data.shape,
          )
  
imports: from __future__ import annotations
  from typing import Union
  import numpy
  import scipy.sparse
  from ..None import errors, types, utils
  from .None import utils
label_desc: Use image node_id 2 for calling a global function with example usage: write_sparse_matrix(data, filepath) without return types