Example #1
0
def test_dendro():
    def counter():
        i = 0
        while True:
            yield i
            i += 1
    gen_i = counter()
    child1 = alphac.AlphaCluster(next(gen_i), 50)
    for x in range(30):
        child1.add(next(gen_i), np.random.uniform(low=30, high=49))
    child2 = alphac.AlphaCluster(next(gen_i), 70)
    for x in range(40):
        child2.add(next(gen_i), np.random.uniform(low=50, high=69))
    root = alphac.AlphaCluster(next(gen_i), 100, child1, child2)
    for x in range(100):
        root.add(next(gen_i), np.random.uniform(low=40, high=99))
    root.freeze(root)
    patches, base_width, lims = utils.dendrogram(root)
    fig, ax = plt.subplots()
    for p in patches:
        print(p)
        ax.add_artist(p)
    plt.xlim((0, 200))
    plt.ylim((10, 100))
    ax.invert_yaxis()
    plt.yscale('log')
    plt.show()
Example #2
0
def quickrun_get_membership():
    # This is for AlphaCluster and should be cleaner
    # Should easily support the MAIN_CLUSTER_THRESHOLD option
    data = get_carina()
    apy.QUIET = False
    apy.ORPHAN_TOLERANCE = 150
    apy.ALPHA_STEP = 0.97
    apy.PERSISTENCE_THRESHOLD = 3
    apy.MAIN_CLUSTER_THRESHOLD = 51
    apy.initialize(data)
    a_x = apy.recurse()
    colors, color_list, recs, base_width, lim = apy.dendrogram(a_x)
    lim_alpha_lo, lim_alpha_hi = lim
    plt.figure()
    ax = plt.subplot(122)
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax.add_artist(r)
    ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax.set_yscale("log")
    ax.set_xlabel("# triangles")
    ax.set_ylabel("$\\alpha$")
    ax.invert_yaxis()
    ax = plt.subplot(121)
    for c, ps in colors.items():
        x, y = zip(*ps)
        plt.scatter(x, y, color=c, alpha=0.8, s=1)
    ax.invert_xaxis()
    ax.set_xlabel("RA")
    ax.set_ylabel("Dec")
    plt.show()
Example #3
0
def quickrun_mean_vps():
    data = get_carina()
    apy.QUIET = False
    apy.ORPHAN_TOLERANCE = 100
    apy.ALPHA_STEP = 0.97
    apy.PERSISTENCE_THRESHOLD = 1
    apy.MAIN_CLUSTER_THRESHOLD = 51
    apy.initialize(data)
    a_x = apy.recurse()
    stack = [a_x]
    mean_vpss = []
    while stack:
        a = stack.pop()
        mean_vpss.append((a.alpha_range, a.mean_vps))
        stack += a.subclusters
    colors, color_list, recs, base_width, lim = apy.dendrogram(a_x)
    lim_alpha_lo, lim_alpha_hi = lim
    plt.figure()
    ax = plt.subplot(223)
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax.add_artist(r)
    ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax.set_yscale("log")
    ax.set_xlabel("# triangles")
    ax.set_ylabel("$\\alpha$")
    ax.invert_yaxis()
    ax = plt.subplot(221)
    for c, ps in colors.items():
        x, y = zip(*ps)
        plt.scatter(x, y, color=c, alpha=0.8, s=1)
    # ax.invert_xaxis()
    ax.invert_yaxis()
    # ax.set_xlabel("RA")
    # ax.set_ylabel("Dec")
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax = plt.subplot(122)
    print()
    coeff = (1 + np.sin(np.pi / 6)) * np.cos(np.pi / 6)
    for m, c in zip(mean_vpss, color_list):
        a_r, m_vps = m
        normed_vps = [abs(x) for x, a in zip(m_vps, a_r[:-1])]
        plt.plot(a_r[:-1], normed_vps, '-', color=c)
    ax.set_xlabel("$\\alpha$")
    ax.set_ylabel("Mean Volume per Simplex, normed to equilateral")
    ax.set_xscale("log")
    ax.set_yscale("log")
    # ax.set_xlim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    # ax.set_ylim([.01, 100])
    ax.invert_xaxis()
    plt.show()
Example #4
0
def test_compare_mst_alpha():
    data = get_carina()
    apy.QUIET = False
    apy.ORPHAN_TOLERANCE = 100
    apy.ALPHA_STEP = 0.97
    apy.PERSISTENCE_THRESHOLD = 3
    apy.MAIN_CLUSTER_THRESHOLD = 51
    apy.initialize(data)
    a_x = apy.recurse()
    colors, color_list, recs, base_width, lim = apy.dendrogram(a_x)
    lim_alpha_lo, lim_alpha_hi = lim
    plt.figure()
    ax_dend = plt.subplot(122)
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax_dend.add_artist(r)
    ax_dend.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax_dend.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax_dend.set_yscale("log")
    ax_dend.set_xlabel("# triangles")
    ax_dend.set_ylabel("$\\alpha$")
    ax_dend.invert_yaxis()
    ax_points = plt.subplot(121)
    for c, ps in colors.items():
        x, y = zip(*ps)
        plt.scatter(x, y, color=c, alpha=0.8, s=1)
    ax_points.invert_xaxis()
    ax_points.set_xlabel("RA")
    ax_points.set_ylabel("Dec")

    print("Halfway..")

    tri = apy.KEY.delaunay
    min_sp_tree = mstcluster.prepare_mst_simple(tri)
    cutoff_alpha = mstcluster.get_cdf_cutoff(min_sp_tree)
    clusters = mstcluster.reduce_mst_clusters(min_sp_tree)
    for c in clusters:
        if len(c) >= apy.ORPHAN_TOLERANCE / 2.:
            coords = np.array([tri.points[i, :] for i in c])
            hull = ConvexHull(coords)
            # noinspection PyUnresolvedReferences
            for simplex in hull.simplices:
                plt.plot(coords[simplex, 0],
                         coords[simplex, 1],
                         '-',
                         color='r',
                         lw=2)
    ax_dend.plot([0, base_width], [cutoff_alpha, cutoff_alpha], '--', 'r')
    plt.show()
Example #5
0
def quickrun_get_membership_3d():
    # This is for AlphaCluster and should be cleaner
    # Should easily support the MAIN_CLUSTER_THRESHOLD option
    data = get_ScoOB()
    apy.QUIET = False
    apy.ORPHAN_TOLERANCE = 150
    apy.ALPHA_STEP = 0.97
    apy.PERSISTENCE_THRESHOLD = 3
    apy.MAIN_CLUSTER_THRESHOLD = 51
    apy.initialize(data)
    a_x = apy.recurse()
    colors, color_list, recs, base_width, lim = apy.dendrogram(a_x)
    lim_alpha_lo, lim_alpha_hi = lim

    plt.figure()
    ax = plt.subplot2grid((2, 4), (1, 3))
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax.add_artist(r)
    ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax.set_yscale("log")
    ax.set_xlabel("# triangles")
    ax.set_ylabel("$\\alpha$")
    ax.invert_yaxis()
    ax = plt.subplot2grid((2, 4), (0, 0),
                          colspan=3,
                          rowspan=2,
                          projection='3d')
    print()
    for c, ps in colors.items():
        x, y, z = zip(*ps)
        ax.plot(x,
                y,
                zs=z,
                marker='.',
                color=c,
                alpha=0.8,
                linestyle='None',
                markersize=1)
    # alpha_of_interest = 0.484 #a_x.alpha_range[int(len(a_x.alpha_range)/2)]
    # faces = apy.alpha_surfaces(a_x, alpha_of_interest)
    # for f in faces:
    #     ax.add_collection3d(f)
    ax.set_xlabel("$\Delta$RA off center")
    ax.set_ylabel("$\Delta$Dec off center")
    ax.set_zlabel("radial distance (kpc)")
    plt.show()
Example #6
0
def speedplot(alpha_root, alpha_of_interest=None, plot_points=True):
    colors, color_list, recs, base_width, lim = apy.dendrogram(alpha_root)
    lim_alpha_lo, lim_alpha_hi = lim
    plt.figure()
    ax = plt.subplot2grid((2, 4), (1, 3))
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax.add_artist(r)
    ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax.set_yscale("log")
    ax.set_xlabel("# triangles")
    ax.set_ylabel("$\\alpha$")
    ax.invert_yaxis()
    ax = plt.subplot2grid((2, 4), (0, 0),
                          colspan=3,
                          rowspan=2,
                          projection='3d')
    print()
    if plot_points:
        for c, ps in colors.items():
            x, y, z = zip(*ps)
            ax.plot(x,
                    y,
                    zs=z,
                    marker='.',
                    color=c,
                    alpha=0.8,
                    linestyle='None',
                    markersize=1)
    if alpha_of_interest is not None:
        faces = apy.alpha_surfaces(alpha_root, alpha_of_interest)
        for f in faces:
            ax.add_collection3d(f)
    ax.set_xlabel("$\Delta$RA off center (deg)")
    ax.set_ylabel("$\Delta$Dec off center (deg)")
    ax.set_zlabel("radial \"angle\" (deg)")
    return ax
Example #7
0
import alphax_utils as apy
import matplotlib.pyplot as plt
import sys

sky = False
data_dir = "../../PyAlpha_drafting/test_data/"
# dat_file = data_dir + "filament5500.dat"
dat_file = data_dir + "filament1937460_sampleNH2_betah1.80.dat"
data = np.genfromtxt(dat_file)

apy.initialize(data)
apy.KEY.alpha_step = 0.6
apy.KEY.orphan_tolerance = 1000
apy.recurse()

rectangles, base_width, lim = apy.dendrogram()
lim_alpha_lo, lim_alpha_hi = lim
fig = plt.figure()
d_ax, m_ax, surface_plotter = apy.prepare_plots(fig)
for r in rectangles:
    d_ax.add_artist(r)
d_ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
d_ax.set_ylim([lim_alpha_lo * 0.9, lim_alpha_hi * 1.1])
if apy.DIM == 3:
    d_ax.set_xlim([175e2, 24e3])
    d_ax.set_ylim([0.227, 0.545])
d_ax.set_yscale('log')
d_ax.set_xlabel("Relative cluster size")
d_ax.set_ylabel("Alpha")
d_ax.set_xticklabels([])
d_ax.invert_yaxis()
Example #8
0
def test_AlphaCluster():
    """
    Testing ground for structure of cluster code
    Uses AlphaCluster objects
    """
    p = get_test_data()
    # print(p.shape)
    # return
    # plt.plot(p[:, 0], p[:, 1], '.')
    # plt.show()
    # return
    tri = Delaunay(p)
    ndim = p.shape[1]
    triangle_coords = p[tri.simplices, :]
    cr_array, vol_array = utils.cayley_menger_vr(triangle_coords)
    simp_sorted_idxs = np.argsort(cr_array)
    simp_lookup = [None]*tri.simplices.shape[0]

    for simp_idx in simp_sorted_idxs:
        if -1 in tri.neighbors[simp_idx]:
            # do not involve edge simplices
            continue
        # all neighboring clusters of this simplex
        neighbors = (simp_lookup[n_idx] for n_idx in tri.neighbors[simp_idx])
        # included if already counted by some cluster
        included = set(n.root for n in neighbors if n is not None)
        # circumradius of this simplex
        cr = cr_array[simp_idx]
        if len(included) == 0:
            # simplex is isolated
            assigned_cluster = alphac.AlphaCluster()
        else:
            # included in at least 1 cluster (assigned to largest/only cluster)
            assigned_cluster = max(included, key=len)
            included.remove(assigned_cluster)
            if any(len(x) < alphac.MINIMUM_MEMBERSHIP for x in included):
                # some small clusters need to be absorbed
                too_small_clusters = [x for x in included if len(x) < alphac.MINIMUM_MEMBERSHIP]
                for small_cluster in too_small_clusters:
                    assigned_cluster.engulf(small_cluster)
                    assert small_cluster.isleaf()
                    for m in small_cluster.members:
                        simp_lookup[m] = assigned_cluster
                    included.remove(small_cluster)
            # for clusters that are large enough, add as children
            assigned_cluster.add_all_children(included, cr)
        # add this simplex to its assigned cluster
        assigned_cluster.add(simp_idx, cr)
        simp_lookup[simp_idx] = assigned_cluster
    # all simplices should have the same root
    clusters = set(x.root for x in simp_lookup if x)
    root = max(clusters, key=len)
    clusters.remove(root)
    print("JOINING {} CLUSTERS".format(len(clusters)))
    root.add_all_children(clusters, max(max(x.alpha_map.keys()) for x in list(clusters)+[root]))
    root.freeze(root)

    fig = plt.figure()
    d_ax, m_ax, surface_plotter = utils.prepare_plots(fig, ndim)
    rectangles, base_width, lims = utils.dendrogram(root)
    for r in rectangles:
        d_ax.add_artist(r)
    d_ax.set_xlim((-0.05*base_width, base_width*1.05))
    d_ax.set_ylim((lims[0]*0.9, lims[1]*1.1))
    d_ax.invert_yaxis()
    d_ax.set_yscale('log')
    d_ax.set_xlabel("Relative cluster size")
    d_ax.set_ylabel("Alpha")
    d_ax.set_xticklabels([])
    surface_list, points_list = utils.alpha_surfaces(root, tri, 2e7)
    for s in surface_list:
        surface_plotter(s)
    for points, color, opacity in points_list:
        m_ax.plot(*points, marker='.', color=color, alpha=opacity, linestyle='None', markersize=1)

    # m_ax.set_xlim([4., 16])
    # m_ax.set_ylim([21, 22])
    # m_ax.set_zlim([20, 22])

    # for setter, i in zip((m_ax.set_xlim, m_ax.set_ylim, m_ax.set_zlim), range(3)):
    #     setter(np.sort(p[:, i])[(0, -1),])
    plt.show()
    return root
Example #9
0
def test_boundary_3d():
    # This is for AlphaCluster and should be cleaner
    # Should easily support the MAIN_CLUSTER_THRESHOLD option
    data = get_gaia_data()
    apy.QUIET = False
    apy.ORPHAN_TOLERANCE = 50
    apy.ALPHA_STEP = .97
    apy.PERSISTENCE_THRESHOLD = 3
    apy.GAP_THRESHOLD = 1
    apy.MAIN_CLUSTER_THRESHOLD = 51
    apy.initialize(data)
    a_x = apy.recurse()
    colors, color_list, recs, base_width, lim = apy.dendrogram(a_x)
    lim_alpha_lo, lim_alpha_hi = lim
    plt.figure()
    ax = plt.subplot2grid((2, 4), (1, 3))
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax.add_artist(r)
    ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax.set_yscale("log")
    ax.set_xlabel("# triangles")
    ax.set_ylabel("$\\alpha$")
    ax.invert_yaxis()
    ax = plt.subplot2grid((2, 4), (0, 0),
                          colspan=3,
                          rowspan=2,
                          projection='3d')
    print()
    stack = [a_x]
    while stack:
        a = stack.pop()
        for i, b_list in enumerate(a.boundary_range):
            if a.alpha_range[i] > 2.1:
                continue
            # We used to check if len(b_list) > 1 but I think that's counterproductive
            # b_list is the list of gap (set(), frozenset()) tuples for a given alpha
            if b_list:
                for b in b_list:
                    # b is a tuple of (set(), frozenset())
                    # it represents a given gap at a given alpha
                    # the set (index 0) gives the boundary edges (SimplexEdge)
                    # the frozenset (index 1) gives the boundary volume elements (SimplexNode)
                    verts = []
                    for s in b[0]:
                        # add triangles to triangulation
                        verts.append(s.coord_array())
                    tri = Poly3DCollection(verts, linewidths=1)
                    tri.set_alpha(0.2)
                    tri.set_facecolor('k')
                    tri.set_edgecolor('k')
                    ax.add_collection3d(tri)
        stack += a.subclusters
    for c, ps in colors.items():
        x, y, z = zip(*ps)
        plt.plot(x, y, zs=z, marker='.', color=c, alpha=0.8, linestyle='None')
    ax.invert_xaxis()
    ax.set_zlim([290, 310])
    ax.set_xlabel("$\Delta$RA off center")
    ax.set_ylabel("$\Delta$Dec off center")
    ax.set_zlabel("radial distance (kpc)")
    plt.show()
Example #10
0
def test_boundary():
    # This is for AlphaCluster and should be cleaner
    # Should easily support the MAIN_CLUSTER_THRESHOLD option
    data = get_carina()
    apy.QUIET = False
    apy.ORPHAN_TOLERANCE = 150
    apy.ALPHA_STEP = .97  #0.97
    apy.PERSISTENCE_THRESHOLD = 1
    apy.GAP_THRESHOLD = 15
    apy.MAIN_CLUSTER_THRESHOLD = 51
    apy.initialize(data)
    a_x = apy.recurse()
    colors, color_list, recs, base_width, lim = apy.dendrogram(a_x)
    lim_alpha_lo, lim_alpha_hi = lim
    plt.figure()
    ax = plt.subplot2grid((6, 6), (3, 4), colspan=2, rowspan=3)
    for i, r_list in enumerate(recs):
        for r in r_list:
            r.set_facecolor(color_list[i])
            ax.add_artist(r)
    ax.set_xlim([-0.05 * base_width, 1.05 * base_width])
    ax.set_ylim([lim_alpha_lo * .9, lim_alpha_hi * 1.1])
    ax.set_yscale("log")
    ax.set_xlabel("# triangles")
    ax.set_ylabel("$\\alpha$")
    ax.invert_yaxis()
    ax = plt.subplot2grid((6, 6), (0, 0), colspan=4, rowspan=5)

    print()
    stack = [a_x]
    while stack:
        a = stack.pop()
        for b_list in a.boundary_range:
            if b_list and len(b_list) > 1:
                # cool_colors = cycle(['k', 'b', 'g', 'y', 'orange', 'navy', 'cyan'])
                for b in b_list:
                    # clr = next(cool_colors)
                    for s in b[1]:
                        ax.add_artist(
                            Polygon(s.coord_array(),
                                    alpha=0.1,
                                    facecolor='k',
                                    edgecolor=None))
                        # x, y = [], []
                        # for p in e:
                        #     x.append(p[0]), y.append(p[1])
                        # if clr == 'r':
                        #     plt.plot(x, y, '--', color=clr)
                        # else:
                        #     plt.plot(x, y, '-', color=clr)
    for c, ps in colors.items():
        x, y = zip(*ps)
        plt.scatter(x, y, color=c, alpha=0.8, s=1)
    ax.invert_xaxis()
    ax.set_xlabel("RA")
    ax.set_ylabel("Dec")
    # ax = plt.subplot(223)
    # plt.scatter(apy.KEY.delaunay.points[:, 0], apy.KEY.delaunay.points[:, 1], color='k', alpha=0.6, s=1)
    # ax.invert_xaxis()
    # ax.set_xlabel("RA")
    # ax.set_ylabel("Dec")
    plt.show()