def test_tracer(): from kaw_analysis import test_vcalc as tv#{{{ N = 64 dta = tv.sin_cos_prod(N, 5, 5) # dta = psi_arrs[-1] dd = field_trace.Derivator(dta, dta.shape[0], dta.shape[1]) nulls = field_trace.find_null_cells(dd) saddles = [null for null in nulls if null.is_saddle()] peaks = [null for null in nulls if not null.is_saddle()] error = 0.0 for null in nulls: error += abs(dd.perp_deriv1_interp.eval(null.x0, null.y0)) error += abs(dd.perp_deriv2_interp.eval(null.x0, null.y0)) error /= len(nulls) saddle0s = [(s.x0, s.y0) for s in saddles] peak0s = [(p.x0, p.y0) for p in peaks] # finished, traces = field_trace.make_skeleton( # arr=dta, deriv=dd, saddles=saddles, ncomb=1, start_offset=1.e-3, hit_radius=1.e-3) traces = [] for saddle in saddles: for outgoing in (True, False): for sgn in (1,-1): trace = field_trace.trace_from_saddle( arr=dta, deriv=dd, start_saddle=saddle, start_offset=sgn*1.e-2, outgoing=outgoing, timeout=0.001) traces.append(trace) if 1: import pylab as pl pl.ion() pl.imshow(dta, cmap='hot') for tr in traces: tr = np.array(tr) for i in range(0, len(tr[0]), 2): X,Y = tr[:,i], tr[:,i+1] pl.scatter(Y, X, c='r') # Plot the grid points if 0: X = np.linspace(0, dta.shape[0]-1, dta.shape[0]) for i in range(dta.shape[0]): Y = np.zeros(dta.shape[1]) Y.fill(i) pl.scatter(Y, X, c='m') X, Y = zip(*saddle0s) pl.scatter(Y, X, c='k') X, Y = zip(*peak0s) pl.scatter(Y, X, c='b') import pdb; pdb.set_trace()#}}}
def _test_find_null_2x2(): class _dummy(object): pass dd = _dummy() oeigsystem = field_trace.eigsystem try: field_trace.eigsystem = lambda x,y,z: (None, None) dd.perp_deriv1 = np.array([[-2.,-2.],[1.,1.]]) dd.perp_deriv2 = np.array([[-1., 2.],[-1., 2]]) null = field_trace.find_null_cells(dd)[0] ok_(np.allclose((null.x0, null.y0), (2./3, 1./3))) dd.perp_deriv1 = np.array([[-1.,-1.],[1.,1.]]) dd.perp_deriv2 = np.array([[-1., 1.],[-1., 1.]]) null = field_trace.find_null_cells(dd)[0] ok_(np.allclose((null.x0, null.y0), (1./2, 1./2))) finally: field_trace.eigsystem = oeigsystem
def test_level_set(): N = 64 n, m = 1, 4 test_data = tv.sin_cos_arr(N, n, m) dd = field_trace.Derivator(test_data) nulls = field_trace.find_null_cells(dd) saddles = [null for null in nulls if null.is_saddle()] peaks = [null for null in nulls if not null.is_saddle()] saddle0s = [(s.x0, s.y0) for s in saddles] peak0s = [(p.x0, p.y0) for p in peaks] x0, y0 = saddle0s[0] psi_interp = Interp2DPeriodic(N, N, test_data) level_val = psi_interp.eval(x0, y0) level_sets = [null.levelset for null in nulls] mask = field_trace.marked_to_mask(test_data.shape, level_sets) masked_data = test_data.copy() masked_data[mask] = test_data.max() if 0: import pylab as pl pl.ion() pl.imshow(masked_data, cmap='hot', interpolation='nearest') # # Plot the grid points # if 0: # X = np.linspace(0, dta.shape[0]-1, dta.shape[0]) # for i in range(dta.shape[0]): # Y = np.zeros(dta.shape[1]) # Y.fill(i) # pl.scatter(Y, X, c='m') X, Y = zip(*saddle0s) pl.scatter(Y, X, c='k') X, Y = zip(*peak0s) pl.scatter(Y, X, c='b') raw_input("enter to continue")
def test_level_set(): # N = 128#{{{ # n, m = 10, 15 # test_data = tv.sin_cos_arr(N, n, m) arr_idx = -2 test_data = psi_arrs[arr_idx].astype(np.double) N = test_data.shape[0] print("locating nulls") dd = field_trace.Derivator(test_data, N, N) nulls = field_trace.find_null_cells(dd) null0s = [(n.x0, n.y0) for n in nulls] psi_interp = Interp2DPeriodic(N, N, test_data) print("computing level sets") levels = [null.levelset for null in nulls] print("classifying nulls") peaks = [] saddles = [] for null in nulls: if null.is_saddle(): saddles.append(null) else: peaks.append(null) peak0s = [(p.x0, p.y0) for p in peaks] saddle0s = [(s.x0, s.y0) for s in saddles] print( "getting min regions") regions = [] for null in nulls: regions.extend(null.regions) print( "number of regions: %d" % len(regions)) min_regions = field_trace.filter_min_regions(regions) print( "number of min regions: %d" % len(min_regions)) print( "plotting") if 1: import pylab as pl pl.ion() pl.figure() dta = np.zeros(test_data.shape, dtype=np.int32) for min_region in min_regions: dta[min_region.xs, min_region.ys] = 1 pl.imshow(dta, interpolation='nearest') pl.figure() pl.imshow(cur_arrs[arr_idx]) pl.figure() pl.imshow(psi_arrs[arr_idx]) raw_input("enter to continue") if 0: import pylab as pl pl.ion() all_masks = field_trace.marked_to_mask(test_data.shape, levels) masked_data = test_data.copy() masked_data[all_masks] = test_data.max() pl.imshow(masked_data, cmap='hot', interpolation='nearest') X, Y = zip(*peak0s) pl.scatter(Y, X, c='k') X, Y = zip(*saddle0s) pl.scatter(Y, X, c='b') if 0: for level in levels: masked_data = test_data.copy() mask = field_trace.marked_to_mask(test_data.shape, [level]) masked_data[mask] = test_data.max() pl.imshow(masked_data, cmap='hot', interpolation='nearest') X, Y = zip(*null0s) pl.scatter(Y, X, c='k') X, Y = zip(*null0s) pl.scatter(Y, X, c='b') raw_input("enter to continue")#}}}