def run_tests(test_cases, test_sizes, n_iter): for case in test_cases: print("Curve type:", case) print("{: <15}| {: <15}| {: <15}| {: <15}".format("Size", "Energy", "Error", "Time(seconds)")) for size in test_sizes: n = size # number of points in domain t = np.linspace(0., 1., n) gamma_sol = ex.gamma_example(case)[0](t) q = ex.curve_example("circle", t)[0][0] inter_func1 = scipy.interpolate.CubicSpline(t, q) p = inter_func1(gamma_sol) elapsed = np.inf energy = 0 tg = 0 gammay = 0 for i in range(n_iter): start = time.time() energy, p_new, q_new, tg, gammay = shapedist.find_shapedist(p, q, 'ud', t1=t, t2=t) end = time.time() if start-end <np.inf: elapsed = end-start error = shapedist.find_error(tg, gammay, ex.gamma_example("bumpy")[0](tg)) print("{: <15}| {: <15}| {: <15}| {: <15}".format(str(size), "%0.10f" % energy, "%0.10f" % error, "%0.10f" % elapsed))
def calc(filename): readFile = open(filename, "r") text = readFile.read() readFile.close() text = text.split("\n") f = open(filename.split("_")[len(filename.split("_"))-1] + "_raw.txt", "w") for i in text: x = i.split(" ") m = int(x[1]) curve_type = str(x[0]) print(m) t = np.linspace(0., 1., m) # t = np.random.rand(m) # # t[0] = 0 # t[t.size - 1] = 1 # t.sort() q = ex.curve_example('bumps', t)[0][1] x_function = CubicSpline(t, q) test = ex.gamma_example(curve_type)[0](t) # # test = np.zeros(m) # # i = 1 # while i < m: # test[i] = np.random.random_sample() # i = i + 1 # test.sort() # test[m-1] = 1 # test[0] = 0 p = x_function(test) # p = np.array(np.sin(t)) # q = np.array(np.exp(t)-1) num = int(np.log2(m) - 4) domain_x, gammax, val = elastic_linear_uniform.find_gamma(np.array([t, p]), np.array([t, q]), 6, 4, num) error = elastic_linear_uniform.find_error(domain_x, ex.gamma_example(curve_type)[0](domain_x), gammax) print(error) # plt.plot(domain_x, gammax, "-r") # plt.show() f.write("Minimum Energy: " + str(val) + " Error: " + str(error) + "\n\n\n") for j in range(3): cProfile.runctx("elastic_linear_uniform.find_gamma(np.array([t, p]), np.array([t, q]), 6, 4, num)", globals(), locals(), filename="statsfile") stream = StringIO() stats = pstats.Stats('statsfile', stream=stream).sort_stats("cumulative") stats.print_stats() f.write(stream.getvalue()) f.close()
def calc(filename): readFile = open(filename, "r") text = readFile.read() readFile.close() text = text.split("\n") f = open( filename.split("_")[len(filename.split("_")) - 1] + "_raw.txt", "w") for i in text: x = i.split(" ") m = int(x[1]) curve_type = str(x[0]) print(m) t = np.linspace(0., 1., m) # t = np.random.rand(m) # # t[0] = 0 # t[t.size - 1] = 1 # t.sort() q = ex.curve_example('bumps', t)[0][0] x_function = InterpolatedUnivariateSpline(t, q) test = ex.gamma_example(curve_type)[0](t) # # test = np.zeros(m) # # i = 1 # while i < m: # test[i] = np.random.random_sample() # i = i + 1 # test.sort() # test[m-1] = 1 # test[0] = 0 p = x_function(test) # p = np.array(np.sin(t)) # q = np.array(np.exp(t)-1) num = int(np.log2(m) - 4) domain_x, gammax, val = elastic_linear_old.find_gamma( np.array([t, p]), np.array([t, q]), 6, 12, num) error = elastic_linear_old.find_error( domain_x, ex.gamma_example(curve_type)[0](domain_x), gammax)
def run_tests(ndim, test_cases, curve_name, test_sizes, n_iter, shape_reps, mode): mode = mode + "d" for shape_rep in shape_reps: for case in test_cases: eprint(curve_name.upper() + " curve, " + case.upper() + " gamma, in {} dimensions with ".format(str(ndim)) + shape_rep.__name__ + " representation") eprint("{: <15}| {: <15}| {: <15}| {: <15}| {: <15}".format( "Size", "Coarsened Size", "Energy", "Error", "Time(seconds)")) for size in test_sizes: n = size # number of points in domain t = np.linspace(0., 1., n) gamma_sol = ex.gamma_example(case)[0](t) if curve_name == "rand": q = np.random.rand(n, ndim) else: if ndim > 2: raise RuntimeWarning( "There are no synthetic curves larger than 2 dimensions currently implemented" ) q = ex.curve_example(curve_name, t)[0][:ndim].T p = np.zeros(q.shape) for d in range(ndim): inter_func = scipy.interpolate.CubicSpline(t, q[:, d]) p[:, d] = inter_func(gamma_sol) elapsed = np.inf energy = 0 tg = 0 gammay = 0 for i in range(n_iter): start = time.time() energy, p_new, q_new, tg, gammay = shapedist.find_shapedist( p, q, mode, t1=t, t2=t, shape_rep=shape_rep) end = time.time() if start - end < elapsed: elapsed = end - start error = shapedist.find_error(tg, gammay, ex.gamma_example(case)[0](tg)) eprint("{: <15}| {: <15}| {: <15}| {: <15}| {: <15}".format( str(size), str(tg.shape[0]), "%0.10f" % energy, "%0.10f" % error, "%0.10f" % elapsed)) eprint("-" * 81)
path_nodes[i][j][0] = k path_nodes[i][j][1] = l l = l + 1 k = k + 1 min_energy_values[i][j] = minimum j = j + 1 i = i + 1 m = 256 n = 256 t = np.linspace(0., 1., m) p = [0, 0] q = ex.curve_example('bumps', t)[0] x_function = InterpolatedUnivariateSpline(t, q[0]) y_function = InterpolatedUnivariateSpline(t, q[1]) test = ex.gamma_example("sine")[0](t) test1 = ex.gamma_example("sine")[0](t) # # test = np.zeros(m) # # i = 1 # while i < m: # test[i] = np.random.random_sample() # i = i + 1 # test.sort() # test[m-1] = 1
print("Loading.....") import sys import matplotlib.pyplot as plt import scipy.interpolate import shapedist from testing import examples as ex import numpy as np import cProfile import pstats from io import StringIO # One example shapdist compuation for gamma n = 4096 # number of points in domain t = np.linspace(0., 1., n) q = ex.curve_example('limacon', t)[0] p = np.zeros(q.shape) x_function = scipy.interpolate.CubicSpline(t, q[0]) y_function = scipy.interpolate.CubicSpline(t, q[1]) test = ex.gamma_example("bumpy")[0] # test1 = ex.gamma_example("sine")[0](t) p[0] = x_function(test(t)) p[1] = y_function(test(t)) p = p.T q = q.T # q = ex.curve_example("circle", t)[0].T # p = ex.curve_example("ellipse", t)[0].T
def calc(filename): readFile = open(filename, "r") text = readFile.read() readFile.close() text = text.split("\n") f = open("errors.csv", "a") f.write("\n") for i in text: x = i.split(" ") m = int(x[1]) curve_type = str(x[0]) print(m) t = np.linspace(0., 1., m) # t = np.random.rand(m) # # t[0] = 0 # t[t.size - 1] = 1 # t.sort() q = ex.curve_example('bumps', t)[0][0] x_function = InterpolatedUnivariateSpline(t, q) test = ex.gamma_example(curve_type)[0](t) # # test = np.zeros(m) # # i = 1 # while i < m: # test[i] = np.random.random_sample() # i = i + 1 # test.sort() # test[m-1] = 1 # test[0] = 0 p = x_function(test) # p = np.array(np.sin(t)) # q = np.array(np.exp(t)-1) f.write(str(m) + ",") i = 9 while i <= 9: x = int(np.log2(t.size) - 4) domain_x, gammax, val = find_gamma(np.array([t, p]), np.array([t, q]), 6, 8, x) error = find_error(domain_x, ex.gamma_example(curve_type)[0](domain_x), gammax) f.write(str(error) + ",") i = i + 1 print(error, x) plt.plot(domain_x, gammax - ex.gamma_example(curve_type)[0](domain_x), "-b") plt.plot(domain_x, gammax, ".-b") plt.plot(domain_x, ex.gamma_example(curve_type)[0](domain_x), "-r") plt.show() f.write("\n") f.close()