original_docstring: 
      Smooth (and optionally differentiate) data with a Savitzky-Golay filter
      The Savitzky-Golay filter removes high frequency noise from data. It has
      the advantage of preserving the original shape and features of the signal
      better than other types of filtering approaches, such as moving averages
      techniques.
  
      The Savitzky-Golay is a type of low-pass filter, particularly suited for
      smoothing noisy data. The main idea behind this approach is to make for
      each point a least-square fit with a polynomial of high order over a
      odd-sized window centered a the point.
  
      A Gram polynomial version of this is used to have better estimates near the
      boundaries of the data.
  
      Parameters
      ----------
       y : array_like, shape (N,)
          the values of the time history of the signal.
       window_size : int
          the length of the window. Must be an odd integer number.
       order : int
          the order of the polynomial used in the filtering. Must be less then
          `window_size` - 1.
       deriv : int
          (optional) the order of the derivative to compute (default = 0 means
          only smoothing)
  
      Returns
      -------
       ys : ndarray, shape (N)
          the smoothed signal (or it's n-th derivative).
  
      References
      ----------
      .. [3] P.A. Gorry, General Least-Squares Smoothing and Differentiation by
         the Convolution (Savitzky-Golay) Method. Analytical Chemistry, 1990, 62,
         pp 570-573
      
docstring: Smooth (and optionally differentiate) data with a Savitzky-Golay filter
  The Savitzky-Golay filter removes high frequency noise from data. It has
  the advantage of preserving the original shape and features of the signal
  better than other types of filtering approaches, such as moving averages
  techniques.
  
  The Savitzky-Golay is a type of low-pass filter, particularly suited for
  smoothing noisy data. The main idea behind this approach is to make for
  each point a least-square fit with a polynomial of high order over a
  odd-sized window centered a the point.
  
  A Gram polynomial version of this is used to have better estimates near the
  boundaries of the data.
  
  Parameters
  
  y : array_like, shape (N,)
  the values of the time history of the signal.
  window_size : int
  the length of the window. Must be an odd integer number.
  order : int
  the order of the polynomial used in the filtering. Must be less then
  `window_size` - 1.
  deriv : int
  (optional) the order of the derivative to compute (default = 0 means
  only smoothing)
  
  Returns
  
  ys : ndarray, shape (N)
  the smoothed signal (or it's n-th derivative).
  
  References
  
  
docstring_tokens: Smooth ( and optionally differentiate ) data with a Savitzky - Golay filter The Savitzky - Golay filter removes high frequency noise from data . It has the advantage of preserving the original shape and features of the signal better than other types of filtering approaches such as moving averages techniques . The Savitzky - Golay is a type of low - pass filter particularly suited for smoothing noisy data . The main idea behind this approach is to make for each point a least - square fit with a polynomial of high order over a odd - sized window centered a the point . A Gram polynomial version of this is used to have better estimates near the boundaries of the data . Parameters y : array_like shape ( N ) the values of the time history of the signal . window_size : int the length of the window . Must be an odd integer number . order : int the order of the polynomial used in the filtering . Must be less then ` window_size ` - 1 . deriv : int ( optional ) the order of the derivative to compute ( default = 0 means only smoothing ) Returns ys : ndarray shape ( N ) the smoothed signal ( or it ' s n - th derivative ) . References
code: def _savitzky_golay_gram(y, window_size, order, deriv=0):
      """
      Smooth (and optionally differentiate) data with a Savitzky-Golay filter
      The Savitzky-Golay filter removes high frequency noise from data. It has
      the advantage of preserving the original shape and features of the signal
      better than other types of filtering approaches, such as moving averages
      techniques.
  
      The Savitzky-Golay is a type of low-pass filter, particularly suited for
      smoothing noisy data. The main idea behind this approach is to make for
      each point a least-square fit with a polynomial of high order over a
      odd-sized window centered a the point.
  
      A Gram polynomial version of this is used to have better estimates near the
      boundaries of the data.
  
      Parameters
      ----------
       y : array_like, shape (N,)
          the values of the time history of the signal.
       window_size : int
          the length of the window. Must be an odd integer number.
       order : int
          the order of the polynomial used in the filtering. Must be less then
          `window_size` - 1.
       deriv : int
          (optional) the order of the derivative to compute (default = 0 means
          only smoothing)
  
      Returns
      -------
       ys : ndarray, shape (N)
          the smoothed signal (or it's n-th derivative).
  
      References
      ----------
      .. [3] P.A. Gorry, General Least-Squares Smoothing and Differentiation by
         the Convolution (Savitzky-Golay) Method. Analytical Chemistry, 1990, 62,
         pp 570-573
      """
      n_order = order
      m_half_filter_size = (window_size - 1) // 2  # 2m + 1 = filter size
      s_derivative_order = deriv
  
      def generalized_factorial(gf_a, gf_b):
          """Generalized factorial"""
  
          g_f = 1
          for j in range(gf_a - gf_b + 1, gf_a + 1):
              g_f *= j
          return g_f
  
      def gram_polynomial(gp_i, gp_m, gp_k, gp_s):
          """
          Calculates the Gram Polynomial (gp_s=0) or its gp_s'th derivative
          evaluated at gp_i, order gp_k, over 2gp_m+1 points
          """
          if gp_k > 0:
              gram_poly = (4. * gp_k - 2.) / (gp_k * (2. * gp_m - gp_k + 1.)) * \
                          (gp_i * gram_polynomial(gp_i, gp_m, gp_k - 1, gp_s) +
                           gp_s * gram_polynomial(gp_i, gp_m, gp_k - 1,
                                                  gp_s - 1)) - \
                          ((gp_k - 1.) * (2. * gp_m + gp_k)) / \
                          (gp_k * (2. * gp_m - gp_k + 1.)) * \
                          gram_polynomial(gp_i, gp_m, gp_k - 2, gp_s)
  
          else:
              if gp_k == 0 and gp_s == 0:
                  gram_poly = 1.
              else:
                  gram_poly = 0.
          return gram_poly
  
      def gram_weight(gw_i, gw_t, gw_m, gw_n, gw_s):
          """
          Calculate the weight og the gw_i'th data point for the gw_t'th
          Least-Square point of the gw_s'th derivative over 2gw_m+1 points,
          order gw_n
          """
          weight = 0
          for k in range(gw_n + 1):
              weight += (2. * k + 1.) * generalized_factorial(2 * gw_m, k) / \
                        generalized_factorial(2 * gw_m + k + 1, k + 1) * \
                        gram_polynomial(gw_i, gw_m, k, 0) * \
                        gram_polynomial(gw_t, gw_m, k, gw_s)
          return weight
  
      # fill weights
      weights = np.empty((2 * m_half_filter_size + 1, 2 * m_half_filter_size + 1))
      for i in range(-m_half_filter_size, m_half_filter_size + 1):
          for j in range(-m_half_filter_size, m_half_filter_size + 1):
              weights[i + m_half_filter_size, j + m_half_filter_size] = \
                  gram_weight(i, j, m_half_filter_size, n_order,
                              s_derivative_order)
  
      # do convolution
      y_len = len(y)
      con_f = np.empty(y_len)
      for i in range(y_len):
          if i < m_half_filter_size:
              y_center = m_half_filter_size
              w_ind = i
          elif y_len - i <= m_half_filter_size:
              y_center = y_len - m_half_filter_size - 1
              w_ind = 2 * m_half_filter_size + 1 - (y_len - i)
          else:
              y_center = i
              w_ind = m_half_filter_size
          con_f[i] = 0
          for k in range(-m_half_filter_size, m_half_filter_size + 1):
              con_f[i] += y[y_center + k] * weights[k + m_half_filter_size, w_ind]
  
      return con_f
code_tokens: def _savitzky_golay_gram ( y , window_size , order , deriv = 0 ) : n_order = order m_half_filter_size = ( window_size - 1 ) // 2 s_derivative_order = deriv def generalized_factorial ( gf_a , gf_b ) : """Generalized factorial""" g_f = 1 for j in range ( gf_a - gf_b + 1 , gf_a + 1 ) : g_f *= j return g_f def gram_polynomial ( gp_i , gp_m , gp_k , gp_s ) : """
          Calculates the Gram Polynomial (gp_s=0) or its gp_s'th derivative
          evaluated at gp_i, order gp_k, over 2gp_m+1 points
          """ if gp_k > 0 : gram_poly = ( 4. * gp_k - 2. ) / ( gp_k * ( 2. * gp_m - gp_k + 1. ) ) * ( gp_i * gram_polynomial ( gp_i , gp_m , gp_k - 1 , gp_s ) + gp_s * gram_polynomial ( gp_i , gp_m , gp_k - 1 , gp_s - 1 ) ) - ( ( gp_k - 1. ) * ( 2. * gp_m + gp_k ) ) / ( gp_k * ( 2. * gp_m - gp_k + 1. ) ) * gram_polynomial ( gp_i , gp_m , gp_k - 2 , gp_s ) else : if gp_k == 0 and gp_s == 0 : gram_poly = 1. else : gram_poly = 0. return gram_poly def gram_weight ( gw_i , gw_t , gw_m , gw_n , gw_s ) : """
          Calculate the weight og the gw_i'th data point for the gw_t'th
          Least-Square point of the gw_s'th derivative over 2gw_m+1 points,
          order gw_n
          """ weight = 0 for k in range ( gw_n + 1 ) : weight += ( 2. * k + 1. ) * generalized_factorial ( 2 * gw_m , k ) / generalized_factorial ( 2 * gw_m + k + 1 , k + 1 ) * gram_polynomial ( gw_i , gw_m , k , 0 ) * gram_polynomial ( gw_t , gw_m , k , gw_s ) return weight weights = np . empty ( ( 2 * m_half_filter_size + 1 , 2 * m_half_filter_size + 1 ) ) for i in range ( - m_half_filter_size , m_half_filter_size + 1 ) : for j in range ( - m_half_filter_size , m_half_filter_size + 1 ) : weights [ i + m_half_filter_size , j + m_half_filter_size ] = gram_weight ( i , j , m_half_filter_size , n_order , s_derivative_order ) y_len = len ( y ) con_f = np . empty ( y_len ) for i in range ( y_len ) : if i < m_half_filter_size : y_center = m_half_filter_size w_ind = i elif y_len - i <= m_half_filter_size : y_center = y_len - m_half_filter_size - 1 w_ind = 2 * m_half_filter_size + 1 - ( y_len - i ) else : y_center = i w_ind = m_half_filter_size con_f [ i ] = 0 for k in range ( - m_half_filter_size , m_half_filter_size + 1 ) : con_f [ i ] += y [ y_center + k ] * weights [ k + m_half_filter_size , w_ind ] return con_f
short_docstring: Smooth (and optionally differentiate) data with a Savitzky-Golay filter
  The Savitzky-Golay filter removes high frequency noise from data.
short_docstring_tokens: Smooth ( and optionally differentiate ) data with a Savitzky - Golay filter The Savitzky - Golay filter removes high frequency noise from data .
comment: """
      Smooth (and optionally differentiate) data with a Savitzky-Golay filter
      The Savitzky-Golay filter removes high frequency noise from data. It has
      the advantage of preserving the original shape and features of the signal
      better than other types of filtering approaches, such as moving averages
      techniques.
  
      The Savitzky-Golay is a type of low-pass filter, particularly suited for
      smoothing noisy data. The main idea behind this approach is to make for
      each point a least-square fit with a polynomial of high order over a
      odd-sized window centered a the point.
  
      A Gram polynomial version of this is used to have better estimates near the
      boundaries of the data.
  
      Parameters
      ----------
       y : array_like, shape (N,)
          the values of the time history of the signal.
       window_size : int
          the length of the window. Must be an odd integer number.
       order : int
          the order of the polynomial used in the filtering. Must be less then
          `window_size` - 1.
       deriv : int
          (optional) the order of the derivative to compute (default = 0 means
          only smoothing)
  
      Returns
      -------
       ys : ndarray, shape (N)
          the smoothed signal (or it's n-th derivative).
  
      References
      ----------
      .. [3] P.A. Gorry, General Least-Squares Smoothing and Differentiation by
         the Convolution (Savitzky-Golay) Method. Analytical Chemistry, 1990, 62,
         pp 570-573
      """$SEP$# 2m + 1 = filter size$SEP$"""Generalized factorial"""$SEP$"""
          Calculates the Gram Polynomial (gp_s=0) or its gp_s'th derivative
          evaluated at gp_i, order gp_k, over 2gp_m+1 points
          """$SEP$"""
          Calculate the weight og the gw_i'th data point for the gw_t'th
          Least-Square point of the gw_s'th derivative over 2gw_m+1 points,
          order gw_n
          """$SEP$# fill weights$SEP$# do convolution