def doCompute(): inlFactor = xa.SI['zstep'] / xa.SI['inldist'] * xa.SI['dipFactor'] crlFactor = xa.SI['zstep'] / xa.SI['crldist'] * xa.SI['dipFactor'] zw = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin'][ 'Value'][0] + 1 filt = xa.params['Select']['Selection'] filtFunc = autojit(xl.vecmean) if filt == 0 else autojit( xl.vmf_l1) if filt == 1 else autojit(xl.vmf_l2) while True: xa.doInput() dx = -xa.Input['Inl_dip'] / inlFactor dy = -xa.Input['Crl_dip'] / crlFactor dz = np.ones(dx.shape) s = np.sqrt(dx * dx + dy * dy + dz * dz) # # Apply the Vector Filter out = np.empty((3, xa.TI['nrsamp'])) xl.vecFilter(np.array([dx / s, dy / s, dz / s]), zw, filtFunc, out) # # Get the output xa.Output['Crl_dip'] = -out[1, :] / out[2, :] * crlFactor xa.Output['Inl_dip'] = -out[0, :] / out[2, :] * inlFactor xa.Output['True Dip'] = np.sqrt( xa.Output['Crl_dip'] * xa.Output['Crl_dip'] + xa.Output['Inl_dip'] * xa.Output['Inl_dip']) xa.Output['Dip Azimuth'] = np.degrees( np.arctan2(xa.Output['Inl_dip'], xa.Output['Crl_dip'])) xa.doOutput()
def compiled(self): # Compile it if this is the first time. if (not hasattr(self, '_compiled')) and NUMBA_AVAILABLE: if self.autojit_kw is not None: self._compiled = numba.autojit(**self.autojit_kw)(self.func) else: self._compiled = numba.autojit(self.func) return self._compiled
def test_autojit(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") def dummy(): pass autojit(dummy) self.assertEqual(len(w), 1)
def doCompute(): xs = xa.SI['nrinl'] ys = xa.SI['nrcrl'] zs = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin']['Value'][0] + 1 filt = xa.params['Select']['Selection'] filtFunc = autojit(xl.vecmean) if filt==0 else autojit(xl.vmf_l1) if filt==1 else autojit(xl.vmf_l2) inlFactor = xa.SI['zstep']/xa.SI['inldist'] * xa.SI['dipFactor'] crlFactor = xa.SI['zstep']/xa.SI['crldist'] * xa.SI['dipFactor'] band = xa.params['Par_1']['Value'] zw = min(2*int(xa.params['Par_0']['Value'])+1,3) N = xa.params['ZSampMargin']['Value'][1] kernel = xl.hilbert_kernel(N, band) while True: xa.doInput() indata = xa.Input['Input'] # # Analytic Signal # ansig = np.apply_along_axis(np.convolve,-1, indata, kernel, mode="same") sr = np.real(ansig) si = np.imag(ansig) # # Compute partial derivatives sx = xl.kroon3( sr, axis=0 ) sy = xl.kroon3( sr, axis=1 ) sz = xl.kroon3( sr, axis=2 ) shx = xl.kroon3( si, axis=0 ) shy = xl.kroon3( si, axis=1 ) shz = xl.kroon3( si, axis=2 ) px = sr[1:xs-1,1:ys-1,:] * shx[1:xs-1,1:ys-1,:] - si[1:xs-1,1:ys-1,:] * sx[1:xs-1,1:ys-1,:] py = sr[1:xs-1,1:ys-1,:] * shy[1:xs-1,1:ys-1,:] - si[1:xs-1,1:ys-1,:] * sy[1:xs-1,1:ys-1,:] pz = sr[1:xs-1,1:ys-1,:] * shz[1:xs-1,1:ys-1,:] - si[1:xs-1,1:ys-1,:] * sz[1:xs-1,1:ys-1,:] # # Normalise the gradients so Z component is positive p = np.sign(pz)/np.sqrt(px*px+py*py+pz*pz) px *= p py *= p pz *= p # # Filter out = np.empty((3,xa.TI['nrsamp'])) xl.vecFilter(np.array([px,py,pz]), zw, filtFunc, out) # # Get the output xa.Output['Crl_dip'] = -out[1,:]/out[2,:]*crlFactor xa.Output['Inl_dip'] = -out[0,:]/out[2,:]*inlFactor xa.Output['True Dip'] = np.sqrt(xa.Output['Crl_dip']*xa.Output['Crl_dip']+xa.Output['Inl_dip']*xa.Output['Inl_dip']) xa.Output['Dip Azimuth'] = np.degrees(np.arctan2(xa.Output['Inl_dip'],xa.Output['Crl_dip'])) xa.doOutput()
def compare_perf(fn, args, numba= True, cpython = True): parakeet_fn = jit(fn) name = fn.__name__ parakeet_result = None numba_result = None cpython_result = None with timer('Parakeet #1 -- %s' % name): parakeet_result = parakeet_fn(*args) with timer('Parakeet #2 -- %s' % name): parakeet_result = parakeet_fn(*args) if numba: numba_fn = autojit(fn) with timer('Numba #1 -- %s' % name): numba_result = numba_fn(*args) with timer('Numba #2 -- %s' % name): numba_result = numba_fn(*args) if parakeet_result is not None and numba_result is not None: assert np.allclose(parakeet_result, numba_result) if cpython: with timer('Python -- %s' % name): python_result = fn(*args) if cpython_result is not None and parakeet_result is not None: assert np.allclose(parakeet_result, python_result)
def create_numba_funcs(scalar_type=SCALAR_TYPE): this = sys.modules[__name__] pixel_type = scalar_type[:] image_type = scalar_type[:, :, :] state_type = scalar_type[:, :, :] this._numba_window_floor = jit(nopython=True, argtypes=[size_t, size_t], restype=size_t)(_py_window_floor) this._numba_window_ceil = jit(nopython=True, argtypes=[size_t, size_t, size_t], restype=size_t)(_py_window_ceil) this._numba_distance = jit(nopython=True, argtypes=[image_type, size_t, size_t, size_t, size_t], restype=scalar_type)(_py_distance) this._numba_np_distance = jit(nopython=False, argtypes=[pixel_type, pixel_type], restype=scalar_type)(_py_np_distance) this._numba_g = jit(nopython=True, argtypes=[scalar_type], restype=scalar_type)(_py_g) this._numba_np_g = jit(nopython=False, argtypes=[pixel_type, pixel_type], restype=scalar_type)(_py_np_g) this._numba_kernel = autojit(nopython=True)(_py_kernel)
def sqr_dists_loops(X,Y): xs = X.shape[0] ys = Y.shape[0] D = np.zeros((xs, ys), dtype=X.dtype) for i in range(xs): for j in range(ys): D[i,j] = np.sqrt(np.sum((X[i,:] - Y[j,:])**2)) return D ndims = 3000 nsamples = 1e5 nclusters = 1 X = np.random.randn(nsamples, ndims) Y = np.random.randn(nclusters, ndims) # scipy cdist - 20.83 secs start = datetime.now() dsci = cdist(X,Y, 'minkowski', p=2) stop = datetime.now() print("\n\n\t\tScript Running Time: %s"%str(stop - start)) ## Numba - 0.54 secs numba_dists = numba.autojit(sqr_dists_loops) start = datetime.now() dnumba = numba_dists(X,Y) stop = datetime.now() print("\n\n\t\tScript Running Time: %s"%str(stop - start))
def create_numba_funcs(scalar_type=SCALAR_TYPE): this = sys.modules[__name__] pixel_type = scalar_type[:] image_type = scalar_type[:, :, :] state_type = scalar_type[:, :, :] this._numba_window_floor = jit(nopython=True, argtypes=[size_t, size_t], restype=size_t)(_py_window_floor) this._numba_window_ceil = jit(nopython=True, argtypes=[size_t, size_t, size_t], restype=size_t)(_py_window_ceil) this._numba_distance = jit( nopython=True, argtypes=[image_type, size_t, size_t, size_t, size_t], restype=scalar_type)(_py_distance) this._numba_np_distance = jit(nopython=False, argtypes=[pixel_type, pixel_type], restype=scalar_type)(_py_np_distance) this._numba_g = jit(nopython=True, argtypes=[scalar_type], restype=scalar_type)(_py_g) this._numba_np_g = jit(nopython=False, argtypes=[pixel_type, pixel_type], restype=scalar_type)(_py_np_g) this._numba_kernel = autojit(nopython=True)(_py_kernel)
def compare_perf(fn, args, numba= True, cpython = True): parakeet_fn = jit(fn) with timer('Parakeet #1'): parakeet_result = parakeet_fn(*args) with timer('Parakeet #2'): parakeet_result = parakeet_fn(*args) if numba: numba_fn = autojit(fn) with timer('Numba #1'): numba_result = numba_fn(*args) with timer('Numba #2'): numba_result = numba_fn(*args) assert np.allclose(parakeet_result, numba_result) if cpython: with timer('Python'): python_result = fn(*args) assert np.allclose(parakeet_result, python_result)
def __init__(self, x, y, z, spatial_resolution, spectrum): self.x = x """ Size of field in x-direction. """ self.y = y """ Size of field in y-direction. """ self.z = z """ Size of field in z-direction. """ self.spatial_resolution = spatial_resolution """ Spatial resolution. """ self.spectrum = spectrum """ Spectrum. """ try: self._generate = numba.autojit(_generate) except NameError: self._generate = _generate
def doCompute(): xs = xa.SI['nrinl'] ys = xa.SI['nrcrl'] zs = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin']['Value'][0] + 1 zw = zs-2 filt = xa.params['Select']['Selection'] filtFunc = autojit(xl.vecmean) if filt==0 else autojit(xl.vmf_l1) if filt==1 else autojit(xl.vmf_l2) inlFactor = xa.SI['zstep']/xa.SI['inldist'] * xa.SI['dipFactor'] crlFactor = xa.SI['zstep']/xa.SI['crldist'] * xa.SI['dipFactor'] while True: xa.doInput() s = xa.Input['Input'] sh = np.imag( hilbert(s) ) # # Compute partial derivatives sx = xl.kroon3( s, axis=0 ) sy = xl.kroon3( s, axis=1 ) sz = xl.kroon3( s, axis=2 ) shx = xl.kroon3( sh, axis=0 ) shy = xl.kroon3( sh, axis=1 ) shz = xl.kroon3( sh, axis=2 ) px = s[1:xs-1,1:ys-1,:] * shx[1:xs-1,1:ys-1,:] - sh[1:xs-1,1:ys-1,:] * sx[1:xs-1,1:ys-1,:] py = s[1:xs-1,1:ys-1,:] * shy[1:xs-1,1:ys-1,:] - sh[1:xs-1,1:ys-1,:] * sy[1:xs-1,1:ys-1,:] pz = s[1:xs-1,1:ys-1,:] * shz[1:xs-1,1:ys-1,:] - sh[1:xs-1,1:ys-1,:] * sz[1:xs-1,1:ys-1,:] # # Normalise the gradients so Z component is positive p = np.sign(pz)/np.sqrt(px*px+py*py+pz*pz) px *= p py *= p pz *= p # # Filter out = np.empty((3,xa.TI['nrsamp'])) xl.vecFilter(np.array([px,py,pz]), zw, filtFunc, out) # # Get the output xa.Output['Crl_dip'] = -out[1,:]/out[2,:]*crlFactor xa.Output['Inl_dip'] = -out[0,:]/out[2,:]*inlFactor xa.Output['True Dip'] = np.sqrt(xa.Output['Crl_dip']*xa.Output['Crl_dip']+xa.Output['Inl_dip']*xa.Output['Inl_dip']) xa.Output['Dip Azimuth'] = np.degrees(np.arctan2(xa.Output['Inl_dip'],xa.Output['Crl_dip'])) xa.doOutput()
def test(test_n=0): entries=100 if entries == -1 or entries > fee.shape[0]: entries = fee.shape[0] res_tmp = {} #res_tmp["entries"] = [entries] if test_n==0: start_t = time() sase_map = dset_loop_chunk(fee, entries) print "Numpy chunked loop: %s" % (time() - start_t) res_tmp["np chunk"] = [time() - start_t,] elif test_n==1: start_t = time() sase_map = dset_loop_chunk_loop(fee, entries) print "Numpy chunked loop loop: %s" % (time() - start_t) res_tmp["np chunk loop"] = [time() - start_t,] elif test_n==2: start_t = time() sase_map = dset_loop_chunk_numba(fee, entries) print "Numba chunked loop: %s" % (time() - start_t) res_tmp["numba chunk"] = [time() - start_t,] # very slow! #if fee[0].shape[0] < 2000: elif test_n==3: dset_loop_numba = autojit(dset_loop) start_t = time() sase_map = dset_loop_numba(fee, entries) print "Numba loop: %s" % (time() - start_t) res_tmp["numba loop"] = [time() - start_t] # very slow! elif test_n==4: start_t = time() sase_map = dset_loop(fee, entries) print "Python loop: %s" % (time() - start_t) res_tmp["python loop"] = [time() - start_t] elif test_n==5: try: start_t = time() sase_map = fee[:entries].sum(axis=2) print "Numpy: %s" % (time() - start_t) res_tmp["np"] = [time() - start_t,] except: res_tmp["np"] = [np.nan] #except: # pass return res_tmp
def doCompute(): xs = xa.SI['nrinl'] ys = xa.SI['nrcrl'] zs = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin'][ 'Value'][0] + 1 zw = zs - 2 filt = xa.params['Select']['Selection'] filtFunc = autojit(xl.vecmean) if filt == 0 else autojit( xl.vmf_l1) if filt == 1 else autojit( xl.vmf_l2) if filt == 2 else autojit(xl.vmf_x3) inlFactor = xa.SI['zstep'] / xa.SI['inldist'] * xa.SI['dipFactor'] crlFactor = xa.SI['zstep'] / xa.SI['crldist'] * xa.SI['dipFactor'] while True: xa.doInput() p = xa.Input['Input'] # # Compute partial derivatives px = xl.kroon3(p, axis=0)[1:xs - 1, 1:ys - 1, :] py = xl.kroon3(p, axis=1)[1:xs - 1, 1:ys - 1, :] pz = xl.kroon3(p, axis=2)[1:xs - 1, 1:ys - 1, :] # # Normalise the gradients so Z component is positive p = np.sign(pz) / np.sqrt(px * px + py * py + pz * pz) px *= p py *= p pz *= p # # Filter out = np.empty((3, xa.TI['nrsamp'])) xl.vecFilter(np.array([px, py, pz]), zw, filtFunc, out) # # Get the output xa.Output['Crl_dip'] = -out[1, :] / out[2, :] * crlFactor xa.Output['Inl_dip'] = -out[0, :] / out[2, :] * inlFactor xa.Output['True Dip'] = np.sqrt( xa.Output['Crl_dip'] * xa.Output['Crl_dip'] + xa.Output['Inl_dip'] * xa.Output['Inl_dip']) xa.Output['Dip Azimuth'] = np.degrees( np.arctan2(xa.Output['Inl_dip'], xa.Output['Crl_dip'])) xa.doOutput()
def doCompute(): inlFactor = xa.SI['zstep']/xa.SI['inldist'] * xa.SI['dipFactor'] crlFactor = xa.SI['zstep']/xa.SI['crldist'] * xa.SI['dipFactor'] zw = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin']['Value'][0] + 1 filt = xa.params['Select']['Selection'] filtFunc = autojit(xl.vecmean) if filt==0 else autojit(xl.vmf_l1) if filt==1 else autojit(xl.vmf_l2) while True: xa.doInput() dx = -xa.Input['Inl_dip']/inlFactor dy = -xa.Input['Crl_dip']/crlFactor dz = np.ones(dx.shape) s = np.sqrt(dx*dx+dy*dy+dz*dz) # # Apply the Vector Filter out = np.empty((3,xa.TI['nrsamp'])) xl.vecFilter(np.array([dx/s,dy/s,dz/s]), zw, filtFunc, out) # # Get the output xa.Output['Crl_dip'] = -out[1,:]/out[2,:]*crlFactor xa.Output['Inl_dip'] = -out[0,:]/out[2,:]*inlFactor xa.Output['True Dip'] = np.sqrt(xa.Output['Crl_dip']*xa.Output['Crl_dip']+xa.Output['Inl_dip']*xa.Output['Inl_dip']) xa.Output['Dip Azimuth'] = np.degrees(np.arctan2(xa.Output['Inl_dip'],xa.Output['Crl_dip'])) xa.doOutput()
def main(): if len(sys.argv) > 1: N = int(sys.argv[1]) else: N = 10000 for name in [ 'sqrt', 'log', 'exp', ]: print '%s python: ' % name, func = globals()[name + '_test'] t = time.time() func(N) print time.time() - t print '%s numba: ' % name, func = autojit(func) t = time.time() func(N) print time.time() - t
def test_valid_compare(): array_nb = autojit(array) a = np.random.rand(1e6) assert np.allclose(array(a), array_nb(a))
# Authors: Olivier Grisel # License: MIT from pairwise import pairwise_python from numba import autojit benchmarks = (("pairwise_numba_nested_for_loops", autojit(pairwise_python.pairwise_python_nested_for_loops)), )
#################### ## TEST FUNCTIONS ## #################### ### TRIPLE FOR def triplefor(taille): count = 0 for i in xrange(taille): for j in xrange(taille): for k in xrange(taille): count += i * j + k return count / taille tripleforN = autojit(triplefor) ####################### ## TIMINGS FUNCTIONS ## ####################### def timings(nb_tries, start=1, stop=200, step=10): values = range(start, stop, step) setup = { 'triplefor': ('triplefor(%s)', 'from __main__ import triplefor'), 'tripleforN': ('tripleforN(%s)', 'from __main__ import tripleforN'), 'tripleforC': ('cython_test(%s)', 'from __main__ import cython_test'),
def arma(parameters,data,p=0,q=0): T = data.size errors = np.zeros_like(data) for t in xrange(T): errors[t] = data[t] - parameters[0] for i in xrange(p): if (t-i) >= 0: errors[t] -= parameters[i+1] * data[t-i] for i in xrange(q): if (t-i) >= 0: errors[t] -= parameters[i+p+1] * errors[t-i] return errors arma_autojit = autojit(arma) arma_jit = jit(double[:](double[:],double[:],int32,int32))(arma) parameters = np.zeros((3)) data = np.random.randn(10000) p = 1 q = 1 if __name__=='__main__': arma(parameters,data,p,q) arma_autojit(parameters,data,p,q) arma_jit(parameters,data,p,q)
arg.start + self.plus, arg.stop + self.plus ) def __rsub__(self, arg): return type(arg)( arg.start - self.mnus, arg.stop - self.mnus ) one = Shift(1,1) sl = slice(2,5) print sl+one try: from numba import autojit, jit, double fast_f = autojit(f) #fast_f = jit(restype=double[:],argtypes=[double[:], double[:], double[:]])(f) t0 = time.time() output = fast_f(Psi_l, Psi_r, CC) print "time: numba", time.time() - t0 print "output numba", output except ImportError: print "Not using Numba nor Numbapro" t1 = time.time() output = f(Psi_l, Psi_r, CC) print "time: numpy", time.time() - t1 print "output numpy", output
# ______________________________________________________________________ def _for_loop_fn_3(stop): acc = 0 for i in range(stop): for j in range(stop): for k in range(stop): for l in range(stop): acc += 1 return acc for_loop_fn_0 = autojit(backend='ast')(_for_loop_fn_0) for_loop_fn_1 = autojit(backend='ast')(_for_loop_fn_1) for_loop_fn_2 = autojit(backend='ast')(_for_loop_fn_2) for_loop_fn_3 = autojit(backend='ast')(_for_loop_fn_3) # ______________________________________________________________________ class TestForLoop(unittest.TestCase): # @unittest.skipUnless(__debug__, "Requires implementation of iteration " # "over arrays.") def test_compiled_for_loop_fn_0(self): for dtype in (np.float32, np.float64, np.int32, np.int64): test_data = np.arange(10, dtype=dtype) result = for_loop_fn_0(test_data) self.assertEqual(result, 45)
def tdo_fft(inputfile, outputfile): ''' Perform Fourier transform and return frequency evolution ''' comp_expf = autojit(comp_exp) # Input parameters fourier_le = 1024 # Fourier length time_le = 1024 # timewindow dfmin = 100 # Frequency resolution dt = 2e-8 # timestep of acquisition # Lecture du fichier fid = open(inputfile, 'rb') fid.seek(512) # Skip useless header V = fromfile(fid, int16, -1, '') fid.close() pstart = 1 # First timewindow pend = int(floor((len(V) - fourier_le) / time_le)) # Last timewindow V = V - mean(V) # Remove zero frequency contribution to FT t = arange(0, fourier_le) * dt # Approximation of main frequency Vf = abs(real(fft(V[0:fourier_le]))) tf = fftfreq(fourier_le, dt) fmax = zeros((pend + 1 - pstart, 3)) fmax[0, 1] = tf[argmax(Vf[0:int(fourier_le / 2)])] fmax[0, 0] = time_le * dt / 2 # Calculation of constants expon = -2j * pi * t deltaf0 = tf[1] / 1000 # Windowing (uncomment desired window function) # Rectangular window = ones(fourier_le) # Cosinus #window = arange(0,fourier_le) #window = 1-cos(window*2*pi/(fourier_le-1)) # Kaiser-Bessel #window = arange(0,fourier_le) #window = 0.402-0.498*cos(window*2*pi/(fourier_le-1))+0.098*cos(window*4*pi/(fourier_le-1))+0.001*cos(window*6*pi/(fourier_le-1)) # Precise determination of oscillating frequency via DFT for i in xrange(pstart, pend): # Utilisation de la dernière valeur comme point de depart a = fmax[i - 1, 1] V_temp = window * V[i * time_le:i * time_le + fourier_le] # Previous frequency spectral weight # Complex exponential time consuming ! Need a smarter way to perform this calculations deltaf = deltaf0 essaimax = abs(local_trapz(V_temp * comp_expf(expon * a))) # Calculation of local derivative of Fourier transform # If derivative positive, then search for frequency in growing direction if abs(local_trapz(V_temp * comp_expf(expon * (a + deltaf)))) > essaimax: while abs(deltaf) > dfmin: F = abs(local_trapz(V_temp * comp_expf(expon * (a + deltaf)))) if F > essaimax: essaimax = F a += deltaf else: deltaf = -deltaf / 5 # Store frequency fmax[i, 0:2] = [(i * time_le + fourier_le / 2) * dt, a - 2.5 * deltaf] # Lower frequency otherwise else: while abs(deltaf) > dfmin: F = abs(local_trapz(V_temp * comp_expf(expon * (a - deltaf)))) if F > essaimax: essaimax = F a -= deltaf else: deltaf = -deltaf / 5 # Store frequency fmax[i, 0:2] = [(i * time_le + fourier_le / 2) * dt, a + 2.5 * deltaf] # Save calculation in file fmax[:, 2] = smooth(fmax[:, 1])[5:-5] savetxt(outputfile, fmax)
from growcut import growcut_python from numba import autojit benchmarks = ( ("growcut_numba", autojit(growcut_python.growcut_python)), )
def primes(limit): # Keep only odd numbers in sieve, mapping from index to number is # num = 2 * idx + 3 # The square of the number corresponding to idx then corresponds to: # idx2 = 2*idx*idx + 6*idx + 3 sieve = [True] * (limit // 2) prime_numbers = set([2]) for j in range(len(sieve)): if sieve[j]: new_prime = 2 * j + 3 prime_numbers.add(new_prime) for k in range((2 * j + 6) * j + 3, len(sieve), new_prime): sieve[k] = False return list(prime_numbers) numba_primes = autojit(primes) start = time() numba_primes(LIMIT) end = time() print("Numba: Time Taken : ", end - start) start = time() primes(LIMIT) end = time() print("Python: Time Taken : ", end - start)
import d2q9_nsnxny_loop as d2q9 from numba import double, jit, autojit # m2f = jit('void(f8[:, :, :], f8[:, :, :])')(d2q9.m2f) # f2m = jit('void(f8[:, :, :], f8[:, :, :])')(d2q9.f2m) # transport = jit('void(f8[:, :, :])')(d2q9.transport) # relaxation = jit('void(f8[:, :, :])')(d2q9.relaxation) # periodic_bc = jit('void(f8[:, :, :])')(d2q9.periodic_bc) m2f = autojit(d2q9.m2f) f2m = autojit(d2q9.f2m) transport = autojit(d2q9.transport) relaxation = autojit(d2q9.relaxation) periodic_bc = autojit(d2q9.periodic_bc) @autojit def one_time_step(f, m): periodic_bc(f) transport(f) f2m(f, m) relaxation(m) m2f(m, f)
def compare_perf(fn, args, numba=True, cpython=True, extra={}, backends=('c', 'openmp', 'cuda'), suppress_output=False, propagate_exceptions=False): parakeet_fn = jit(fn) name = fn.__name__ parakeet_result = None numba_result = None cpython_result = None kwargs = { 'suppress_stdout': suppress_output, 'suppress_stderr': suppress_output, 'propagate_exceptions': propagate_exceptions } backend = None for backend in backends: with timer('Parakeet (backend = %s) #1 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend=backend) with timer('Parakeet (backend = %s) #2 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend=backend) if numba: from numba import autojit, config numba_fn = autojit(fn) with timer('Numba #1 -- %s' % name, **kwargs): numba_result = numba_fn(*args) with timer('Numba #2 -- %s' % name, **kwargs): numba_result = numba_fn(*args) if cpython: with timer('Python -- %s' % name, **kwargs): cpython_result = fn(*args) for name, impl in extra.iteritems(): with timer("%s #1" % name, **kwargs): impl(*args) with timer("%s #2" % name, **kwargs): extra_result = impl(*args) if python_result is not None: diffs = np.abs(parakeet_result - extra_result) assert np.allclose(parakeet_result, extra_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and %s = %s (median = %s, min = %s)" % \ (name, np.max(diffs), np.median(diffs), np.min(diffs)) rtol = 0.0001 if backend in ('cuda', 'gpu'): atol = 0.001 else: atol = 0.00001 if parakeet_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - parakeet_result) assert np.allclose(cpython_result, parakeet_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs)) if numba_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - numba_result) assert np.allclose(cpython_result, numba_result, atol = atol, rtol = rtol), \ "Max elt difference between Numba and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs))
julia = np.empty((N, N), dtype=np.uint32) grid_x = np.linspace(-bound, bound, N) for i, x in enumerate(grid_x): for j, y in enumerate(grid_x): julia[i,j] = kernel(x, y, cr, ci, lim, cutoff=cutoff) return julia def julia(cr, ci, N, bound=1.5, lim=1000., cutoff=1e6): grid_x = np.linspace(-bound, bound, N) return np.array([[kernel(x,y,cr,ci,lim,cutoff=cutoff) for x in grid_x] for y in grid_x]) from compare_perf import compare_perf cr=0.285 ci=0.01 N=1200 bound = 1.5 lim = 1000 cutoff = 1e6 extra = {} try: from numba import autojit extra['numba'] = autojit(julia_loops) except: print "Failed to import Numba" compare_perf(julia, [cr, ci, N, bound, lim, cutoff], numba = False, extra = extra)
# function to check if the point is on the polygon EPSILON = 2 def pointOnPolygon(point, poly): for i in range(len(poly)): [a, b] = poly[i - 1], poly[i] if abs( vincenty(a, point, miles=False) + vincenty(b, point, miles=False) - vincenty(a, b, miles=False)) < EPSILON: return True return False pointOnPolygon = autojit(pointOnPolygon) # compute list of channels that are not available; chan_NA is the list of list of channels not available for the center node df = pd.read_csv('Columbus_TVData_GradeB_SepDis.csv') cols_to_use = df.columns cols_to_use = [] for degree in range(1, 360, 2): cols_to_use.append("Latitude." + str(degree)) cols_to_use.append("Longitude." + str(degree)) dfa = df[cols_to_use] fr = dfa fr.values.shape poly_overall = fr.values.reshape((len(dfa), 180, 2)) path_overall = [] for row in range(0, len(dfa)):
# Authors: Yuancheng Peng # License: MIT from arc_distance import arc_distance_python as adp from numba import autojit benchmarks = (("arc_distance_numba_for_loops", autojit(adp.arc_distance_python_nested_for_loops)), )
break ## Bin to the left elif xr <= xnewl: pass ## This condition should not happen else: pass i += 1 ## Add the sum to ynew if weight != 0: ynew[ii] = val/weight ii += 1 return ynew if 'numba' in sys.modules: Interp_integrate = autojit(Interp_integrate) def Resample_linlog(xold): """Resample_linlog(xold) Resample a linear wavelength vector to a log space and returns the new vector and the Doppler shift z. The resampling is done such that the largest wavelength interval is conserved in order to preserve the spectral resolution. The Doppler shift is: 1+z = lambda_1 / lambda_0 In the non-relativistic limit: z = v/c
import time def f(n): t0 = time.time() result = 0.0 for i in range(n): for j in range(n * i): result += sin(pi / 2) return int(result), time.time() - t0 n = 250 res_py = f(n) print "Number of Loops %8d" % res_py[0] print "Time in Sec for Python %8.3f" % res_py[1] import numba as nb f_nb = nb.autojit(f) res_nb = f_nb(n) print "Number of Loops %8d" % res_nb[0] print "Time in Sec for Python %8.3f" % res_nb[1] print "Number of Loops %8d" % res_py[0] print "Speed-up of Numba %8d" % (res_py[1] / res_nb[1])
def tdo_fft(inputfile, outputfile): ''' Perform Fourier transform and return frequency evolution ''' comp_expf=autojit(comp_exp) # Input parameters fourier_le = 1024 # Fourier length time_le = 1024 # timewindow dfmin = 100 # Frequency resolution dt = 2e-8 # timestep of acquisition # Lecture du fichier fid = open(inputfile, 'rb') fid.seek(512) # Skip useless header V = fromfile(fid, int16, -1, '') fid.close() pstart = 1 # First timewindow pend = int(floor((len(V)-fourier_le)/time_le)) # Last timewindow V = V-mean(V) # Remove zero frequency contribution to FT t = arange(0, fourier_le)*dt # Approximation of main frequency Vf = abs(real(fft(V[0:fourier_le]))) tf = fftfreq(fourier_le, dt) fmax = zeros((pend+1-pstart, 3)) fmax[0, 1] = tf[argmax(Vf[0:int(fourier_le/2)])] fmax[0, 0] = time_le*dt/2 # Calculation of constants expon = -2j*pi*t deltaf0 = tf[1]/1000 # Windowing (uncomment desired window function) # Rectangular window = ones(fourier_le) # Cosinus #window = arange(0,fourier_le) #window = 1-cos(window*2*pi/(fourier_le-1)) # Kaiser-Bessel #window = arange(0,fourier_le) #window = 0.402-0.498*cos(window*2*pi/(fourier_le-1))+0.098*cos(window*4*pi/(fourier_le-1))+0.001*cos(window*6*pi/(fourier_le-1)) # Precise determination of oscillating frequency via DFT for i in xrange(pstart, pend): # Utilisation de la dernière valeur comme point de depart a = fmax[i-1, 1] V_temp = window*V[i*time_le:i*time_le+fourier_le] # Previous frequency spectral weight # Complex exponential time consuming ! Need a smarter way to perform this calculations deltaf = deltaf0 essaimax=abs(local_trapz(V_temp*comp_expf(expon*a))) # Calculation of local derivative of Fourier transform # If derivative positive, then search for frequency in growing direction if abs(local_trapz(V_temp*comp_expf(expon*(a+deltaf)))) > essaimax: while abs(deltaf)>dfmin: F = abs(local_trapz(V_temp*comp_expf(expon*(a+deltaf)))) if F > essaimax: essaimax = F a += deltaf else: deltaf = -deltaf/5 # Store frequency fmax[i, 0:2] = [(i*time_le+fourier_le/2)*dt, a-2.5*deltaf] # Lower frequency otherwise else: while abs(deltaf)>dfmin: F = abs(local_trapz(V_temp*comp_expf(expon*(a-deltaf)))) if F > essaimax: essaimax = F a -= deltaf else: deltaf = -deltaf/5 # Store frequency fmax[i, 0:2] = [(i*time_le+fourier_le/2)*dt, a+2.5*deltaf] # Save calculation in file fmax[:,2] = smooth(fmax[:,1])[5:-5] savetxt(outputfile, fmax)
def regridx(Variable): '''Regrids Xp1 to X (Time averaged) Future : check if 4 dimentions for non timeaveraged? ''' if len(np.shape(Variable)) == 4: Vc = (Variable[:, :, :, 0:-1] + Variable[:, :, :, 1::]) / 2 else: Vc = (Variable[:, :, 0:-1] + Variable[:, :, 1::]) / 2 return Vc # Numba them numba_regridy = autojit()(regridy) numba_regridy.func_name = "numba_regridy" numba_regridz = autojit()(regridz) numba_regridz.func_name = "numba_regridz" numba_regridx = autojit()(regridx) numba_regridx.func_name = "numba_regridx" def maxmag(var): m1 = np.max(var, axis=0) m2 = np.min(var, axis=0) if m1 < abs(m2): val = m2 else: val = m1
with timer('Python (comprehensions)'): sqr_dists(X, Y) with timer('Python (loops)'): sqr_dists_loops(X, Y) # # Numba # import numba # # Numba's @autojit just like Parakeet's @jit # numba_dists = numba.autojit(sqr_dists) with timer('Numba (comprehensions) #1'): numba_dists(X, Y) with timer('Numba (comprehensions) #2'): numba_dists(X, Y) numba_dists_loops = numba.autojit(sqr_dists_loops) with timer('Numba (loops) #1'): numba_dists_loops(X, Y) with timer('Numba (loops) #2'): numba_dists_loops(X, Y)
with timer('Python (loops)'): sqr_dists_loops(X,Y) # # Numba # import numba # # Numba's @autojit just like Parakeet's @jit # numba_dists = numba.autojit(sqr_dists) with timer('Numba (comprehensions) #1'): numba_dists(X,Y) with timer('Numba (comprehensions) #2'): numba_dists(X,Y) numba_dists_loops = numba.autojit(sqr_dists_loops) with timer('Numba (loops) #1'): numba_dists_loops(X,Y) with timer('Numba (loops) #2'): numba_dists_loops(X,Y)
for t in range(1, 1501): train_sum = np.zeros(x_train.shape[0]) test_sum = np.zeros(x_test.shape[0]) for i in range(t): train_sum += alphas[i] * linearPred(x_train, ls_w_arr[i]) test_sum += alphas[i] * linearPred(x_test, ls_w_arr[i]) train_err_arr[t - 1] = np.sum( y_train != np.sign(train_sum)) / y_train.shape[0] test_err_arr[t - 1] = np.sum(y_test != np.sign(test_sum)) / y_test.shape[0] return train_err_arr, test_err_arr # In[105]: autojit(linearPred) autojit(train_test_errors) # In[106]: train_err_arr, test_err_arr = train_test_errors(bd.x_train, bd.x_test, bd.y_train, bd.y_test, alphas, ls_w_arr) # In[107]: plt.figure() sb.axes_style("darkgrid") plt.plot(range(1, 1501), train_err_arr, 'g', label="training error") plt.plot(range(1, 1501), test_err_arr, 'r', label="testing error") plt.xlabel("Iteratons t")
def compare_perf(fn, args, numba= True, cpython = True, extra = {}, backends = ('c', 'openmp', 'cuda'), suppress_output = False, propagate_exceptions = False): parakeet_fn = jit(fn) name = fn.__name__ parakeet_result = None numba_result = None cpython_result = None kwargs = {'suppress_stdout': suppress_output, 'suppress_stderr':suppress_output, 'propagate_exceptions' : propagate_exceptions } backend = None for backend in backends: with timer('Parakeet (backend = %s) #1 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend = backend) with timer('Parakeet (backend = %s) #2 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend = backend) if numba: from numba import autojit, config numba_fn = autojit(fn) with timer('Numba #1 -- %s' % name, **kwargs): numba_result = numba_fn(*args) with timer('Numba #2 -- %s' % name, **kwargs): numba_result = numba_fn(*args) if cpython: with timer('Python -- %s' % name, **kwargs): cpython_result = fn(*args) for name, impl in extra.iteritems(): with timer("%s #1" % name, **kwargs): impl(*args) with timer("%s #2" % name, **kwargs): extra_result = impl(*args) if python_result is not None: diffs = np.abs(parakeet_result - extra_result) assert np.allclose(parakeet_result, extra_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and %s = %s (median = %s, min = %s)" % \ (name, np.max(diffs), np.median(diffs), np.min(diffs)) rtol = 0.0001 if backend in ('cuda', 'gpu'): atol = 0.001 else: atol = 0.00001 if parakeet_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - parakeet_result) assert np.allclose(cpython_result, parakeet_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs)) if numba_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - numba_result) assert np.allclose(cpython_result, numba_result, atol = atol, rtol = rtol), \ "Max elt difference between Numba and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs))
# Authors: Olivier Grisel # License: MIT from pairwise import pairwise_python from numba import autojit benchmarks = ( ("pairwise_numba_nested_for_loops", autojit(pairwise_python.pairwise_python_nested_for_loops)), )
ii] = np.apply_along_axis(interp, 0, ZFF, Z[::-1], Vc[tt, ::-1, jj, ii]) for kk in range(len(ZFF)): Tpff[tt, kk, jj, ii] = find_nearest(Rho, Tpff[tt, kk, jj, ii]) Vprime = Vpff - VTavff Tprime = Tpff[:] - TTavffbin[:] VTprime = Vprime * Tprime VTprimetav = np.mean(VTprime, axis=0) VTprimetav20 = (VTprimetav20 + VTprimetav / (6 * total)) VTbar = np.mean(Vpff * Tpff, axis=0) VTbar20 = VTbar20 + VTbar / (6 * total) return VTprimetav20, VTbar20 numba_eddyfluxbin = autojit()(eddyfluxbin) numba_eddyfluxbin.func_name = "eddyfluxbin" # Here we go... print 'Setup done... starting flux calc' for file in lists: print file # Where am i? #VTprimetav20, VTbar20 = numba_eddyfluxbin(file, VTprimetav20, # VTbar20, 'VVEL') file2 = netCDF4.Dataset(file, 'r') Temp = file2.variables['THETA'][:] V = file2.variables['VVEL'][:] Vc = numba_regridy(V) for yr in range((6)): yr1 = 100 * yr yr2 = yr1 + 100 Vc = Vc[yr1:yr2]
def test(): f_ = autojit(f) for v in range(-10,10): assert f_(v)==f(v) assert f_(float(v))==f(float(v))
from julia import julia_python from numba import autojit benchmarks = (("julia_numba_for_loops", autojit(julia_python.julia_python_for_loops)), )
# Authors: Yuancheng Peng # License: MIT from arc_distance import arc_distance_python from numba import autojit benchmarks = ( ("arc_distance_numba_for_loops", autojit(arc_distance_python.arc_distance_python_nested_for_loops)), )
def test_ifexp(): f_ = autojit(f) for args in product(range(3), range(3)): assert f_(*args) == f(*args)
def test_ifexp(): f_ = autojit(f) for args in product(range(3), range(3)): assert f_(*args)==f(*args)
num = 0.0 for ii in range(Mf): for jj in range(Nf): num += (filt[Mf - 1 - ii, Nf - 1 - jj] * image[i - Mf2 + ii, j - Nf2 + jj]) result[i, j] = num return result # This kind of quadruply-nested for-loop is going to be quite slow. # Using Numba we can compile this code to LLVM which then gets # compiled to machine code: # Now fastfilter_2d runs at speeds as if you had first translated # it to C, compiled the code and wrapped it with Python fastfilter_2d = jit(double[:, :](double[:, :], double[:, :]))(filter2d) autofilter_2d = autojit(filter2d) # Use utool to time this imports = ut.codeblock(r''' # STARTBLOCK import numpy as np from numba import double, jit, autojit # ENDBLOCK ''') datas = ut.codeblock(r''' # STARTBLOCK fastfilter_2d = jit(double[:, :](double[:, :], double[:, :]))(filter2d) autofilter_2d = autojit(filter2d) rng = np.random.RandomState(0) image = rng.rand(100, 100)
from __future__ import print_function, division, absolute_import import numpy as np from numba import autojit def test(a, b): r = np.empty(3, dtype=bool) for i in range(len(a)): r[i] = a[i] != b[i] return r test_nb = autojit(test) a = np.arange(3, dtype=complex) b = np.arange(3, dtype=complex) b[1] += 1j assert np.array_equal(test(a, b), test_nb(a, b))
A_r = 255 - A_r + 0.001 #Prevent division by zero out = np.empty_like(img, dtype=np.float32) row, col = img.shape[:2] for x in xrange(row): for y in xrange(col): x_init = max(x-1, 0) x_final = min(x+2, row-1) y_init = max(y-1,0) y_final = min(y+2, col-1) out[x,y,0] = np.min(img[x_init:x_final, y_init:y_final,0])/A_b out[x,y,1] = np.min(img[x_init:x_final, y_init:y_final,1])/A_g out[x,y,2] = np.min(255 - img[x_init:x_final, y_init:y_final,2])/A_r out = np.max(1 - np.min(out, axis=2), 0.1) #0.1 is the minimum transmission map value return out tMap = autojit(calcTransmissionMap) def waterlight_estimation(img): """Estimate brighest pixel in the image""" img = np.float32(img) b,g,r = cv2.split(np.float32(img)) minval,maxval,minloc,maxloc = cv2.minMaxLoc(r) waterlight = img[maxloc[1], maxloc[0]] return waterlight def redchannelprior(img): airlight = waterlight_estimation(img) t = tMap(img, airlight) return img def minkowski_norm(grayimg, p):
def __init__(self, plus, mnus): self.plus = plus self.mnus = mnus def __radd__(self, arg): return type(arg)(arg.start + self.plus, arg.stop + self.plus) def __rsub__(self, arg): return type(arg)(arg.start - self.mnus, arg.stop - self.mnus) one = Shift(1, 1) sl = slice(2, 5) print sl + one try: from numba import autojit, jit, double fast_f = autojit(f) #fast_f = jit(restype=double[:],argtypes=[double[:], double[:], double[:]])(f) t0 = time.time() output = fast_f(Psi_l, Psi_r, CC) print "time: numba", time.time() - t0 print "output numba", output except ImportError: print "Not using Numba nor Numbapro" t1 = time.time() output = f(Psi_l, Psi_r, CC) print "time: numpy", time.time() - t1 print "output numpy", output
@parakeet.jit def parakeet_local_maxima(data, wsize, mode=wrap): def is_max(pos): def is_smaller_neighbor(offset): neighbor_idx = tuple(mode(p, o-w/2, w) for (p, o, w) in zip(pos, offset, wsize)) return data[neighbor_idx] <= data[pos] return np.all(parakeet.imap(is_smaller_neighbor, wsize)) return parakeet.imap(is_max, data.shape) ### # Numba fails no matter what I do with the code, giving up for now # import numba numba_local_maxima = numba.autojit(python_local_maxima) if __name__ == '__main__': from timer import timer shape = (30,10,10,12) x = np.random.randn(*shape) wsize = (3,3,3,3) with timer("Parakeet (first run)"): parakeet_result = parakeet_local_maxima(x, wsize) with timer("Parakeet (second run)"): parakeet_result = parakeet_local_maxima(x, wsize) with timer("Numba (first run)"): numba_result = numba_local_maxima(x, wsize, wrap) with timer("Numba (second run)"): numba_result = numba_local_maxima(x, wsize, wrap) with timer("Python"):
grid_x = np.linspace(-bound, bound, N) for i, x in enumerate(grid_x): for j, y in enumerate(grid_x): julia[i, j] = kernel(x, y, cr, ci, lim, cutoff=cutoff) return julia def julia(cr, ci, N, bound=1.5, lim=1000., cutoff=1e6): grid_x = np.linspace(-bound, bound, N) return np.array( [[kernel(x, y, cr, ci, lim, cutoff=cutoff) for x in grid_x] for y in grid_x]) from compare_perf import compare_perf cr = 0.285 ci = 0.01 N = 1200 bound = 1.5 lim = 1000 cutoff = 1e6 extra = {} try: from numba import autojit extra['numba'] = autojit(julia_loops) except: print "Failed to import Numba" compare_perf(julia, [cr, ci, N, bound, lim, cutoff], numba=False, extra=extra)
X, Y = np.meshgrid(x, y) w = np.exp(-(X**2 + Y**2) / 2 / 17.5**2) # Conversion to nanoseconds... 'D' is nanoseconds Z = raster[:, :] - np.max(raster[:, :]) D = (abs(Z) / 0.15) + 10 # Start all recordings after 10 ns for i in range(200): for j in range(200): ibin = np.int(D[i, j]) #finding ibin #add power within pixel (i,j)-which is w(i,j) to the returned #power in the bin number ibin if ibin < 544: cumpowr[ibin] = cumpowr[ibin] + w[i, j] return cumpowr wave_numba = autojit(waveformC) folder = "/Users/sgrigsby/Desktop/April_21st_tile_output/" idx = [] for i, filename in enumerate(os.listdir(folder)): fn, ext = os.path.splitext(filename) # source = gdal.Open(folder + filename) # (source.RasterYSize & source.RasterXSize) == 200 idx.append(fn) #April_20th_wf_no_conv = pd.DataFrame(index=idx,columns=np.r_[0:544:1]) space = np.zeros((len(idx), 544), dtype=float) for i, filename in enumerate(tqdm(os.listdir(folder))): #fn, ext = os.path.splitext(filename)
from growcut import growcut_python from numba import autojit benchmarks = (("growcut_numba", autojit(growcut_python.growcut_python)), )
## Bin to the left elif xr <= xnewl: pass ## This condition should not happen else: pass i += 1 ## Add the sum to ynew if weight != 0: ynew[ii] = val / weight ii += 1 return ynew if 'numba' in sys.modules: Interp_integrate = autojit(Interp_integrate) def Resample_linlog(xold): """Resample_linlog(xold) Resample a linear wavelength vector to a log space and returns the new vector and the Doppler shift z. The resampling is done such that the largest wavelength interval is conserved in order to preserve the spectral resolution. The Doppler shift is: 1+z = lambda_1 / lambda_0 In the non-relativistic limit: z = v/c
from julia import julia_python from numba import autojit benchmarks = ( ("julia_numba_for_loops", autojit(julia_python.julia_python_for_loops)), )
break ## Bin to the left elif xr <= xnewl: pass ## This condition should not happen else: pass i += 1 ## Add the sum to ynew if weight != 0: ynew[ii] = val/weight ii += 1 return ynew if 'numba' in sys.modules: Interp_linear_integrate = autojit(Interp_linear_integrate) def Resample_linlog(xold): """ Resample a linear wavelength vector to a log space and returns the new vector and the Doppler shift z. The resampling is done such that the largest wavelength interval is conserved in order to preserve the spectral resolution. The Doppler shift is: 1+z = lambda_1 / lambda_0 In the non-relativistic limit: z = v/c