code: def _build_formula_contrasts(spec, fields, order):
      """Build formula and contrast in event / block space
  
      Parameters
      ----------
      spec : structured array
          Structured array containing at least fields listed in `fields`.
      fields : sequence of str
          Sequence of field names containing names of factors.
      order : int
          Maximum order of interactions between main effects.
  
      Returns
      -------
      e_factors : :class:`Formula` instance
          Formula for factors given by `fields`
      e_contrasts : dict
          Dictionary containing contrasts of main effects and interactions
          between factors.
      """
      if len(fields) == 0:
          raise ValueError("Specify at least one field")
      e_factors = [Factor(n, np.unique(spec[n])) for n in fields]
      e_formula = reduce(mul, e_factors)
      e_contrasts = {}
      # Add contrasts for factors and factor interactions
      max_order = min(len(e_factors), order)
      for i in range(1, max_order + 1):
          for comb in combinations(zip(fields, e_factors), i):
              names = [c[0] for c in comb]
              # Collect factors where there is more than one level
              fs = [
                  fc.main_effect
                  for fn, fc in comb
                  if len(fc.levels) > 1
              ]
              if len(fs) > 0:
                  e_contrast = reduce(mul, fs).design(spec)
                  e_contrasts[":".join(names)] = e_contrast
      e_contrasts["constant"] = formulae.I.design(spec)
      return e_formula, e_contrasts
  
imports: import itertools
  from functools import reduce
  from operator import mul
  import numpy
  from nipy.algorithms.statistics.formula import formulae
  from nipy.algorithms.statistics.formula.formulae import Factor, Formula, Term, make_recarray
  from nipy.algorithms.statistics.utils import combinations
  from .hrf import glover
  from .utils import T, blocks, convolve_functions, events
  from .utils import fourier_basis
label_desc: Use image node_id 3 for calling a global function with example usage: _build_formula_contrasts(spec, fields, order) and returns: e_formula,  e_contrasts