def run_grid_interpolation(tfunc, dct, N, low, high, scale=1.e-8): Abscissas = Ostap.Math.Interpolation.Abscissas ## uniform abscissas i0 = interpolate(dct) ## bernstein interpolant i1 = interpolate_bernstein(dct, None, low, high) xx = [] for i in range(100000): xx.append(random.uniform(low, high)) xx.sort() c0 = SE() c1 = SE() for x in xx: f = tfunc(x) f0 = i0(x) f1 = i1(x) d0 = f0 - f d1 = f1 - f c0 += abs(d0) / scale c1 += abs(d1) / scale logger.info('Grid precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c0.mean().value(), c0.rms(), c0.max())) logger.info('Bernstein precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c1.mean().value(), c1.rms(), c1.max()))
def test_pickle(): logger = getLogger('test_pickle') logger.info('Check pickling/unpickling') bad = False import pickle rows = [('#', 'before', 'after', 'mean', 'rms')] for i, f in enumerate(progress_bar(functions), start=1): n, ff = f fs = pickle.loads(pickle.dumps(ff)) s = SE() for j in range(1000): x = random.uniform(ff.xmin(), ff.xmax()) s += abs(fs(x) - ff(x)) mean = '%-+.6g' % s.mean() rms = '%-+.6g' % s.rms() if 1.e-7 < s.mean(): mean = attention(mean) if 1.e-7 < s.rms(): rms = attention(rms) row = '%d' % i, ff.__class__.__name__, fs.__class__.__name__, mean, rms ## row = '%d' % i , '%s' % ff , '%s' % fs , '%-+.5g' % s.mean() , '%-+.5g' % s.rms() rows.append(row) import ostap.logger.table as T title = "Compare before/after serialisation" table = T.table(rows, title=title, prefix='# ', alignment='rllll') if bad: logger.warning('%s\n%s' % (title, table)) else: logger.info('%s\n%s' % (title, table))
def test_pickle(): logger = getLogger('test_pickle') logger.info('Check pickling/unpickling') import pickle rows = [('#', 'before', 'after', 'mean', 'rms')] for i, f in enumerate(functions, start=1): fs = pickle.loads(pickle.dumps(f)) s = SE() for j in range(1000): x = random.uniform(f.xmin(), f.xmax()) s += abs(fs(x) - f(x)) row = '%d' % i, f.__class__.__name__, fs.__class__.__name__, '%-+.4g' % s.mean( ), '%-+.4g' % s.rms() rows.append(row) import ostap.logger.table as T title = "Compare before/after eserialisation" table = T.table(rows, title=title, prefix='# ', alignment='rllll') logger.info('%s\n%s' % (title, table))
def run_func_interpolation(fun, N, low, high, scale=1.e-8): Abscissas = Ostap.Math.Interpolation.Abscissas ## uniform abscissas i0 = interpolate(fun, Abscissas(N, low, high, 0)) ## Chebyshev abscissas i1 = interpolate(fun, Abscissas(N, low, high, 1)) ## lobatto abscissas i2 = interpolate(fun, Abscissas(N, low, high, 2)) ## random abscissas i3 = interpolate(fun, [random.uniform(low, high) for i in range(N)]) ## bernstein interpolant i4 = interpolate_bernstein(fun, Abscissas(N, low, high, 2), low, high) xx = [] for i in range(100000): xx.append(random.uniform(low, high)) xx.sort() c0 = SE() c1 = SE() c2 = SE() c3 = SE() c4 = SE() for x in xx: f = fun(x) f0 = i0(x) f1 = i1(x) f2 = i2(x) f3 = i3(x) f4 = i4(x) d0 = f0 - f d1 = f1 - f d2 = f2 - f d3 = f3 - f d4 = f4 - f c0 += abs(d0) / scale c1 += abs(d1) / scale c2 += abs(d2) / scale c3 += abs(d3) / scale c4 += abs(d4) / scale logger.info('Uniform precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c0.mean().value(), c0.rms(), c0.max())) logger.info('Chebyshev precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c1.mean().value(), c1.rms(), c1.max())) logger.info('Lobatto precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c2.mean().value(), c2.rms(), c2.max())) logger.info('Random precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c3.mean().value(), c3.rms(), c3.max())) logger.info('Bernstein precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c4.mean().value(), c4.rms(), c4.max()))
def run_grid_interpolation ( tfunc , dct , N , low , high , scale = 1.e-8 ) : Abscissas = Ostap.Math.Interpolation.Abscissas data = points ( dct ) ## uniform abscissas i0 = interpolate ( data ) ## bernstein interpolant i1 = interpolate_bernstein ( data , None , low , high ) ## neville interpolant i2 = Ostap.Math.Neville ( data ) ## largange interpolant i3 = Ostap.Math.Lagrange ( data ) ## newton interpolant i4 = Ostap.Math.Newton ( data ) ## bspline interpolant degree = 3 ## bs = Ostap.Math.BSpline ( low , high , len ( data ) - 1 - degree , degree ) i5 = interpolate_bspline ( data , None , degree ) xx = [] for i in range ( 100000 ) : xx.append ( random.uniform ( low , high ) ) xx.sort() c0 = SE () c1 = SE () c2 = SE () c3 = SE () c4 = SE () c5 = SE () for x in xx : f = tfunc ( x ) f0 = i0 ( x ) f1 = i1 ( x ) f2 = i2 ( x ) f3 = i3 ( x ) f4 = i4 ( x ) f5 = i5 ( x ) d0 = f0 - f d1 = f1 - f d2 = f2 - f d3 = f3 - f d4 = f4 - f d5 = f5 - f c0 += abs ( d0 ) / scale c1 += abs ( d1 ) / scale c2 += abs ( d2 ) / scale c3 += abs ( d3 ) / scale c4 += abs ( d4 ) / scale c5 += abs ( d5 ) / scale logger.info ( 'Grid precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c0.mean () . value () , c0.rms () , c0.max () ) ) logger.info ( 'Bernstein precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c1.mean () . value () , c1.rms () , c1.max () ) ) logger.info ( 'Neville precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c2.mean () . value () , c2.rms () , c2.max () ) ) logger.info ( 'Lagrange precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c3.mean () . value () , c3.rms () , c3.max () ) ) logger.info ( 'Newton precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c4.mean () . value () , c4.rms () , c4.max () ) ) logger.info ( 'bSpline precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c5.mean () . value () , c5.rms () , c5.max () ) ) import time i5.draw() time.sleep(5)