def cross(history): """Iterate by crossing history and history[1:] """ backup = 0 if len(history): yield state.start(), history[0][0], 1 for (s1, t1), (s2, t2) in izip(history, history[1:]): duration = t2 - t1 if s1 == s2: backup += duration else: if backup + duration <= MAX_DURATION \ and not state.terminal(s1): yield s1, s2, backup + duration else: yield s1, state.finish(), 1 yield state.start(), s2, 1 backup = 0 yield history[-1][0], state.finish(), 1
def split(history): """Split history into segmentation """ splitted = [] segmented = [] for h in cross(history): s1, s2, dt = h segmented.append(h) if s2 == state.finish(): splitted.append(segmented) segmented = [] return splitted