Python scipy.signal.savgol_filter() Examples

The following are 30 code examples of scipy.signal.savgol_filter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.signal , or try the search function .
Example #1
Source File: analyse_raw_telemetry.py    From SpaceXtract with MIT License 7 votes vote down vote up
def find_angle_graph(velocity, vertical_velocity, interp=False):
    angle = []

    for i in range(len(velocity)):
        if velocity[i] == 0:
            angle.append(angle[-1])
        else:
            ratio = max(-1, min(vertical_velocity[i] / velocity[i], 1))
            angle.append(asin(ratio))

    angle = savgol_filter(angle, 5, 1)

    if interp:
        angle = savgol_filter(angle, 11, 1)
        return ss.medfilt(angle, kernel_size=7)

    return angle 
Example #2
Source File: vis_log.py    From masktextspotter.caffe2 with Apache License 2.0 6 votes vote down vote up
def plot_log(x, y, save_name, key):
	fig = plt.figure()
	plt.title(key)
	plt.xlabel("iter")
	plt.ylabel(key)
	if key in ["loss"]:
		plt.axis([x[0], x[-1], 0, 2.5])
	if key in ["loss_rpn_bbox_fpn2", "loss_rpn_bbox_fpn3", "loss_rpn_bbox_fpn4", "loss_rpn_bbox_fpn5", "loss_rpn_bbox_fpn6"]:
		plt.axis([x[0], x[-1], 0, 0.01])
	if key in ["loss_rpn_cls_fpn2", "loss_rpn_cls_fpn3", "loss_rpn_cls_fpn4", "loss_rpn_cls_fpn5", "loss_rpn_cls_fpn6"]:
		plt.axis([x[0], x[-1], 0, 0.02])
	if key in ["loss_char_bbox"]:
		plt.axis([x[0], x[-1], 0, 0.05])
	if key in ["loss_global_mask", "loss_char_mask"]:
		plt.axis([x[0], x[-1], 0, 0.4])
	if key in ["accuracy_cls"]:
		plt.axis([x[0], x[-1], 0.9, 1])
	if key in ["loss_char_bbox"]:
		plt.axis([x[0], x[-1], 0, 0.01])
	plt.plot(x, y, 'r-', lw=2)
	plt.plot(x, smooth(y, 20), 'g-', lw=2)
	# plt.plot(x, savgol_filter(y, 51, 3), 'g-', lw=2)

	fig.savefig(save_name) 
Example #3
Source File: plot_acrobot.py    From ILPO with MIT License 6 votes vote down vote up
def getdata(name):
    all_results = []

    for i in range(0, 50):
        f = open(name + str(i) + ".csv", "r")
        results = []
        time = []

        for line in f:
            step, reward = (line.replace("\n", "")).split(",")
            results.append(float(reward))
            time.append(float(step))
        results = signal.savgol_filter(results, 7, 3, axis=-1)
        all_results.append(results)

    return all_results, time


# Load the data. 
Example #4
Source File: calcium.py    From neuroglia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def transform(self,X):
        """Detrend each column of X

        Parameters
        ----------
        X : DataFrame in `traces` structure [n_samples, n_traces]

        Returns
        -------
        Xt : DataFrame in `traces` structure [n_samples, n_traces]
            The detrended data.
        """
        self.fit_params = {}
        X_new = X.copy()
        for col in X.columns:
            tmp_data = X[col].values.astype(np.double)
            sgf = savgol_filter(tmp_data, self.window, self.order)
            self.fit_params[col] = dict(sgf=sgf)
            X_new[col] = tmp_data - sgf

        return X_new 
Example #5
Source File: smooth.py    From tierpsy-tracker with MIT License 6 votes vote down vote up
def _h_smooth_curve(curve, window=5, pol_degree=3):
    '''smooth curves using the savgol_filter'''

    if curve.shape[0] < window:
        # nothing to do here return an empty array
        return np.full_like(curve, np.nan)

    # consider the case of one (widths) or two dimensions (skeletons, contours)
    if curve.ndim == 1:
        smoothed_curve = savgol_filter(curve, window, pol_degree)
    else:
        smoothed_curve = np.zeros_like(curve)
        for nn in range(curve.ndim):
            smoothed_curve[:, nn] = savgol_filter(
                curve[:, nn], window, pol_degree)

    return smoothed_curve 
Example #6
Source File: plot_results.py    From nasbench-1shot1 with Apache License 2.0 6 votes vote down vote up
def plot_epoch_y_curves_correlation(metric_dict, title, xlabel, ylabel, foldername, x_log, y_log, smoothing=False,
                                    filename=None, max_iter=None):
    plt.figure()
    for config, values in metric_dict.items():
        if smoothing:
            values = [savgol_filter(value, 11, 3) for value in values]

        plt.plot(np.arange(len(values)), values, label=config)
    ax = plt.gca()
    if x_log:
        ax.set_xscale('log')
    if y_log:
        ax.set_yscale('log')
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.legend()

    plt.grid(True, which="both", ls="-", alpha=0.5)
    plt.tight_layout()
    folder_path = os.path.join(os.getcwd(), foldername)
    os.makedirs(folder_path, exist_ok=True)
    filepath = os.path.join(folder_path, '{}.pdf'.format(filename)),
    plt.savefig(filepath[0])
    plt.close() 
Example #7
Source File: transport.py    From qkit with GNU General Public License v2.0 6 votes vote down vote up
def get_dVdI(self):
        """
        Gets the internal dVdI parameter, to decide weather the differential resistance (dV/dI) is calculated or not.
        
        Parameters
        ----------
        None
        
        Returns
        -------
        status: bool
            Status if numerical derivative is calculated.
        func: function
            Function to calculate numerical derivative, e.g. scipy.signal.savgol_filter (default), numpy.gradient, ...
        *args: array_likes
            Arguments for derivation function.
        **kwargs: dictionary_likes
            Keyword arguments for derivation function.
        """
        return self._dVdI, self._numder_func, self._numder_args, self._numder_kwargs 
Example #8
Source File: getIntensityProfile.py    From tierpsy-tracker with MIT License 6 votes vote down vote up
def smoothSkeletons(
        skeleton,
        length_resampling=131,
        smooth_win=11,
        pol_degree=3):
    xx = savgol_filter(skeleton[:, 0], smooth_win, pol_degree)
    yy = savgol_filter(skeleton[:, 1], smooth_win, pol_degree)

    ii = np.arange(xx.size)
    ii_new = np.linspace(0, xx.size - 1, length_resampling)

    fx = interp1d(ii, xx)
    fy = interp1d(ii, yy)

    xx_new = fx(ii_new)
    yy_new = fy(ii_new)

    skel_new = np.vstack((xx_new, yy_new)).T
    return skel_new 
Example #9
Source File: obtainFeaturesHelper.py    From tierpsy-tracker with MIT License 6 votes vote down vote up
def _h_smooth_curve(curve, window=5, pol_degree=3):
    '''smooth curves using the savgol_filter'''

    if curve.shape[0] < window:
        # nothing to do here return an empty array
        return np.full_like(curve, np.nan)

    # consider the case of one (widths) or two dimensions (skeletons, contours)
    if curve.ndim == 1:
        smoothed_curve = savgol_filter(curve, window, pol_degree)
    else:
        smoothed_curve = np.zeros_like(curve)
        for nn in range(curve.ndim):
            smoothed_curve[:, nn] = savgol_filter(
                curve[:, nn], window, pol_degree)

    return smoothed_curve 
Example #10
Source File: smooth_sg.py    From hrnet with MIT License 6 votes vote down vote up
def smooth_filter(kpts):
    if len(kpt_queue) < 6:
        kpt_queue.append(kpts)
        return kpts

    queue_length = len(kpt_queue)
    if queue_length == 20:
        kpt_queue.pop(0)
    kpt_queue.append(kpts)

    # transpose to shape (17, 2, num, 50) 关节点、横纵坐标、每帧人数、帧数
    transKpts = np.array(kpt_queue).transpose(1,2,3,0)

    window_length = queue_length - 1 if queue_length % 2 == 0 else queue_length - 2
    # array, window_length越大越好, polyorder
    result = savgol_filter(transKpts, window_length, 3).transpose(3, 0, 1, 2) #shape(frame_num, human_num, 17, 2)

    # return the 2rd last frame
    return result[-2]


##### load model 
Example #11
Source File: grain.py    From openMotor with GNU General Public License v3.0 6 votes vote down vote up
def generateRegressionMap(self):
        """Uses the fast marching method to generate an image of how the grain regresses from the core map. The map
        is stored under self.regressionMap."""
        masked = np.ma.MaskedArray(self.coreMap, self.mask)
        cellSize = 1 / self.mapDim
        self.regressionMap = skfmm.distance(masked, dx=cellSize) * 2
        maxDist = np.amax(self.regressionMap)
        self.wallWeb = self.unNormalize(maxDist)
        faceArea = []
        polled = []
        valid = np.logical_not(self.mask)
        for i in range(int(maxDist * self.mapDim) + 2):
            polled.append(i / self.mapDim)
            faceArea.append(self.mapToArea(np.count_nonzero(np.logical_and(self.regressionMap > (i / self.mapDim), valid))))
        self.faceArea = savgol_filter(faceArea, 31, 5)
        self.faceAreaFunc = interpolate.interp1d(polled, self.faceArea) 
Example #12
Source File: video.py    From video-to-pose3D with MIT License 6 votes vote down vote up
def smooth_filter(kpts):
    if len(kpt_queue) < 6:
        kpt_queue.append(kpts)
        return kpts

    queue_length = len(kpt_queue)
    if queue_length == 50:
        kpt_queue.pop(0)
    kpt_queue.append(kpts)

    # transpose to shape (17, 2, num, 50) 关节点keypoints num、横纵坐标、每帧人数、帧数
    transKpts = np.array(kpt_queue).transpose(1, 2, 3, 0)

    window_length = queue_length - 1 if queue_length % 2 == 0 else queue_length - 2
    # array, window_length(bigger is better), polyorder
    result = savgol_filter(transKpts, window_length, 3).transpose(3, 0, 1, 2)  # shape(frame_num, human_num, 17, 2)

    # 返回倒数第几帧 return third from last frame
    return result[-3] 
Example #13
Source File: beam.py    From ocelot with GNU General Public License v3.0 6 votes vote down vote up
def smear(self, sw):
        _logger.debug('smearing the beam by {:.2e} m'.format(sw))
        self.equidist()
        sn = (sw / self.ds).astype(int)
        if sn < 2:
            return
        if not sn % 2:
            sn += 1

        for attr in self.params():
            if attr is 's':
                continue
            val = getattr(self, attr)
            val = savgol_filter(val, sn, 2, mode='nearest')

            if attr in ['E', 'I', 'beta_x', 'beta_y', 'emit_x', 'emit_y', 'sigma_E']:
                # print('attribute {:s} < 0, setting to 0'.format(attr))
                val[val < 0] = 0
            #    val = convolve(val,spike,mode='same')
            setattr(self, attr, val) 
Example #14
Source File: util.py    From object_centric_VAD with MIT License 6 votes vote down vote up
def CAE_dataset_feed_dict(prefix,np_path_box,dataset_name):
    path_box_list=np.load(np_path_box)
    (image_height, image_width) = image_size_map[dataset_name]
    former_paths,gray_paths,back_paths,boxes,class_indexes=split_path_boxes(prefix,path_box_list,dataset_name,image_height,image_width)

    f_imgs=[]
    g_imgs=[]
    b_imgs=[]
    for f_path,g_path,b_path,box in zip(former_paths,gray_paths,back_paths,boxes):
        f_imgs.append(box_image_crop(f_path,box))
        g_imgs.append(box_image_crop(g_path,box))
        b_imgs.append(box_image_crop(b_path,box))

    return f_imgs,g_imgs,b_imgs,class_indexes

# def score_smoothing(score):
#     score_len=score.shape[0]//9
#     if score_len%2==0:
#         score_len+=1
#     score=savgol_filter(score,score_len,3)
#
#     return score
# 
Example #15
Source File: QAAnalysis_signal.py    From QUANTAXIS with MIT License 6 votes vote down vote up
def find_peak_vextors_eagerly(price, offest=0):
    """
    (饥渴的)在 MACD 上坡的时候查找更多的极值点
    """
    xn = price

    # pass 0
    window_size, poly_order = 5, 1
    yy_sg = savgol_filter(xn, window_size, poly_order)

    # pass 1
    x_tp_min, x_tp_max = signal.argrelextrema(yy_sg, np.less)[0], signal.argrelextrema(yy_sg, np.greater)[0]
    n = int(len(price) / (len(x_tp_min) + len(x_tp_max))) * 2

    # peakutils 似乎一根筋只能查最大极值,通过曲线反相的方式查找极小点
    mirrors = (yy_sg * -1) + np.mean(price) * 2

    # pass 2 使用 peakutils 查找
    x_tp_max = peakutils.indexes(yy_sg, thres=0.01 / max(price), min_dist=n)
    x_tp_min = peakutils.indexes(mirrors, thres=0.01 / max(price), min_dist=n)

    return x_tp_min + offest, x_tp_max + offest 
Example #16
Source File: SGfilter.py    From video-to-pose3D with MIT License 6 votes vote down vote up
def smooth_filter(kpts):
    if len(kpt_queue) < 6:
        kpt_queue.append(kpts)
        return kpts

    queue_length = len(kpt_queue)
    if queue_length == 50:
        kpt_queue.pop(0)
    kpt_queue.append(kpts)

    # transpose to shape (17, 2, num, 50) 关节点、横纵坐标、每帧人数、帧数
    transKpts = np.array(kpt_queue).transpose(1, 2, 3, 0)

    window_length = queue_length - 1 if queue_length % 2 == 0 else queue_length - 2
    # array, window_length越大越好, polyorder 
    result = savgol_filter(transKpts, window_length, 3).transpose(3, 0, 1, 2)  # shape(frame_num, human_num, 17, 2)

    # 返回倒数第几帧
    return result[-3]


##### load model 
Example #17
Source File: motion_preprocessor.py    From youtube-gesture-dataset with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def smooth_motion(self):
        for i in range(24):
            self.skeletons[:, i] = savgol_filter(self.skeletons[:, i], 5, 2) 
Example #18
Source File: generate_labels.py    From LSTM-Crypto-Price-Prediction with MIT License 5 votes vote down vote up
def apply_filter(self, deriv):
        # apply a Savitzky-Golay filter to historical prices
        return savgol_filter(self.hist, self.window, self.polyorder, deriv=deriv) 
Example #19
Source File: detectionMAP.py    From wtalc-pytorch with MIT License 5 votes vote down vote up
def smooth(v):
   return v
   #l = min(351, len(v)); l = l - (1-l%2)
   #if len(v) <= 3:
   #   return v
   #return savgol_filter(v, l, 1) #savgol_filter(v, l, 1) #0.5*(np.concatenate([v[1:],v[-1:]],axis=0) + v) 
Example #20
Source File: zebrafishAnalysis.py    From tierpsy-tracker with MIT License 5 votes vote down vote up
def smoothMaskModel(mask_canvas, config):

    points = mask_canvas.points

    points = np.array(points)
    points = points.astype(int)

    x = points[:, 0]
    y = points[:, 1]

    window_length = len(y)

    if window_length % 2 == 0:
        window_length -= 1

    polyorder = 3
    yhat = savgol_filter(y, window_length, polyorder)

    output_points = []
    for a, b in zip(x, yhat):
        new_point = [a, b]
        output_points.append(new_point)
    output_points = np.array(output_points)
    output_points = output_points.astype(int)

    mask = np.zeros(mask_canvas.canvas.shape, np.uint8)
    cv2.polylines(mask, [output_points], False, 255, thickness=config.smoothed_segment_width)

    return mask, output_points 
Example #21
Source File: food.py    From tierpsy-tracker with MIT License 5 votes vote down vote up
def _h_smooth_cnt(food_cnt, resampling_N = 1000, smooth_window=None, _is_debug=False):
    if smooth_window is None:
        smooth_window = resampling_N//20
    
    if not _is_valid_cnt(food_cnt):
        #invalid contour arrays
        return food_cnt
        
    smooth_window = smooth_window if smooth_window%2 == 1 else smooth_window+1
    # calculate the cumulative length for each segment in the curve
    dx = np.diff(food_cnt[:, 0])
    dy = np.diff(food_cnt[:, 1])
    dr = np.sqrt(dx * dx + dy * dy)
    lengths = np.cumsum(dr)
    lengths = np.hstack((0, lengths))  # add the first point
    tot_length = lengths[-1]
    fx = interp1d(lengths, food_cnt[:, 0])
    fy = interp1d(lengths, food_cnt[:, 1])
    subLengths = np.linspace(0 + np.finfo(float).eps, tot_length, resampling_N)
    
    rx = fx(subLengths)
    ry = fy(subLengths)
    
    pol_degree = 3
    rx = savgol_filter(rx, smooth_window, pol_degree, mode='wrap')
    ry = savgol_filter(ry, smooth_window, pol_degree, mode='wrap')
    
    food_cnt_s = np.stack((rx, ry), axis=1)
    
    if _is_debug:
        import matplotlib.pylab as plt
        plt.figure()
        plt.plot(food_cnt[:, 0], food_cnt[:, 1], '.-')
        plt.plot(food_cnt_s[:, 0], food_cnt_s[:, 1], '.-')
        plt.axis('equal')
        plt.title('smoothed contour')
    
    return food_cnt_s

#%% 
Example #22
Source File: curvatures.py    From tierpsy-tracker with MIT License 5 votes vote down vote up
def _curvature_savgol(skeletons, window_length = None, length=None):
    '''
    Calculate the curvature using univariate splines. This method is slower and can fail
    badly if the fit does not work, so I am only using it as testing
    '''

    if window_length is None:
        window_length = 7

    def _fitted_curvature(skel):
        if np.any(np.isnan(skel)):
            return np.full(skel.shape[0], np.nan)
        
        x = skel[:, 0]
        y = skel[:, 1]

        x_d = savgol_filter(x, window_length=window_length, polyorder=3, deriv=1)
        y_d = savgol_filter(y, window_length=window_length, polyorder=3, deriv=1)
        x_dd = savgol_filter(x, window_length=window_length, polyorder=3, deriv=2)
        y_dd = savgol_filter(y, window_length=window_length, polyorder=3, deriv=2)
        curvature = _curvature_fun(x_d, y_d, x_dd, y_dd)
        return  curvature

    
    curvatures_fit = np.array([_fitted_curvature(skel) for skel in skeletons])
    return curvatures_fit 
Example #23
Source File: smooth.py    From tierpsy-tracker with MIT License 5 votes vote down vote up
def _h_interp_and_smooth(self, x, y, x_pred, s_win):
        f = interp1d(x, y)
        y_interp = f(x_pred)

        if (s_win is None) or (y_interp.size <= s_win):
            return y_interp
            
        y_smooth = savgol_filter(y_interp, s_win, self.pol_degree)
        return y_smooth 
Example #24
Source File: smooth_savitzky_golay.py    From openeo-python-client with Apache License 2.0 5 votes vote down vote up
def apply_datacube(cube: DataCube, context: Dict) -> DataCube:
    """
    Applies a savitzky-golay smoothing to a timeseries datacube.
    This UDF preserves dimensionality, and assumes a datacube with a temporal dimension 't' as input.
    """
    from scipy.signal import savgol_filter

    array: xarray.DataArray = cube.get_array()
    filled = array.interpolate_na(dim='t')
    smoothed_array = savgol_filter(filled.values, 5, 2, axis=0)
    return DataCube(xarray.DataArray(smoothed_array,dims=array.dims,coords=array.coords)) 
Example #25
Source File: array.py    From hypers with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def smoothen(self, method: str='savgol', **kwargs) -> 'hparray':
        """
        Returns smoothened hp.hparray

        Parameters
        ----------
        method: str
            Method to use to smooth the array. Default is 'savgol'.
            + 'savgol': Savitzky-Golay filter.

        **kwargs
            Keyword arguments for the relevant method used.
            + method='savgol'
                kwargs for the `scipy.signal.savgol_filter` implementation

        Returns
        -------
        hp.hparray
            The smoothened array with the same dimensions as the original
            array.
        """
        smooth_array = self.copy()
        for index in np.ndindex(self.shape[:-1]):
            if method == 'savgol':
                smooth_array[index] = savgol_filter(smooth_array[index], **kwargs)
            else:
                raise ValueError
        return smooth_array 
Example #26
Source File: trajectory.py    From mushroom-rl with MIT License 5 votes vote down vote up
def _smooth_vel_profile(self, vel, use_simple_mean=False, window_size=1001,
                            polyorder=2):
        if use_simple_mean:
            filtered = np.tile(np.mean(vel, axis=1),
                               reps=(self.trajectory.shape[1], 1)).T
        else:
            filtered = signal.savgol_filter(vel, window_length=window_size,
                                            polyorder=polyorder, axis=1)
        return filtered 
Example #27
Source File: SavgolFilter.py    From mltk-algo-contrib with Apache License 2.0 5 votes vote down vote up
def fit(self, df, options):
        X = df.copy()
        X, nans, columns = df_util.prepare_features(X, self.feature_variables)

        def f(x):
            return savgol_filter(x, self.window_length, self.polyorder, self.deriv)

        y_hat = np.apply_along_axis(f, 0, X)

        names = ['SG_%s' % col for col in columns]
        output_df = df_util.create_output_dataframe(y_hat, nans, names)
        df = df_util.merge_predictions(df, output_df)

        return df 
Example #28
Source File: happy.py    From rapidtide with Apache License 2.0 5 votes vote down vote up
def savgolsmooth(data, smoothlen=101, polyorder=3):
    return savgol_filter(data, smoothlen, polyorder) 
Example #29
Source File: feature_extractor.py    From plastering with MIT License 5 votes vote down vote up
def get_noise_by_sgfilter(data):
    filtered = savgol_filter(data, 9,2)
    diffData = [abs(val1-val2) for val1, val2 in zip(data,filtered)]
    return sum(diffData) 
Example #30
Source File: main.py    From bokeh with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_dataset(src, name, distribution):
    df = src[src.airport == name].copy()
    del df['airport']
    df['date'] = pd.to_datetime(df.date)
    # timedelta here instead of pd.DateOffset to avoid pandas bug < 0.18 (Pandas issue #11925)
    df['left'] = df.date - datetime.timedelta(days=0.5)
    df['right'] = df.date + datetime.timedelta(days=0.5)
    df = df.set_index(['date'])
    df.sort_index(inplace=True)
    if distribution == 'Smoothed':
        window, order = 51, 3
        for key in STATISTICS:
            df[key] = savgol_filter(df[key], window, order)

    return ColumnDataSource(data=df)