def test_merge_path_sequence_1():
    """ test_merge_path_sequence_1
  """
    sa_0 = State(0, 0.0, None)
    sa_1 = State(1, 0.0, None)
    sa_2 = State(2, 0.0, None)
    sb_0 = State(0, 1.0, None)
    sb_1 = State(1, 2.0, None)
    sb_2 = State(2, 3.0, None)
    sc_0 = State(0, 0.0, None)
    sc_1 = State(1, 0.0, None)
    sc_2 = State(2, 0.0, None)
    paths_a = [Path(sa_0, [0], sb_0), \
               Path(sa_1, [1], sb_1), \
               Path(sa_2, [2], sb_2)]
    paths_b = [Path(sb_0, [0], sc_0), \
               Path(sb_1, [1], sc_1), \
               Path(sb_2, [2], sc_2)]
    trans1_a = [(0, 0), (1, 1), (2, 2)]
    trans1_b = [(0, 0), (1, 1), (2, 2)]
    trans2_a = [(0, 0), (1, 1), (2, 2)]
    trans2_b = [(0, 0), (1, 1), (2, 2)]
    best_idx_a = 1
    best_idx_b = 1
    (new_trans1, new_paths, new_trans2, new_best_idx) = \
      merge_path_sequence(trans1_a, paths_a, trans2_a, trans1_b, paths_b, \
                          trans2_b, best_idx_a, best_idx_b)
    assert len(new_paths) == 3
    assert new_trans1 == [(0, 0), (1, 1), (2, 2)]
    assert new_trans2 == [(0, 0), (1, 1), (2, 2)]
    assert new_best_idx == 1
def test_merge_path_sequence_2():
    """ test_merge_path_sequence_2
  """
    sa_0 = State(0, 0.0)
    sa_1 = State(1, 0.0)
    sb_0 = State(0, 1.0)
    sb_1 = State(1, 1.0)
    sc_0 = State(0, 2.0)
    sc_1 = State(1, 2.0)
    paths_a = [Path(sa_0, [0], sb_0), \
               Path(sa_0, [0, 1], sb_1), \
               Path(sa_1, [1, 0], sb_0), \
               Path(sa_1, [1], sb_1)]
    paths_b = [Path(sb_0, [0], sc_0), \
               Path(sb_0, [0, 1], sc_1), \
               Path(sb_1, [1, 0], sc_0), \
               Path(sb_1, [1], sc_1)]
    trans1_a = [(0, 0), (0, 1), (1, 2), (1, 3)]
    trans2_a = [(0, 0), (1, 1), (2, 0), (3, 1)]
    trans1_b = [(0, 0), (0, 1), (1, 2), (1, 3)]
    trans2_b = [(0, 0), (1, 1), (2, 0), (3, 1)]
    best_idx_a = 0
    best_idx_b = 1
    (new_trans1, new_paths, new_trans2, new_best_idx) = \
      merge_path_sequence(trans1_a, paths_a, trans2_a, trans1_b, paths_b, \
                          trans2_b, best_idx_a, best_idx_b)
    assert len(new_paths) == 8, len(new_paths)
    assert new_trans1 == [(0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (1, 5),
                          (1, 6), (1, 7)], new_trans1
    assert new_trans2 == [(0, 0), (1, 1), (2, 0), (3, 1), (4, 0), (5, 1),
                          (6, 0), (7, 1)], new_trans2
    assert new_best_idx == 1, new_best_idx
def test_merge_path_sequence_1():
  """ test_merge_path_sequence_1
  """
  sa_0 = State(0, 0.0, None)
  sa_1 = State(1, 0.0, None)
  sa_2 = State(2, 0.0, None)
  sb_0 = State(0, 1.0, None)
  sb_1 = State(1, 2.0, None)
  sb_2 = State(2, 3.0, None)
  sc_0 = State(0, 0.0, None)
  sc_1 = State(1, 0.0, None)
  sc_2 = State(2, 0.0, None)
  paths_a = [Path(sa_0, [0], sb_0), \
             Path(sa_1, [1], sb_1), \
             Path(sa_2, [2], sb_2)]
  paths_b = [Path(sb_0, [0], sc_0), \
             Path(sb_1, [1], sc_1), \
             Path(sb_2, [2], sc_2)]
  trans1_a = [(0, 0), (1, 1), (2, 2)]
  trans1_b = [(0, 0), (1, 1), (2, 2)]
  trans2_a = [(0, 0), (1, 1), (2, 2)]
  trans2_b = [(0, 0), (1, 1), (2, 2)]
  best_idx_a = 1
  best_idx_b = 1
  (new_trans1, new_paths, new_trans2, new_best_idx) = \
    merge_path_sequence(trans1_a, paths_a, trans2_a, trans1_b, paths_b, \
                        trans2_b, best_idx_a, best_idx_b)
  assert len(new_paths) == 3
  assert new_trans1 == [(0, 0), (1, 1), (2, 2)]
  assert new_trans2 == [(0, 0), (1, 1), (2, 2)]
  assert new_best_idx == 1
def test_merge_path_sequence_2():
  """ test_merge_path_sequence_2
  """
  sa_0 = State(0, 0.0)
  sa_1 = State(1, 0.0)
  sb_0 = State(0, 1.0)
  sb_1 = State(1, 1.0)
  sc_0 = State(0, 2.0)
  sc_1 = State(1, 2.0)
  paths_a = [Path(sa_0, [0], sb_0), \
             Path(sa_0, [0, 1], sb_1), \
             Path(sa_1, [1, 0], sb_0), \
             Path(sa_1, [1], sb_1)]
  paths_b = [Path(sb_0, [0], sc_0), \
             Path(sb_0, [0, 1], sc_1), \
             Path(sb_1, [1, 0], sc_0), \
             Path(sb_1, [1], sc_1)]
  trans1_a = [(0, 0), (0, 1), (1, 2), (1, 3)]
  trans2_a = [(0, 0), (1, 1), (2, 0), (3, 1)]
  trans1_b = [(0, 0), (0, 1), (1, 2), (1, 3)]
  trans2_b = [(0, 0), (1, 1), (2, 0), (3, 1)]
  best_idx_a = 0
  best_idx_b = 1
  (new_trans1, new_paths, new_trans2, new_best_idx) = \
    merge_path_sequence(trans1_a, paths_a, trans2_a, trans1_b, paths_b, \
                        trans2_b, best_idx_a, best_idx_b)
  assert len(new_paths) == 8, len(new_paths)
  assert new_trans1 == [(0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (1, 5), (1, 6), (1, 7)], new_trans1
  assert new_trans2 == [(0, 0), (1, 1), (2, 0), (3, 1), (4, 0), (5, 1), (6, 0), (7, 1)], new_trans2
  assert new_best_idx == 1, new_best_idx
 def call(self, dct):
   """ Closure.
   """
   # All the hard elements are already coded in mapping.
   # Here is how the mapping procedure works in high frequency:
   # At first, a new point is added to the beginning, with empty paths.
   # When a new point (with its paths) arrives, two cases:
   # We were right after the start point, then we simply add
   # the path and the point. Otherwise, we decimate paths and points,
   # and we merge the paths together.
   (trans1, paths_dct, trans2, sc_dct) = dct
   paths = [decode_Path(path_dct) for path_dct in paths_dct]
   del paths_dct
   sc = decode_StateCollection(sc_dct)
   del sc_dct
   # The index of sc
   point_idx = 2 * self.count
   # The index of the paths
   path_idx = 2 * self.count - 1
   self.count += 1
   (new_decim_sc, new_end_mapping) = \
     decimate_point(sc, self.probas[point_idx], self.viterbi_idxs[point_idx])
   new_most_likely_sc_idx = None
   if point_idx >= 0:
     assert self.viterbi_idxs[point_idx] in new_end_mapping, \
       (point_idx, new_end_mapping)
     new_most_likely_sc_idx = new_end_mapping[self.viterbi_idxs[point_idx]]
   # First element??
   if not self.start_point:
     self.start_point = new_decim_sc
     self.start_mapping = new_end_mapping
     assert new_most_likely_sc_idx is not None
     self.most_likely_indexes.append(new_most_likely_sc_idx)
     return ([], [], [], encode_StateCollection(self.start_point))
   # Try to add a new elment:
   if self.paths is None:
     assert self.start_mapping is not None
     assert self.start_point is not None
     # Start a new set of paths
     (new_trans1, decimated_paths, new_trans2, paths_mapping) = \
       decimate_path_simple(self.start_mapping, trans1, paths, \
                            trans2, new_end_mapping)
     assert self.viterbi_idxs[point_idx] in new_end_mapping
     assert self.viterbi_idxs[path_idx] in paths_mapping
     assert (self.start_mapping[self.viterbi_idxs[path_idx-1]], \
               paths_mapping[self.viterbi_idxs[path_idx]]) in new_trans1
     assert (paths_mapping[self.viterbi_idxs[path_idx]], \
               new_end_mapping[self.viterbi_idxs[point_idx]]) in new_trans2
     self.end_point = new_decim_sc
     self.end_mapping = new_end_mapping
     self.start_trans = new_trans1
     self.paths = decimated_paths
     assert self.paths, self.paths
     self.best_idx = paths_mapping[self.viterbi_idxs[path_idx]]
     self.end_trans = new_trans2
   else:
     assert self.start_mapping is not None
     assert self.start_point is not None
     assert self.start_trans is not None
     assert self.end_trans is not None
     assert self.paths is not None
     # First decimate the paths
     (new_trans1, decimated_paths, new_trans2, paths_mapping) = \
       decimate_path_simple(self.end_mapping, trans1, paths, trans2, \
                            new_end_mapping)
     assert self.viterbi_idxs[path_idx] in paths_mapping
     assert self.viterbi_idxs[path_idx-1] in self.end_mapping
     assert (self.end_mapping[self.viterbi_idxs[path_idx-1]], \
             paths_mapping[self.viterbi_idxs[path_idx]]) in new_trans1
     assert (paths_mapping[self.viterbi_idxs[path_idx]], \
             new_end_mapping[self.viterbi_idxs[point_idx]]) in new_trans2
     best_idx2 = paths_mapping[self.viterbi_idxs[path_idx]]
     # Merge the paths together
     (merged_trans1, merged_paths, merged_trans2, merged_best_idx) = \
       merge_path_sequence(self.start_trans, self.paths, self.end_trans, \
                           new_trans1, decimated_paths, new_trans2, \
                           self.best_idx, best_idx2)
     self.end_point = new_decim_sc
     self.end_mapping = new_end_mapping
     self.start_trans = merged_trans1
     self.paths = merged_paths
     self.best_idx = merged_best_idx
     self.end_trans = merged_trans2
   # Time to send a new element to the output and restart?  
   if (self.count-1) % self.decimation_factor == 0:
     assert self.paths
     assert self.end_trans
     assert self.start_trans
     assert self.best_idx is not None
     encoded_paths = [encode_Path(path) for path in self.paths]
     print len(encoded_paths), " paths", len(self.end_point.states), " states"
     result = (self.start_trans, encoded_paths, \
               self.end_trans, encode_StateCollection(self.end_point))
     # Adding the most likely index of the path and of the next point.
     self.most_likely_indexes.append(self.best_idx)
     assert new_most_likely_sc_idx is not None
     self.most_likely_indexes.append(new_most_likely_sc_idx)
     # Restart computations:
     self.start_point = self.end_point
     self.start_mapping = self.end_mapping
     del self.paths
     self.start_trans = None
     self.end_trans = None
     self.paths = None
     self.best_idx = None
     return result
   # Nothing to return for this input, continuing.
   return None
 def call(self, dct):
     """ Closure.
 """
     # All the hard elements are already coded in mapping.
     # Here is how the mapping procedure works in high frequency:
     # At first, a new point is added to the beginning, with empty paths.
     # When a new point (with its paths) arrives, two cases:
     # We were right after the start point, then we simply add
     # the path and the point. Otherwise, we decimate paths and points,
     # and we merge the paths together.
     (trans1, paths_dct, trans2, sc_dct) = dct
     paths = [decode_Path(path_dct) for path_dct in paths_dct]
     del paths_dct
     sc = decode_StateCollection(sc_dct)
     del sc_dct
     # The index of sc
     point_idx = 2 * self.count
     # The index of the paths
     path_idx = 2 * self.count - 1
     self.count += 1
     (new_decim_sc, new_end_mapping) = \
       decimate_point(sc, self.probas[point_idx], self.viterbi_idxs[point_idx])
     new_most_likely_sc_idx = None
     if point_idx >= 0:
         assert self.viterbi_idxs[point_idx] in new_end_mapping, \
           (point_idx, new_end_mapping)
         new_most_likely_sc_idx = new_end_mapping[
             self.viterbi_idxs[point_idx]]
     # First element??
     if not self.start_point:
         self.start_point = new_decim_sc
         self.start_mapping = new_end_mapping
         assert new_most_likely_sc_idx is not None
         self.most_likely_indexes.append(new_most_likely_sc_idx)
         return ([], [], [], encode_StateCollection(self.start_point))
     # Try to add a new elment:
     if self.paths is None:
         assert self.start_mapping is not None
         assert self.start_point is not None
         # Start a new set of paths
         (new_trans1, decimated_paths, new_trans2, paths_mapping) = \
           decimate_path_simple(self.start_mapping, trans1, paths, \
                                trans2, new_end_mapping)
         assert self.viterbi_idxs[point_idx] in new_end_mapping
         assert self.viterbi_idxs[path_idx] in paths_mapping
         assert (self.start_mapping[self.viterbi_idxs[path_idx-1]], \
                   paths_mapping[self.viterbi_idxs[path_idx]]) in new_trans1
         assert (paths_mapping[self.viterbi_idxs[path_idx]], \
                   new_end_mapping[self.viterbi_idxs[point_idx]]) in new_trans2
         self.end_point = new_decim_sc
         self.end_mapping = new_end_mapping
         self.start_trans = new_trans1
         self.paths = decimated_paths
         assert self.paths, self.paths
         self.best_idx = paths_mapping[self.viterbi_idxs[path_idx]]
         self.end_trans = new_trans2
     else:
         assert self.start_mapping is not None
         assert self.start_point is not None
         assert self.start_trans is not None
         assert self.end_trans is not None
         assert self.paths is not None
         # First decimate the paths
         (new_trans1, decimated_paths, new_trans2, paths_mapping) = \
           decimate_path_simple(self.end_mapping, trans1, paths, trans2, \
                                new_end_mapping)
         assert self.viterbi_idxs[path_idx] in paths_mapping
         assert self.viterbi_idxs[path_idx - 1] in self.end_mapping
         assert (self.end_mapping[self.viterbi_idxs[path_idx-1]], \
                 paths_mapping[self.viterbi_idxs[path_idx]]) in new_trans1
         assert (paths_mapping[self.viterbi_idxs[path_idx]], \
                 new_end_mapping[self.viterbi_idxs[point_idx]]) in new_trans2
         best_idx2 = paths_mapping[self.viterbi_idxs[path_idx]]
         # Merge the paths together
         (merged_trans1, merged_paths, merged_trans2, merged_best_idx) = \
           merge_path_sequence(self.start_trans, self.paths, self.end_trans, \
                               new_trans1, decimated_paths, new_trans2, \
                               self.best_idx, best_idx2)
         self.end_point = new_decim_sc
         self.end_mapping = new_end_mapping
         self.start_trans = merged_trans1
         self.paths = merged_paths
         self.best_idx = merged_best_idx
         self.end_trans = merged_trans2
     # Time to send a new element to the output and restart?
     if (self.count - 1) % self.decimation_factor == 0:
         assert self.paths
         assert self.end_trans
         assert self.start_trans
         assert self.best_idx is not None
         encoded_paths = [encode_Path(path) for path in self.paths]
         print len(encoded_paths), " paths", len(
             self.end_point.states), " states"
         result = (self.start_trans, encoded_paths, \
                   self.end_trans, encode_StateCollection(self.end_point))
         # Adding the most likely index of the path and of the next point.
         self.most_likely_indexes.append(self.best_idx)
         assert new_most_likely_sc_idx is not None
         self.most_likely_indexes.append(new_most_likely_sc_idx)
         # Restart computations:
         self.start_point = self.end_point
         self.start_mapping = self.end_mapping
         del self.paths
         self.start_trans = None
         self.end_trans = None
         self.paths = None
         self.best_idx = None
         return result
     # Nothing to return for this input, continuing.
     return None