def test_speed(self): mod_name = 'list_speed'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = list(range(1000000)) code = """ int v, sum = 0; for(int i = 0; i < a.len(); i++) { v = a[i]; if (v % 2) sum += v; else sum -= v; } return_val = sum; """ with_cxx = ext_tools.ext_function('with_cxx',code,['a']) mod.add_function(with_cxx) code = """ long vv, sum = 0; PyObject *v; for(int i = 0; i < a.len(); i++) { v = PyList_GetItem(py_a,i); //didn't set error here -- just speed test vv = PyLong_AsLong(v); if (vv % 2) sum += vv; else sum -= vv; } return_val = sum; """ no_checking = ext_tools.ext_function('no_checking',code,['a']) mod.add_function(no_checking) mod.compile(location=test_dir, compiler=self.compiler) loaded_module = __import__(mod_name) with_cxx = loaded_module.with_cxx no_checking = loaded_module.no_checking t1 = time.time() sum1 = with_cxx(a) t2 = time.time() debug_print('speed test for list access') debug_print('compiler:', self.compiler) debug_print('scxx:', t2 - t1) t1 = time.time() sum2 = no_checking(a) t2 = time.time() debug_print('C, no checking:', t2 - t1) sum3 = 0 t1 = time.time() for i in a: if i % 2: sum3 += i else: sum3 -= i t2 = time.time() debug_print('python:', t2 - t1) assert_(sum1 == sum2 and sum1 == sum3)
def test_speed(self): mod_name = 'list_speed'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = list(range(1000000)) code = """ int v, sum = 0; for(int i = 0; i < a.len(); i++) { v = a[i]; if (v % 2) sum += v; else sum -= v; } return_val = sum; """ with_cxx = ext_tools.ext_function('with_cxx',code,['a']) mod.add_function(with_cxx) code = """ int vv, sum = 0; PyObject *v; for(int i = 0; i < a.len(); i++) { v = PyList_GetItem(py_a,i); //didn't set error here -- just speed test vv = py_to_int(v,"list item"); if (vv % 2) sum += vv; else sum -= vv; } return_val = sum; """ no_checking = ext_tools.ext_function('no_checking',code,['a']) mod.add_function(no_checking) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import with_cxx, no_checking') t1 = time.time() sum1 = with_cxx(a) t2 = time.time() debug_print('speed test for list access') debug_print('compiler:', self.compiler) debug_print('scxx:', t2 - t1) t1 = time.time() sum2 = no_checking(a) t2 = time.time() debug_print('C, no checking:', t2 - t1) sum3 = 0 t1 = time.time() for i in a: if i % 2: sum3 += i else: sum3 -= i t2 = time.time() debug_print('python:', t2 - t1) assert_(sum1 == sum2 and sum1 == sum3)
def build_fibonacci(): """ Builds an extension module with fibonacci calculators. """ mod = ext_tools.ext_module('fibonacci_ext') a = 1 # this is effectively a type declaration # recursive fibonacci in C fib_code = """ int fib1(int a) { if(a <= 2) return 1; else return fib1(a-2) + fib1(a-1); } """ ext_code = """ return_val = fib1(a); """ fib = ext_tools.ext_function('c_fib1', ext_code, ['a']) fib.customize.add_support_code(fib_code) mod.add_function(fib) # looping fibonacci in C fib_code = """ int fib2( int a ) { int last, next_to_last, result; if( a <= 2 ) return 1; last = next_to_last = 1; for(int i = 2; i < a; i++ ) { result = last + next_to_last; next_to_last = last; last = result; } return result; } """ ext_code = """ return_val = fib2(a); """ fib = ext_tools.ext_function('c_fib2', ext_code, ['a']) fib.customize.add_support_code(fib_code) mod.add_function(fib) mod.compile()
def check_var_in(self,level=5): mod = ext_tools.ext_module('wx_var_in',compiler='msvc') mod.customize.add_header('<wx/string.h>') mod.customize.add_extra_compile_arg(' '.join(self.s.extra_compile_args)) mod.customize.add_extra_link_arg(' '.join(self.s.extra_link_args)) a = wx.Frame(None,-1,'bob') code = """ py::tuple args(1); args[0] = py::object("jim"); a.mcall("SetTitle",args); """ test = ext_tools.ext_function('test',code,['a'],locals(),globals()) mod.add_function(test) mod.compile() import wx_var_in b=a wx_var_in.test(b) assert(b.GetTitle() == "jim") try: b = 1. wx_var_in.test(b) except TypeError: pass try: b = 1 wx_var_in.test(b) except TypeError: pass
def build_increment_ext(): """ Build a simple extension with functions that increment numbers. The extension will be built in the local directory. """ mod = ext_tools.ext_module('increment_ext') # Effectively a type declaration for 'a' in the following functions. a = 1 ext_code = "return_val = PyLong_FromLong(a+1);" func = ext_tools.ext_function('increment',ext_code,['a']) mod.add_function(func) ext_code = "return_val = PyLong_FromLong(a+2);" func = ext_tools.ext_function('increment_by_2',ext_code,['a']) mod.add_function(func) mod.compile()
def no_test_no_check_return(self,level=5): mod = ext_tools.ext_module('wx_return') a = 'string' code = """ a= Py::wx("hello"); return_val = Py::new_reference_to(a); """ test = ext_tools.ext_function('test',code,['a'],locals()) mod.add_function(test) mod.compile() import wx_return b='bub' c = wx_return.test(b) assert(c == 'hello')
def check_string_and_int(self,level=5): # decalaring variables a = 2;b = 'string' # declare module mod = ext_tools.ext_module('ext_string_and_int') code = """ a=b.length(); return_val = PyInt_FromLong(a); """ test = ext_tools.ext_function('test',code,['a','b']) mod.add_function(test) mod.compile(location = build_dir) import ext_string_and_int c = ext_string_and_int.test(a,b) assert(c == len(b))
def test_return(self): mod_name = 'string_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = """ a= std::string("hello"); return_val = PyUnicode_FromString(a.c_str()); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = 'bub' c = test(b) assert_(c == 'hello')
def test_complex_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1.+1j code = """ a= a + std::complex<double>(2.,2.); return_val = PyComplex_FromDoubles(a.real(),a.imag()); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = 1.+1j c = test(b) assert_(c == 3.+3j)
def test_float_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = """ a=a+2.; return_val = PyFloat_FromDouble(a); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = 1. c = test(b) assert_(c == 3.)
def test_float_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = """ a=a+2.; return_val = PyFloat_FromDouble(a); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1. c = test(b) assert_(c == 3.)
def test_string_and_int(self): # decalaring variables a = 2 b = 'string' # declare module mod = ext_tools.ext_module('ext_string_and_int') code = """ a=b.length(); return_val = PyLong_FromLong(a); """ test = ext_tools.ext_function('test',code,['a','b']) mod.add_function(test) mod.compile(location=build_dir) import ext_string_and_int c = ext_string_and_int.test(a,b) assert_(c == len(b))
def test_complex_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1.+1j code = """ a= a + std::complex<double>(2.,2.); return_val = PyComplex_FromDoubles(a.real(),a.imag()); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1.+1j c = test(b) assert_(c == 3.+3j)
def test_return(self): mod_name = 'string_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = """ a= std::string("hello"); return_val = PyString_FromString(a.c_str()); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 'bub' c = test(b) assert_(c == 'hello')
def test_return(self): mod_name = 'list_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = [1] code = """ a=py::list(); a.append("hello"); return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = [1,2] c = test(b) assert_(c == ['hello'])
def test_return(self): mod_name = 'dict_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = {'z':1} code = """ a=py::dict(); a["hello"] = 5; return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = {'z':2} c = test(b) assert_(c['hello'] == 5)
def test_return(self): mod_name = 'list_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = [1] code = """ a=py::list(); a.append("hello"); return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = [1,2] c = test(b) assert_(c == ['hello'])
def test_int_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1 code = """ a=a+2; return_val = PyInt_FromLong(a); """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1 c = test(b) assert_(c == 3)
def test_return(self): mod_name = 'dict_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = {'z':1} code = """ a=py::dict(); a["hello"] = 5; return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = {'z':2} c = test(b) assert_(c['hello'] == 5)
def test_return(self): mod_name = 'tuple_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = (1,) code = """ a=py::tuple(2); a[0] = "hello"; a.set_item(1,py::None); return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = (1,2) c = test(b) assert_(c == ('hello',None))
def test_return(self): mod_name = 'tuple_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = (1,) code = """ a=py::tuple(2); a[0] = "hello"; a.set_item(1,py::None); return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = (1,2) c = test(b) assert_(c == ('hello',None))
def build_ramp_ext(): mod = ext_tools.ext_module('ramp_ext') # type declarations result = array([0], float64) start, end = 0., 0. code = """ const int size = Nresult[0]; const double step = (end-start)/(size-1); double val = start; for (int i = 0; i < size; i++) { result[i] = val; val += step; } """ func = ext_tools.ext_function('Ramp', code, ['result', 'start', 'end']) mod.add_function(func) mod.compile(compiler='gcc')
def test_return_tuple(self): # decalaring variables a = 2 # declare module mod = ext_tools.ext_module('ext_return_tuple') var_specs = ext_tools.assign_variable_types(['a'],locals()) code = """ int b; b = a + 1; py::tuple returned(2); returned[0] = a; returned[1] = b; return_val = returned; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=build_dir) import ext_return_tuple c,d = ext_return_tuple.test(a) assert_(c == a and d == a+1)
def test_complex_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1.+1j code = "a=std::complex<double>(2.,2.);" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = 1.+1j test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'dict_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = {'z':1} code = 'a=py::dict();' # This just checks to make sure the type is correct test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = {'y':2} test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_complex_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1.+1j code = "a=std::complex<double>(2.,2.);" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1.+1j test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'tuple_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = (1,) code = 'a=py::tuple();' test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = (1,2) test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_float_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = "a=2.;" test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1. test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'int_var_in' + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1 code = "a=2;" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1 test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'dict_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = {'z':1} code = 'a=py::dict();' # This just checks to make sure the type is correct test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = {'y':2} test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'tuple_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = (1,) code = 'a=py::tuple();' test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = (1,2) test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'list_var_in' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = [1] code = 'a=py::list();' test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = [1, 2] test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'int_var_in' + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1 code = "a=2;" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = 1 test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'string_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = 'a=std::string("hello");' test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 'bub' test(b) try: b = 1. test(b) except TypeError: pass try: b = 1 test(b) except TypeError: pass
def test_var_in(self): mod_name = 'string_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = 'a=std::string("hello");' test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) test = __import__(mod_name).test b = 'bub' test(b) try: b = 1. test(b) except TypeError: pass try: b = 1 test(b) except TypeError: pass
def sxrd(): """ Function to build C++ extension modules used for sxrd calculations """ mod = ext_tools.ext_module('sxrd_ext') # Defs to set the types of the input arrays h = array([0, 1, 2], dtype=float64) k, l, x, y, z, u, oc, dinv = [h] * 8 f = (x[:, newaxis] * x).astype(complex128) Pt = array([ c_[array([[1, 0], [0, 1]]), array([0, 0])], c_[array([[1, 0], [0, 1]]), array([0, 0])] ], dtype=float64) code = ''' double pi = 3.14159265358979311599796346854418516159057617187500; int ij = 0; int offset = 0; std::complex<double> im(0.0, 1.0), tmp; PyObject* out_array = PyArray_SimpleNew(1, &Nh[0], NPY_COMPLEX128); std::complex<double>* fs = (std::complex<double>*) ((PyArrayObject*) out_array)->data; //printf("Atoms: %d, Points: %d, Symmetries: %d\\n", Noc[0], Nh[0], NPt[0]); // Loop over all data points for(int i = 0; i < Nh[0]; i++){ fs[i] = 0; // Loop over all atoms //printf("l = %f\\n", l[i]); for(int j = 0; j < Noc[0]; j++){ ij = i + j*Nh[0]; //printf(" x = %f, y = %f, z = %f, u = %f, oc = %f \\n", x[j], y[j], z[j], u[j], oc[j]); // Loop over symmetry operations tmp = 0; for(int m = 0; m < NPt[0]; m++){ offset = m*6; tmp += exp(2.0*pi*im*(h[i]*( Pt[0 + offset]*x[j] + Pt[1 + offset]*y[j] + Pt[2 + offset])+ k[i]*(Pt[3+offset]*x[j] + Pt[4+offset]*y[j]+ Pt[5 + offset]) + l[i]*z[j])); if(i == 0 && j == 0 && false){ printf("P = [%f, %f] [%f, %f]", Pt[0 + offset], Pt[1 + offset], Pt[3 + offset], Pt[4 + offset]); printf(", t = [%f, %f]\\n", Pt[2 + offset], Pt[5+offset]); } // End if statement } // End symmetry loop index m fs[i] += oc[j]*f[ij]*exp(-2.0*pow(pi*dinv[i],2.0)*u[j])*tmp; } // End atom loop index j } // End data point (h,k,l) loop return_val = out_array; Py_XDECREF(out_array); ''' ext = ext_tools.ext_function( 'surface_lattice_sum', code, ['x', 'y', 'z', 'h', 'k', 'l', 'u', 'oc', 'f', 'Pt', 'dinv']) mod.add_function(ext) mod.compile()
def paratt(): """ Function to build c extension of different reincarnations of the Paratt's algorithm. """ mod = ext_tools.ext_module('paratt_ext') # Inline code for Refl function code = ''' int points=Ntheta[0]; int interfaces=Nsigma[0]-1; int j=0; double k = 2*3.141592/lamda; double torad = 3.141592/180.0; double costheta2; std::complex<double> Qj, Qj1, r, rp, p, imag(0.0,1.0); std::complex<double> n2amb = n[interfaces]*n[interfaces]; //PyObject* out_array = PyArray_SimpleNew(1, Ntheta, NPY_DOUBLE); //double* r_array = (double*) ((PyArrayObject*) out_array)->data; PyObject* out_array = PyArray_SimpleNew(1, Ntheta, NPY_COMPLEX128); std::complex<double>* r_array = (std::complex<double> *) ((PyArrayObject*) out_array)->data; for(int i = 0; i < points; i++){ costheta2 = cos(theta[i]*torad); costheta2 *= costheta2; Qj = 2.0*k*sqrt(n[0]*n[0] - n2amb*costheta2); Qj1 = 2.0*k*sqrt(n[1]*n[1] - n2amb*costheta2); rp=(Qj - Qj1)/(Qj1 + Qj)*exp(-Qj1*Qj/2.0*sigma[0]*sigma[0]); r = rp; for(j = 1; j < interfaces; j++){ Qj = Qj1; Qj1 = 2.0*k*sqrt(n[j+1]*n[j+1] - n2amb*costheta2); rp = (Qj-Qj1)/(Qj1+Qj)*exp(-Qj1*Qj/2.0*sigma[j]*sigma[j]); p = exp(imag*d[j]*Qj); r = (rp+r*p)/(1.0 + rp*r*p); } //r_array[i]=std::real(r*conj(r)); r_array[i]=r; } return_val = out_array; Py_XDECREF(out_array); ''' lamda = 1.54 theta = array([0, 1, 2, 3], dtype=float64) n = array([0, 0, 0]).astype(complex128) d = array([0, 0, 0]).astype(float64) sigma = d.astype(float64) refl = ext_tools.ext_function('refl', code, [ 'theta', 'lamda', 'n', 'd', 'sigma', ]) mod.add_function(refl) # Generating module for reflectivity with q input. lamda = 1.54 Q = array([0, 1, 2, 3], dtype=float64) n = array([0, 0, 0]).astype(complex128) d = array([0, 0, 0]).astype(float64) sigma = d.astype(float64) code = ''' int points=NQ[0]; int interfaces=Nsigma[0]-1; double Q02 = 16*3.141592*3.141592/lamda/lamda; std::complex<double> Q2, Qj, Qj1, r, rp, p, imag(0.0,1.0); std::complex<double> n2amb = n[interfaces]*n[interfaces]; PyObject* out_array = PyArray_SimpleNew(1, NQ, NPY_COMPLEX128); std::complex<double>* r_array = (std::complex<double> *) ((PyArrayObject*) out_array)->data; int j=0; for(int i = 0; i < points; i++){ Q2 = Q[i]*Q[i]; Qj = sqrt((n[0]*n[0] - n2amb)*Q02 + n2amb*Q2); Qj1 = sqrt((n[1]*n[1] - n2amb)*Q02 + n2amb*Q2); rp=(Qj - Qj1)/(Qj1 + Qj)*exp(-Qj1*Qj/2.0*sigma[0]*sigma[0]); r = rp; for(j = 1; j < interfaces; j++){ Qj = Qj1; Qj1 = sqrt((n[j+1]*n[j+1] - n2amb)*Q02 + n2amb*Q2); rp = (Qj-Qj1)/(Qj1+Qj)*exp(-Qj1*Qj/2.0*sigma[j]*sigma[j]); p = exp(imag*d[j]*Qj); r = (rp+r*p)/(1.0 + rp*r*p); } //r_array[i] = std::real(r*conj(r)); r_array[i] = r; } return_val = out_array; Py_XDECREF(out_array); ''' reflq = ext_tools.ext_function('reflq', code, [ 'Q', 'lamda', 'n', 'd', 'sigma', ]) mod.add_function(reflq) # Code for generating Refl_nvary2 theta = array([0, 1, 2, 3], dtype=float64) lamda = 1.54 * ones(theta.shape) n = array([0, 0, 0]).astype(complex128)[:, newaxis] * ones(theta.shape) d = array([0, 0, 0]).astype(float64) sigma = d.astype(float64) code = ''' int points=Ntheta[0]; int layers = Nn[0]; int interfaces=Nsigma[0]-1; int j, m; double costheta2, k; double pi = 3.141592; double torad = pi/180.0; std::complex<double> Qj, Qj1, r, rp, p, imag(0.0, 1.0); std::complex<double> n2amb; //PyObject* out_array = PyArray_SimpleNew(1, Ntheta, NPY_DOUBLE); //double* r_array = (double*) ((PyArrayObject*) out_array)->data; PyObject* out_array = PyArray_SimpleNew(1, Ntheta, NPY_COMPLEX128); std::complex<double>* r_array = (std::complex<double> *) ((PyArrayObject*) out_array)->data; for(int i = 0;i < points; i++){ costheta2 = cos(theta[i]*torad); costheta2 *= costheta2; n2amb = n[interfaces*points + i]; n2amb *= n2amb; k = 4*pi/lamda[i]; Qj = k*sqrt(n[0*points + i]*n[0*points + i] - n2amb*costheta2); Qj1 = k*sqrt(n[1*points + i]*n[1*points + i] - n2amb*costheta2); rp = (Qj - Qj1)/(Qj1 + Qj)*exp(-Qj1*Qj/2.0*sigma[0]*sigma[0]); r = rp; for(j = 1; j < interfaces; j++){ Qj = Qj1; Qj1 = k*sqrt(n[(j+1)*points + i]*n[(j+1)*points + i] - n2amb*costheta2); rp = (Qj - Qj1)/(Qj1 + Qj)*exp(-Qj1*Qj/2.0*sigma[j]*sigma[j]); p = exp(imag*d[j]*Qj); r = (rp+r*p)/(1.0+rp*r*p); } //r_array[i] = real(r*conj(r)); r_array[i] = r; } return_val = out_array; Py_XDECREF(out_array); ''' refl_nvary2 = ext_tools.ext_function('refl_nvary2', code, [ 'theta', 'lamda', 'n', 'd', 'sigma', ]) mod.add_function(refl_nvary2) #Code for generating refl_nvary2_nosigma code = ''' int points=Ntheta[0]; int layers = Nn[0]; int interfaces=Nd[0]-1; int j, m; double costheta2, k; double pi = 3.141592; double torad = pi/180.0; std::complex<double> Qj, Qj1, r, rp, p, imag(0.0, 1.0); std::complex<double> n2amb; //PyObject* out_array = PyArray_SimpleNew(1, Ntheta, NPY_DOUBLE); //double* r_array = (double*) ((PyArrayObject*) out_array)->data; PyObject* out_array = PyArray_SimpleNew(1, Ntheta, NPY_COMPLEX128); std::complex<double>* r_array = (std::complex<double> *) ((PyArrayObject*) out_array)->data; for(int i = 0;i < points; i++){ costheta2 = cos(theta[i]*torad); costheta2 *= costheta2; n2amb = n[interfaces*points + i]; n2amb *= n2amb; k = 4*pi/lamda[i]; Qj = k*sqrt(n[0*points + i]*n[0*points + i] - n2amb*costheta2); Qj1 = k*sqrt(n[1*points + i]*n[1*points + i] - n2amb*costheta2); rp = (Qj - Qj1)/(Qj1 + Qj); r = rp; for(j = 1; j < interfaces; j++){ Qj = Qj1; Qj1 = k*sqrt(n[(j+1)*points + i]*n[(j+1)*points + i] - n2amb*costheta2); rp = (Qj - Qj1)/(Qj1 + Qj); p = exp(imag*d[j]*Qj); r = (rp+r*p)/(1.0+rp*r*p); } //r_array[i] = real(r*conj(r)); r_array[i] = r; } return_val = out_array; Py_XDECREF(out_array); ''' refl_nvary2_nosigma = ext_tools.ext_function('refl_nvary2_nosigma', code, ['theta', 'lamda', 'n', 'd']) mod.add_function(refl_nvary2_nosigma) mod.compile()
def build_polygonmask(): print('Compiling polymask_ext...') verts = np.array([[549., 1096.], [144., 51.], [989., 38.], [549., 1096.]]) points = np.array([[0, 0], [0, 1], [0, 2]]) verts = verts.astype(np.float64) points = points.astype(np.float64) xp = np.ascontiguousarray(verts[:, 0]) yp = np.ascontiguousarray(verts[:, 1]) x = np.ascontiguousarray(points[:, 0]) y = np.ascontiguousarray(points[:, 1]) out = np.empty(len(points), dtype=np.uint8) mod = ext_tools.ext_module('polygonmask_ext') code = """ /* Code from: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html Copyright (c) 1970-2003, Wm. Randolph Franklin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers. 2. Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution. 3. The name of W. Randolph Franklin may not be used to endorse or promote products derived from this Software without specific prior written permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ int i,j,n; unsigned int c; int nr_verts = Nxp[0]; for (n = 0; n < Nx[0]; n++) { c = 0; for (i = 0, j = nr_verts-1; i < nr_verts; j = i++) { if ((((yp(i)<=y(n)) && (y(n)<yp(j))) || ((yp(j)<=y(n)) && (y(n)<yp(i)))) && (x(n) < (xp(j) - xp(i)) * (y(n) - yp(i)) / (yp(j) - yp(i)) + xp(i))) c = !c; } out(n) = c; } """ #weave.inline(code, ['xp','yp','x','y','out'], type_converters=weave.converters.blitz) polymsk = ext_tools.ext_function('polymsk', code, ['xp', 'yp', 'x', 'y', 'out'], type_converters=converters.blitz) mod.add_function(polymsk) kw, file = mod.build_kw_and_file(workdir, {}) success = build_tools.build_extension(file, temp_dir=temp_dir, compiler_name='gcc', verbose=0, **kw) if success: print('\n****** polymask_ext module compiled succesfully! *********\n')
def offspec(): """ Function to build C++ extension modules used for off-specular calculations using DWBA """ mod = ext_tools.ext_module('offspec_ext') # ext_tools need the input variables to detemine the number of dimensions? and types. qx = array([0, 0, 0], dtype=float) qz = qx sqn = array([0, 0], dtype=complex) sigma = sqn[:].astype(dtype=complex) sigmaid = sqn[:].astype(dtype=complex) z = sqn[:].astype(dtype=float) qlay = sqn[:, newaxis] * qx G = array([qlay, qlay, qlay, qlay], dtype=complex) q = array([qlay, qlay, qlay, qlay], dtype=complex) eta = 200.0 eta_z = 2000.0 h = 1.0 q_min = qx.astype(dtype=float) table = qx.astype(dtype=complex) fn = arange(1.0, 3) code = """ int Layers = NG[1]; int Points = Nqx[0]; int nind = 0; int npind = 0; double xnew = 0; int lower = 0; std::complex<double> s(0.0, 0.0), Im(0.0, 1.0), Itemp(0.0, 0.0); PyObject* out_array = PyArray_SimpleNew(1, Nqx, NPY_DOUBLE); double* I = (double*) ((PyArrayObject*) out_array)->data; for(int m=0;m < Points;m++){ Itemp = 0; for(int i=0;i < Layers;i++){ for(int j=0;j < Layers;j++){ for(int k=0;k < 4; k++){ for(int l=0;l < 4; l++){ nind=(k*Layers+i)*Points+m; npind=(l*Layers+j)*Points+m; for(int p=0; p < Nfn[0]; p++){ //added abs sigan 051201 xnew=fabs(qx[m]*eta/pow(p+1.0,1.0/2.0/h)); //xnew=qx[m]*eta/pow(p+1.0,1.0/2.0/h); lower=int((xnew-q_min[0])/(q_min[1]-q_min[0])); s += 2.0*pow(q[nind]*conj(q[npind])*sigma[i]*sigma[j]*exp(-abs(z[i]-z[j])/eta_z),p+1.0)/fn[p]*eta/pow(p+1.0,1.0/2.0/h)*((table[lower+1]-table[lower])/(q_min[lower+1]-q_min[lower])*(xnew-q_min[lower])+table[lower]); } Itemp += (sqn[i]-sqn[i+1])*conj(sqn[j]-sqn[j+1])*G[nind]*exp(-0.5*pow(sigmaid[i]*q[nind],2))*conj(G[npind]*exp(-0.5*pow(sigmaid[j]*q[npind],2)))*exp(-0.5*(pow(q[nind]*sigma[i],2)+pow(conj(q[npind])*sigma[j],2)))/q[nind]/conj(q[npind])*s; //*exp(-Im*q[nind]*z[i]-conj(Im*q[npind])*z[j]) s = 0; } } } } I[m] = std::real(Itemp); } return_val = out_array; Py_XDECREF(out_array); """ ext = ext_tools.ext_function('dwba_interdiff_sum', code, [ 'qx', 'G', 'q', 'eta', 'h', 'sigma', 'sigmaid', 'sqn', 'z', 'table', 'q_min', 'fn', 'eta_z' ]) mod.add_function(ext) code = """ int Layers=NG[1]; int Points=Nqx[0]; int nind=0; int npind=0; double xnew=0; int lower=0; std::complex<double> s(0.0, 0.0), Im(0.0, 1.0), Itemp(0.0, 0.0); PyObject* out_array = PyArray_SimpleNew(1, Nqx, NPY_DOUBLE); double* I = (double*) ((PyArrayObject*) out_array)->data; for(int m=0;m < Points;m++){ Itemp = 0; for(int i=0;i < Layers;i++){ for(int j=0;j < Layers;j++){ for(int k=0;k < 4; k++){ for(int l=0;l < 4; l++){ nind=(k*Layers+i)*Points+m; npind=(l*Layers+j)*Points+m; for(int p=0; p < Nfn[0]; p++){ xnew = fabs(qx[m]*eta/pow(p+1.0,1.0/2.0/h)); lower=int((xnew-q_min[0])/(q_min[1]-q_min[0])); s+=2.0*pow(q[nind]*conj(q[npind])*sigma[i]*sigma[j]*exp(-abs(z[i]-z[j])/eta_z),p+1.0)/fn[p]*eta/pow(p+1.0,1.0/2.0/h)*((table[lower+1]-table[lower])/(q_min[lower+1]-q_min[lower])*(xnew-q_min[lower])+table[lower]); } Itemp += (sqn[i]-sqn[i+1])*conj(sqn[j]-sqn[j+1])*G[nind]*conj(G[npind])*exp(-0.5*(pow(q[nind]*sigma[i],2.0)+pow(conj(q[npind])*sigma[j],2.0)))/q[nind]/conj(q[npind])*s; //*exp(-Im*q[nind]*z[i]-conj(Im*q[npind])*z[j]) s = 0; } } } } I[m] = std::real(Itemp); } return_val = out_array; Py_XDECREF(out_array); """ ext = ext_tools.ext_function('dwba_sum', code, [ 'qx', 'G', 'q', 'eta', 'h', 'sigma', 'sqn', 'z', 'table', 'q_min', 'fn', 'eta_z' ]) mod.add_function(ext) # Code for Born approximation code = """ int Layers=Nsigma[0]; int Points=Nqx[0]; double xnew=0; int lower=0; std::complex<double> s(0.0, 0.0), Im(0.0, 1.0), Itemp(0.0, 0.0); PyObject* out_array = PyArray_SimpleNew(1, Nqx, NPY_DOUBLE); double* I = (double*) ((PyArrayObject*) out_array)->data; for(int m=0;m < Points;m++){ Itemp = 0; for(int i=0;i < Layers;i++){ for(int j=0;j < Layers;j++){ for(int p=0; p < Nfn[0]; p++){ xnew=qx[m]*eta/pow(p+1.0,1.0/2.0/h); lower=int((xnew-q_min[0])/(q_min[1]-q_min[0])); s += 2.0*pow(qz[m]*qz[m]*sigma[i]*sigma[j]*exp(-abs(z[i]-z[j])/eta_z),p+1.0)/fn[p]*eta/pow(p+1.0,1.0/2.0/h)*((table[lower+1]-table[lower])/(q_min[lower+1]-q_min[lower])*(xnew-q_min[lower])+table[lower]); } Itemp+=(sqn[i]-sqn[i+1])*conj(sqn[j]-sqn[j+1])*exp(-0.5*(pow(qz[m]*sigma[i],2.0)+pow(qz[m]*sigma[j],2.0)))/qz[m]/qz[m]*s; s = 0; } } I[m] = std::real(Itemp); } return_val = out_array; Py_XDECREF(out_array); """ ext = ext_tools.ext_function('born_sum', code, [ 'qx', 'qz', 'eta', 'h', 'sigma', 'sqn', 'z', 'table', 'q_min', 'fn', 'eta_z' ]) mod.add_function(ext) mod.compile()
def build_testCode(): print('Compiling testmodule...') tst = np.ones(30, dtype=np.long) qmatrix = np.zeros((10, 10), dtype=np.float64) qmatrix[:, 1] = 1 mod = ext_tools.ext_module('test_ext') code = """ #define WINDOW_LENGTH 30 size_t tst_length; long tstval; long *data; long *window_ptr; double *qmatrix_ptr; int win_len,half_len; double std; blitz::Array<double,1> subarray; /* Creation of blitz array for blitz testing */ //subarray = qmatrix(blitz::Range(window_start_idx, window_end_idx), int(r)); long window[WINDOW_LENGTH]; int start_idx = 0; int end_idx = WINDOW_LENGTH; win_len = WINDOW_LENGTH; window_ptr = &window[0]; /* Initialize pointer to window array */ window[0] = 999; window[4] = 10; qmatrix_ptr = (double *) qmatrix_array->data; data = (long *) tst_array->data; /* Cast pointer to numpy array so it works as a regular C array */ tst_length = PyObject_Length(py_tst); /* Use python method to determine length on python object */ printf("Tst length: %d\\n", (int) tst_length); /** Use stride info to increment an array idx **/ //printf("Element 2: %d\\n", *(long *) (tst_array->data + 2*tst_array->strides[0])); printf("Element 0: %d", *(long *) data); printf("\\nElement 6: %d", data[5]); printf("\\nWindow 0: %d", *window_ptr); data[5] += 1; printf("\\nWindow 6: %d\\n", window_ptr[5]); data[5] = 10; data = (long *) tst_array->data; data = data + 30; data++; /* Pointers needs to be moved back since */ window_ptr--; /* *data++ doesn't work, but *++data does.. strange! */ data = data + start_idx; /* Move pointer forward to where the window should start */ qmatrix_ptr = (double *) PyArray_GETPTR2(qmatrix_array, 1, 2); printf("qval: %f\\n", *qmatrix_ptr); qmatrix(1, 5) = 10; while(win_len--) /* Move values from data array to the window array */ { *++window_ptr = *--data; printf("%d, %d\\n", *data, *window_ptr); } int t = 5; while(t--) /* Move values from data array to the window array */ { ++qmatrix_ptr; printf("q: %f\\n", *qmatrix_ptr); } window_ptr = &window[0]; /* Reset pointers */ data = (long *) tst_array->data; std = getStd(window_ptr, WINDOW_LENGTH); win_len = WINDOW_LENGTH; window_ptr = window; /* Reset pointers */ half_len = win_len / 2.0; printf("Win len: %d\\n", win_len); printf("Half len: %d\\n", half_len); printf("Half len val: %d\\n", window_ptr[half_len]); printf("Half len val: %d\\n", window_ptr[15]); printf("Standard Deviation %f", std); printf("\\nWindow 5: %d\\n\\n", window_ptr[4]); quicksort(window, 0, WINDOW_LENGTH-1); printlist(window, WINDOW_LENGTH); """ sup_code = """ double getStd(long *window_ptr, int win_len) { int half_win_len; double mean, variance_n, variance, M2, n, delta; M2 = 0; n = 0.0; mean = 0; variance = 1; delta=0; half_win_len = win_len / 2; while(half_win_len--) { ++n; delta = ((double) *window_ptr) - mean; mean = mean + (delta/n); M2 = M2 + (delta * (((double) *window_ptr) - mean)); ++window_ptr; } if(n > 1) variance = M2/(n - 1); /* To avoid devide by zero */ return sqrt(variance); } void printlist(long list[],long n) { int i; for(i = 0; i < n; i++) printf("%d\t", list[i]); } void swap(long *x, long *y) { long temp; temp = *x; *x = *y; *y = temp; } long choose_pivot(long i, long j) { return((i+j) /2); } void quicksort(long list[], long m, long n) { long key,i,j,k; if( m < n) { k = choose_pivot(m, n); swap(&list[m], &list[k]); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(&list[i], &list[j]); } // swap two elements swap(&list[m], &list[j]); // recursively sort the lesser list quicksort(list, m, j-1); quicksort(list, j+1, n); } } """ test_func = ext_tools.ext_function('test', code, ['tst', 'qmatrix'], type_converters=converters.blitz) test_func.customize.add_support_code(sup_code) mod.add_function(test_func) kw, file = mod.build_kw_and_file(workdir, {}) success = build_tools.build_extension(file, temp_dir=temp_dir, compiler_name='gcc', verbose=0, **kw) if success: print('\n****** test module compiled succesfully! *********\n') else: print('\n****** test module failed! *********\n')
def weave_compile(): """Compile C++ simulation code using numpy.weave so that it can be used in the Python program. Generate c_code.so file. """ # ---------------------Specify variable types-------------------- # The parameters below are used only to specify the types of variables # when compiling the C++ code, and does not affect the actual application # when it's run. params = { 'Gradient Intensity': [0.0, 0.0, 1.52], 'Cell Density': 0.5, 'Angular Inertia': 2.5284, 'Alignment Strength': 1.5284, 'Pinned Cells': ['none', 'none', 'none'], 'Gradient Angle': [0.79, 0.47, 1.83], 'Alignment Range': 11.1, 'Affinity': [[3.25, 1.13, 1.15], [1.13, 1.36, 4.0], [1.15, 4.0, 1.48]], 'Attraction-Repulsion Strength': 4.7298, 'Cell Ratio': [0.5, 0.5, 0.], 'Noise Intensity': 0.28, 'Velocity': [0.034, 0.016, 0.169], 'Attraction-Repulsion Range': 10.2 } steps = 1 # Number of steps # Empty list for storing global properties global_stats = np.zeros(N_GLOBAL_STATS * steps) sf = 1. # Scale factor size_x = 10./sf # Size of arena size_y = 10./sf # Size of arena r0 = 0.1 # Core radius r0_x_2 = r0 * 2 # Diameter of a particle # Number of particles n = int(params["Cell Density"]*(size_x*size_y)/(2*(3**0.5)*r0*r0)) eff_nop = float(n) # Effective number of particles noise_coef=params["Noise Intensity"] iner_coef = params["Angular Inertia"] r1 = params["Attraction-Repulsion Range"]*r0 rv = params["Alignment Range"]*r0 f0 = params["Attraction-Repulsion Strength"] fa = params["Alignment Strength"] v0 = np.array(params["Velocity"]) beta = np.array(params["Affinity"]) n_per_species = np.array([n/2, n+1]).astype(np.int32) grad_x = np.array([np.cos(d*np.pi) * i for d, i in zip(params["Gradient Angle"], params["Gradient Intensity"])]) grad_y = np.array([np.sin(d*np.pi) * i for d, i in zip(params["Gradient Angle"], params["Gradient Intensity"])]) pinned = np.array([0 if x == "none" else 1 for x in params["Pinned Cells"]]).astype(np.int32) # Particles positions and velocities pos_x = np.random.random(n)*size_x pos_y = np.random.random(n)*size_y dir_x = np.zeros(n) dir_y = np.zeros(n) # ---------------------C file name--------------------- mod = ext_tools.ext_module('c_code') # ---------------------Main code: fixed boundary--------------------- # Measure distance for fixed boundary condition with open("_c_code/fb_dist.cpp", "r") as infile: fb_dist = infile.read() # Fit coordinates into the arena for fixed boundary condition with open("_c_code/fb_fit.cpp", "r") as infile: fb_fit = infile.read() # Run simulation for a give number of steps under fixed boundary with open("_c_code/fb_main_code.cpp", "r") as infile: fb_main_code = infile.read() # ---------------------Main code: periodic boundary--------------------- # Measure distance for periodic boundary condition with open("_c_code/pb_dist.cpp", "r") as infile: pb_dist = infile.read() # Fit coordinates into the arena for periodic boundary condition with open("_c_code/pb_fit.cpp", "r") as infile: pb_fit = infile.read() # Run simulation for a give number of steps under periodic boundary with open("_c_code/pb_main_code.cpp", "r") as infile: pb_main_code = infile.read() # ---------------------Fixed boundary--------------------- # Create main function from C++ code and specify input fb_tick_func = ext_tools.ext_function( 'fb_tick', fb_main_code, ["n", "eff_nop", "size_x", "size_y", "r0_x_2", "r1", "rv", "iner_coef", "f0", "fa", "noise_coef", "v0", "pinned", "n_per_species", "beta", "grad_x", "grad_y", "pos_x", "pos_y", "dir_x", "dir_y", "global_stats", "steps"]) # Add helper functions to main function fb_tick_func.customize.add_support_code(fb_dist) fb_tick_func.customize.add_support_code(fb_fit) fb_tick_func.customize.add_header("<math.h>") # Add main function to module mod.add_function(fb_tick_func) # ---------------------Periodic boundary--------------------- # Create main function from C++ code and specify input pb_tick_func = ext_tools.ext_function( 'pb_tick', pb_main_code, ["n", "eff_nop", "size_x", "size_y", "r0_x_2", "r1", "rv", "iner_coef", "f0", "fa", "noise_coef", "v0", "pinned", "n_per_species", "beta", "grad_x", "grad_y", "pos_x", "pos_y", "dir_x", "dir_y", "global_stats", "steps"]) # Add helper functions to main function pb_tick_func.customize.add_support_code(pb_dist) pb_tick_func.customize.add_support_code(pb_fit) pb_tick_func.customize.add_header("<math.h>") # Add main function to module mod.add_function(pb_tick_func) # Compile mod.compile(compiler="gcc", verbose=1)
def build_radavg(): print('Compiling ravg_ext...') mod = ext_tools.ext_module('ravg_ext') ################################################# # Type definitions: ################################################# in_image = np.ones((10, 10), dtype=np.float64) xlen = np.int(in_image.shape[0]) ylen = np.int(in_image.shape[1]) x_c = np.float(1.0) y_c = np.float(1.0) mask = np.ones((xlen, ylen), dtype=np.float64) readoutNoiseFound = np.int(1) readoutNoise_mask = np.zeros((xlen, ylen), dtype=np.float64) readoutN = np.zeros((1, 4), dtype=np.float64) low_q = np.int(0) high_q = np.int(5) hist = np.zeros(5, dtype=np.float64) histlen = int(len(hist)) hist_count = np.zeros( (10, 10), dtype=np.float64 ) # -----" --------- for number of pixels in a circle at a certain q zinger_threshold = np.int(60000.0) # Too Hardcoded! res = np.zeros((1, 1), dtype=np.float64) qmatrix = np.zeros((100, 1000), dtype=np.float64) dezingering = 1 dezing_sensitivity = 4.0 code = """ #define WINDOW_LENGTH 30 double rel_x, rel_y, r, delta, deltaN, qmat_cnt, std; int i, x, y, half_window_size, window_start_idx, q_idx, point_idx; double window[WINDOW_LENGTH]; double median; double *window_ptr, *data; int win_len = WINDOW_LENGTH; int half_win_len; int hist_length; hist_length = PyObject_Length(py_hist); half_window_size = 15; data = (double *) qmatrix_array->data; /* Pointer to the numpy array version of qmatrix */ window_ptr = window; half_window_size = (WINDOW_LENGTH / 2.0); win_len = WINDOW_LENGTH; for( x = 0; x < xlen; x++) for( y = 0; y < ylen; y++) { rel_x = x-x_c; rel_y = y_c-y; r = int( std::sqrt((rel_y*rel_y) + (rel_x*rel_x)) ); //res(x,y) = r; if( r < high_q && r > low_q && mask(x,y) == 1) // && in_image(x,y) > 0) { q_idx = int(r); /* res2(x,y) = r; */ /* A test image, gives the included range image */ hist(r) = hist(r) + in_image(x,y); /* Integration of pixel values */ qmat_cnt = hist_count(0, q_idx); /* Number of pixels in a bin */ qmatrix(q_idx, int(qmat_cnt)) = in_image(x,y); /* Save pixel value for later analysis */ hist_count(0, q_idx) = hist_count(0, q_idx) + 1; /* Number of pixels in a bin */ delta = in_image(x,y) - hist_count(1, q_idx); /* Calculation of variance start */ hist_count(1, q_idx) = hist_count(1, q_idx) + (delta / hist_count(0, q_idx)); hist_count(2, q_idx) = hist_count(2, q_idx) + (delta * (in_image(x,y)-hist_count(1, q_idx))); /* ******************* Dezingering ******************** */ if ( (hist_count(0, int(r)) >= WINDOW_LENGTH) && (dezingering == 1)) { point_idx = (int) hist_count(0, q_idx); window_start_idx = point_idx - win_len; window_ptr = window; /* Reset pointers */ data = (double *) PyArray_GETPTR2(qmatrix_array, q_idx, point_idx); moveDataToWindow(window_ptr, data, win_len); quicksort(window, 0, WINDOW_LENGTH-1); std = getStd(window_ptr, win_len); window_ptr = window; /* Reset pointers */ median = window_ptr[half_window_size]; //printf("median: %f\\n", median); half_win_len = point_idx - half_window_size; if (qmatrix(q_idx, half_win_len) > (median + (dezing_sensitivity * std))){ qmatrix(q_idx, half_win_len) = median; } } // end dezinger if case } if ( readoutNoiseFound == 1 && r < high_q-1 && r > low_q && readoutNoise_mask(x,y) == 0) { readoutN(0,0) = readoutN(0,0) + 1; readoutN(0,1) = readoutN(0,1) + in_image(x,y); deltaN = in_image(x,y) - readoutN(0,2); readoutN(0,2) = readoutN(0,2) + (deltaN / readoutN(0,0)); readoutN(0,3) = readoutN(0,3) + (deltaN * (in_image(x,y)-readoutN(0,2))); } } /* ********************************************* */ /* Remove zingers at the first (window/2) points */ /* ********************************************* */ if (dezingering == 1){ half_window_size = (int) (WINDOW_LENGTH / 2.0); win_len = WINDOW_LENGTH; for(q_idx = 0; q_idx < hist_length; q_idx++){ if (hist_count(0, q_idx) > (win_len + half_window_size)){ for(i = (win_len + half_window_size); i > win_len; i--){ point_idx = (int) i; data = (double *) PyArray_GETPTR2(qmatrix_array, q_idx, point_idx); window_start_idx = point_idx - win_len; window_ptr = window; moveDataToWindow(window_ptr, data, win_len); quicksort(window, 0, WINDOW_LENGTH-1); std = getStd(window_ptr, win_len); window_ptr = window; /* Reset pointers */ median = window_ptr[half_window_size]; half_win_len = point_idx - win_len; if (qmatrix(q_idx, half_win_len) > (median + (dezing_sensitivity * std))){ qmatrix(q_idx, half_win_len) = median; } } } } } printf("\\n\\n********* Radial Averaging and dezingering ********\\n"); printf("Done!"); """ code2 = """ double getStd(double *window_ptr, int win_len) { int half_win_len; double mean, variance, M2, n, delta; M2 = 0; n = 0.0; mean = 0; variance = 1; delta = 0; half_win_len = win_len / 2; while(half_win_len--) { ++n; delta = ((double) *window_ptr) - mean; mean = mean + (delta/n); M2 = M2 + (delta * (((double) *window_ptr) - mean)); ++window_ptr; } if(n > 1) variance = M2/(n - 1); /* To avoid devide by zero */ return sqrt(variance); } void moveDataToWindow(double *window_ptr, double *data, int win_len) { data++; /* Pointers needs to be moved back/forward since */ window_ptr--; /* *data++ doesn't work, but *++data does.. strange! */ while(win_len--) { *++window_ptr = *--data; /* Move values from data array to the window array */ } } void swap(double *x, double *y) { long temp; temp = *x; *x = *y; *y = temp; } long choose_pivot(long i, long j) { return((i+j) /2); } void quicksort(double list[], long m, long n) { long key,i,j,k; if( m < n) { k = choose_pivot(m, n); swap(&list[m], &list[k]); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(&list[i], &list[j]); } // swap two elements swap(&list[m], &list[j]); // recursively sort the lesser list quicksort(list, m, j-1); quicksort(list, j+1, n); } } """ # # stdwin = np.sort(window) # std = np.std(stdwin[:-half_window]) # median = np.median(window) # # plus_threshold = median + (std * sensitivity) # minus_threshold = median - (std * sensitivity) # # if intensity_array[i] > plus_threshold or intensity_array[i] < minus_threshold: # intensity_array[i] = median ravg = ext_tools.ext_function( 'ravg', code, [ 'readoutNoiseFound', 'readoutN', 'readoutNoise_mask', 'xlen', 'ylen', 'x_c', 'y_c', 'hist', 'low_q', 'high_q', 'in_image', 'hist_count', 'mask', 'qmatrix', 'dezingering', 'dezing_sensitivity' ], type_converters=converters.blitz) ravg.customize.add_support_code(code2) mod.add_function(ravg) #SYSTEM TEMP DIR MIGHT NOT HAVE WRITE PERMISSION OR HAS SPACES IN IT => FAIL! #EXTREMELY ANNOYING THAT THE TEMP DIR CAN'T BE SET FROM mod.compile()! .. This is a work around: kw, file = mod.build_kw_and_file(workdir, {}) success = build_tools.build_extension(file, temp_dir=temp_dir, compiler_name='gcc', verbose=0, **kw) if success: print('\n****** ravg_ext module compiled succesfully! *********\n')