def test_merge_path_simple():
  """ test_merge_path_simple
  """
  s1 = State(1, 1.0, None)
  s2 = State(2, 3.0, None)
  s3 = State(3, 1.0, None)
  p1 = Path(s1, [1, 2], s2)
  p2 = Path(s2, [2, 3], s3)
  p = merge_path(p1, p2)
  assert p.start == s1
  assert p.end == s3
  assert p.links == [1, 2, 3]
def test_merge_path_simple():
    """ test_merge_path_simple
  """
    s1 = State(1, 1.0, None)
    s2 = State(2, 3.0, None)
    s3 = State(3, 1.0, None)
    p1 = Path(s1, [1, 2], s2)
    p2 = Path(s2, [2, 3], s3)
    p = merge_path(p1, p2)
    assert p.start == s1
    assert p.end == s3
    assert p.links == [1, 2, 3]
 def call(self, dct):
   """ Closure.
   """
   (_, paths_dct, _, sc_dct) = 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_most_likely_sc_idx = self.viterbi_idxs[point_idx]
   # First element??
   if not self.start_point:
     self.start_point = sc
     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))
   # Only decode the most likely path, we do not need the other paths.
   new_best_path = decode_Path(paths_dct[self.viterbi_idxs[path_idx]])
   del paths_dct
   # Try to add a new element:
   # All this code is much more complicated than it should be now.
   if self.best_path is None:
     assert self.start_point is not None
     self.best_path = new_best_path
   else:
     assert self.start_point is not None
     self.best_path = merge_path(self.best_path, new_best_path)
     assert self.best_path.start in self.start_point.states
     assert self.best_path.end in sc.states
   # Time to send a new element to the output and restart?  
   if (self.count-1) % self.decimation_factor == 0:
     # Time to find all the other paths
     (other_trans1, other_paths, other_trans2) = \
       self.path_builder.getPathsBetweenCollections(self.start_point, sc)
     # If we have the first path already in, no need to integrate it:
     try:
       best_path_idx = other_paths.index(self.best_path)
       new_trans1 = other_trans1
       new_paths = other_paths
       new_trans2 = other_trans2
     except ValueError:
       # We need to append it:
       best_path_idx = len(other_paths)
       prev_best_idx = self.most_likely_indexes[-1]
       new_trans1 = other_trans1 + [(prev_best_idx, best_path_idx)]
       new_paths = other_paths + [self.best_path]
       new_trans2 = other_trans2 + [(best_path_idx, new_most_likely_sc_idx)]
     
     encoded_paths = [encode_Path(path) for path in new_paths]
     print len(encoded_paths), " paths", len(sc.states), " states",
     if len(other_paths) != len(new_paths):
       print '(forced insertion)'
     else:
       print ''
     result = (new_trans1, encoded_paths, \
               new_trans2, encode_StateCollection(sc))
     # Adding the most likely index of the path and of the next point.
     self.most_likely_indexes.append(best_path_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 = sc
     self.best_path = None
     return result
   # Nothing to return for this input, continuing.
   return None
    def call(self, dct):
        """ Closure.
    """
        (_, paths_dct, _, sc_dct) = 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_most_likely_sc_idx = self.viterbi_idxs[point_idx]
        # First element??
        if not self.start_point:
            self.start_point = sc
            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))
        # Only decode the most likely path, we do not need the other paths.
        new_best_path = decode_Path(paths_dct[self.viterbi_idxs[path_idx]])
        del paths_dct
        # Try to add a new element:
        # All this code is much more complicated than it should be now.
        if self.best_path is None:
            assert self.start_point is not None
            self.best_path = new_best_path
        else:
            assert self.start_point is not None
            self.best_path = merge_path(self.best_path, new_best_path)
            assert self.best_path.start in self.start_point.states
            assert self.best_path.end in sc.states
        # Time to send a new element to the output and restart?
        if (self.count - 1) % self.decimation_factor == 0:
            # Time to find all the other paths
            (other_trans1, other_paths, other_trans2) = \
              self.path_builder.getPathsBetweenCollections(self.start_point, sc)
            # If we have the first path already in, no need to integrate it:
            try:
                best_path_idx = other_paths.index(self.best_path)
                new_trans1 = other_trans1
                new_paths = other_paths
                new_trans2 = other_trans2
            except ValueError:
                # We need to append it:
                best_path_idx = len(other_paths)
                prev_best_idx = self.most_likely_indexes[-1]
                new_trans1 = other_trans1 + [(prev_best_idx, best_path_idx)]
                new_paths = other_paths + [self.best_path]
                new_trans2 = other_trans2 + [
                    (best_path_idx, new_most_likely_sc_idx)
                ]

            encoded_paths = [encode_Path(path) for path in new_paths]
            print len(encoded_paths), " paths", len(sc.states), " states",
            if len(other_paths) != len(new_paths):
                print '(forced insertion)'
            else:
                print ''
            result = (new_trans1, encoded_paths, \
                      new_trans2, encode_StateCollection(sc))
            # Adding the most likely index of the path and of the next point.
            self.most_likely_indexes.append(best_path_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 = sc
            self.best_path = None
            return result
        # Nothing to return for this input, continuing.
        return None