def projectManyPoints(self, points, consecutive_data = True, print_every = 100): self.approximated_points = [] self.approximated_points_res = [] U0 = None t0 = _t() for p, i in zip(points.T, range(len(points.T))): if consecutive_data: _, _, _, _, U0 = self.projectPointsGetPoly(p, U0) else: _, _, _, _, _ = self.projectPointsGetPoly(p, U0) if i%print_every == print_every-1: print("projecting point number", i, "Av. proj. time is:", (_t()-t0)/(i+1)) return np.asarray(self.approximated_points_res).T[0]
def genGeodesDirection(self, point, direction, num_of_steps, step_size_x, step_size_y = None, num_bias_removal_iter=0, verbose = False): ''' gen_geodes_direction(self, point, direction) generates a set of points from a point "point" on (or near) the manifold, and a direction "direction", that approximates a geodesic line on the manifold from the point in the given direction. The method is an iterative process that: 1) moves a step in a direction and than project the new point on to the manifold (thus generating anew point). 2) parallel transports the direction to the new point, to get the new direction. Input: point - Dx1 array (D is the dimension of the data) Direction - dx1 array (d is the manifold dimension) num_of_steps - the number of steps to talk. step_size_x - if step_size_y == 0 than step_size_x is the step size on the tangent for each iteration step_size_y - if given, insrtead of moving on the tangent in each iteration, the points moves long the local polynomial approxiamtion, and step_size_y is the pount on the move on the "y-axis". In this case step_size_x is the initial/upperbound to the step size ''' projected_p = [] if step_size_y==None: move_with_polynomail_approx = False else: move_with_polynomail_approx = True U = None tt = _t() for ind in range(0,num_of_steps): if verbose: print("Starting iteration:",ind, "time for iteration: ",_t() - tt) tt = _t() #Projecting the point xx, q, coeffs, Base,U = self.projectPointsGetPoly(point, U0 = U, num_bias_removal_iter = num_bias_removal_iter) projected_p.append(xx) if ind == 0: direction_vec_proj = np.array([step_size_x]) else: direction_vec = projected_p[-1] - projected_p[-2] direction_vec_proj = np.dot(U.T, direction_vec) direction_vec_proj = direction_vec_proj/np.linalg.norm(direction_vec_proj) * step_size_x if move_with_polynomail_approx == True: #calculating the new point which will be delta_x away projected_r0 = self.evaluatePolynomial(coeffs, Base, np.asarray([direction_vec_proj]).T)[:,0] while np.linalg.norm( projected_r0 - xx+q)> step_size_y: direction_vec_proj = direction_vec_proj/2 if verbose: print('reducing step size to ', np.linalg.norm(direction_vec_proj), ' distance on y-axis:,', np.linalg.norm( projected_r0 - xx+q)) projected_r0 = self.evaluatePolynomial(coeffs, Base, np.asarray([direction_vec_proj]).T)[:,0] point = projected_r0 + q else: point = q + np.dot(U,direction_vec_proj) return projected_p
def approximateMany(self, point_set, flag_project = False): ''' func_value_arr = approximateMany(point_set, flag_project = False) Approximate multiple points in one function call ''' self.f_approximated_points = [] self.f_approximated_points_res = [] U0 = None t0 = _t() for p, i in zip(point_set.T, range(len(point_set.T))): _, _, _, _, U0, _ = self.approximateAtPointsGetPoly(p, U0=U0, flag_project=flag_project) if i%10 == 0: print("\tprojectAllData:: projecting point number", i) print("\tAverage ptojection time is:", (_t()-t0)/(i+1)) return np.asarray(self.f_approximated_points_res).T[0]
def getSPAM(message): if message == "=>": print('Syntax error') return '' index = message[2:-1].strip() if index == "": print('Syntax error') return '' index = index.rsplit("(", 1) try: if index[0] == "": return '' if index[1] == "": return '' except IndexError: print('Syntax error') return '' word = index[0] + " " number = index[1] try: spam = word * int(number) except (TypeError, ValueError, OverflowError, MemoryError): print('Syntax error') return '' _t(.1) return spam
# reference_tree = spectraltree.balanced_binary(num_taxa) # for x in reference_tree.preorder_edge_iter(): # x.length = 0.5 print("Genration observations by JC and HKY") observationsJC, metaJC = spectraltree.simulate_sequences( N, tree_model=reference_tree, seq_model=jc, mutation_rate=mutation_rate, alphabet="DNA") ################################# ## SNJ - Jukes_Cantor ################################# t0 = _t() snj = spectraltree.SpectralNeighborJoining(spectraltree.JC_similarity_matrix) sim = spectraltree.JC_similarity_matrix(observationsJC, metaJC) inside_log = np.clip(sim, a_min=1e-16, a_max=None) dis = -np.log(inside_log) disC = np.array(metricNearness.metricNearness(dis)) simC = np.exp(-disC) tree_rec = snj.reconstruct_from_similarity(sim, taxa_metadata=metaJC) tree_recC = snj.reconstruct_from_similarity(simC, taxa_metadata=metaJC) RFC, F1C = spectraltree.compare_trees(tree_recC, reference_tree) RF, F1 = spectraltree.compare_trees(tree_rec, reference_tree) print("###################") print("SNJ - Jukes_Cantor:") print("time:", _t() - t0) print("RF = ", RF, " F1% = ", F1)
# Define a function over the data (currently we take the z-axis) f_data = z + (0.5 - np.random.rand(*z.shape)) * fN_ratio * Mz # Create the MMLS object #------------------------- ma_f = manapprox.ManApproxF(x_vec_clean, f_data) ma_f.manifold_dim = DIM ma_f.poly_deg = DEG ma_f.sparse_factor = SPARSE_FACTOR ma_f.calculateSigma() ma_f.createTree() print(ma_f) # Approximate #------------- print("Approximating All Data points -- Take 1") t0 = _t() approximated_data = ma_f.approximateMany(ma_f.data, True) print("Finshed projection after", _t() - t0, "secs") projected_data = np.asarray(ma_f.approximated_points_res).T[0] # Plot the data #------------------------- fig = plt.figure(figsize=plt.figaspect(0.5)) ax = fig.add_subplot(1, 3, 1, projection='3d') eps = 0 axim = ax.scatter(x, y, z, c=z, cmap=color_scheme, s=100) plt.title("Clean Sample") fig.colorbar(axim) ax = fig.add_subplot(1, 3, 2, projection='3d') eps = 0
def timestamp(): """Used to timestamp IPython Notebooks when they are compiled""" from time import strftime as _t print("Compiled on: {} {}".format(_t("%m/%d/%Y"), _t("%H:%M:%S")))