Ejemplo n.º 1
0
def main():
    n_points = 100
    mass = np.random.rand(n_points)
    xy = np.random.randn(n_points, 2)
    accretor = EqualMassAccretor(xy, mass, 5.)
    accretor.accrete()
    node_xy = accretor.nodes()
    print node_xy.shape
Ejemplo n.º 2
0
def main():
    img_shape = (1024, 1024)
    mean = (512., 512.)
    cov = 50000. * np.array(((1., 0.5), (0.5, 1.)))
    point_xy = np.random.multivariate_normal(mean, cov, 10000)
    s = np.where((point_xy[:, 0] >= 0.) & (point_xy[:, 1] >= 0.)
                 & (point_xy[:, 0] < img_shape[0] - 1.)
                 & (point_xy[:, 1] < img_shape[1] - 1.))[0]
    point_xy = point_xy[s, :]
    point_mass = np.ones(point_xy.shape[0])

    plot_points(point_xy, img_shape, "density_demo_points")

    # Bin points with a CVT
    accretor = EqualMassAccretor(point_xy, point_mass, 100.)
    accretor.accrete()
    generator_xy = accretor.nodes()
    cvt = CVTessellation(point_xy[:, 0],
                         point_xy[:, 1],
                         point_mass,
                         node_xy=generator_xy)
    node_xy = cvt.nodes

    # FIXME issue with this rendering
    plot_voronoi_tessellation(cvt, img_shape, "density_demo_voronoi_cells")

    # Density estimate
    delaunay = DelaunayTessellation(node_xy[:, 0], node_xy[:, 1])
    dtfe = DelaunayDensityEstimator(delaunay)
    dens = dtfe.estimate_density((0., img_shape[1]), (0., img_shape[0]),
                                 cvt.node_weights)

    del_dens_map = delaunay.render_delaunay_field(
        dens,
        (0., img_shape[1]),
        (0., img_shape[0]),
        1.,
        1.,
    )

    # FIXME this is broken too
    vor_dens_map = delaunay.render_voronoi_field(
        dens,
        (0., img_shape[1]),
        (0., img_shape[0]),
        1.,
        1.,
    )

    plot_density_map(del_dens_map, "density_demo_delaunay_field")
    plot_density_map(vor_dens_map, "density_demo_voronoi_field")
Ejemplo n.º 3
0
def main():
    img_shape = (1024, 1024)
    mean = (512., 512.)
    cov = 50000. * np.array(((1., 0.5), (0.5, 1.)))
    point_xy = np.random.multivariate_normal(mean, cov, 10000)
    s = np.where((point_xy[:, 0] >= 0.) &
                 (point_xy[:, 1] >= 0.) &
                 (point_xy[:, 0] < img_shape[0] - 1.) &
                 (point_xy[:, 1] < img_shape[1] - 1.))[0]
    point_xy = point_xy[s, :]
    point_mass = np.ones(point_xy.shape[0])

    plot_points(point_xy, img_shape, "density_demo_points")

    # Bin points with a CVT
    accretor = EqualMassAccretor(point_xy, point_mass, 100.)
    accretor.accrete()
    generator_xy = accretor.nodes()
    cvt = CVTessellation(point_xy[:, 0], point_xy[:, 1], point_mass,
                         node_xy=generator_xy)
    node_xy = cvt.nodes

    # FIXME issue with this rendering
    plot_voronoi_tessellation(cvt, img_shape, "density_demo_voronoi_cells")

    # Density estimate
    delaunay = DelaunayTessellation(node_xy[:, 0], node_xy[:, 1])
    dtfe = DelaunayDensityEstimator(delaunay)
    dens = dtfe.estimate_density((0., img_shape[1]),
                                 (0., img_shape[0]),
                                 cvt.node_weights)

    del_dens_map = delaunay.render_delaunay_field(dens,
                                                  (0., img_shape[1]),
                                                  (0., img_shape[0]),
                                                  1., 1.,)

    # FIXME this is broken too
    vor_dens_map = delaunay.render_voronoi_field(dens,
                                                 (0., img_shape[1]),
                                                 (0., img_shape[0]),
                                                 1., 1.,)

    plot_density_map(del_dens_map, "density_demo_delaunay_field")
    plot_density_map(vor_dens_map, "density_demo_voronoi_field")
Ejemplo n.º 4
0
def main():
    # Set up the size of the mock image
    xRange = [0, 500]
    yRange = [0, 500]
    # We'll generate data distributed in a 2D guassian space
    x, y = guassian_point_process(250, 250, 100, 50, 20000)
    inRange = np.where((x > 0) & (y > 0)
                       & (x < xRange[1]) & (y < yRange[1]))[0]
    x = x[inRange]
    y = y[inRange]
    nStars = len(x)
    weight = np.random.uniform(0.1, 1., size=(nStars, ))

    # Centroidal Voronoi Tessellation, meeting a target cell mass
    target_mass = 20.
    xy = np.column_stack((x, y))
    gen = EqualMassAccretor(xy, weight, target_mass)
    gen.accrete()
    node_xy = gen.nodes()
    print "Original node_xy.shape", node_xy.shape
    gen.cleanup()  # re-allocate failed bins
    node_xy = gen.nodes()
    print "Cleaned node_xy.shape", node_xy.shape
    cvt = CVTessellation(x, y, weight, node_xy=node_xy)

    # Set up the pixel grid (or use set_fits_grid if using a FITS image)
    cvt.set_pixel_grid(xRange, yRange)

    # Map of Voronoi cell IDs (saving to FITS)
    cvt.save_segmap("voronoi_segmap.fits")

    # Compute Density of Points and save to FITS
    cvt.compute_cell_areas()
    cellDensity = cvt.cell_point_density(x, y, mass=weight)
    densityField = cvt.render_voronoi_field(cellDensity)
    astropy.io.fits.writeto("voronoi_densitymap.fits",
                            densityField,
                            clobber=True)

    # Save CSV table of Voronoi bins, areas, and densities
    save_cell_table(cvt, cellDensity)

    # Save a list of points with assignment to Voronoi cells
    save_cell_membership(cvt, x, y)
Ejemplo n.º 5
0
def main():
    # Set up the size of the mock image
    xRange = [0, 500]
    yRange = [0, 500]
    # We'll generate data distributed in a 2D guassian space
    x, y = guassian_point_process(250, 250, 100, 50, 20000)
    inRange = np.where((x > 0) & (y > 0)
            & (x < xRange[1]) & (y < yRange[1]))[0]
    x = x[inRange]
    y = y[inRange]
    nStars = len(x)
    weight = np.random.uniform(0.1, 1., size=(nStars,))

    # Centroidal Voronoi Tessellation, meeting a target cell mass
    target_mass = 20.
    xy = np.column_stack((x, y))
    gen = EqualMassAccretor(xy, weight, target_mass)
    gen.accrete()
    node_xy = gen.nodes()
    print "Original node_xy.shape", node_xy.shape
    gen.cleanup()  # re-allocate failed bins
    node_xy = gen.nodes()
    print "Cleaned node_xy.shape", node_xy.shape
    cvt = CVTessellation(x, y, weight, node_xy=node_xy)

    # Set up the pixel grid (or use set_fits_grid if using a FITS image)
    cvt.set_pixel_grid(xRange, yRange)

    # Map of Voronoi cell IDs (saving to FITS)
    cvt.save_segmap("voronoi_segmap.fits")

    # Compute Density of Points and save to FITS
    cvt.compute_cell_areas()
    cellDensity = cvt.cell_point_density(x, y, mass=weight)
    densityField = cvt.render_voronoi_field(cellDensity)
    astropy.io.fits.writeto("voronoi_densitymap.fits", densityField, clobber=True)

    # Save CSV table of Voronoi bins, areas, and densities
    save_cell_table(cvt, cellDensity)

    # Save a list of points with assignment to Voronoi cells
    save_cell_membership(cvt, x, y)