def sum_responses(stimuli, responses, during_cs=None, during_ctx=None, during_us=None):
  """Calculate sum of responses for a single trial, optionally only counting timesteps occuring
  during the specified `during_cs`, `during_ctx`, or `during_us` values. For `during_cs`, the `cs` must match all provided vlaue (will not count if is a subset)."""

  # (cs, ctx, us) = stimuli[timestep]
  # response = responses[timestep]
  def include(stimulus):
    cs, ctx, us = stimulus
    result = True
    if during_cs is not None:
      result &= len(cs) == len(during_cs) and all(stim in during_cs and mag > 0 for stim, mag in cs)
    if during_ctx is not None:
      result &= ctx == during_ctx
    if during_us is not None:
      result &= us > 0 if during_us else us == 0
    return result

  return sum(responses[i] for i, stimulus in enumerate(stimuli) if include(stimulus))