示例#1
0
 def test_prune_regions(self):
     contour_tree = ct.contour_tree(self.mesh, self.height_func)
     crit_pts0 = ct.critical_points(contour_tree)
     pits, passes, peaks = crit_pts0.pits, crit_pts0.passes, crit_pts0.peaks
     regions = ct.get_regions(contour_tree)
     domain = set()
     for r in regions.values():
         domain.update(r)
     ctree_copy = contour_tree.copy()
     thresh = 0
     while len(contour_tree) > 2:
         ct.prune_regions(contour_tree, region_func=self.region_func, threshold=thresh)
         regions = ct.get_regions(contour_tree)
         domain_pruned = set()
         for r in regions.values():
             domain_pruned.update(r)
         pruned_crit_pts = ct.critical_points(contour_tree)
         ppeaks = pruned_crit_pts.peaks
         ppits = pruned_crit_pts.pits
         ppasses = pruned_crit_pts.passes
         eq_(domain, domain_pruned)
         eq_(len(ppeaks)+len(ppits)-len(ppasses), len(pits)+len(peaks)-len(passes))
         ok_(ppeaks <= peaks)
         ok_(ppasses <= passes)
         ok_(ppits <= pits)
         if 0:
             import pylab as pl
             pl.ion()
             _nx.draw(ctree_copy)
             pl.figure()
             _nx.draw(contour_tree)
             raw_input("enter to continue")
         thresh += 1
示例#2
0
 def test_arr_full(self):
     arr = random_periodic_upsample(self.NN, 4, seed=1)
     for _ in range(4):
         _set_nbr_height_equal(arr)
     mesh = ct.make_mesh(arr)
     def height_func(n):
         return (arr[n], n)
     def region_func(r):
         hs = [height_func(p) for p in r]
         return max(hs)[0] - min(hs)[0]
     def region_area_func(r):
         return len(r)
     contour_tree = ct.contour_tree(mesh, height_func)
     def remove_edge_cb(region, interior, leaf):
         h = height_func(interior)[0]
         for pt in region:
             arr[pt] = h
     ct.prune_regions(contour_tree,
             region_func=region_area_func,
             # threshold=(arr.max()-arr.min())/4.0,
             threshold=3,
             remove_edge_cb=remove_edge_cb)
     cpts = ct.critical_points(contour_tree)
     peaks = cpts.peaks
     passes = cpts.passes
     pits = cpts.pits
     print "peaks + pits - passes = %d" % (len(peaks) + len(pits) - len(passes))
     print "len(crit_pts) = %d" % (len(peaks) + len(pits) + len(passes))
     print 'tot points covered: %d' % len(contour_tree)
     coverage = set()
     regions = ct.get_regions(contour_tree)
     for r in regions.values():
         coverage.update(r)
     eq_(len(coverage), arr.size)
     if 0:
         vis(arr, height_func=height_func, crit_pts=cpts, regions=regions)
示例#3
0
def test_contour_tree():
    ctr = 0#{{{
    flux_tube_areas_record = []
    for bx_arr, by_arr, psi_arr in h5_gen('data.h5', ('bx', 'by', 'psi')):
        b_mag = np.sqrt(bx_arr**2 + by_arr**2)
        logger("array memory size: %d" % psi_arr.nbytes)
        def height_func(n):
            return (psi_arr[n], n)
        logger("meshing array...")
        mesh = ct.make_mesh(psi_arr)
        logger("done")
        logger("mesh memory size: %d" % total_graph_memory(mesh))

        logger("computing contour tree...")
        c_tree = ct.contour_tree(mesh, height_func)
        logger("done")

        def region_func(r):
            return len(r)

        logger("pruning small regions...")
        ct.prune_regions(
                c_tree,
                region_func=region_func,
                threshold=3,
                )
        logger("done")

        logger("computing critical points...")
        cpts = ct.critical_points(c_tree)
        logger("done")

        logger("condensing small regions...")
        ct.prune_regions(
                c_tree,
                region_func=region_func,
                threshold=psi_arr.size/200,
                )
        logger("done")

        logger("computing regions...")
        regions = ct.get_regions(c_tree)
        logger("done")

        flux_tubes = ct.get_flux_tubes(c_tree)
        flux_tube_areas = np.array([len(ft) for (e, ft) in flux_tubes], dtype=np.int64)
        flux_tube_areas_record.append(flux_tube_areas)

        pickle.dump(flux_tube_areas_record, open('flux_tube_areas.dat', 'wb'))

        flux_tube_mask = np.zeros(psi_arr.shape, dtype=np.bool_)
        for (e, ft) in flux_tubes:
            for p in ft:
                flux_tube_mask[p] = True

        peaks = cpts.peaks
        passes = cpts.passes
        pits = cpts.pits
        all_cpts = peaks.union(passes).union(pits)

        logger("peaks + pits - passes = %d" % (len(peaks) + len(pits) - len(passes)))
        logger("len(crit_pts) = %d" % (len(peaks) + len(pits) + len(passes)))

        cpt_grads = [b_mag[cpt] / b_mag.min() for cpt in all_cpts]
        logger("b_mag.mean()=%f" % b_mag.mean())
        logger("b_mag.std()=%f" % b_mag.std())
        logger("b_mag.max()=%f" % b_mag.max())
        logger("b_mag.min()=%f" % b_mag.min())
        if 1:
            pl.figure()
            pl.hist(cpt_grads, bins=pl.sqrt(len(cpt_grads)))
            pl.title("cpoint gradient values")
            pl.figure()
            pl.hist(flux_tube_areas, bins=pl.sqrt(len(flux_tube_areas)))
            pl.title("flux tube areas")
            pl.figure()
            filled_arr = psi_arr.copy()
            filled_arr[flux_tube_mask] = 2*psi_arr.max()
            visualize(filled_arr, crit_pts=cpts, ncontours=None, cmap='hot', new_fig=False, save_fig='psi_flux_tubes%03d' % ctr)
            pl.figure()
            filled_arr = b_mag.copy()
            filled_arr[flux_tube_mask] = 2*b_mag.max()
            visualize(filled_arr, crit_pts=cpts, ncontours=None, cmap='hot', new_fig=False, save_fig='b_mag_flux_tubes%03d' % ctr)
            pl.close('all')
            if 0:
                filled_arr = psi_arr.copy()
                def hf((a,b)): return height_func(a), height_func(b)
                ctr = 0
                for region in sorted(regions, key=hf, reverse=True):
                    X = [_[0] for _ in regions[region]]
                    Y = [_[1] for _ in regions[region]]
                    filled_arr[X,Y] = 2*psi_arr.max()
                    if not ctr:
                        visualize(filled_arr, crit_pts=cpts, ncontours=None, cmap='gray', new_fig=False)
                        raw_input("enter to continue")
                    ctr += 1
                    ctr %= len(regions) / 20
                visualize(filled_arr, crit_pts=cpts, ncontours=None, cmap='gray', new_fig=False)
                raw_input("enter to continue")
                pl.close('all')
        ctr += 1

        del c_tree
        del regions
        del cpts#}}}