def main(): data = getdata() data = list(map(preprocess_row, data)) data = sorted(data, key=lambda x: x[3]) array = numpy.array(data) array = array.astype(numpy.float) X = array[:,:-1] y = array[:,-1] times = X[:,3].reshape(-1, 1) times_divided = numpy.linspace(times.min(), times.max(), times.size) # no duplicate X values xnew = numpy.linspace(times_divided.min(), times_divided.max(), times.size / 12) xs = UnivariateSpline(times_divided, y) xs.set_smoothing_factor(10) svr_rbf = sklearn.svm.SVR(kernel='rbf', C=1, gamma=5) print('fitting rbf') y_rbf = svr_rbf.fit(times, y).predict(times) fig, ax1 = plt.subplots() ax1.plot(xnew, xs(xnew), color='green') ax2 = ax1.twinx() ax2.plot(times, y_rbf, color='red') fig.tight_layout() plt.show()
def smoothfit(x, y, smooth=0, res=1000): """ Smooth data of the form f(x) = y with a spline """ z = y.copy() w = isnan(z) z[w] = 0 spl = UnivariateSpline(x, z, w=~w) spl.set_smoothing_factor(smooth) xs = linspace(min(x), max(x), res) ys = spl(xs) ys[ys < 0] = 0 if w[0]: if len(where(~w)[0]): first = where(~w)[0][0] first = x[first] first = where(xs >= first)[0][0] - 1 ys[:first] = nan if w[-1]: if len(where(~w)[0]): last = where(~w)[0][-1] last = x[last] last = where(xs >= last)[0][0] + 1 ys[last:] = nan return xs, ys
def getPeakPosition(scores, makePlot=False, plotID=None): print() spline = UnivariateSpline(scores.T[0], scores.T[1]) spline.set_smoothing_factor(0.005) xs = np.linspace(scores.T[0][0], scores.T[0][-1], 1000) data = np.vstack((xs, spline(xs))).T data_all = data.copy() data = data[data.T[0] > 4.] peaks = scipy.signal.find_peaks(data.T[1])[0] if len(peaks) == 0: selected_peak = 5 print('WARNING: no peak found') else: selected_peak = np.round( data.T[0][peaks[np.argmax(data.T[1][peaks])]], 0).astype(int) selected_peak_value = scores.T[1][np.argwhere( scores.T[0] == selected_peak)[0][0]] peaks = np.round(data.T[0][peaks], 0).astype(int) if len(peaks) != 0 else peaks #if makePlot: # makePlotOfPeak(data_all, scores, selected_peak, selected_peak_value, plotID) print(selected_peak, peaks) return selected_peak, peaks
def curve_smoothing(x, smooth_factor=0.2): plt.style.use('fivethirtyeight') xs = np.linspace(min(x), max(x), len(x)) spl = UnivariateSpline(xs, x) spl.set_smoothing_factor(smooth_factor) return spl(xs)
def compute_best_k(x, y, occurrencies, plot=False, points=1000, sf=0.9): import numpy as np if len(x) < 5: b_k = max(1, round(np.sqrt(occurrencies / 2))) if plot: import pylab pylab.plot(x, y) pylab.scatter(x[b_k], y[b_k], s=20, marker='o') pylab.text(x[b_k], y[b_k], "bestK %s" % (b_k)) return b_k, pylab return b_k from scipy.interpolate import interp1d from scipy.interpolate import UnivariateSpline spl = UnivariateSpline(x, y) spl.set_smoothing_factor(sf) xs = np.linspace(min(x), max(x), points) ys = spl(xs) idx_better_k = get_change_point(xs, ys) if plot: import pylab pylab.plot(xs, ys) pylab.scatter(xs[idx_better_k], ys[idx_better_k], s=20, marker='o') pylab.text(xs[idx_better_k], ys[idx_better_k], "bestK %s" % (np.round(xs[idx_better_k]))) return int(np.round(xs[idx_better_k])), pylab return int(np.round(xs[idx_better_k]))
def fit_piecewise_smoothing(x, y, smoothing_factor=0.05): """ piecewise_smoothing, default 0.85 :param x: :param y: :param w: :param smoothing_factor: :return: """ # print('piecewise_smoothing') # print(f'len(x) = {len(x)}, x={x}') # print(f'len(y) = {len(y)}, y={y}') # print(f'len(w) = {len(w)}, w={w}') # k = 1 is the best if the annotation is very sparse, # and more importantly, the fitting results are determined. # k = len(y) -1 if len(y) <= 3 else 3 k = 1 # print(f'k = {k}, smoothing_factor={smoothing_factor}') spl = UnivariateSpline(x, y, k=k) # print(f) if k > 1: spl.set_smoothing_factor(smoothing_factor) # else: # spl.set_smoothing_factor(0) return spl
def smooth(self, genome, which_x, which_y): interpolationPointsQty = SMOOTHING_WINDOW which_y_InterpolationNeighborhood = interpolationPointsQty / 2 minimunInterpolationNeighborhoodSize = interpolationPointsQty / 4 if which_y - interpolationPointsQty / 2 < 0: interpolationPointsQty -= abs(which_y - which_y_InterpolationNeighborhood) * 2 which_y_InterpolationNeighborhood = interpolationPointsQty / 2 elif which_y + interpolationPointsQty / 2 > genome.getHeight() - 1: interpolationPointsQty -= (which_y + which_y_InterpolationNeighborhood - (genome.getHeight() - 1)) * 2 which_y_InterpolationNeighborhood = interpolationPointsQty / 2 if which_y_InterpolationNeighborhood >= minimunInterpolationNeighborhoodSize: x = np.ndarray(interpolationPointsQty) y = np.ndarray(interpolationPointsQty) for k in xrange(interpolationPointsQty): poseToSmooth = which_y - which_y_InterpolationNeighborhood + k x[k] = poseToSmooth y[k] = genome[poseToSmooth][which_x] spl = UnivariateSpline(x, y) spl.set_smoothing_factor(SPLINE_SMOOTHING_FACTOR_SPLINE/10) for k in xrange(interpolationPointsQty): if y[k] != sysConstants.JOINT_SENTINEL: newValue = spl(int(x[k])) genome.setItem(int(x[k]), which_x, newValue)
def smoothfit(x, y, smooth=0, res=1000): """ Smooth data of the form f(x) = y with a spline """ z = y.copy() w = isnan(z) z[w] = 0 spl = UnivariateSpline(x, z, w=~w) spl.set_smoothing_factor(smooth) xs = linspace(min(x), max(x), res) ys = spl(xs) ys[ys < 0] = 0 if w[0]: if len(where(~w)[0]): first = where(~w)[0][0] first = x[first] first = where(xs >= first)[0][0] - 1 ys[:first] = nan if w[-1]: if len(where(~w)[0]): last = where(~w)[0][-1] last = x[last] last = where(xs >= last)[0][0] + 1 ys[last:] = nan return xs, ys
def spline_smooth(y, window): from scipy.interpolate import UnivariateSpline x = np.arange(len(y)) no_nan = np.where(np.isfinite(y)) spl = UnivariateSpline(x[no_nan], y[no_nan], s=len(y) / window, k=4) spl.set_smoothing_factor(0.25) return spl(x)
def fit_frc_curve(data_set, degree, fit_type='spline'): """ Calculate a least squares curve fit to the FRC Data :return: None. Will modify the frc argument in place """ assert isinstance(data_set, FourierCorrelationData) data = data_set.correlation["correlation"] if fit_type == 'smooth-spline': equation = UnivariateSpline(data_set.correlation["frequency"], data) equation.set_smoothing_factor(0.25) # equation = interp1d(data_set.correlation["frequency"], # data, kind='slinear') elif fit_type == 'spline': equation = interp1d(data_set.correlation["frequency"], data, kind='slinear') elif fit_type == 'polynomial': coeff = np.polyfit(data_set.correlation["frequency"], data, degree, w=1 - data_set.correlation["frequency"]**3) equation = np.poly1d(coeff) else: raise AttributeError(fit_type) data_set.correlation["curve-fit"] = equation( data_set.correlation["frequency"]) return equation
def tune_smoothness(self): sum_res = [[] for _ in range(len(self.t))] loo = LeaveOneOut() sweep = [0] + list(np.logspace(-4, 4, 100)) + [len(self.t)] with Timer( f"CV loop for SmoothingSplinePreprocessor on {self.spline_id}" ): for case_idx, (train_index, test_index) in enumerate(loo.split(self.t)): if self.weights is not None: w = self.weights.iloc[train_index] else: w = None X_train, X_test = ( self.t.iloc[train_index], self.t.iloc[test_index], ) y_train, y_test = ( self.y.iloc[train_index], self.y.iloc[test_index], ) spl = UnivariateSpline(X_train, y_train, w=w) for s in sweep: spl.set_smoothing_factor(s=s) sum_res[case_idx].append( np.square(float(y_test - spl(X_test)))) total = np.sum(np.array(sum_res), axis=0) s_opt_idx = np.argmin(total) self.s = sweep[s_opt_idx]
def plot_variability(lst): x=np.linspace(1,1474,1474) y=lst spl = UnivariateSpline(x, y) xs=np.linspace(1,1474,1474) spl.set_smoothing_factor(80) plt.plot(xs,spl(xs)) plt.show()
def interpolate(self, x): if (x < self.bot_bound[0]): return self.bot_bound[1] elif x > self.top_bound[0]: return self.top_bound[1] spl = UnivariateSpline(list(self.values.keys()), list(self.values.values())) spl.set_smoothing_factor(0.5) return spl(x)
def my_interpolate(x,y,smoothing_factor): from scipy.interpolate import UnivariateSpline spl = UnivariateSpline(x, y) spl.set_smoothing_factor(smoothing_factor) new_x = np.linspace(np.min(x),np.max(x),200) new_y = spl(new_x) return new_y
def do_the_job(file_name): data_x, data_y, data_z = get_data(file_name) shape_x = len(np.unique(data_x)) shape_y = len(np.unique(data_y)) X = data_x.reshape(shape_x, shape_y) Y = data_y.reshape(shape_x, shape_y) Z = data_z.reshape(shape_x, shape_y) fig = plt.figure(figsize=(20, 10)) ax1 = fig.add_subplot(121) ax1.pcolormesh(X, Y, Z) #ax1.pcolor(X, Y, Z, norm=LogNorm()) angle_and_intensity_average = radial_average(data_x, data_y, data_z) # normalize to 1 angle_and_intensity_average = ( angle_and_intensity_average - angle_and_intensity_average.min() ) / (angle_and_intensity_average.max() - angle_and_intensity_average.min()) x = np.arange(450) ax2 = fig.add_subplot(122) ax2.plot(x, angle_and_intensity_average, 'b.', label="raw") # histogram / rebinning # the histogram of the data # Integration n_bins = 50 bin_means, bin_edges, binnumber = stats.binned_statistic( x, angle_and_intensity_average, statistic='sum', bins=n_bins) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width / 2 # normalize to 1 bin_means = (bin_means - bin_means.min()) / (bin_means.max() - bin_means.min()) ax2.plot(bin_centers, bin_means, 'r--', label="binning") # Spline interpolation spl = UnivariateSpline(bin_centers, bin_means) spl.set_smoothing_factor(0.5) xs = np.linspace(bin_centers.min(), bin_centers.max(), 1000) ax2.plot(xs, spl(xs), 'g', label="spline") ## Fit 2 gaussians: [center, amplitude, width] guess = [180, 1, 30, 360, 1, 30] popt, pcov = curve_fit(multiple_gaussian, xs, spl(xs), p0=guess) print 80 * "-" print "Gaussian1 center = %.2f, amplitude = %.2f, std = %.2f, FWHM = %.2f" % ( popt[0], popt[1], popt[2], 2 * np.sqrt(2 * np.log(2)) * popt[2]) print "Gaussian2 center = %.2f, amplitude = %.2f, std = %.2f, FWHM = %.2f" % ( popt[3], popt[4], popt[5], 2 * np.sqrt(2 * np.log(2)) * popt[5]) fit = multiple_gaussian(xs, *popt) ax2.plot(xs, fit, 'y', label="gaussian") ax2.legend() plt.show()
def interpolate(self, genome, which_x, which_y, wich_y_is_fixed_data=0): interpolationPointsQty = SMOOTHING_WINDOW which_y_InterpolationNeighborhood = interpolationPointsQty / 2 minimunInterpolationNeighborhoodSize = interpolationPointsQty / 4 array_size = 0 if which_y - which_y_InterpolationNeighborhood < 0: interpolationPointsQty -= abs(which_y - which_y_InterpolationNeighborhood) * 2 which_y_InterpolationNeighborhood = interpolationPointsQty / 2 elif which_y + interpolationPointsQty / 2 > genome.getHeight() - 1: interpolationPointsQty -= (which_y + which_y_InterpolationNeighborhood - (genome.getHeight() - 1)) * 2 which_y_InterpolationNeighborhood = interpolationPointsQty / 2 interpolationWindowRadius = interpolationPointsQty / 4 if which_y_InterpolationNeighborhood >= minimunInterpolationNeighborhoodSize: array_size = interpolationPointsQty - interpolationWindowRadius * 2 if wich_y_is_fixed_data: array_size += 1 x = np.ndarray(array_size) y = np.ndarray(array_size) splineIndexCounter = 0 for k in xrange(interpolationPointsQty + 1): poseToSmooth = which_y - which_y_InterpolationNeighborhood + k if poseToSmooth <= which_y - interpolationWindowRadius or poseToSmooth > which_y + interpolationWindowRadius: x[splineIndexCounter] = poseToSmooth y[splineIndexCounter] = genome[poseToSmooth][which_x] splineIndexCounter += 1 if wich_y_is_fixed_data: x[splineIndexCounter] = which_y y[splineIndexCounter] = genome[which_y][which_x] splineIndexCounter += 1 if genome[which_y - interpolationWindowRadius][which_x] == genome[which_y + interpolationWindowRadius][wich_x]: spl = interp1d(x, y) else: x_order = np.argsort(x) spl = UnivariateSpline(x_order, y) spl.set_smoothing_factor(SPLINE_SMOOTHING_FACTOR_INTERPOLATION/10) for k in xrange(interpolationPointsQty): iter = which_y - which_y_InterpolationNeighborhood + k if genome[iter][which_x] != sysConstants.JOINT_SENTINEL: if iter > which_y - interpolationWindowRadius and iter <= which_y + interpolationWindowRadius: if wich_y_is_fixed_data: #if fixed data do not change the which_y point if iter != which_y: newValue = spl(iter) genome.setItem(iter, which_x, newValue) else: newValue = spl(iter) genome.setItem(iter, which_x, newValue)
def __perform(self, dataset): factor = self.model.custom_data['Factor'] for cd in dataset.curves_data.all(): yvec = cd.yVector xvec = cd.xVector spline_fit = UnivariateSpline(xvec, yvec) spline_fit.set_smoothing_factor(factor) newyvec = spline_fit(xvec) dataset.updateCurve(self.model, cd, newyvec) dataset.save()
def fit_SED(SUv_MIr, FIR, PHOT, z): lam, f = np.loadtxt(SUv_MIr, comments="#", usecols=(2, 1), unpack=True) F = lam * f * 1.e10 Lam = lam * 1.e-4 #wavelength on microns lam, f = np.loadtxt(FIR, comments="#", usecols=(2, 1), unpack=True) FIR_lam, FIR_f = np.loadtxt(FIR, comments="#", usecols=(0, 1), unpack=True) #passing the lambda to emitted, since it is in rest frame, i.e. redshifting FIR_lam = FIR_lam * (1. + z) FIR_F = FIR_lam * FIR_f * 10000000 * 1.e10 #extrapolation of both sides (initial and final) FIR_lam_i = FIR_lam[0] - (FIR_lam[2] - FIR_lam[1]) FIR_F_i = FIR_F[0:5].mean() L = len(FIR_lam) FIR_lam_f = FIR_lam[L - 1] + (FIR_lam[L - 2] - FIR_lam[L - 3]) FIR_F_f = FIR_F[L - 5:L - 1].mean() FIR_F = np.concatenate((FIR_F_i, FIR_F, FIR_F_f), axis=None) FIR_lam = np.concatenate((FIR_lam_i, FIR_lam, FIR_lam_f), axis=None) nu_phot, f_phot = np.loadtxt(PHOT, comments="#", usecols=(0, 1), unpack=True) F_phot = f_phot * 1.e-23 * nu_phot * 1.e10 lam_phot = (c / nu_phot) * 1.e6 #fit of the mm+ tail mask = lam_phot > 200. F_phot = F_phot[mask] lam_phot = lam_phot[mask] F_phot_s = [] lam_phot_s = [] bins = np.logspace(2.2, 6, 30) for i in range(len(bins) - 1): mask0 = lam_phot > bins[i] mask1 = lam_phot < bins[i + 1] mean_bin = (bins[i] + bins[i + 1]) / 2. mask = mask0 * mask1 if len(F_phot[mask]) > 1: F_phot_s.append(F_phot[mask].mean()) lam_phot_s.append(mean_bin) if len(F_phot[mask]) == 1: F_phot_s.append(F_phot[mask][0]) lam_phot_s.append(mean_bin) if len(F_phot[mask] == 0): pass F_phot_s = np.asarray(F_phot_s) lam_phot_s = np.asarray(lam_phot_s) xs = np.logspace(2.3, 6, 500) interp = np.interp(xs, lam_phot_s, F_phot_s) logx = np.log(lam_phot_s) logy = np.log(F_phot_s) interp = UnivariateSpline(logx, logy) interp.set_smoothing_factor(0.5) yfit = lambda x: np.exp(interp(np.log(x))) lam_tot = np.concatenate((Lam, FIR_lam, xs), axis=None) F_tot = np.concatenate((F, FIR_F, yfit(xs)), axis=None) return lam_tot, F_tot
def Interpolate_With_Smooth_Spline(data, smooth=0.85): N = len(data) x = np.linspace(1, N, N) y = data w = np.isnan(y) s = UnivariateSpline(x[~w], y[~w]) s.set_smoothing_factor(smooth) return s(x)
def get_startx(source, reqY): rawX = [] rawY = [] for now, vel in source(filename): rawX.append(now) rawY.append(vel) sgX, sgY = subgraph(rawX, rawY, 15, 25) xs = np.linspace(sgX[0], sgX[-1], 1000) spl = UnivariateSpline(sgX, sgY) spl.set_smoothing_factor(2) return resolve(xs, spl(xs), 20)
def sentiment(cluster): cluster['net'] = cluster.pos_score - cluster.neg_score smoothing_factor = 0.02 year_df_m = cluster.groupby('year').mean().reset_index() ts = year_df_m[['year','net']] #ts = ts[ts.year >= 1850] spl = UnivariateSpline(ts.year, ts.net) spl.set_smoothing_factor(smoothing_factor) xs = np.linspace(1605,2017,413) stdev = np.std(cluster.net) spline_df = pd.DataFrame({'year': xs, 'spline_mean': spl(xs)}) spline_df['mean_plus'] = spline_df.spline_mean + stdev spline_df['mean_minus'] = spline_df.spline_mean - stdev cluster_with_mean = pd.merge(cluster[['year', 'author', 'book_title', 'net']], spline_df, on = 'year') above_sd = (cluster_with_mean.net > cluster_with_mean.mean_plus) below_sd = (cluster_with_mean.net < cluster_with_mean.mean_minus) cluster_with_mean['category'] = 1.*above_sd - 1.*below_sd fig = plt.figure() fig.suptitle('Net Sentiment', fontsize=12, fontweight='bold') ax = fig.add_subplot(111, axisbg = 'white') ## all books plt.plot(spline_df.year, spline_df.spline_mean, linewidth = 2, c = 'k') plt.plot(spline_df.year, spline_df.mean_plus, linewidth = 1, c = 'k', ls = '--') plt.plot(spline_df.year, spline_df.mean_minus, linewidth = 1, c = 'k', ls = '--') c_pos = cluster_with_mean[cluster_with_mean.category == 1] c_neg = cluster_with_mean[cluster_with_mean.category == -1] c_neu = cluster_with_mean[cluster_with_mean.category == 0] plt.scatter(c_pos.year, c_pos.net, s = 3, color = '#3498DB', edgecolors = None) plt.scatter(c_neg.year, c_neg.net, s = 3, color = '#CD5C5C', edgecolors = None) plt.scatter(c_neu.year, c_neu.net, s = 3, color = '#d3d3d3', edgecolors = None) plt.axis([1850,1923,-0.2,0.3]) plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) fig.savefig('sentiment_over_time.png', facecolor='white', edgecolor='none') return cluster_with_mean.category
def spline_j(c): def j(frac, dndt): return -1 / (c.dTdt * c.area * (frac * c.N)) * (dndt * c.N) x, y = c.frac[:, 0], c.frac[:, 1] x.sort() #must be sorted this way spl = UnivariateSpline(x, y) spl.set_smoothing_factor(0.01) xs = np.linspace(x[0], x[-1], 1000) n = list(spl(xs)) dndT = list(spl.derivative(1)(xs)) y = [j(n[i], val) for i, val in enumerate(dndT)] return xs, y
def extract_smooth_fapar(product="fapar", year=2018, smoother=100): golden_ratio = 0.61803398875 mask57 = 0b11100000 # Select bits 5, 6 and 7 product = product.lower() year = year-2003 x = np.arange(1, 366, 8) xs = np.arange(1, 366) y = np.loadtxt(f"data/mcd15_{product}_2003_2018_-022611_106965.txt")[:, year] qa = np.loadtxt("data/mcd15_qa_2003_2018_-022611_106965.txt", dtype=np.uint8)[:, year] unc = np.power(golden_ratio, np.right_shift(np.bitwise_and(qa, mask57), 5).astype(np.float32)) spl = UnivariateSpline(x, y, w=(1./unc)**2) spl.set_smoothing_factor(smoother) return spl(xs)
def filter(data): data = np.array(data) # filter_detrend = signal.detrend(data) # baseline drift notch_b, notch_a = signal.iirnotch(0.4, 30.0) filter_data_1 = signal.filtfilt(notch_b, notch_a, data) b, a = signal.butter(8, [0.008, 0.4], btype='bandpass', analog=False) # 1Hz-50Hz filter_data_2 = signal.filtfilt(b, a, filter_data_1) # numpy.ndarray # smooth s_x = np.linspace(0, len(filter_data_2)-1, len(filter_data_2)) spl = UnivariateSpline(s_x, filter_data_2, s=2) spl.set_smoothing_factor(0.5) data_list = spl(s_x) # list return data_list.tolist()
def _generate_population(self): # draw_axe(plt, 5) # const draw_axe(plt, 8) # params lux = 1.0 dis = 1.0 S = -0.1 B = 0.0 # raw calculations B = B * constants.richness_inf S *= 0.5 dis *= 2 K = dis/lux x = [i for i in frange(-0.1, 6, 0.2)] y = [K/(i + K*k - S*0.5) - K*k + B + S*0.5 for i in x] # plt.plot(x, y) spl = UnivariateSpline(x, y) spl.set_smoothing_factor(0.1) x = [i for i in frange(-0.1, 6, 0.001)] plt.plot(x, spl(x)) # params lux = 1.0 dis = 1.0 S = 0 B = 0.0 t = 2 # raw calculations B = B * constants.richness_inf S *= 0.5 dis *= 2 K = dis/lux x = [i for i in frange(-0.1, 6, 0.2)] y = [(K/(i + K*k - S) - K*k + B + S)*t for i in x] # plt.plot(x, y) spl = UnivariateSpline(x, y) spl.set_smoothing_factor(0.1) x = [i for i in frange(-0.1, 6, 0.001)] plt.plot(x, spl(x), color="red") plt.show()
def do_the_job(file_name): data_x, data_y, data_z = get_data(file_name) shape_x = len(np.unique(data_x)) shape_y = len(np.unique(data_y)) X = data_x.reshape(shape_x, shape_y) Y = data_y.reshape(shape_x, shape_y) Z = data_z.reshape(shape_x, shape_y) fig = plt.figure(figsize=(20, 10)) ax1 = fig.add_subplot(121) ax1.pcolormesh(X,Y,Z) #ax1.pcolor(X, Y, Z, norm=LogNorm()) angle_and_intensity_average = radial_average(data_x, data_y, data_z) # normalize to 1 angle_and_intensity_average = (angle_and_intensity_average - angle_and_intensity_average.min()) / (angle_and_intensity_average.max() - angle_and_intensity_average.min()) x = np.arange(450) ax2 = fig.add_subplot(122) ax2.plot(x,angle_and_intensity_average,'b.',label="raw") # histogram / rebinning # the histogram of the data # Integration n_bins = 50 bin_means, bin_edges, binnumber = stats.binned_statistic(x, angle_and_intensity_average, statistic='sum', bins=n_bins) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width/2 # normalize to 1 bin_means = (bin_means - bin_means.min()) / (bin_means.max() - bin_means.min()) ax2.plot(bin_centers,bin_means,'r--', label="binning") # Spline interpolation spl = UnivariateSpline(bin_centers, bin_means) spl.set_smoothing_factor(0.5) xs = np.linspace(bin_centers.min(), bin_centers.max(), 1000) ax2.plot(xs, spl(xs), 'g',label="spline") ## Fit 2 gaussians: [center, amplitude, width] guess = [180, 1, 30, 360, 1, 30] popt, pcov = curve_fit(multiple_gaussian, xs, spl(xs), p0=guess) print 80*"-" print "Gaussian1 center = %.2f, amplitude = %.2f, std = %.2f, FWHM = %.2f"%(popt[0],popt[1],popt[2], 2 * np.sqrt(2*np.log(2))*popt[2]) print "Gaussian2 center = %.2f, amplitude = %.2f, std = %.2f, FWHM = %.2f"%(popt[3],popt[4],popt[5], 2 * np.sqrt(2*np.log(2))*popt[5]) fit = multiple_gaussian(xs, *popt) ax2.plot(xs, fit , 'y', label="gaussian") ax2.legend() plt.show()
def my_interpolate(x, y, smoothing_factor): k = 3 if len(x) > k: from scipy.interpolate import UnivariateSpline spl = UnivariateSpline(x, y, k=k) spl.set_smoothing_factor(smoothing_factor) new_x = x # new_x = np.linspace(np.min(x),np.max(x),200) new_y = spl(new_x) else: new_y = y return new_x, new_y
def vel(phi,phi_w,v_max,v_w): # possible degree between -180 and 180 # angle starts from x-Axis and is in counterclock direction positiv # and with cllock direction negativ #velocity polar diagram (numbers can be ignored) v30 = 0.756 v45 = 0.837 v60 = 0.895 v75 = 0.93 v90 = 0.988 v105 = 1 v120 = 0.965 v135 = 0.93 v150 = 0.907 v165 = 0.884 x=np.array([0,15,30,45,60,75,90,105,120,135,150,165,180]) y=np.array([0,v165, v150,v135,v120,v105,v90,v75,v60, v45,v30,0,0]) w=np.array([100,100,50,50,50,50,50,50,50,50,50,100,100]) spl = UnivariateSpline(x, y, w,bbox=[None, None], k=5) xs = np.linspace(0, 180, 10000) spl.set_smoothing_factor(0.5) # phi_b is the angle between boat angle and wind angle phi_b = phi - phi_w # narrowed angle range from -180 till 180 if phi_b <= -180: phi_b = phi_b + 360 if phi_b > 180: phi_b = phi_b - 360 # velocity of boat is either v_max or v_wind if v_max < v_w: v_akt = v_max else: v_akt = v_w # interpolating the velocity depending of phi_b if 0 <= abs(phi_b) <= 15: vel = 0 elif abs(phi_b) > 150: vel = 0 else: vel = spl(abs(phi_b))+0 return vel * v_akt
def plot_by_cluster_single_dr(cluster, moving_avg_df, labels, color_map, agg_type ='sum', filename_out = 'cluster_over_time.png', title = 'Topic Cluster Over Time', wide = 1, cluster_num = 2): book_by_year = pd.DataFrame(cluster.groupby('year').count()['author']).reset_index() book_by_year.columns = ['year', 'total'] cluster3_by_year = pd.DataFrame(cluster[cluster.cluster_5 == 1].groupby('year').count()['author']).reset_index() cluster3_by_year.columns = ['year', 'cluster_num'] ww = pd.merge(book_by_year, cluster3_by_year, on = 'year', how = 'left') ww = ww.fillna(0) ww['pct'] = ww.cluster_num / ww.total if wide == 1: fig = plt.figure(figsize = (10,6)) else: fig = plt.figure(figsize = (6,6)) fig.suptitle(title, fontsize=12, fontweight='bold') ax = fig.add_subplot(111, axisbg = 'white') cluster_range = xrange(0,5) column_name = 'cluster_{}'.format(cluster_num) #spl = UnivariateSpline(ww.year, ww.pct) spl = UnivariateSpline(moving_avg_df.year, moving_avg_df[column_name]/moving_avg_df.cluster_total) xs = np.linspace(1800,1923, 1000) spl.set_smoothing_factor(.05) plt.plot(xs, np.clip(spl(xs),0,1), label = '"Drawingroom" Books (% Total Novels)', color = color_map[cluster_num], linewidth = 2) occupation_df = pd.read_csv('occupation.csv') plt.plot(occupation_df.year, occupation_df.class1_pct, color = 'k', linewidth = 2, linestyle = '--', label = 'Professional Class (% Labor Force)') plt.legend(fontsize = 'small', frameon = False, loc="best") plt.scatter(ww.year, ww.pct, s = 5, color = color_map[cluster_num]) plt.axis([1800,1923,0,1.01]) vals = ax.get_yticks() ax.set_yticklabels(['{:3.0f}%'.format(x*100) for x in vals]) plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) fig.savefig(filename_out, facecolor='white', edgecolor='none')
def isolate_events(line,tolerance,sigma,smoothing_window=55,tail_factor=0.1): # baseline, noise = determine_baseline(line) x = np.arange(len(line)) spline = UnivariateSpline(x,line) spline.set_smoothing_factor(0) spline_deriv = spline.derivative() smooth_deriv_abs = wiener(abs(spline_deriv(x)),smoothing_window) deriv_baseline, deriv_noise = determine_baseline(smooth_deriv_abs,sigma) deriv_threshold = deriv_baseline+deriv_noise # plt.plot(smooth_deriv_abs) # plt.plot(smooth_deriv_abs*0+deriv_threshold) # plt.show() cross_point_groups = [] i = 0 while i < len(smooth_deriv_abs)-1: if smooth_deriv_abs[i] < deriv_threshold and smooth_deriv_abs[i+1] >= deriv_threshold: # print("Found first upper") cross_up = i+1 i += 1 while i < len(smooth_deriv_abs) and smooth_deriv_abs[i] > deriv_threshold: i += 1 cross_down = i-1 cross_point_groups.append([cross_up,cross_down]) else: i += 1 line_threshold = line.mean()+tolerance events = [] cross_I = 0 while cross_I < len(cross_point_groups): if line[cross_point_groups[cross_I][0]] < line_threshold: event_start = cross_point_groups[cross_I][0] event_end = None #use a while True: if flag: break to emulate a do while loop while True: event_end = cross_point_groups[cross_I][1] if line[event_end] < line_threshold: break else: cross_I += 1 if event_end - event_start > 0 and line[event_start:event_end].max() > line_threshold: center = (event_start+event_end)/2 split = (event_end - event_start)/2 scaled = split*(1+tail_factor) event_start = max(0,int(center - scaled)) event_end = min(len(line)-1,int(center+scaled)) events.append([event_start,event_end]) cross_I += 1 event_slices = list(map(lambda event: line[event[0]:event[1]], events)) return (events,event_slices)
def plot_by_cluster2(cluster, labels, color_map, agg_type ='sum', filename_out = 'cluster_over_time.png', title = 'Topic Cluster Over Time', wide = 1): book_by_year = pd.DataFrame(cluster.groupby('year').count()['author']).reset_index() book_by_year.columns = ['year', 'total'] if wide == 1: fig = plt.figure(figsize = (10,6)) else: fig = plt.figure(figsize = (6,6)) fig.suptitle(title, fontsize=12, fontweight='bold') ax = fig.add_subplot(111, axisbg = 'white') cluster_range = xrange(0,5) for x in xrange(0,5): cluster_by_year = pd.DataFrame(cluster[cluster.cluster_5 == x].groupby('year').count()['author']).reset_index() cluster_by_year.columns = ['year', 'cluster_num'] ww = pd.merge(book_by_year, cluster_by_year, on = 'year', how = 'left') ww = ww.fillna(0) ww['pct'] = ww.cluster_num / ww.total if agg_type == 'sum': spl = UnivariateSpline(ww.year, ww.pct) else: spl = UnivariateSpline(ww.year, ww.cluster_num) xs = np.linspace(1800,1923, 1000) spl.set_smoothing_factor(10) plt.plot(xs, np.clip(spl(xs),0,1), label = labels[x], color = color_map[x], linewidth = 2) plt.axis([1800,1923,0,1]) vals = ax.get_yticks() ax.set_yticklabels(['{:3.0f}%'.format(x*100) for x in vals]) plt.legend(labels, fontsize = 'small', frameon = False, loc="best") plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) fig.savefig(filename_out, facecolor='white', edgecolor='none')
def cubic_spline_interpolation(arr, factor): new_time, expanded_time = expand_time(arr, factor) t = arr[:, 0] x = arr[:, 1] y = arr[:, 2] z = arr[:, 3] qx = arr[:, 4] qy = arr[:, 5] qz = arr[:, 6] qw = arr[:, 7] to_expand = [x, y, z, qx, qy, qz, qw] for i in range(len(to_expand)): spl = UnivariateSpline(t, to_expand[i]) spl.set_smoothing_factor(0) to_expand[i] = np.matrix(spl(new_time)) new_matrix = np.matrix(expanded_time) for i in to_expand: new_matrix = np.concatenate((new_matrix, np.matrix(i)), axis = 0) return new_matrix.T
def __init__(self, log_file): times = [] self.temps = [] self.ons = [] with open(log_file, 'r') as f: state = 0 tail = 0 for line in f: s = line.strip().split() if state == 0 and bool(int(s[3])): state = 1 elif state == 1 and not bool(int(s[3])): state = 2 if state == 1: tail += 2 if state == 2: tail -= 1 if tail == 0: state = 3 if state == 1 or state == 2: times.append(s[0]) self.temps.append(float(s[1])) self.ons.append(bool(int(s[3]))) self.seconds = [] first = None for t in times: s = t.split(':') v = int(s[0]) * 3600 + int(s[1]) * 60 + int(s[2]) if first is None: first = v self.seconds.append(v - first) if len(self.temps) > 0: x = numpy.linspace(0, len(self.temps), len(self.temps)) spl = UnivariateSpline(x, self.temps) spl.set_smoothing_factor(0.75) self.smoothed_temps = spl(x) else: self.smoothed_temps = []
def plot_data_per_interval(): xvalues = [] yvalues = [] with open('15minutesweek.csv', 'r') as f: reader = csv.reader(f) alldata = [] for row in reader: alldata.append([int(row[0]), float(row[1]) / float(row[2])]) alldata = sorted(alldata, key=lambda x: x[0]) xvalues = [x[0] for x in alldata] yvalues = [y[1] for y in alldata] scaler = sklearn.preprocessing.MinMaxScaler(feature_range=( -0.1, 0.1, )) yvalues = scaler.fit_transform(numpy.array(yvalues)) smooth = UnivariateSpline(xvalues, yvalues) smooth.set_smoothing_factor(0) xnew = numpy.linspace(0, len(xvalues) - 1, 7 * 24) plt.plot(xnew, smooth(xnew), linestyle='--') e = [] for d in dataeelco: for d2 in d: e.append(d2) smooth3 = UnivariateSpline(xvalues, e) smooth3.set_smoothing_factor(0.5) plt.plot(xnew, smooth3(xnew)) plt.show()
def find_profile_min_smooth(pro, rbins, s=0, delta=0.1): from numpy import log10, amin, amax, argmin, argmax, linspace from scipy.interpolate import UnivariateSpline, InterpolatedUnivariateSpline from scipy.signal import savgol_filter lrbins = log10(rbins) minx = amin(lrbins) maxx = amax(lrbins) x = linspace(minx, maxx, 1000) yspline = UnivariateSpline(lrbins, pro) yspline.set_smoothing_factor(0.1) ys = yspline(x) maxtab, mintab = peakdet(ys, delta, x) imin = argmin(mintab[:, 1]) rmin = 10**(mintab[:, 0][imin]) pmin = mintab[:, 1][imin] return (pmin, rmin)
def __init__(self, log_file): times = [] self.temps = [] self.ons = [] with open(log_file, 'r') as f: state = 0 tail = 0 for line in f: s = line.strip().split() if state == 0 and bool(int(s[3])): state = 1 elif state == 1 and not bool(int(s[3])): state = 2 if state == 1: tail += 2 if state == 2: tail -= 1 if tail == 0: state = 3 if state == 1 or state == 2: times.append(s[0]) self.temps.append(float(s[1])) self.ons.append(bool(int(s[3]))) self.seconds = [] first = None for t in times: s = t.split(':') v = int(s[0]) * 3600 + int(s[1]) * 60 + int(s[2]) if first is None: first = v self.seconds.append(v - first) if len(self.temps) > 0: x = numpy.linspace(0, len(self.temps), len(self.temps)) spl = UnivariateSpline(x, self.temps) spl.set_smoothing_factor(0.75) self.smoothed_temps = spl(x) else: self.smoothed_temps = []
def spline(x, y, axis='y', s=3.0, **kwargs): """Replace y (x) data with a spline fit. See scipy.interpolate.UnivariateSpline for spline details. Args: axis: Either 'x' or 'y'. Indicates the axis to be fit. """ from scipy.interpolate import UnivariateSpline as Spline _verify_axis(axis) if axis == 'y': xlin = np.arange(0, len(x)) spl = Spline(xlin, y) spl.set_smoothing_factor(s) return x, spl(xlin) if axis == 'x': ylin = np.arange(0, len(y)) spl = Spline(ylin, x) spl.set_smoothing_factor(s) return spl(ylin), y
def agregarPuntosCamino(self,x,y,puntos = [], pan = 0, tilt = 0, tiempo = None): if len(x) > 3 and point_between_point > 1: spl = UnivariateSpline(x, y) xs = np.linspace(min(x),max(x),len(x)*point_between_point) spl.set_smoothing_factor(0.5) ys = spl(xs) pans = [] tilts = [] tiempos = [] for i in range(len(pan)): pans += [pan[i]/point_between_point]*point_between_point tilts += [tilt[i]/point_between_point]*point_between_point tiempos += [tiempo[i]/point_between_point]*point_between_point else: xs = x ys = y pans = pan tilts = tilt tiempos = tiempo if len(puntos) == 0: self.puntos_a_seguir = [] self.pan_tilt = [] self.lista_tiempos = [] ang_pos_final = 0 q = None for i in range(len(xs)): if i == (len(xs) - 1): #q = Quat((ang_pos_final,0,0)) self.puntos_a_seguir.append(Pose(Point(xs[i], ys[i], 0.000), Quaternion(q.q[0], q.q[1], q.q[2], q.q[3]))) # self.pan_tilt.append([pans[i],tilts[i]]) self.lista_tiempos.append(tiempos[i]) else: ang_pos_final = degrees(atan(float(abs(ys[i+1]-ys[i]))/float(abs(xs[i+1]-xs[i])))) q = Quat((ang_pos_final,0,0)) self.puntos_a_seguir.append(Pose(Point(xs[i], ys[i], 0.000), Quaternion(q.q[0], q.q[1], q.q[2], q.q[3]))) #q.q[0], q.q[1], q.q[2], q.q[3] self.pan_tilt.append([pans[i],tilts[i]]) self.lista_tiempos.append(tiempos[i])
def smooth_signal(signal,length): xi=np.linspace(0,1,len(signal)) xi2=np.linspace(0,1,length) iss=UnivariateSpline(xi,signal,k=3) iss.set_smoothing_factor(.00001) return iss(xi2)
def mainFun(pointList,nVsteps=100,minVdep=1,Graph=0): polygonXSorig = Polygon(pointList) #~ definition line of XS borderXS = LineString(pointList) minY=polygonXSorig.bounds[1] maxY=polygonXSorig.bounds[-1] #~ definition polygon of XS pointList.insert(0,(polygonXSorig.bounds[0],maxY+1)) pointList.append((polygonXSorig.bounds[2],maxY+1)) polygonXS = Polygon(pointList) depts = np.linspace(minY+0.1, maxY-0.1, nVsteps) HydRad = np.array([]) HydDept = np.array([]) for dept in depts: wdep=hdepth(polygonXSorig,dept) wdepLine = WTable(polygonXSorig,dept) wetArea = polygonXS.intersection(wdep) wetPerimeter=borderXS.intersection(wdep) wetWTLine = wdepLine.intersection(polygonXS) HydRad = np.append(HydRad,wetArea.area/wetPerimeter.length) HydDept = np.append(HydDept,wetArea.area/wetWTLine.length) #smoothing function #estract local maxima of HydDept and depts using smoothing function in R deptsLM, HydDeptLM , spar = splineR(depts,HydDept) from scipy.interpolate import UnivariateSpline splHydDept= UnivariateSpline(depts, HydDept) splHydDept.set_smoothing_factor(spar) HydDept_smth=splHydDept(depts) xfine = np.linspace(min(depts),max(depts),1000) HydDept_smthfine= splHydDept(xfine) #~ first maxima location of HydDept if len(deptsLM)>0: #~ skip local maxima_locations if lower then value set by user #~ previous method now replaced max_loc_filtered = [i for i in range(len(HydDeptLM)) if HydDeptLM[i] >= minVdep] #~ shapely polygon for bankfull bankfullIndex = max_loc_filtered[0] bankfullLine = WTable(polygonXSorig,deptsLM[bankfullIndex]) wdep=hdepth(polygonXSorig,deptsLM[bankfullIndex]) else: bankfullLine = WTable(polygonXSorig,depts[-1]) wdep=hdepth(polygonXSorig,depts[-1]) #~ new method turning_points = local_maxmin(HydDept) terrace = [] for i in range(len(turning_points['maxima_locations'])): if turning_points['maxima_ranks'][i] == max(turning_points['maxima_ranks']) : terrace.append(turning_points['maxima_locations'][i]) #~ max_loc_filtered = [i for i in max_loc_filtered if HydDept[i] > minVdep] #~ -- #~ shapely polygon for terrace terraceIndex=terrace[0] terraceLine=WTable(polygonXSorig,depts[terraceIndex]) tdep=hdepth(polygonXSorig,depts[terraceIndex]) tArea = polygonXS.intersection(tdep) wetArea = polygonXS.intersection(wdep) boundsOK = () Area = 0 if wetArea.type is 'MultiPolygon': nchannel=str(len(wetArea)) for wetPolygon in wetArea: if wetPolygon.area > Area: Area = wetPolygon.area boundsOK = wetPolygon.bounds else: boundsOK = wetArea.bounds nchannel='1' if Graph == 1: #~ definition of figure for XS plot from matplotlib import pyplot from descartes.patch import PolygonPatch from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas fig = pyplot.figure(1, figsize=(4,3), dpi=300) fig = pyplot.figure() ax = fig.add_subplot(211) ax.clear() #~ plot_coords(ax, borderXS,'#999999') # plot single points on XS plot_line(ax,borderXS,'#6699cc') # plot line of XS plot_line(ax,bankfullLine,'#0000F5') # plot hor line of bankfull #~ plot_line(ax,terraceLine,'#FFE066') # plot hor line of terrace ax.set_title('Cross Section') if wetArea.type is 'MultiPolygon': for wetPolygon in wetArea: patch = PolygonPatch(wetPolygon, fc='#00FFCC', ec='#B8B8B8', alpha=0.5, zorder=2) ax.add_patch(patch) else: patch = PolygonPatch(wetArea, fc='#00FFCC', ec='#B8B8B8', alpha=0.5, zorder=2) ax.add_patch(patch) ax = fig.add_subplot(212) ax.clear() ax.plot(depts,HydDept,'bo') ax.plot(xfine,HydDept_smthfine) ax.plot(deptsLM[bankfullIndex],HydDeptLM[bankfullIndex],'rs') ax.set_title('hydraulic depth') #~ pyplot.show() canvas = FigureCanvas(fig) canvas.updateGeometry() return canvas else: #~ write some useful information to csv file filecsv = open("/tmp/test.csv","a") filecsv.write(str(wetArea.bounds[2]-wetArea.bounds[0])) #bankfull width filecsv.write(',') filecsv.write(nchannel) #n channels filecsv.write(',') filecsv.write(str(wetArea.area)) #Area filecsv.write(',') filecsv.write(str(wetArea.length)) #Perimeter filecsv.write(',') filecsv.write(str(wetArea.bounds[1])) #min height filecsv.write(',') filecsv.write(str(wetArea.bounds[3])) #min height filecsv.write('\n') return boundsOK[0],boundsOK[2] #,len(wetArea),wetArea.area, wetArea.length
def read_in_WHAM_file(filename,base_dir='./'): """ subroutine to read in the WHAM file - note, the format must be distance is first column, and RDF is the third column ASSUMPTION: ALL WHAM FILES HAVE THE SAME NUMBER OF BINS AND CUTOFF AS THE POTENTIAL FILES. """ print "reading WHAM %s" % (filename) #numofbins, cutoff, o = lmp_io.get_number_of_bins_and_cutoff("%s" % (filename), 0) numofbins=0 # TODO - what if we have a NAN in our data set, due to incomplete sampling? LIST_IN = open("%s" % (os.path.join(base_dir,filename)), 'r') index = 0 x = [] y = [] number_of_inf=0 number_of_non_inf=0 print numofbins wham_array = [] #np.zeros(numofbins+1) wham_array_distance= [] #np.zeros(numofbins+1) extrapolate_list=[] interpolate_list=[] for line in LIST_IN: NewRow = (line.strip()).split() mystring = NewRow[0][0:1] if mystring != "#": if len(NewRow)>2: if NewRow[1] != "inf": # wham_array[index] = float(NewRow[1]) number_of_non_inf +=1 x.append(float(NewRow[0])) y.append(float(NewRow[1])) else: if number_of_non_inf == 0: extrapolate_list.append(index) else: interpolate_list.append(index) if number_of_non_inf > 0: wham_array_distance.append(float(NewRow[0])) if NewRow[1] == "inf": wham_array.append(0.0) else : wham_array.append(float(NewRow[1])) index += 1 LIST_IN.close() np.asarray(x) np.asarray(y) np.asarray(wham_array_distance) np.asarray(wham_array) spl = UnivariateSpline(x, y) spl.set_smoothing_factor(0.2) for i, distance in enumerate(wham_array_distance): if i in interpolate_list: wham_array[i]=float(spl(distance)) print "WHAM INTER", distance, wham_array[i] #print wham_array_distance #print wham_array np.trim_zeros(wham_array, 'b') np.trim_zeros(wham_array_distance, 'b') last_element=wham_array[index-1] np.subtract(wham_array,last_element) # check whether this is the same as the the one in the potential file cutoff=wham_array_distance[index-1] print wham_array_distance print wham_array print len(wham_array_distance) print len(wham_array) # need to fill in the infs return wham_array, wham_array_distance, len(wham_array_distance), float(cutoff)
1.01120868, 0.86717364, 1.05338103, 0.99149619, 0.68564621, 0.76683369, 0.63125403, 0.47328888, 0.37570884, 0.44782711, 0.30278194, 0.23449642, -0.00893587, 0.12843641, -0.07914852, 0.05046878, 0.03702803, 0.08291754, 0.05008077, -0.09366895, -0.05902218, 0.19701947, -0.03468384, -0.06500214, -0.07205329, 0.2006148 ] y2 = [ 0.05743072, 0.00940577, 0.05848502, -0.16221342, 0.07314822, 0.08029109, -0.05064357, 0.0695457, 0.05350962, 0.03007541, 0.28596007, 0.05292537, 0.20403795, 0.13377805, 0.30783861, 0.21393345, 0.403338, 0.37650354, 0.5565254, 0.7653728, 0.73612424, 0.83256118, 0.96323466, 0.96258791, 1.01120868, 0.86717364, 1.25338103, 0.99149619, 0.68564621, 0.76683369, 0.63125403, 0.47328888, 0.37570884, 0.44782711, 0.30278194, 0.23449642, -0.00893587, 0.12843641, -0.07914852, 0.05046878, 0.03702803, 0.08291754, 0.05008077, -0.09366895, -0.05902218, 0.19701947, -0.03468384, -0.06500214, -0.07205329, 0.2006148 ] xs = np.linspace(-3, 3, 1000) spl = UnivariateSpline(x, y) spl2 = UnivariateSpline(x, y2) for i in range(1,11): plt.plot(x, y, 'ro', ms=5) smoothing_factor = i / 10.0 print smoothing_factor spl.set_smoothing_factor(smoothing_factor) spl2.set_smoothing_factor(smoothing_factor) plt.plot(xs, spl(xs), 'g', lw=3) plt.plot(xs, spl2(xs), 'ro', lw=3) plt.show()
def calculateGapByCubicInterpolation(self, referenceWindowRadius, interpolationWindow, splineSmoothingFactor, cycleSize, cycleRepetition, graphicalRepresentation=False): x = np.ndarray(referenceWindowRadius * 2) y = np.ndarray(referenceWindowRadius * 2) poseQty = len(self.geneticMatrix) poseLength = len(self.geneticMatrix[0]) #print "poseQty: ", poseQty, "poseLength: ", poseLength, "lp.getFrameQty(): ", lp.getFrameQty() for joint in range(poseLength): interpolationDataIter = referenceWindowRadius - 1 for k in xrange(referenceWindowRadius + 1): referenceFrame = cycleSize + k x[interpolationDataIter] = referenceFrame y[interpolationDataIter] = self.geneticMatrix[referenceFrame][joint] interpolationDataIter += 1 interpolationDataIter = referenceWindowRadius - 2 for k in xrange(referenceWindowRadius + 1): referenceFrame= cycleSize -1 - (interpolationWindow + k) x[interpolationDataIter] = referenceFrame y[interpolationDataIter] = self.geneticMatrix[referenceFrame][joint] interpolationDataIter -= 1 x = np.sort(x) if abs(self.geneticMatrix[cycleSize - interpolationWindow][joint] - self.geneticMatrix[cycleSize][joint]) < 3: spl = interp1d(x, y) else: spl = UnivariateSpline(x, y) spl.set_smoothing_factor(splineSmoothingFactor/10.0) if graphicalRepresentation: px = linspace(x[0], x[len(x)-1], len(x)) py = spl(px) plt.plot(x, y, '.-') plt.plot(px, py) xinter = np.ndarray(interpolationWindow) yinter = np.ndarray(interpolationWindow) for k in xrange(interpolationWindow): smoothFrameIter = cycleSize - 1 - k xinter[k] = smoothFrameIter yinter[k] = self.geneticMatrix[smoothFrameIter][joint] plt.plot(xinter, yinter, '.-') #original data for k in xrange(interpolationWindow): smoothFrameIter = cycleSize - 1 - k xinter[k] = smoothFrameIter yinter[k] = spl(smoothFrameIter) plt.plot(xinter, yinter, '*-') #interpolated data plt.title(self.jointNameIDMapping[joint]) plt.show() print "gap between first and last: ", self.getConcatenationGap() for i in xrange(cycleRepetition): for k in range(cycleSize - 1 - interpolationWindow, cycleSize): newValue = spl(k) self.geneticMatrix[cycleSize * i + k][joint] = newValue
def smoothed_tail_temps(self): x = numpy.linspace(0, len(self.tail_temps), len(self.tail_temps)) spl = UnivariateSpline(x, self.tail_temps) spl.set_smoothing_factor(0.75) return spl(x)
enabled = [] with open('17-01-2015.log', 'r') as f: for line in f: s = line.strip().split() time.append(s[0]) temp.append(float(s[1])) target.append(float(s[2])) on.append(bool(int(s[3]))) enabled.append(bool(int(s[4]))) N = len(temp) x = numpy.linspace(0, N, N) spl = UnivariateSpline(x, temp) spl.set_smoothing_factor(0.75) xi = numpy.linspace(0, N, N * 4) smoothed = spl(xi) gradient = numpy.gradient(smoothed) gradient2 = numpy.gradient(gradient) # M = len(stemp) # dtemp = numpy.zeros(M) # dtemp[0] = (stemp[2] + 4 * stemp[1] - 3 * stemp[0]) / 2 # for i in range(1, M - 1): # dtemp[i] = (stemp[i + 1] - stemp[i - 1]) / 2 # dtemp[M - 1] = (stemp[M - 3] - 4 * stemp[M - 2] + 3 * stemp[M - 1]) / 2 # dtemp[0] = 0
def spline(p,ye,mask): spl = UnivariateSpline(p[0][mask], p[1][mask],k=functions.deg) spl.set_smoothing_factor(5000000) return spl(p[0])
y = histo[0] xBinSize = x[1] - x[0] indexMax, valueMax = max(enumerate(x), key=operator.itemgetter(1)) np.insert(x,0,(x[0] - xBinSize)) np.insert(y,0,y[indexMax]) indexMin, valueMin = min(enumerate(x), key=operator.itemgetter(1)) np.append(x,(x[-1] + xBinSize)) np.append(y,y[indexMin]) f2 = UnivariateSpline(x, y, k=5) m = len(x) var = np.var(y) plt.hist(dist,50,color=flatColorDBlue) plt.xlabel('Keyspace Position') plt.ylabel('Number of Nodes in Bin') plt.title('Curve Fitting of Keyspace Distribution') ax = plt.gca() ax.set_xlim([0,k]) vals = [1,1.5,4] n = 0 for i in vals: print i scalingFactor = int((m * var)/i) f2.set_smoothing_factor(scalingFactor) pltHandle = plt.plot(xsto,f2(xsto),lw=2,label=('S = ' + str(scalingFactor)),color=flatColorMatrix[n]) n += 1 plt.legend(loc='best',fancybox=True, framealpha=0.8) plt.savefig('histogram.pdf',format='pdf') plt.show()
s_0_5_6 = lut.sp[0,:,0,5,6] s,n = nanmasked(s_0_5_6) snorm = norm2max(s_0_5_6) [i1000,i1077,i1493,i1600,i1200,i1300,i530,i610, i1565,i1634,i1193,i1198,i1236,i1248,i1270,i1644, i1050,i1040,i1065,i600,i870,i515] = find_closest(lut.wvl,np.array([1000,1077,1493,1600,1200,1300,530, 610,1565,1634,1193,1198,1236,1248, 1270,1644,1050,1040,1065,600,870,515])) norm2 = s_0_5_6/s_0_5_6[i1000] dsp = smooth(np.gradient(norm2,lut.wvl/1000.),2) # <codecell> norm2_uni = UnivariateSpline(lut.wvl/1000.0,norm2,k=5) norm2_uni.set_smoothing_factor(1) dnorm2 = norm2_uni.derivative() # <codecell> norm2_bspline = splrep(lut.wvl/1000.0,norm2,k=5) norm2_b = splev(lut.wvl/1000.0,norm2_bspline,der=0) dbnorm2 = splev(lut.wvl/1000.0,norm2_bspline,der=1) # <codecell> dsp2 = smooth(deriv(norm2,lut.wvl/1000.),2) # <codecell> plt.figure()