def ignore_this_demo(args, total_reward, t, last_info):
  """Check to see if we should filter out a scripted demo episode.

  Several filters: if t==0, that means the initial state was a success. For
  the bag tasks, we can exit gracefully if reaching a failure state, which
  speeds up computation.

  Args:
    args: main arguments.
    total_reward: demonstration cumulative reward.
    t: num time steps.
    last_info: last observation info (last_obs_info).

  Returns:
    bool indicating if the demo should be ignored.
  """
  last_extras = last_info['extras']

  # For bag tasks.
  if 'exit_gracefully' in last_extras:
    assert last_extras['exit_gracefully']
    return True

  # Easier bag-items. We can get 0.5 reward by placing the cube only (bad).
  if args.task == 'bag-items-easy' and total_reward <= 0.5:
    return True

  # Harder: ignore if (a) no beads in zone, OR, (b) didn't get both items.
  if args.task == 'bag-items-hard':
    return (last_extras['zone_items_rew'] < 0.5 or
            last_extras['zone_beads_rew'] == 0)

  return t == 0