def plot_loss_against_k(save_to, losses, plot_title, y_label, losses2=None, plot_title2=None, y_label2=None): #plot loss as function of k. #Use spline to make graph smooth rcParams['figure.figsize'] = 6.5,5.5 ks = range(13) t, c, k = interpolate.splrep(ks, losses) spline = interpolate.BSpline(t, c, k) ks_new = np.linspace(0, 12, 200) plt.figure(); plt.plot(ks_new, spline(ks_new), '-', label = y_label); if not losses2: plt.plot(ks, losses, 'ro'); #Plot the dots only if only one loss is being plotted if losses2: t2, c2, k2 = interpolate.splrep(ks, losses2) spline2 = interpolate.BSpline(t2, c2, k2) plt.plot(ks_new, spline2(ks_new), '-', label = y_label2); plt.grid(); if losses2: plt.legend(loc='best', fontsize=16); #show legend only if there are 2 losses being plotted together plt.xlabel('k', fontsize=16); plt.ylabel(y_label if not losses2 else 'Loss', fontsize=16); plt.title(plot_title, fontsize=16); plt.savefig(save_to) #save figure
def scipy_spl(raw_gt_pose): spl_param_x = interpolate.splrep(raw_gt_pose[0], raw_gt_pose[1], s=0.01, k=4) spl_param_y = interpolate.splrep(raw_gt_pose[0], raw_gt_pose[2], s=0.01, k=4) spl_param_z = interpolate.splrep(raw_gt_pose[0], raw_gt_pose[3], s=0.01, k=4) spl_x = interpolate.BSpline(*spl_param_x, extrapolate=False) spl_y = interpolate.BSpline(*spl_param_y, extrapolate=False) spl_z = interpolate.BSpline(*spl_param_z, extrapolate=False) spl_txyz = np.vstack((raw_gt_pose[0], spl_x(raw_gt_pose[0]), spl_y(raw_gt_pose[0]), spl_z(raw_gt_pose[0]))) spl_v_txyz = np.vstack((raw_gt_pose[0], spl_x.derivative()(raw_gt_pose[0]), spl_y.derivative()(raw_gt_pose[0]), spl_z.derivative()(raw_gt_pose[0]))) spl_a_txyz = np.vstack( (raw_gt_pose[0], spl_x.derivative().derivative()(raw_gt_pose[0]), spl_y.derivative().derivative()(raw_gt_pose[0]), spl_z.derivative().derivative()(raw_gt_pose[0]))) return spl_txyz, spl_v_txyz, spl_a_txyz
def spline_coordinate_calculation(self, xnew: Union[list, np.array, str], k: int = 3, s: int = 0, **kwargs): arrsort = lambda xarr, zarr: np.array([[xi, zi] for xi, zi in sorted( zip(xarr, zarr), key=lambda pair: pair[0])]) if type(xnew) == list: xnew = np.array(xnew) elif type(xnew) == str: if 'n' not in kwargs.keys(): kwargs['n'] = 50 xnew = xnew.lower() if xnew == 'uniform' or xnew == 'linear': xnew = np.concatenate( (np.linspace(1, 0, kwargs['n']), np.linspace(0, 1, kwargs['n']))) elif xnew == 'cosine_spacing' or xnew == 'cosinespacing' or xnew == 'cosine': cosine = 0.5 * (1 - np.cos(np.linspace( 0, np.pi, kwargs['n']))) * self.chord xnew = np.concatenate((cosine[::-1], cosine)) idx_t_old = self.__find_turning_point() idx_t_new = self.__turning_point(xnew) upper_coordinates = arrsort(self.coordinates['x'][:idx_t_old], self.coordinates['z'][:idx_t_old]) lower_coordinates = arrsort(self.coordinates['x'][idx_t_old:], self.coordinates['z'][idx_t_old:]) tU, cU, kU = interpolate.splrep(upper_coordinates[:, 0], upper_coordinates[:, 1], k=k, s=s) tL, cL, kL = interpolate.splrep(lower_coordinates[:, 0], lower_coordinates[:, 1], k=k, s=s) upper_spline = interpolate.BSpline(tU, cU, kU, extrapolate=False) lower_spline = interpolate.BSpline(tL, cL, kL, extrapolate=False) self.coordinates = { 'x': xnew, 'z': np.concatenate((upper_spline(xnew[:idx_t_new]), lower_spline(xnew[idx_t_new:]))) } self.coordinates['z'][np.isnan(self.coordinates['z'])] = 0 return self.coordinates
def desenhaGrafico(x, y, w, xl="Nº de Elementos", yl="Tempo(s)"): fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) xnew = np.linspace(min(x), max(x), 10 * (max(x) - min(x))) a, b, c = interpolate.splrep(x, y, s=0, k=2) suave = interpolate.BSpline(a, b, c, extrapolate=False) d, e, f = interpolate.splrep(x, w, s=0, k=2) suave2 = interpolate.BSpline(d, e, f, extrapolate=False) plt.plot(xnew, suave2(xnew), label="Decrescente") plt.plot(xnew, suave(xnew), label="Aleatória") ax.legend(bbox_to_anchor=(1, 1), bbox_transform=plt.gcf().transFigure) plt.ylabel(yl) plt.xlabel(xl) plt.title('heapsort', fontsize=18) fig.savefig('heapsort.png')
def interpolate(self, nb_points): concat = self.concat_drawing() concat = concat[np.argsort(concat[:, Drawing.T]), :] p = concat.shape[1] res = np.ndarray((nb_points, p)) t = concat[:, Drawing.T] new_t = np.linspace(np.min(t), np.max(t), nb_points) res[:, Drawing.T] = new_t for axis in range(p): if axis is Drawing.T: continue elif axis is Drawing.Z: interp = np.array( interpolate.splrep(concat[:, Drawing.T], concat[:, axis], s=20, k=3)) else: interp = np.array( interpolate.splrep(concat[:, Drawing.T], concat[:, axis], s=0.01, k=3)) spline_func = interpolate.BSpline( interp[0], interp[1], interp[2], extrapolate=True) # Do I return this ? res[:, axis] = spline_func(new_t) return np.nan_to_num(res)
def bsplinterpol(x, y, x0): ''' Monte-Carlo propagated error calculator ------ INPUT ------ x in base x y in data y x0 out base x ------ OUTPUT ------ bspl(x0) B-spline interpol out data ''' mask = [] for i, yi in enumerate(y): if np.isnan(yi) == 1 or yi == 0: mask.append(i) if len(x) - len(mask) > 4: # number of knots (avoid all NaNs col) x = np.delete(x, mask) y = np.delete(y, mask) t, c, k = interpolate.splrep(x, y, s=0, k=4) # s - smooth # print('''\ # t: {} # c: {} # k: {} # '''.format(t, c, k)) bspl = interpolate.BSpline(t, c, k, extrapolate=False) return bspl(x0)
def test_1d_eval(ndspline): """ compare evaluation of 1-d NDSpline to scipy.interpolate.BSpline """ bn = ndspline query_points = get_query_points(bn) bs = interpolate.BSpline(bn.knots[0], bn.coefficients, bn.degrees[0]) for nu in range(bn.degrees[0]+1): bs.extrapolate = True bn.extrapolate = True bn.periodic = False bs_res = bs(query_points, nu).squeeze() bn_res = bn(query_points, nu).squeeze() assert_allclose(bn_res, bs_res) bs.extrapolate = False bn.extrapolate = False bs_res = bs(query_points, nu).squeeze() bn_res = bn(query_points, nu).squeeze() assert_allclose(bn_res[~np.isnan(bs_res)], bs_res[~np.isnan(bs_res)]) bs.extrapolate = 'periodic' bn.periodic = True bs_res = bs(query_points, nu).squeeze() bn_res = bn(query_points, nu).squeeze() assert_allclose(bn_res, bs_res)
def estimate_from_periodogram(self, per): """ Estimate PSD from the periodogram """ # If there is only one periodogram if type(per) == np.ndarray: self.log_psd_fn = self.spline_lsqr(per) self.beta = self.log_psd_fn.get_coeffs() elif type(per) == list: # If there are several periodograms, average the estimates spl_list = [ self.spline_lsqr(I0) for I0 in per if self.fs / len(I0) < self.f_knots[0] ] self.beta = sum([spl.get_coeffs for spl in spl_list]) / len(per) self.log_psd_fn = interpolate.BSpline(spl_list[0].get_knots(), self.beta, self.D) # Estimate psd at positive Fourier log-frequencies self.logS = self.log_psd_fn(self.logf[self.N]) # # Spline estimator of the variance of the log-PSD estimate # self.logvar_fn = interpolate.LSQUnivariateSpline(self.logf[self.N], # (np.log(I[1:self.n+1]) - self.C0 - self.logS)**2, # self.logf_knots, k=1, ext='const') # Update PSD control values (for Bayesian estimation) # self.varlogSc = self.logvar_fn(self.logfc) self.logSc = self.log_psd_fn(self.logfc)
def smooth_graph(ori_x, ori_y): import numpy as np from scipy.signal import savgol_filter import scipy.interpolate as interpolate result_list = [] + ori_y window_size = len(ori_y) // 10 sparse_flag = False if window_size < 15: sparse_flag = True window_size = len(ori_y) // 4 if window_size % 2 == 0: window_size += 1 polyorder = 3 if polyorder >= window_size: polyorder = window_size - 1 if (polyorder < 0): return result_list result_list = savgol_filter(result_list, window_size, polyorder) result_list = savgol_filter(result_list, window_size, polyorder) result_list = savgol_filter(result_list, window_size, polyorder) if sparse_flag: t, c, k = interpolate.splrep(ori_x, result_list, s=0, k=4) new_x_range = np.linspace(min(ori_x), max(ori_x), 3 * len(result_list)) spline = interpolate.BSpline(t, c, k, extrapolate=False) return (new_x_range, spline(new_x_range)) # print("length of 3rd smoothing: "+str(len(temp))) # temp = savgol_filter(temp, 161, 3) # print("length of 4th smoothing: "+str(len(temp))) return ([] + ori_x, result_list)
def create_1d_regression(data_dict, model): assert len(data_dict) == 2 pixel_indices, wavenumbers = remove_zeros_and_sort( data_dict['pixel_indices'], data_dict['wavenumbers']) if model == 'B-Spline': t, c, k = interpolate.splrep(pixel_indices, wavenumbers, k=raman_configs.DEFAULT_B_SPLINE_PARAMS_K, s=raman_configs.DEFAULT_B_SPLINE_PARAMS_S) spline = interpolate.BSpline(t, c, k, extrapolate=True) return spline if model == 'Polynomial-2': fit_params = np.polyfit(pixel_indices, wavenumbers, 2) poly = np.poly1d(fit_params) return poly if model == 'Polynomial-3': fit_params = np.polyfit(pixel_indices, wavenumbers, 3) poly = np.poly1d(fit_params) return poly if model == 'Linear': fit_params = np.polyfit(pixel_indices, wavenumbers, 1) poly = np.poly1d(fit_params) return poly if model == 'User Defined': return raman_configs.USER_DEFINED_MODEL(pixel_indices, wavenumbers) # if model is not available, do nothing return lambda x: x
def plot(self, x, N=300) : ''' Plot given vector as smoothed distribution (histogram). x has to be 1D numpy vector. Method uses 'histogram' and BSpline smoothing to display data gathered form histogram method. Distribution is rendered as curve. Args: x (array or doubles): values to plot N (int): number of points for plotting ''' import matplotlib.pyplot as plt plt.figure() plt.grid(True) bins, bin_edges = np.histogram(x, bins='auto', density=True) num_bins = len(bins) xAxis = [0.5*(bin_edges[j]+bin_edges[j+1]) for j in range(num_bins)] # smoothed curve import scipy.interpolate as interpolate from scipy.interpolate import BSpline t, c, k = interpolate.splrep(xAxis, bins) xnew = np.linspace(np.min(xAxis),np.max(xAxis),N) #300 represents number of points to make between T.min and T.max spline = interpolate.BSpline(t, c, k) plt.plot(xnew, spline(xnew), linestyle='solid', linewidth=1) plt.show() return 0
def plotSmoothCurve(self): """ Originally to smooth the sentiment graph to look like continuous values, but was less informative than discrete data points and so is never used. Left in as an ex. of splines and interpolation """ # %matplotlib inline df = pd.read_csv('../data/monthSentiment/' + 'MSFT' + '.csv') # x = np.arange(30) # y = df['Median Sentiment'] # f = interp1d(x,y, kind='linear') x = np.arange(30) y = df['Median Sentiment'] t, c, k = interpolate.splrep(x, y, s=0, k=4) N = 100 xmin, xmax = x.min(), x.max() xx = np.linspace(xmin, xmax, N) spline = interpolate.BSpline(t, c, k, extrapolate=False) # plt.plot(x, y, 'bo', label='Original points') plt.plot(xx, spline(xx), 'r', label='BSpline') # plt.plot(x, f(x)) plt.show()
def bspline_basis(n, eval_points, degree=3): n_knots = n + degree + 1 knots = np.linspace(0, 1, n_knots - 2 * degree) knots = np.r_[[0] * degree, knots, [1] * degree] basis_funcs = interpolate.BSpline(knots, np.eye(n), k=degree) Bx = basis_funcs(eval_points) return sparse.csr_matrix(Bx)
def scipy_bspline(cv, n=100, degree=3, periodic=False): """ Calculate n samples on a bspline cv : Array ov control vertices n : Number of samples to return degree: Curve degree periodic: True - Curve is closed """ cv = np.asarray(cv) count = cv.shape[0] # Closed curve if periodic: kv = np.arange(-degree, count + degree + 1) factor, fraction = divmod(count + degree + 1, count) cv = np.roll(np.concatenate((cv, ) * factor + (cv[:fraction], )), -1, axis=0) degree = np.clip(degree, 1, degree) # Opened curve else: degree = np.clip(degree, 1, count - 1) kv = np.clip(np.arange(count + degree + 1) - degree, 0, count - degree) # Return samples max_param = count - (degree * (1 - periodic)) spl = si.BSpline(kv, cv, degree) return spl(np.linspace(0, max_param, n))
def __init__(self, x, t_length, closed=True): t = np.linspace(0, t_length, num=x.size, endpoint=True) that, c, k = interpolate.splrep(t, x, s=0, k=4) self.spline = interpolate.BSpline(that, c, k, extrapolate=True) self.tmin = t.min() self.tmax = t.max() self.closed = closed
def interpolate_hotspot(N_frames, N_ave=5, N_movie_frames=1024, image_size=128): """ requires pixel_dict.pkl to be created & saved in ../data/interm """ N_ave = int(N_ave) # original had 1 period in 50 frames x = np.linspace(0, 50, N_frames * N_ave) # only calculate up to the # of frames we need x = x[:int(N_movie_frames * N_ave)] # we resized the movie to 128x128 interp_hotspot = np.zeros((len(x), image_size, image_size)) pixel_arr = np.arange(image_size) with open('../data/interim/pixel_dict.pkl', 'rb') as f: pixel_dict = pickle.load(f) for i in pixel_arr: for j in pixel_arr: t, c, k = pixel_dict[(i, j)] spline = interpolate.BSpline(t, c, k, extrapolate='periodic') interp_hotspot[:, i, j] = spline(x) # frame averaging interp_hotspot = np.array([ np.mean(interp_hotspot[i:i + N_ave, :, :], axis=0) for i in range(0, len(interp_hotspot), N_ave) ]) return (interp_hotspot)
def bspline(self, n, degree): self.points = np.asarray(self.points) count = self.points.shape[0] degree = np.clip(degree, 1, count - 1) kv = np.clip(np.arange(count + degree + 1) - degree, 0, count - degree) max_param = count - (degree * (1 - False)) spl = inter.BSpline(kv, self.points, degree) return spl(np.linspace(0, max_param, n))
def generate_Curve(coef, size, order=2): numKnotsTotal = coef.shape[0] + order + 1 numKnotsInterior = coef.shape[0] - order sep = (size + 1) / numKnotsInterior knots = np.linspace(0, (numKnotsTotal - 1) * sep, numKnotsTotal) knots -= knots[order] func = spi.BSpline(knots, coef, order, extrapolate=False) vals = func(np.arange(0, size)) return vals
def drawGraph(x, y, l, k, n, xl="Nº de Elementos", yl="Tempo(s)"): xnew = np.linspace(min(x), max(x), 10 * (max(x) - min(x))) a, b, c = interpolate.splrep(x, y, s=0, k=2) suave = interpolate.BSpline(a, b, c, extrapolate=False) plt.subplot(n) plt.plot(xnew, suave(xnew), label="Curva Suave: " + k) plt.legend(bbox_to_anchor=(1, 1), bbox_transform=plt.gcf().transFigure) plt.ylabel(yl) plt.xlabel(xl) plt.title(l, fontsize=12)
def bspline(self, k=2): c = np.array(self) n = c.shape[0] if n <= k: msg = "The number of points must be more than {}." raise ValueError(msg.format(k)) t = np.zeros(n + k + 1, dtype=np.float64) t[n + 1:] = 1 t[k:n + 1] = np.linspace(0, 1, n - k + 1) return interpolate.BSpline(t, c, k, axis=0)
def plot(line, col, data, x_index = 0, y_index = 1, filtered=True, filter_window=21): x_val = [e[x_index] for e in data] y_val = [e[y_index] for e in data] check_duplicates(x_val) if filtered: y_val = savgol_filter(y_val, filter_window, 3) t, c, k = interpolate.splrep(x_val, y_val) spline = interpolate.BSpline(t, c, k, extrapolate=False) xx = np.arange(1, x_val[-1], 50) axs[line, col].plot(xx, spline(xx), lineTypes[scenarioName], label=scenarioName)
def fit_spline(self, t, c, k): """ This methods fits t = knots, c = spline coefficients, k = B-spline order to a B-spline :param t: knots :param c: spline coefficients :param k: B-spline order :return: fitted B-spline """ return interpolate.BSpline(t, c, k)
def bspline(M, smile, sections, degree=3): idx = np.linspace(0, len(M) - 1, sections+1, endpoint=True).round(0).astype('int') x = M[idx] y = smile[idx] t, c, k = interpolate.splrep(x, y, s=0, k=degree) spline = interpolate.BSpline(t, c, k, extrapolate=True) pars = {'t': t, 'c': c, 'deg': k} points = {'x': x, 'y': y} return pars, spline, points
def bspline(x, y, sections, degree=3): idx = np.linspace(0, len(x) - 1, sections + 1, endpoint=True).round(0).astype("int") x = x[idx] y = y[idx] t, c, k = interpolate.splrep(x, y, s=0, k=degree) spline = interpolate.BSpline(t, c, k, extrapolate=True) pars = {"t": t, "c": c, "deg": k} points = {"x": x, "y": y} return pars, spline, points
def closed_bspline(self, epsilon=2, k=2): pl = self._close_polyline(epsilon=epsilon) c = np.array(pl) if np.any(c[0, :] != c[-1, :]): c = np.vstack((c, c[0, :])) c = np.vstack((c, c[1:k, :])) n = c.shape[0] dt = 1 / (n - k) t0 = -k * dt tm = 1 + k * dt t = np.linspace(t0, tm, n + k + 1) return interpolate.BSpline(t, c, k, axis=0)
def plotInterpolated(line, data, x_index=0, y_index=1): x_val = [e[x_index] for e in data] y_val = [e[y_index] for e in data] check_duplicates(x_val) t, c, k = interpolate.splrep(x_val, y_val) spline = interpolate.BSpline(t, c, k, extrapolate=False) xx = np.arange(0, 520, 20) xx[0] = 1 axs[line, i].plot(xx, spline(xx), lineTypes[algName], label=algNames[algName])
def smoothCurve(self, df): x = np.arange(30) y = df['Median Sentiment'] t, c, k = interpolate.splrep(x, y, s=0, k=4) N = 100 xmin, xmax = x.min(), x.max() xx = np.linspace(xmin, xmax, N) spline = interpolate.BSpline(t, c, k, extrapolate=False) return (xx, spline)
def fit_B_spline_vec(vec, bplot = False): n = len(vec) x_rng = range(1,n+1) t, c, k = interpolate.splrep(x_rng, vec, s=0, k=4) spline = interpolate.BSpline(t, c, k, extrapolate=False) if bplot: xx = np.linspace(min(x_rng),max(x_rng),100) plt.plot(x_rng, vec, 'bo', label='Original points') plt.plot(xx, spline(xx), 'r', label='BSpline') plt.grid() plt.legend(loc='best') plt.show() return spline
def desenhaGrafico(x, y, cor, titulo): xnew = np.linspace(min(x), max(x), 10 * (max(x) - min(x))) t, c, k = interpolate.splrep(x, y, s=0, k=2) suave = interpolate.BSpline(t, c, k, extrapolate=False) plt.figure('Gráfico 1') plt.title(titulo) plt.plot(xnew, negativo(suave(xnew)), color=cor) plt.scatter(xnew, negativo(suave(xnew)), color='#87CEFA') plt.ylabel("Intensidade da luz") plt.figure("Gráfico 2") montarimagem(negativo(suave(xnew)), titulo, color3(cor)) plt.show() plt.show()
def Init(z,r,c,z_int,s_dim) : print('shapec',np.shape(c)) print(np.shape(z)) if s_dim == 1 : t,c,k = interp.splrep(z,c,k = 5) f_i = interp.BSpline(t,c,k,extrapolate=True) c_int = f_i(z_int) elif s_dim == 2 : #zr,rr = np.meshgrid(z,r) f_i = interp.RectBivariateSpline(z,r,c, kx = 5, ky = 5, s = 0) return f_i