Exemplo n.º 1
0
 def add_rectangles_by_path(self, path1, path2, start_offset): 
       path_len = 0
       for p in path1:
         path_len += p.len
       # path_len -= start_offset
       first_shift = start_offset
       second_shift = 0 
       pos_first_path = 0
       pos_second_path = 0
       first_len = first_shift
      
       while first_len < path_len:
         ed1 = path1[pos_first_path]
         ed2 = path2[pos_second_path]
         rectangle = Rectangle(ed1,ed2)
         rectangle.add_diagonal(self.d, self.d + first_shift - second_shift)
         rect_diag = rectangle.get_closest_diagonal(self.d + first_shift - second_shift) 
         self.add_diagonal_and_conj(rect_diag)
         print "ADD DIAGS", rect_diag
         if ed2.len - second_shift < ed1.len - first_shift:
           pos_second_path += 1
           first_shift += ed2.len - second_shift
           first_len += ed2.len - second_shift
           second_shift = 0
         elif ed1.len - first_shift < ed2.len - second_shift:
           pos_first_path += 1
           first_len += ed1.len - first_shift
           second_shift += ed1.len - first_shift
           first_shift = 0
         else:
           first_len += ed1.len - first_shift
           pos_second_path += 1
           pos_first_path += 1
           first_shift = 0
           second_shift = 0
Exemplo n.º 2
0
    def add_rectangles_by_path(self, path1, path2, start_offset):
        path_len = 0
        for p in path1:
            path_len += p.len
            # path_len -= start_offset
        first_shift = start_offset
        second_shift = 0
        pos_first_path = 0
        pos_second_path = 0
        first_len = first_shift

        while first_len < path_len:
            ed1 = path1[pos_first_path]
            ed2 = path2[pos_second_path]
            rectangle = Rectangle(ed1, ed2)
            rectangle.add_diagonal(self.d, self.d + first_shift - second_shift)
            rect_diag = rectangle.get_closest_diagonal(self.d + first_shift -
                                                       second_shift)
            self.add_diagonal_and_conj(rect_diag)
            if ed2.len - second_shift < ed1.len - first_shift:
                pos_second_path += 1
                first_shift += ed2.len - second_shift
                first_len += ed2.len - second_shift
                second_shift = 0
            elif ed1.len - first_shift < ed2.len - second_shift:
                pos_first_path += 1
                first_len += ed1.len - first_shift
                second_shift += ed1.len - first_shift
                first_shift = 0
            else:
                first_len += ed1.len - first_shift
                pos_second_path += 1
                pos_first_path += 1
                first_shift = 0
                second_shift = 0
Exemplo n.º 3
0
 def add_diagonal_and_conj(self, diag):
   for old_diag in self.diagonals:
     if diag.rectangle.e1 == old_diag.rectangle.e1 and diag.rectangle.e2 == old_diag.rectangle.e2:
       if diag.D == old_diag.D:
         return
   rect = diag.rectangle
   rect_conj = Rectangle(rect.e2.conj, rect.e1.conj)
   conjugate(rect, rect_conj)
   D = diag.D - diag.rectangle.e1.len + diag.rectangle.e2.len       
   pathset = diag.pathset.conj() if experimental.filter == experimental.Filter.pathsets else None
   rect_conj.add_diagonal(self.d, D, pathset)
   diag_conj = rect.conj.diagonals[D, pathset]       
   conjugate(diag, diag_conj)
       
   return self.add_diagonal(diag)
Exemplo n.º 4
0
    def add_diagonal_and_conj(self, diag):
        if diag in self.diagonals:
            return
        rect = diag.rectangle
        rect_conj = Rectangle(rect.e2.conj, rect.e1.conj)
        conjugate(rect, rect_conj)
        D = diag.D - diag.rectangle.e1.len + diag.rectangle.e2.len
        if experimental.filter == experimental.Filter.pathsets:
            pathset = diag.pathset.conj()
        else:
            pathset = None
        rect_conj.add_diagonal(self.d, D, pathset)
        diag_conj = rect.conj.diagonals[D, pathset]
        conjugate(diag, diag_conj)

        return self.add_diagonal(diag)
Exemplo n.º 5
0
 def choose_best_path(self, paired_paths, rectangeles_set, diag1, diag2, d, added_paths):
     best_support = 0
     best_len = 10000
     best_rectangles = []
     best_diags = []
     best_path = paired_paths[0]
     best_not_supported = 0
       
     for paired_path in paired_paths:
       (path1, path2, path_len) = paired_path
       if paired_path in added_paths:
         continue
       first_shift = diag1.offseta
       second_shift = diag1.offsetb
       path1.append(diag2.rectangle.e1)
       path2.append(diag2.rectangle.e2)
       rectangles = []
       diags = []
       not_supported = []
       path_support = 0
       pos_first_path = 0
       pos_second_path = 0
       first_len = first_shift
       make_less_N50 = False
       while not make_less_N50 and first_len < path_len + diag2.offseta:
         ed1 = path1[pos_first_path]
         ed2 = path2[pos_second_path]
         rectangle = Rectangle(ed1,ed2)
         rectangle.add_diagonal(d, d + first_shift - second_shift)
         rect_diag = rectangle.get_closest_diagonal(d + first_shift - second_shift) 
         if (not (rect_diag.key1 == diag1.key1 and rect_diag.key2 == diag1.key2) and not(rect_diag.key1 == diag2.key1 and rect_diag.key2 == diag2.key2)):
           can_use = [diag1.key1, diag1.key2, diag2.key1, diag2.key2]
           if (rect_diag.key1 in self.vs and rect_diag.key1 not in can_use) or  (rect_diag.key2 in self.vs and rect_diag.key2 not in can_use):
             make_less_N50 = True
             continue
         diags.append(rect_diag)
         rectangles.append(rectangle)
         rectangeles_set.use_prd_diag(rect_diag)
         #if rect_diag.prd_support < 0.00001 and (ed2.len > 10 and ed1.len > 10):
         #  make_less_N50 = True
         #  continue
         path_support += rect_diag.prd_support 
         if ed2.len - second_shift < ed1.len - first_shift:
           pos_second_path += 1
           first_shift += ed2.len - second_shift
           first_len += ed2.len - second_shift
           if rect_diag.prd_support < 0.000000001:
             not_supported.append(ed2.len - second_shift)
           second_shift = 0
         elif ed1.len - first_shift < ed2.len - second_shift:
           pos_first_path += 1
           first_len += ed1.len - first_shift
           second_shift += ed1.len - first_shift
           if rect_diag.prd_support < 0.000000001:
             not_supported.append(ed1.len - first_shift)
           first_shift = 0
         else:
           first_len += ed1.len - first_shift
           pos_second_path += 1
           pos_first_path += 1
           first_shift = 0
           second_shift = 0
       if not make_less_N50 and path_len > 1 and  path_support / path_len < 1000 and  path_support / path_len > best_support:
         best_support = path_support 
         best_len = path_len
         best_rectangles = rectangles
         best_diags = diags
         best_path = (path1, path2, path_len)
         best_not_supported = not_supported
     return (best_support,best_len, best_rectangles, best_diags, best_path)
Exemplo n.º 6
0
  def delete_missing_loops(self, DG_loops, K, L, threshold):
    begs_related_to_loop = dict()
    begin_loops = dict()
    end_loops = dict()
    for eeid1, (long_eid1, long_eid2, busheids, path, visited_vs) in DG_loops.items():
      for k, be in self.es.items():
        for diag in be.diagonals:
          rect = diag.rectangle
          eids = [rect.e1.eid, rect.e2.eid ]
          if rect.e1.eid not in busheids or rect.e2.eid not in busheids:
            continue
          for eid in eids:
            if eid not in busheids:
              continue
            if rect.e1.eid == long_eid1:
              if rect.e2.eid == long_eid1: 
                begin_loops[long_eid1] = (diag, be)
            if rect.e2.eid == long_eid2:
              if rect.e1.eid == long_eid2:  
                end_loops[long_eid1] = (diag, be)
            if eeid1 not in begs_related_to_loop:
              begs_related_to_loop[eeid1] = set()
            begs_related_to_loop[eeid1].add(be)
    diag_to_add = set()
    for eid, begs in begs_related_to_loop.items():
      (long_eid1, long_eid2, busheids, path, visited_vs)  =  DG_loops[eid]
      if len(begs) < 2:
        continue
      if eid not in begin_loops or eid not in end_loops:
        print "not find begin_end"
        continue
      begin_diag = begin_loops[eid][0]
      end_diag = end_loops[eid][0]
      path.append(end_loops[eid][0].rectangle.e1)
      first_shift = begin_diag.offseta
      second_shift = begin_diag.offsetb
      path_len = 0
      for e in path:
        path_len += e.len
      rectangles = []
      diags = []
      pos_first_path = 0
      pos_second_path = 0
      first_len = first_shift
      while first_len < path_len and pos_second_path < len(path):
        ed1 = path[pos_first_path]
        ed2 = path[pos_second_path]
        rectangle = Rectangle(ed1,ed2)
        rectangle.add_diagonal(self.d, self.d + first_shift - second_shift)
        rect_diag = rectangle.get_closest_diagonal(self.d + first_shift - second_shift) 
        rectangles.append(rectangle)
        diags.append(rect_diag)
        if ed2.len - second_shift < ed1.len - first_shift:
            pos_second_path += 1
            first_shift += ed2.len - second_shift
            first_len += ed2.len - second_shift
            second_shift = 0
        elif ed1.len - first_shift < ed2.len - second_shift:
            pos_first_path += 1
            first_len += ed1.len - first_shift
            second_shift += ed1.len - first_shift
            first_shift = 0
        else:
            first_len += ed1.len - first_shift
            pos_second_path += 1
            pos_first_path += 1
            first_shift = 0
            second_shift = 0
      for diag in diags:
        diag_to_add.add(diag)
        
      for bedge in begs:
        self.__remove_bedge__(bedge)
        self.__remove_bedge__(bedge.conj)
        
        for diag in bedge.diagonals:
          if diag.rectangle.e1.eid not in busheids or diag.rectangle.e2.eid not in busheids:
            diag_to_add.add(diag)
        
        if begin_diag in bedge.diagonals:
          for diag in bedge.diagonals:
            if diag == begin_diag:
              break
            diag_to_add.add(diag)
          
        elif end_diag in bedge.diagonals:
          bedge.diagonals.reverse()
          for diag in bedge.diagonals:
            if diag == end_diag:
              break
            diag_to_add.add(diag)

    for diag in diag_to_add:
          self.add_diagonal_and_conj(diag)
Exemplo n.º 7
0
    def delete_missing_loops(self, DG_loops, K, L, threshold):
        if DG_loops == None:
            return
        begs_related_to_loop = defaultdict(set)
        begin_loops = dict()
        end_loops = dict()
        for k, be in self.es.items():
            for diag in be.diagonals:
                for eeid1, (long_eid1, long_eid2, busheids, path, visited_vs,
                            all_paths) in DG_loops.items():
                    rect = diag.rectangle
                    if not set([rect.e1.eid, rect.e2.eid]) <= busheids:
                        continue

                    if rect.e1.eid == long_eid1 and rect.e2.eid == long_eid1:
                        begin_loops[long_eid1] = (diag, be)

                    if rect.e1.eid == long_eid2 and rect.e2.eid == long_eid2:
                        end_loops[long_eid1] = (diag, be)

                    begs_related_to_loop[eeid1].add(be)

        diag_to_add = []
        for eid, begs in begs_related_to_loop.items():
            (long_eid1, long_eid2, busheids, path, visited_vs,
             all_paths) = DG_loops[eid]
            path = self.choose_best_path(all_paths, long_eid1, long_eid2, begs)
            if len(begs) < 2:
                continue
            if eid not in begin_loops or eid not in end_loops:
                continue
            begin_diag = begin_loops[eid][0]
            if begin_diag.rectangle.e1.len < 2 * self.d or len(
                    path) < 1 or path[0] != begin_diag.rectangle.e1 or path[
                        0].len < 2 * self.d:
                self.logger.info("BAD CASE " + str(eid) + " " +
                                 str(begin_diag.rectangle.e1.len) + " " +
                                 str((long_eid1, long_eid2, busheids, path,
                                      visited_vs)))
                continue
            end_diag = end_loops[eid][0]
            path.append(end_loops[eid][0].rectangle.e1)
            first_shift = begin_diag.offseta
            second_shift = begin_diag.offsetb

            path_len = reduce(lambda s, e: s + e.len, path, 0)
            rectangles = []
            diags = []
            pos_first_path = 0
            pos_second_path = 0
            first_len = first_shift
            while first_len < path_len and pos_second_path < len(path):
                ed1 = path[pos_first_path]
                ed2 = path[pos_second_path]
                rectangle = Rectangle(ed1, ed2)
                rectangle.add_diagonal(self.d,
                                       self.d + first_shift - second_shift)
                rect_diag = rectangle.get_closest_diagonal(self.d +
                                                           first_shift -
                                                           second_shift)
                rectangles.append(rectangle)
                diags.append(rect_diag)
                if ed2.len - second_shift < ed1.len - first_shift:
                    pos_second_path += 1
                    first_shift += ed2.len - second_shift
                    first_len += ed2.len - second_shift
                    second_shift = 0
                elif ed1.len - first_shift < ed2.len - second_shift:
                    pos_first_path += 1
                    first_len += ed1.len - first_shift
                    second_shift += ed1.len - first_shift
                    first_shift = 0
                else:
                    first_len += ed1.len - first_shift
                    pos_second_path += 1
                    pos_first_path += 1
                    first_shift = 0
                    second_shift = 0

            diag_to_add.extend(diags)

            for bedge in begs:
                self.__remove_bedge_and_conj(bedge)

                for diag in bedge.diagonals:
                    if diag.rectangle.e1.eid not in busheids or diag.rectangle.e2.eid not in busheids:
                        diag_to_add.append(diag)

                if begin_diag in bedge.diagonals:
                    for diag in bedge.diagonals:
                        if diag == begin_diag:
                            break
                        diag_to_add.append(diag)

                elif end_diag in bedge.diagonals:
                    bedge.diagonals.reverse()
                    for diag in bedge.diagonals:
                        if diag == end_diag:
                            break
                        diag_to_add.append(diag)

        for diag in diag_to_add:
            self.add_diagonal_and_conj(diag)
Exemplo n.º 8
0
    def delete_missing_loops(self, DG_loops, K, L, threshold):
        if DG_loops == None:
            return
        begs_related_to_loop = defaultdict(set)
        begin_loops = dict()
        end_loops = dict()
        for k, be in self.es.items():
            for diag in be.diagonals:
                for eeid1, (long_eid1, long_eid2, busheids, path, visited_vs, all_paths) in DG_loops.items():
                    rect = diag.rectangle
                    if not set([rect.e1.eid, rect.e2.eid]) <= busheids:
                        continue
                    
                    if rect.e1.eid == long_eid1 and rect.e2.eid == long_eid1:
                        begin_loops[long_eid1] = (diag, be)
                    
                    if rect.e1.eid == long_eid2 and  rect.e2.eid == long_eid2:
                        end_loops[long_eid1] = (diag, be)                        
                    
                    begs_related_to_loop[eeid1].add(be)

        diag_to_add = []
        for eid, begs in begs_related_to_loop.items():
            (long_eid1, long_eid2, busheids, path, visited_vs, all_paths) = DG_loops[eid]
            path = self.choose_best_path(all_paths, long_eid1, long_eid2, begs)
            if len(begs) < 2:
                continue
            if eid not in begin_loops or eid not in end_loops:
                continue
            begin_diag = begin_loops[eid][0]
            if begin_diag.rectangle.e1.len < 2*self.d or len(path) < 1 or path[0] != begin_diag.rectangle.e1 or path[0].len < 2 * self.d:
                self.logger.info("BAD CASE " + str( eid)+ " " + str( begin_diag.rectangle.e1.len) + " " +str((long_eid1, long_eid2, busheids, path, visited_vs))) 
                continue
            end_diag = end_loops[eid][0]
            path.append(end_loops[eid][0].rectangle.e1)
            first_shift = begin_diag.offseta
            second_shift = begin_diag.offsetb
            
            path_len = reduce(lambda s, e: s + e.len, path, 0)           
            rectangles = []
            diags = []
            pos_first_path = 0
            pos_second_path = 0
            first_len = first_shift
            while first_len < path_len and pos_second_path < len(path):
                ed1 = path[pos_first_path]
                ed2 = path[pos_second_path]
                rectangle = Rectangle(ed1, ed2)
                rectangle.add_diagonal(self.d, self.d + first_shift - second_shift)
                rect_diag = rectangle.get_closest_diagonal(self.d + first_shift - second_shift)
                rectangles.append(rectangle)
                diags.append(rect_diag)
                if ed2.len - second_shift < ed1.len - first_shift:
                    pos_second_path += 1
                    first_shift += ed2.len - second_shift
                    first_len += ed2.len - second_shift
                    second_shift = 0
                elif ed1.len - first_shift < ed2.len - second_shift:
                    pos_first_path += 1
                    first_len += ed1.len - first_shift
                    second_shift += ed1.len - first_shift
                    first_shift = 0
                else:
                    first_len += ed1.len - first_shift
                    pos_second_path += 1
                    pos_first_path += 1
                    first_shift = 0
                    second_shift = 0
            
            diag_to_add.extend(diags)
            
            for bedge in begs:
                self.__remove_bedge_and_conj(bedge)
             
                for diag in bedge.diagonals:
                    if diag.rectangle.e1.eid not in busheids or diag.rectangle.e2.eid not in busheids:
                        diag_to_add.append(diag)

                if begin_diag in bedge.diagonals:
                    for diag in bedge.diagonals:
                        if diag == begin_diag:
                            break
                        diag_to_add.append(diag)

                elif end_diag in bedge.diagonals:
                    bedge.diagonals.reverse()
                    for diag in bedge.diagonals:
                        if diag == end_diag:
                            break
                        diag_to_add.append(diag)

        for diag in diag_to_add:
            self.add_diagonal_and_conj(diag)