code: def validate_dataset_compression_arg(
      dataset_compression: Mapping[str, Any], memory_limit: int
  ) -> DatasetCompressionSpec:
      """Validates and return a correct dataset_compression argument
  
      The returned value can be safely used with `reduce_dataset_size_if_too_large`.
  
      Parameters
      ----------
      dataset_compression: Mapping[str, Any]
          The arg to validate
  
      Returns
      -------
      DatasetCompressionSpec
          The validated and correct dataset compression spec
      """
      if isinstance(dataset_compression, Mapping):
          # Fill with defaults if they don't exist
          dataset_compression = {
              **default_dataset_compression_arg,
              **dataset_compression,
          }
  
          parsed_keys = set(dataset_compression.keys())
          default_keys = set(default_dataset_compression_arg.keys())
  
          # Must contain known keys
          if parsed_keys != default_keys:
              raise ValueError(
                  f"Unknown key(s) in ``dataset_compression``, {parsed_keys}."
                  f"\nPossible keys are {default_keys}"
              )
  
          memory_allocation = dataset_compression["memory_allocation"]
  
          # "memory_allocation" must be float or int
          if not (
              isinstance(memory_allocation, float)
              or isinstance(memory_allocation, int)
          ):
              raise ValueError(
                  "key 'memory_allocation' must be an `int` or `float`"
                  f"\ntype = {memory_allocation}"
                  f"\ndataset_compression = {dataset_compression}"
              )
  
          # "memory_allocation" must be in (0,1) if float
          if isinstance(memory_allocation, float) and not (
              0.0 < memory_allocation < 1.0
          ):
              raise ValueError(
                  "key 'memory_allocation' if float must be in (0, 1)"
                  f"\nmemory_allocation = {memory_allocation}"
                  f"\ndataset_compression = {dataset_compression}"
              )
  
          # "memory_allocation" if absolute, should be > 0 and < memory_limit
          if isinstance(memory_allocation, int) and not (
              0 < memory_allocation < memory_limit
          ):
              raise ValueError(
                  f"key 'memory_allocation' if int must be in (0, {memory_limit})"
                  f"\nmemory_allocation = {memory_allocation}"
                  f"\ndataset_compression = {dataset_compression}"
              )
  
          # "methods" must be non-empty sequence
          if (
              not isinstance(dataset_compression["methods"], Sequence)
              or len(dataset_compression["methods"]) <= 0
          ):
              raise ValueError(
                  "key 'methods' must be a non-empty list"
                  f"\nmethods = {dataset_compression['methods']}"
                  f"\ndataset_compression = {dataset_compression}"
              )
  
          # "methods" must contain known methods
          if any(
              method
              not in cast(
                  Sequence, default_dataset_compression_arg["methods"]
              )  # mypy
              for method in dataset_compression["methods"]
          ):
              valid_methods = default_dataset_compression_arg["methods"]
              raise ValueError(
                  f"key 'methods' can only contain {valid_methods}"
                  f"\nmethods = {dataset_compression['methods']}"
                  f"\ndataset_compression = {dataset_compression}"
              )
  
          return cast(DatasetCompressionSpec, dataset_compression)
      else:
          raise ValueError(
              f"Unknown type for `dataset_compression` {type(dataset_compression)}"
              f"\ndataset_compression = {dataset_compression}"
          )
  
imports: from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple, Type, Union, cast
  import warnings
  import numpy
  import pandas
  from scipy.sparse import spmatrix
  from sklearn.model_selection import train_test_split
  from autosklearn.evaluation.splitter import CustomStratifiedShuffleSplit
label_desc: Use image node_id 1 for calling a global function with example usage: validate_dataset_compression_arg(dataset_compression, memory_limit) and returns: cast