code: def test_contrast():
      from patsy.contrasts import ContrastMatrix, Sum
  
      values = ["a1", "a3", "a1", "a2"]
  
      # No intercept in model, full-rank coding of 'a'
      m = make_matrix(
          {"a": C(values)},
          3,
          [["a"]],
          column_names=["a[a1]", "a[a2]", "a[a3]"],
      )
  
      assert np.allclose(
          m, [[1, 0, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]]
      )
  
      for s in (Sum, Sum()):
          m = make_matrix(
              {"a": C(values, s)},
              3,
              [["a"]],
              column_names=["a[mean]", "a[S.a1]", "a[S.a2]"],
          )
          # Output from R
          assert np.allclose(
              m, [[1, 1, 0], [1, -1, -1], [1, 1, 0], [1, 0, 1]]
          )
  
      m = make_matrix(
          {"a": C(values, Sum(omit=0))},
          3,
          [["a"]],
          column_names=["a[mean]", "a[S.a2]", "a[S.a3]"],
      )
      # Output from R
      assert np.allclose(
          m, [[1, -1, -1], [1, 0, 1], [1, -1, -1], [1, 1, 0]]
      )
  
      # Intercept in model, non-full-rank coding of 'a'
      m = make_matrix(
          {"a": C(values)},
          3,
          [[], ["a"]],
          column_names=["Intercept", "a[T.a2]", "a[T.a3]"],
      )
  
      assert np.allclose(
          m, [[1, 0, 0], [1, 0, 1], [1, 0, 0], [1, 1, 0]]
      )
  
      for s in (Sum, Sum()):
          m = make_matrix(
              {"a": C(values, s)},
              3,
              [[], ["a"]],
              column_names=["Intercept", "a[S.a1]", "a[S.a2]"],
          )
          # Output from R
          assert np.allclose(
              m, [[1, 1, 0], [1, -1, -1], [1, 1, 0], [1, 0, 1]]
          )
  
      m = make_matrix(
          {"a": C(values, Sum(omit=0))},
          3,
          [[], ["a"]],
          column_names=["Intercept", "a[S.a2]", "a[S.a3]"],
      )
      # Output from R
      assert np.allclose(
          m, [[1, -1, -1], [1, 0, 1], [1, -1, -1], [1, 1, 0]]
      )
  
      # Weird ad hoc less-than-full-rank coding of 'a'
      m = make_matrix(
          {"a": C(values, [[7, 12], [2, 13], [8, -1]])},
          2,
          [["a"]],
          column_names=["a[custom0]", "a[custom1]"],
      )
      assert np.allclose(m, [[7, 12], [8, -1], [7, 12], [2, 13]])
  
      m = make_matrix(
          {
              "a": C(
                  values,
                  ContrastMatrix(
                      [[7, 12], [2, 13], [8, -1]], ["[foo]", "[bar]"]
                  ),
              )
          },
          2,
          [["a"]],
          column_names=["a[foo]", "a[bar]"],
      )
      assert np.allclose(m, [[7, 12], [8, -1], [7, 12], [2, 13]])
  
imports: from __future__ import print_function
  import six
  import numpy
  import pytest
  from patsy import PatsyError
  from patsy.util import atleast_2d_column_default, have_pandas, have_pandas_categorical
  from patsy.desc import Term, INTERCEPT
  from patsy.build import *
  from patsy.categorical import C
  from patsy.user_util import balanced, LookupFactor
  from patsy.design_info import DesignMatrix, DesignInfo
label_desc: Use image node_id 20 for calling a global function with example usage: test_contrast() without return types