Ejemplo n.º 1
0
def get_concave_hull(xy, alpha):
    complex = di.Filtration()
    di.fill_alpha2D_complex(xy.tolist(), complex)

    d = defaultdict(int)
    for simplex in complex:
        if simplex.data[0] <= alpha:
            verts = list(simplex.vertices)
            if len(verts) == 3:
                i, j, k = sorted(verts)
                d[(i, j)] += 1
                d[(i, k)] += 1
                d[(j, k)] += 1
    G = nx.Graph()
    G.add_edges_from(edge for (edge, count) in d.items() if count == 1)
    components = nx.connected_components(G)
    i_largest = np.argmax([len(comp) for comp in components])
    inds = components[i_largest]
    inds_sorted = [inds[0]]
    for _ in xrange(len(inds) - 1):
        for nei in G.neighbors(inds_sorted[-1]):
            if len(inds_sorted) == 1 or nei != inds_sorted[-2]:
                inds_sorted.append(nei)
                break
    return xy[inds_sorted], inds_sorted
Ejemplo n.º 2
0
def get_concave_hull(xy,alpha):
    complex = di.Filtration()
    di.fill_alpha2D_complex(xy.tolist(), complex)


    d = defaultdict(int)
    for simplex in complex:
        if simplex.data[0] <= alpha:
            verts = list(simplex.vertices)
            if len(verts) == 3:
                i,j,k = sorted(verts)
                d[(i,j)] += 1
                d[(i,k)] += 1
                d[(j,k)] += 1
    G = nx.Graph()
    G.add_edges_from(edge for (edge,count) in d.items() if count==1)
    components = nx.connected_components(G)
    i_largest = np.argmax([len(comp) for comp in components])
    inds = components[i_largest]
    inds_sorted = [inds[0]]
    for _ in xrange(len(inds)-1):
        for nei in G.neighbors(inds_sorted[-1]):
            if len(inds_sorted) ==1 or nei != inds_sorted[-2]:
                inds_sorted.append(nei)
                break
    return xy[inds_sorted], inds_sorted
Ejemplo n.º 3
0
# (decided dynamically based on the input file)

from    dionysus        import Filtration, StaticPersistence, data_dim_cmp, vertex_cmp, \
                               fill_alpha3D_complex, fill_alpha2D_complex, points_file
from    sys             import argv, exit
from    math            import sqrt


if len(argv) < 2:
    print "Usage: %s POINTS" % argv[0]
    exit()

points = [p for p in points_file(argv[1])]
f = Filtration()
if   len(points[0]) == 2:           # 2D
    fill_alpha2D_complex(points, f)
elif len(points[1]) == 3:           # 3D
    fill_alpha3D_complex(points, f)

print "Total number of simplices:", len(f)

f.sort(data_dim_cmp)
print "Filtration initialized"

p = StaticPersistence(f)
print "StaticPersistence initialized" 

p.pair_simplices()
print "Simplices paired"

print "Outputting persistence diagram"
Ejemplo n.º 4
0
import dionysus as di
import numpy as np
import matplotlib.pyplot as plt
from collections import defaultdict
plt.close('all')
x = np.random.rand(10,2)


complex = di.Filtration()
di.fill_alpha2D_complex(x.tolist(), complex)
alphashape = [s for s in complex if s.data[0] <= .5]

plt.figure(1)

d = defaultdict(int)
for simplex in complex:
    if simplex.data[0] <= .1:
        verts = list(simplex.vertices)
        if len(verts) == 3:
            i,j,k = sorted(verts)
            d[(i,j)] += 1
            d[(i,k)] += 1
            d[(j,k)] += 1
for (edge, count) in d.items(): 
    if count ==1:
        i,j = edge
        edge_pts=np.array(x[[i,j]])
        plt.plot(edge_pts[:,0], edge_pts[:,1],'b')
        plt.annotate(str(count), edge_pts.mean(axis=0))
    
    def handle(self, *args, **options):
        source_name = args[0]
        
        past = datetime.datetime(datetime.MINYEAR, 1, 1)
        future = datetime.datetime(datetime.MAXYEAR, 12, 31)

        for ds in DataSource.objects.filter(name__contains=source_name):
            lat_points = ds.fetch_data('LocationProbe', 'LATITUDE', past, future)
            lon_points = ds.fetch_data('LocationProbe', 'LONGITUDE', past, future)
            
            pairs = []
            
            if len(lat_points) == len(lon_points):
                for i in range(0, len(lat_points)):
                    if random.random() < RAND_SELECTION:
                        lat_point = lat_points[i]
                        lon_point = lon_points[i]
                    
                        pairs.append([lat_point[1], lon_point[1]])
        
            doc = KML.kml()
            
            root = KML.Document()
            
#            for i in range(0, len(pairs)):
#                pair = pairs[i]
#
#                pm = KML.Placemark(KML.name('Point ' + str(i)))
#                point =  KML.Point(KML.coordinates(str(pair[1]) + ',' + str(pair[0])))
#                pm.append(point)
#                
#                root.append(pm)
                
            doc.append(root)
#            print('PAIRS LENGTH: ' + str(len(pairs)))

            db = DBSCAN(eps=EPS, min_samples=MIN_SAMPLES).fit(scipy.array(pairs))
            core_samples = db.core_sample_indices_
            labels = db.labels_
            
            n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
            
#            print('LABELS: ' + str(labels))

            clusters = {}
            
            for i in range(0, len(labels)):
                if i >= 0:
                    label = labels[i]
                
                    cluster = []
                
                    try:
                        cluster = clusters[str(label)]
                    except KeyError:
                        clusters[str(label)] = cluster
                    
                    pair = pairs[i]
                
                    cluster.append(pair)

#            print('Estimated number of clusters: %d' % n_clusters_)
#            print('Found number of clusters: %d' % len(clusters.keys()))
            
            for k, points in clusters.iteritems():
                pm = KML.Placemark(KML.name('Cluster ' + k))
                
                complex = dionysus.Filtration()
                dionysus.fill_alpha2D_complex(points, complex)
                
                shape = [s for s in complex if s.data[0] <= 0.5]

                mgeom = KML.MultiGeometry()
                
                for simplex in shape:
                    if simplex.dimension() == 2:
                        coords_string = ''
                        
                        vertices = list(simplex.vertices)
                        
                        one_x = points[vertices[0]][1]
                        one_y = points[vertices[0]][0]

                        two_x = points[vertices[1]][1]
                        two_y = points[vertices[1]][0]

                        three_x = points[vertices[2]][1]
                        three_y = points[vertices[2]][0]
                        
                        go_on = True
                        
                        go_on = (abs(one_x - two_x) < EPS) and (abs(one_x - three_x) < EPS) and (abs(two_x - three_x) < EPS)
                         
                        if go_on:
                            go_on = (abs(one_y - two_y) < EPS) and (abs(one_y - three_y) < EPS) and (abs(two_y - three_y) < EPS)
                                               
                        if go_on:
                            coords_string += (str(one_x) + ',' + str(one_y) + ',0\n')
                            coords_string += (str(two_x) + ',' + str(two_y) + ',0\n')
                            coords_string += (str(three_x) + ',' + str(three_y) + ',0\n')
                            coords_string += (str(one_x) + ',' + str(one_y) + ',0')

                            poly = KML.Polygon(KML.outerBoundaryIs(KML.LinearRing(KML.coordinates(coords_string))))

                            mgeom.append(poly)
                        
                pm.append(mgeom)
                root.append(pm)

            print(etree.tostring(doc, pretty_print=True))
Ejemplo n.º 6
0
import dionysus as di
import numpy as np
import matplotlib.pyplot as plt
from collections import defaultdict
plt.close('all')
x = np.random.rand(10, 2)

complex = di.Filtration()
di.fill_alpha2D_complex(x.tolist(), complex)
alphashape = [s for s in complex if s.data[0] <= .5]

plt.figure(1)

d = defaultdict(int)
for simplex in complex:
    if simplex.data[0] <= .1:
        verts = list(simplex.vertices)
        if len(verts) == 3:
            i, j, k = sorted(verts)
            d[(i, j)] += 1
            d[(i, k)] += 1
            d[(j, k)] += 1
for (edge, count) in d.items():
    if count == 1:
        i, j = edge
        edge_pts = np.array(x[[i, j]])
        plt.plot(edge_pts[:, 0], edge_pts[:, 1], 'b')
        plt.annotate(str(count), edge_pts.mean(axis=0))

#plt.figure(2)
#edges = [list(s.vertices) for s in complex if s.data[0] <= .5 and s.data[1] and len(list(s.vertices))==2]