code: def test_generic_distribution_to_funsor(case):
      HIGHER_ORDER_DISTS = [
          backend_dist.Independent,
          backend_dist.TransformedDistribution,
      ] + (
          [backend_dist.torch_distribution.ExpandedDistribution]
          if get_backend() == "torch"
          else [backend_dist.ExpandedDistribution]
      )
  
      raw_dist = case.get_dist()
      expected_value_domain = case.expected_value_domain
  
      dim_to_name, name_to_dim = _default_dim_to_name(
          raw_dist.batch_shape
      )
      with eager_no_dists:
          with xfail_if_not_implemented(match="try upgrading backend"):
              funsor_dist = to_funsor(
                  raw_dist, output=funsor.Real, dim_to_name=dim_to_name
              )
      assert funsor_dist.inputs["value"] == expected_value_domain
  
      while isinstance(funsor_dist, funsor.cnf.Contraction):
          funsor_dist = [
              term
              for term in funsor_dist.terms
              if isinstance(
                  term,
                  (
                      funsor.distribution.Distribution,
                      funsor.terms.Independent,
                  ),
              )
          ][0]
  
      actual_dist = to_data(funsor_dist, name_to_dim=name_to_dim)
  
      assert isinstance(actual_dist, backend_dist.Distribution)
      orig_raw_dist = raw_dist
      while type(raw_dist) in HIGHER_ORDER_DISTS:
          raw_dist = raw_dist.base_dist
          actual_dist = (
              actual_dist.base_dist
              if type(actual_dist) in HIGHER_ORDER_DISTS
              else actual_dist
          )
          assert isinstance(actual_dist, backend_dist.Distribution)
      assert issubclass(
          type(actual_dist), type(raw_dist)
      )  # subclass to handle wrappers
  
      if "ExpandedDistribution" in case.raw_dist:
          assert orig_raw_dist.batch_shape == actual_dist.batch_shape
          return
  
      for param_name, _ in case.raw_params:
          assert hasattr(raw_dist, param_name)
          assert_close(
              getattr(actual_dist, param_name),
              getattr(raw_dist, param_name),
          )
  
imports: import re
  from collections import OrderedDict, namedtuple
  from importlib import import_module
  import numpy
  import pytest
  import funsor
  import funsor.ops
  from funsor.distribution import BACKEND_TO_DISTRIBUTIONS_BACKEND
  from funsor.interpretations import CallableInterpretation, eager, lazy, normalize, reflect
  from funsor.terms import to_data, to_funsor
  from funsor.testing import assert_close, check_funsor, excludes_backend, rand, randint, randn, random_scale_tril, xfail_if_not_found, xfail_if_not_implemented, xfail_param
  from funsor.util import get_backend
label_desc: Use image node_id 3 for calling a global function with example usage: test_generic_distribution_to_funsor(case) without return types