code: def _generate_ratio_sparsity(
      metrics: _METRICS, target_spaces: _PRUNING_TARGET_SPACES
  ) -> _MASKS:
      # NOTE: smaller metric value means more un-important
      masks = defaultdict(dict)
      for module_name, ts in target_spaces.items():
          for target_name, target_space in ts.items():
              metric = metrics[module_name][target_name]
              min_sparse_ratio = (
                  target_space.min_sparse_ratio
                  if target_space.min_sparse_ratio
                  else 0.0
              )
              max_sparse_ratio = (
                  target_space.max_sparse_ratio
                  if target_space.max_sparse_ratio
                  else 1.0
              )
              sparse_ratio = min(max_sparse_ratio, max(min_sparse_ratio, target_space.sparse_ratio))  # type: ignore
              masks[module_name][target_name] = _ratio_mask(
                  metric, sparse_ratio
              )
      return masks
  
imports: from __future__ import annotations
  from collections import defaultdict
  from functools import reduce
  import heapq
  from typing import Callable, Dict, List, Tuple
  import numpy
  import torch
  from .calculate_metrics import _METRICS
  from ...base.compressor import _PRUNING_TARGET_SPACES
  from ...base.target_space import PruningTargetSpace, TargetType
label_desc: Use image node_id 3 for calling a global function with example usage: _generate_ratio_sparsity(metrics, target_spaces) and returns: masks