Ejemplo n.º 1
0
 def test_invalid_acc_raises_exception(self):
     with self.assertRaises(ValueError):
         coefficients(deriv=1, acc=3)
     with self.assertRaises(ValueError):
         coefficients(deriv=1, acc=0)
     with self.assertRaises(ValueError):
         coefficients_non_uni(1, 3, None, None)
     with self.assertRaises(ValueError):
         coefficients_non_uni(1, 0, None, None)
Ejemplo n.º 2
0
 def _setup_der_coef(self):
     self._derwindow_ = self.par["acc"] + 1
     if self.par["der"] > 0:
         self._dercoef_ = findiff.coefficients(
             deriv=self.par["der"],
             acc=self.par["acc"])["backward"]["coefficients"]
     else:
         self._dercoef_ = np.array(self._derwindow_ *
                                   [1 / self._derwindow_])
     self._ylist_ = deque(self._derwindow_ * [np.NAN])
Ejemplo n.º 3
0
    def test_non_uniform(self):

        x = np.linspace(0, 10, 100)
        dx = x[1] - x[0]

        c_uni = coefficients(deriv=2, acc=2)
        coefs_uni = c_uni["center"]["coefficients"] / dx**2

        c_non_uni = coefficients_non_uni(deriv=2, acc=2, coords=x, idx=5)
        coefs_non_uni = c_non_uni["coefficients"]

        np.testing.assert_array_almost_equal(coefs_non_uni, coefs_uni)
Ejemplo n.º 4
0
    def trendln(self, mintrend, maxtrend):

        mom, momacc = self.momentum()
        self.df['Mom Acc'] = np.round(momacc, 2)
        self.df['Mom'] = np.round(mom, 2)

        coeff = findiff.coefficients(deriv=1, acc=1)

        slope_support = mintrend[-1][1][0]
        intercept_support = mintrend[-1][1][1]

        slope_resistance = maxtrend[-1][1][0]
        intercept_resistance = maxtrend[-1][1][1]

        df = self.get_trend_line(slope_support, intercept_support,
                                 slope_resistance, intercept_resistance)

        return df
Ejemplo n.º 5
0
def make_central_diffs ( order ) :
    """Helper function to get table for central differences
    """
    
    import findiff
    from   ostap.math.base        import lcm 
    from   math                   import fsum, sqrt 
    from   ostap.math.finitediffs import calc_dot 

    def fact ( n ) :
        return 1 if 0 == n else n * fact ( n - 1 ) 

    
    D = order 
    for I in range ( 0 , 26 ) :
    ## for I in range ( 0 , 10 ) :
        
        r = findiff.coefficients(deriv=D, offsets=tuple ( k for k in range (-I-D, I+D+1 ) ), symbolic=True)
        c = r['coefficients']
        ## print ( 'offsets' , r['offsets'] ) 
        denom = lcm (  *[ i.q for i in c ] )
        
        coeffs = [ denom*i for i in c ]
        e      = ( fsum (   ( 1.0 * k * k for k in coeffs ) ) ** 0.5 ) / ( denom * sqrt ( 12.0 ) )
        
        if all (  [ float(i) == i for i in coeffs ] ) :
            
            n       = r['accuracy']
            d       = D 
            stencil = tuple ( range ( -I - D , I + D + 1 ) )

            fun   = lambda x : x**(n+d)
            ff    = fact ( n + d )
            a     = denom * ff
            b     = calc_dot ( fun , 0.0 , 1.0 , stencil , coeffs )
            term  = a / b
            
            to    = topt ( d , n ) 
            
            line = ' RuleConf ( %d , darray ( %s ) , %s , %d , darray ( range ( %s , %s ) ) , %.10g , %.1f , %.4f ) , ' % ( D , coeffs , denom , n , - I - D , I + D + 1 , e , term , to )
            roff = r['offsets']
            assert roff == stencil , 'forward invalid stencil!'
            print ( line )
Ejemplo n.º 6
0
    def test_order2_acc2(self):

        c = coefficients(deriv=2, acc=2)

        coefs = c["center"]["coefficients"]
        np.testing.assert_array_almost_equal(np.array([1., -2., 1.]), coefs)
        offs = c["center"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([-1, 0, 1]), offs)

        coefs = c["forward"]["coefficients"]
        np.testing.assert_array_almost_equal(np.array([2, -5, 4, -1]), coefs)
        offs = c["forward"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([0, 1, 2, 3]), offs)

        coefs = c["backward"]["coefficients"]
        np.testing.assert_array_almost_equal(np.array(([2, -5, 4, -1])[::-1]),
                                             coefs)
        offs = c["backward"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([-3, -2, -1, 0]), offs)
Ejemplo n.º 7
0
    def test_order1_acc2(self):

        c = coefficients(deriv=1, acc=2)

        coefs = c["center"]["coefficients"]
        np.testing.assert_array_almost_equal(np.array([-0.5, 0, 0.5]), coefs)
        offs = c["center"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([-1, 0, 1]), offs)

        coefs = c["forward"]["coefficients"]
        np.testing.assert_array_almost_equal(np.array([-1.5, 2, -0.5]), coefs)
        offs = c["forward"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([0, 1, 2]), offs)

        coefs = c["backward"]["coefficients"]
        np.testing.assert_array_almost_equal(-np.array([-1.5, 2, -0.5])[::-1],
                                             coefs)
        offs = c["backward"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([-2, -1, 0]), offs)
Ejemplo n.º 8
0
    def getCenteredWeights(self, window, coefficients="gaussian", std=5):
        """
        returns the coefficient and the indexes
        """
        offsets = range(-window // 2, window // 2)

        if coefficients == "findiff":
            coefs = findiff.coefficients(1, window)["center"]
            return (coefs["coefficients"], coefs["offsets"])
        elif coefficients == "gaussian":
            coefs = signal.gaussian(window, std,
                                    sym=True)  # get the guaussien filters
            coefs = coefs * 2 / np.sum(
                coefs)  # scale them to sum to one for each side
            coefs = [
                coef if i >= window // 2 else -coef
                for i, coef in enumerate(coefs)
            ]  # invert the first weights
            return coefs, offsets
        else:
            coefs = [
                1 / window if i >= window // 2 else -1 / window
                for i in range(window)
            ]
            offsets = [range(-window // 2, window // 2)]
            return (coefs, offsets)


# import matplotlib.pyplot as plt

# d = Derivative()

# w1, _ = d.getCenteredWeights(16, std=4)
# plt.plot(_, w1, label="std 4")
# w1, _ = d.getCenteredWeights(16, std=100)
# plt.plot(_, w1, label="std 100")
# w1, _ = d.getCenteredWeights(16, std=16//3)
# plt.plot(_, w1, label="std 16/3")

# plt.legend()
# plt.show()
Ejemplo n.º 9
0
    def test_order1_acc4(self):

        c = coefficients(deriv=1, acc=4)
        coefs = c["center"]["coefficients"]
        np.testing.assert_array_almost_equal(
            np.array([1 / 12, -2 / 3, 0, 2 / 3, -1 / 12]), coefs)
        offs = c["center"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([-2, -1, 0, 1, 2]), offs)

        coefs = c["forward"]["coefficients"]
        np.testing.assert_array_almost_equal(
            np.array([-25 / 12, 4, -3, 4 / 3, -1 / 4]), coefs)
        offs = c["forward"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([0, 1, 2, 3, 4]), offs)

        coefs = c["backward"]["coefficients"]
        np.testing.assert_array_almost_equal(
            -np.array([-25 / 12, 4, -3, 4 / 3, -1 / 4])[::-1], coefs)
        offs = c["backward"]["offsets"]
        np.testing.assert_array_almost_equal(-np.array([0, 1, 2, 3, 4])[::-1],
                                             offs)
Ejemplo n.º 10
0
    def test_order2_acc4(self):

        c = coefficients(deriv=2, acc=4)
        coefs = c["center"]["coefficients"]
        np.testing.assert_array_almost_equal(
            np.array([-1 / 12, 4 / 3, -2.5, 4 / 3, -1 / 12]), coefs)
        offs = c["center"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([-2, -1, 0, 1, 2]), offs)

        coefs = c["forward"]["coefficients"]
        np.testing.assert_array_almost_equal(
            np.array([15 / 4, -77 / 6, 107 / 6, -13, 61 / 12, -5 / 6]), coefs)
        offs = c["forward"]["offsets"]
        np.testing.assert_array_almost_equal(np.array([0, 1, 2, 3, 4, 5]),
                                             offs)

        coefs = c["backward"]["coefficients"]
        np.testing.assert_array_almost_equal(
            np.array([15 / 4, -77 / 6, 107 / 6, -13, 61 / 12, -5 / 6])[::-1],
            coefs)
        offs = c["backward"]["offsets"]
        np.testing.assert_array_almost_equal(
            -np.array([0, 1, 2, 3, 4, 5])[::-1], offs)
Ejemplo n.º 11
0
 def test_high_accuracy_results_in_type_error(self):
     # in issue 25 the following line resulted in a TypeError
     findiff.coefficients(deriv=1, acc=15)
Ejemplo n.º 12
0
def derkern(size = 5):
    """ Difference kernel (derivative approximation )"""
    if size == 0:
        return [-1, 1]
    size = size * 2
    return findiff.coefficients(deriv = 1, acc = size)['center']['coefficients']
Ejemplo n.º 13
0
 def test_invalid_deriv_raises_exception(self):
     with self.assertRaises(ValueError):
         coefficients(-1, 2)
     with self.assertRaises(ValueError):
         coefficients_non_uni(-1, 2, None, None)
Ejemplo n.º 14
0
    def test_calc_accuracy_from_offsets_symbolic(self):
        coefs = coefficients(1, offsets=[-4, -2, 0, 2, 4], symbolic=True)

        self.assertEqual(4, coefs["accuracy"])