code: def map_variables(
      fn: Callable[..., Any],
      mapped_collections: CollectionFilter,
      map_in_fn: Callable[..., Any] = id_fn,
      map_out_fn: Callable[..., Any] = id_fn,
      init: bool = False,
      mutable: bool = False,
      rngs: PRNGSequenceFilter = True,
      variables: CollectionFilter = True,
  ) -> Callable[..., Any]:
      """Map Variables inside a scope.
  
      Args:
        fn: the function to be transformed.
        mapped_collections: the collection(s) to be transformed.
        map_in_fn: creates a view of the target variables.
        map_out_fn: transforms the updated variables in the view after mutation.
        init: If True, variables are initialized before transformation.
        mutable: If True, the mapped variable collections will be mutable.
        rngs: PRNGSequences added to the transformed scope (default: all).
        variables: Additional Variable collections added to the transformed scope.
          Besides those specified by `target` (default: all).
  
      Returns:
        A callable expecting a scope as the first argument.
      """
      is_target_out = mutable or init
  
      def wrapper(
          scope_fn, repack, variable_groups, rng_groups, *args, **kwargs
      ):
          target, variables = variable_groups
          if init:
              scopes = scope_fn((target, variables), rng_groups)
              has_mutable_cols = any(
                  not is_filter_empty(scope.mutable)
                  for scope in jax.tree_util.tree_leaves(scopes)
              )
              if has_mutable_cols:
                  fn(scopes, *args, **kwargs)
                  target, _ = repack(scopes)
                  target = tuple(map_out_fn(x) for x in target)
          target = tuple(map_in_fn(unfreeze(x)) for x in target)
          mfilter = True
          if not is_target_out:
              # mapped collections should not be mutable
              # unless the mapping supports it (by init=True or mutable=True)
              mfilter = subtract_filters(mfilter, mapped_collections)
          scopes = scope_fn(
              (target, variables), rng_groups, mutable_filter=mfilter
          )
          y = fn(scopes, *args, **kwargs)
          out_target, out_vars = repack(scopes)
          if is_target_out:
              out_target = tuple(map_out_fn(x) for x in out_target)
          return y, (out_target, out_vars)
  
      in_vars = (mapped_collections, variables)
      out_vars = (
          in_vars
          if is_target_out
          else (False, subtract_filters(variables, mapped_collections))
      )
      return pack(
          wrapper,
          in_vars,
          out_vars,
          (rngs,),
          enable_kwargs=True,
          name="map_variables",
      )
  
imports: import collections
  import dataclasses
  import functools
  from typing import Any, Callable, Dict, Generic, Iterable, List, Mapping, Optional, Sequence, Tuple, TypeVar, Union
  import warnings
  from flax import traceback_util
  import jax
  from jax import random
  from .None import axes_scan, meta
  from .frozen_dict import freeze, unfreeze
  from .scope import CollectionFilter, DenyList, Filter, PRNGSequenceFilter, Scope, group_collections, in_filter, intersect_filters, is_filter_empty, subtract_filters, union_filters
label_desc: Use image node_id 7 for calling a global function with example usage: map_variables(fn, mapped_collections, map_in_fn, map_out_fn, init, mutable, rngs, variables) and returns: pack, y,  out_target,  out_vars