def compute_dgm_from_edges( all_edges_for_diagrams, astuple: bool = True, negate: bool = True ): _prepare_edges_for_diagram(all_edges_for_diagrams) # Dionysus computations (persistent diagrams) # logger.info(f"Before filtration") #logger.info(f"len all_edges_for_diagrams = {len(all_edges_for_diagrams)}") f = Filtration() for vertices, weight in all_edges_for_diagrams: if negate: timing = -weight.round(3) else: timing = weight.round(3) f.append(Simplex(vertices, timing)) f.sort() #logger.info(f"{f}") m = homology_persistence(f) dgms = init_diagrams(m, f) dgm = dgms[0] if not astuple: return dgm else: ret = list() for pt in dgm: # ret.append((pt.birth, pt.death if not np.isposinf(pt.death) else 2**64)) ret.append((round(pt.birth, 2), round(pt.death, 2))) return ret
def draw_complex(X, simplicial_complex, ax=None): xmin, ymin, xmax, ymax = _get_padded_box(X, padding=0.1) if ax is None: fig = plt.figure() ax = fig.add_subplot(111) point_complex = [Simplex([i]) for i in range(len(X))] sc = point_complex + simplicial_complex sc.sort(lambda s1, s2: -cmp(s1.dimension(), s2.dimension())) for s in sc: vertices = [v for v in s.vertices] points = X[np.array(vertices)] if s.dimension() == 0: ax.scatter(X[:,0], X[:,1], c='c') else: polygon = Polygon(points, closed=False, linewidth=20) p = PatchCollection([polygon]) ax.add_collection(p) ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) return ax
def lsf(values_filename, simplices_filename): # Read vertices vertices = [] with open(values_filename) as f: for line in f: if line.startswith('#'): continue vertices.append(float(line.split()[0])) # Read simplices fltr = Filtration() with open(simplices_filename) as f: for line in f: if line.startswith('#'): continue fltr.append(Simplex(map(int, line.split()))) fltr.sort(lambda x, y: max_vertex_cmp(x, y, vertices)) # Compute persistence p = StaticPersistence(fltr) p.pair_simplices() # Output the persistence diagram smap = p.make_simplex_map(fltr) for i in p: if not i.sign(): continue b = smap[i] d = smap[i.pair()] if i.unpaired(): print b.dimension(), max_vertex(b, vertices), "inf" continue print b.dimension(), max_vertex(b, vertices), max_vertex(d, vertices)
def compute_grid_diagram(f, sublevel=True, max_death=None): """Workflow to construct a Persistence Diagram object from the level sets of the given function. Arguments 1. Returns 1. Raises 1. """ # assume kernel methods are used here if sublevel == False and max_death is None: max_death = 0 # construct list of times the simplicies are # added to the simplicial complex filtration = levelset_filtration_2d(f=f, sublevel=sublevel) # construct a simplex list for Dionysus scomplex = [Simplex(a, b) for (a, b) in filtration] # construct Dionysus filtration object filt = Filtration(scomplex, data_cmp) # compute persistent homology p = StaticPersistence(filt) p.pair_simplices(True) smap = p.make_simplex_map(filt) # generate numpy persistence diagram pd = _persistence_diagram_grid(smap, p, max_death) if not sublevel: pd[:, 1:] *= -1 pd = pd[:, (0, 2, 1)] return (dg.PersistenceDiagram(PD=pd))
def __init__(self, points, complex=None, values=None, subcomplex=None, point_size=3.): self.display_list = None PyGLWidget.__init__(self) #glEnable( GL_BLEND ) #glEnable( GL_LINE_SMOOTH ) #glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) self.point_size = point_size self.points = points if complex: self.complex = [s for s in complex] else: # Create vertex simplices if no complex provided self.complex = [Simplex([i]) for i in xrange(len(self.points))] if subcomplex: self.subcomplex = subcomplex else: self.subcomplex = [] self.values = values if not values: self.values = [0] * len(self.points) self.maxval, self.minval = max(self.values), min(self.values) center, radius = self.center_radius() self.set_radius(radius) self.set_center(center) self.make_display_list()
def parse_line(line): r, sx = line.strip()[:-1].split('<') return Simplex(map(int, sx.split(',')), float(r))
from dionysus import Simplex, Filtration, StaticPersistence, \ vertex_cmp, data_cmp, data_dim_cmp \ complex = [Simplex((0,), 0), # A Simplex((1,), 1), # B Simplex((2,), 2), # C Simplex((0,1), 2.5), # AB Simplex((1,2), 2.9), # BC Simplex((0,2), 3.5), # CA Simplex((0,1,2), 5)] # ABC print "Complex:", complex print "Vertex: ", sorted(complex, vertex_cmp) print "Data: ", sorted(complex, data_cmp) print "DataDim:", sorted(complex, data_dim_cmp) f = Filtration(complex, data_cmp) print "Complex in the filtration order:", ', '.join((str(s) for s in f)) p = StaticPersistence(f) print "Persistence initialized" p.pair_simplices(True) print "Simplices paired" smap = p.make_simplex_map(f) for i in p: print i.sign(), i.pair().sign() print "%s (%d) - %s (%d)" % (smap[i], i.sign(), smap[i.pair()], i.pair().sign()) print "Cycle (%d):" % len(i.cycle), " + ".join((str(smap[ii]) for ii in i.cycle)) print "Number of unpaired simplices:", len([i for i in p if i.unpaired()])
from dionysus import Simplex, Filtration, DynamicPersistenceChains, \ vertex_cmp, data_cmp, data_dim_cmp \ complex = [ Simplex((0, ), 0), # A Simplex((1, ), 1), # B Simplex((2, ), 2), # C Simplex((0, 1), 2.5), # AB Simplex((1, 2), 2.9), # BC Simplex((0, 2), 3.5) ] # CA print "Complex:", complex print "Vertex: ", sorted(complex, vertex_cmp) print "Data: ", sorted(complex, data_cmp) print "DataDim:", sorted(complex, data_dim_cmp) f = Filtration(complex, data_cmp) print "Complex in the filtration order:", ', '.join((str(s) for s in f)) p = DynamicPersistenceChains(f) print "Persistence initialized" p.pair_simplices() print "Simplices paired" smap = p.make_simplex_map(f) for i in p: print i.sign(), i.pair().sign() print "%s (%d) - %s (%d)" % (smap[i], i.sign(), smap[i.pair()], i.pair().sign()) print "Cycle (%d):" % len(i.cycle), " + ".join(
from dionysus import Simplex, ZigzagPersistence, \ vertex_cmp, data_cmp \ # ,enable_log complex = { Simplex((0, ), 0): None, # A Simplex((1, ), 1): None, # B Simplex((2, ), 2): None, # C Simplex((0, 1), 2.5): None, # AB Simplex((1, 2), 2.9): None, # BC Simplex((0, 2), 3.5): None, # CA Simplex((0, 1, 2), 5): None } # ABC print "Complex:" for s in sorted(complex.keys()): print s print #enable_log("topology/persistence") zz = ZigzagPersistence() # Add all the simplices b = 1 for s in sorted(complex.keys(), data_cmp): print "%d: Adding %s" % (b, s) i, d = zz.add([complex[ss] for ss in s.boundary], b) complex[s] = i if d: print "Interval (%d, %d)" % (d, b - 1) b += 1