code: def _get_compression(filepath, compression):
      """
      Get the compression method for ``filepath``, depending on its file extension
      and the value of ``compression``. Also validate the given values.
      """
      # user has specified "no compression"
      if compression is None:
          return None
      # user wants us to infer compression from filepath
      elif compression == "infer":
          ext = filepath.suffix
          try:
              return _ext_to_compression[ext.lower()]
          except KeyError:
              return None
      # user has specified compression; validate it
      elif compression in _ext_to_compression.values():
          return compression
      else:
          valid_values = [None, "infer"] + sorted(
              _ext_to_compression.values()
          )
          raise ValueError(
              errors_.value_invalid_msg(
                  "compression", compression, valid_values
              )
          )
  
imports: from __future__ import annotations
  import bz2
  import gzip
  import io
  import itertools
  import logging
  import lzma
  import os
  import re
  import shutil
  import tarfile
  import urllib
  import zipfile
  from typing import IO, Iterable, Literal, Optional
  from cytoolz import itertoolz
  from ..None import constants
  from ..None import errors
  from ..None import types, utils
  from .http import write_http_stream
label_desc: Use image node_id 2 for calling a global function with example usage: _get_compression(filepath, compression) and returns: None, _ext_to_compression, compression, None