code: def test_event_design():
      # Test event design helper function
      # An event design with one event type
      onsets = np.array([0, 20, 40, 60])
      durations = np.array([2, 3, 4, 5])
      offsets = onsets + durations
      c_fac = np.array([1, 1, 1, 1])  # constant factor (one level)
      fac_1 = np.array([0, 1, 0, 1])  # factor 1, two levels
      fac_2 = np.array([0, 0, 1, 1])  # factor 2, two levels
      t = np.arange(0, 100, 2)
  
      def mk_ev_spec(factors, names):
          names = ("time",) + tuple(names)
          if len(factors) == 0:
              return make_recarray(onsets, names)
          return make_recarray(zip(onsets, *factors), names)
  
      def mk_blk_spec(factors, names):
          names = ("start", "end") + tuple(names)
          return make_recarray(zip(onsets, offsets, *factors), names)
  
      def mk_ev_tc(ev_inds):
          # Make real time course for given event onsets
          return lambdify_t(events(onsets[ev_inds], f=glover))(t)
  
      def mk_blk_tc(ev_inds):
          # Make real time course for block onset / offsets
          B = blocks(zip(onsets[ev_inds], offsets[ev_inds]))
          term = convolve_functions(
              B,
              glover(T),
              (-5, 70),  # step func support
              (0, 30.0),  # conv kernel support
              0.02,
          )  # dt
          return lambdify_t(term)(t)
  
      for d_maker, spec_maker, tc_maker, null_name in (
          (event_design, mk_ev_spec, mk_ev_tc, "_event_"),
          (block_design, mk_blk_spec, mk_blk_tc, "_block_"),
      ):
          # Event spec with no event factor -> single column design, no contrasts
          spec_0 = spec_maker((), ())
          X_0, contrasts_0 = d_maker(spec_0, t)
          exp_x_0 = tc_maker(onsets == onsets)
          assert_almost_equal(X_0, exp_x_0)
          assert_dict_almost_equal(contrasts_0, {"constant_0": 1})
          X_0, contrasts_0 = d_maker(spec_0, t, level_contrasts=True)
          assert_almost_equal(X_0, exp_x_0)
          assert_dict_almost_equal(
              contrasts_0, {"constant_0": 1, null_name + "_1_0": 1}
          )
          # Event spec with single factor, but only one level
          spec_1c = spec_maker((c_fac,), ("smt",))
          X_1c, contrasts_1c = d_maker(spec_1c, t)
          assert_almost_equal(X_1c, exp_x_0)
          assert_dict_almost_equal(contrasts_1c, {"constant_0": 1})
          X_1c, contrasts_1c = d_maker(spec_1c, t, level_contrasts=True)
          assert_dict_almost_equal(
              contrasts_1c, {"constant_0": 1, "smt_1_0": 1}
          )
          # Event spec with single factor, two levels
          spec_1d = spec_maker((fac_1,), ("smt",))
          exp_x_0 = tc_maker(fac_1 == 0)
          exp_x_1 = tc_maker(fac_1 == 1)
          X_1d, contrasts_1d = d_maker(spec_1d, t)
          assert_almost_equal(X_1d, np.c_[exp_x_0, exp_x_1])
          assert_dict_almost_equal(
              contrasts_1d, {"constant_0": [1, 1], "smt_0": [1, -1]}
          )
          X_1d, contrasts_1d = d_maker(spec_1d, t, level_contrasts=True)
          assert_dict_almost_equal(
              contrasts_1d,
              {
                  "constant_0": 1,
                  "smt_0": [1, -1],  # main effect
                  "smt_0_0": [1, 0],  # level 0, hrf 0
                  "smt_1_0": [0, 1],
              },
          )  # level 1, hrf 0
          # Event spec with two factors, one with two levels, another with one
          spec_2dc = spec_maker((fac_1, c_fac), ("smt", "smte"))
          X_2dc, contrasts_2dc = d_maker(spec_2dc, t)
          assert_almost_equal(X_2dc, np.c_[exp_x_0, exp_x_1])
          assert_dict_almost_equal(
              contrasts_2dc,
              {
                  "constant_0": [1, 1],
                  "smt_0": [1, -1],  # main effect
                  "smt:smte_0": [1, -1],  # interaction
              },
          )
          X_2dc, contrasts_2dc = d_maker(
              spec_2dc, t, level_contrasts=True
          )
          assert_dict_almost_equal(
              contrasts_2dc,
              {
                  "constant_0": [1, 1],
                  "smt_0": [1, -1],  # main effect
                  "smt:smte_0": [1, -1],  # interaction
                  "smt_0*smte_1_0": [1, 0],  # smt 0, smte 0, hrf 0
                  "smt_1*smte_1_0": [0, 1],  # smt 1, smte 0, hrf 0
              },
          )
          # Event spec with two factors, both with two levels
          spec_2dd = spec_maker((fac_1, fac_2), ("smt", "smte"))
          exp_x_0 = tc_maker((fac_1 == 0) & (fac_2 == 0))
          exp_x_1 = tc_maker((fac_1 == 0) & (fac_2 == 1))
          exp_x_2 = tc_maker((fac_1 == 1) & (fac_2 == 0))
          exp_x_3 = tc_maker((fac_1 == 1) & (fac_2 == 1))
          X_2dd, contrasts_2dd = d_maker(spec_2dd, t)
          assert_almost_equal(
              X_2dd, np.c_[exp_x_0, exp_x_1, exp_x_2, exp_x_3]
          )
          exp_cons = {
              "constant_0": [1, 1, 1, 1],
              "smt_0": [1, 1, -1, -1],  # main effect fac_1
              "smte_0": [1, -1, 1, -1],  # main effect fac_2
              "smt:smte_0": [1, -1, -1, 1],  # interaction
          }
          assert_dict_almost_equal(contrasts_2dd, exp_cons)
          X_2dd, contrasts_2dd = d_maker(
              spec_2dd, t, level_contrasts=True
          )
          level_cons = exp_cons.copy()
          level_cons.update(
              {
                  "smt_0*smte_0_0": [
                      1,
                      0,
                      0,
                      0,
                  ],  # smt 0, smte 0, hrf 0
                  "smt_0*smte_1_0": [
                      0,
                      1,
                      0,
                      0,
                  ],  # smt 0, smte 1, hrf 0
                  "smt_1*smte_0_0": [
                      0,
                      0,
                      1,
                      0,
                  ],  # smt 1, smte 0, hrf 0
                  "smt_1*smte_1_0": [
                      0,
                      0,
                      0,
                      1,
                  ],  # smt 1, smte 1, hrf 0
              }
          )
          assert_dict_almost_equal(contrasts_2dd, level_cons)
          # Test max order >> 2, no error
          X_2dd, contrasts_2dd = d_maker(spec_2dd, t, order=100)
          assert_almost_equal(
              X_2dd, np.c_[exp_x_0, exp_x_1, exp_x_2, exp_x_3]
          )
          assert_dict_almost_equal(contrasts_2dd, exp_cons)
          # Test max order = 1
          X_2dd, contrasts_2dd = d_maker(spec_2dd, t, order=1)
          assert_almost_equal(
              X_2dd, np.c_[exp_x_0, exp_x_1, exp_x_2, exp_x_3]
          )
          # No interaction
          assert_dict_almost_equal(
              contrasts_2dd,
              {
                  "constant_0": [1, 1, 1, 1],
                  "smt_0": [1, 1, -1, -1],  # main effect fac_1
                  "smte_0": [1, -1, 1, -1],  # main effect fac_2
              },
          )
      # events : test field called "time" is necessary
      spec_1d = make_recarray(zip(onsets, fac_1), ("brighteyes", "smt"))
      pytest.raises(ValueError, event_design, spec_1d, t)
      # blocks : test fields called "start" and "end" are necessary
      spec_1d = make_recarray(
          zip(onsets, offsets, fac_1), ("mister", "brighteyes", "smt")
      )
      pytest.raises(ValueError, block_design, spec_1d, t)
      spec_1d = make_recarray(
          zip(onsets, offsets, fac_1), ("start", "brighteyes", "smt")
      )
      pytest.raises(ValueError, block_design, spec_1d, t)
      spec_1d = make_recarray(
          zip(onsets, offsets, fac_1), ("mister", "end", "smt")
      )
      pytest.raises(ValueError, block_design, spec_1d, t)
  
file_path: nipy/nipy/modalities/fmri/tests/test_design.py
imports: from os.path import dirname
  from os.path import join
  import numpy
  import pytest
  from numpy.testing import assert_almost_equal, assert_array_equal
  from nipy.algorithms.statistics.formula import make_recarray
  from ..design import block_amplitudes, block_design, event_design, openfmri2nipy, stack2designs, stack_designs
  from ..hrf import dglover, glover
  from ..utils import T, blocks, convolve_functions, events, lambdify_t
label_desc: Use image node_id 2 for calling a global function with example usage: test_event_design() and returns: make_recarray, make_recarray, lambdify_t, lambdify_t, make_recarray