From 90d038a639fe0623e69badb2a8eb2b00b44ed066 Mon Sep 17 00:00:00 2001 From: Jan Caron <caron@fz-juelich.de> Date: Mon, 6 May 2013 17:00:34 +0200 Subject: [PATCH] Added initcython.py (import this befor importing .pyx files to use Cython-Code) --- scripts/get_jacobi.py | 2 ++ src/pyramex/.sconsign.dblite | Bin 2379 -> 0 bytes src/pyramex/SConstruct | 12 +++++++++--- src/pyramex/initcython.py | 32 +++++++++++++++++++++++++++++++ src/pyramex/setup.py | 28 ++++----------------------- src/pyramid/phasemap.py | 36 ++++++++++++++++++++++------------- 6 files changed, 70 insertions(+), 40 deletions(-) delete mode 100644 src/pyramex/.sconsign.dblite create mode 100644 src/pyramex/initcython.py diff --git a/scripts/get_jacobi.py b/scripts/get_jacobi.py index ad3091b..42de910 100644 --- a/scripts/get_jacobi.py +++ b/scripts/get_jacobi.py @@ -48,6 +48,8 @@ def phase_from_mag(): np.savetxt('../output/jacobi.npy', jacobi) print 'Time for Real Space Approach with Jacobi-Matrix (Slab): ' + str(toc - tic) + + return jacobi diff --git a/src/pyramex/.sconsign.dblite b/src/pyramex/.sconsign.dblite deleted file mode 100644 index 0e8ccb44f58bbb31c7780f36f464f52b6d17a122..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2379 zcmdUxe{<V37{^y=*VSFSb?v%#uo0j@U^KBUOSa4S(S@`dOFOQq5B{hxXUTG8u1)g9 z4y@eyz3?XB^�w>Wtm>Uv`)C>Eu}YKF|02k)6R@)mvwKXM@XB^n0d#mat(wNR!il z=5E}`V9wK6ey7&_wF&Pr)*oDUhP<oE3+wlibU0#Zf2hIxRn~6EiCTV*yFG0(X!g?K zr3Uk?)1jmIJdJyuj@GBYah7VZaGajN%?uW+-a;?#MSZv>rb`|7LtZ;*YX|W;SK}A* zz6MJoe@M$<x#}%<e&@p^?)N%z2)AF(c}r=0!ILz&7{E%)K-jW%0~t<B^X6F+cVX45 zAO}%|kU^On1VlG2?$~-jImM<CI6(w=y!j;ll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ex/SConstruct +++ b/src/pyramex/SConstruct @@ -6,11 +6,17 @@ PYTHON_LIBPATH = distutils.sysconfig.get_python_lib() PYTHON_INCPATH = distutils.sysconfig.get_python_inc() PYTHON_LIBRARY = "python" + sys.version[:3] -cythonBuilder = Builder(action ='cython $SOURCE', +CythonBuilder = Builder(action ='cython $SOURCE', suffix = '.c', src_suffix = '.pyx') +def PydBuilder(env,source): + cCode = env.Py2C(source) + env.SharedLibrary(target='karl.pyd', source=cCode, LIBPREFIX='') + env = Environment(ENV = os.environ, - BUILDERS = {'Py2C':cythonBuilder}) + BUILDERS = {'Py2C':CythonBuilder}) + +env.AddMethod(CythonBuilder,'Cython') -env.Py2C('c1.c', 'c1.pyx') \ No newline at end of file +env.Cython('c1') \ No newline at end of file diff --git a/src/pyramex/initcython.py b/src/pyramex/initcython.py new file mode 100644 index 0000000..010a693 --- /dev/null +++ b/src/pyramex/initcython.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +"""Script to initialize Cythons pyximport to ensure compatibility with MinGW compiler and NumPy.""" + + +import os +import numpy +import pyximport + + +if os.name == 'nt': + if os.environ.has_key('CPATH'): + os.environ['CPATH'] = os.environ['CPATH'] + numpy.get_include() + else: + os.environ['CPATH'] = numpy.get_include() +# # XXX: assuming that MinGW is installed in C:\MinGW (default) +# # for PythonXY the default is C:\MinGW-xy +# if os.environ.has_key('PATH'): +# os.environ['PATH'] = os.environ['PATH'] + ';C:\MinGW\bin' +# else: +# os.environ['PATH'] = 'C:\MinGW\bin' + + mingw_setup_args = { 'options': { 'build_ext': { 'compiler': 'mingw32' } } } + pyximport.install(setup_args=mingw_setup_args) + + +elif os.name == 'posix': + if os.environ.has_key('CFLAGS'): + os.environ['CFLAGS'] = os.environ['CFLAGS'] + ' -I' + numpy.get_include() + else: + os.environ['CFLAGS'] = ' -I' + numpy.get_include() + + pyximport.install() \ No newline at end of file diff --git a/src/pyramex/setup.py b/src/pyramex/setup.py index b802b79..57ac98d 100644 --- a/src/pyramex/setup.py +++ b/src/pyramex/setup.py @@ -5,32 +5,12 @@ Created on Fri May 03 10:27:04 2013 @author: Jan """ -#from distutils.core import setup, Extension -# -#module1 = Extension('c1', -# sources = ['c1.pyx']) -# -#setup (name = 'pyramex', -# version = '1.0', -# description = 'This is a demo package', -# ext_modules = [module1]) - - +#call with: python setup.py build_ext --inplace --compiler=mingw32 from distutils.core import setup -from distutils.extension import Extension -from Cython.Distutils import build_ext - -def get_extensions(): - return [ - Extension('hello', ['hello.pyx']), - Extension('c1', ['c1.pyx']), - Extension('c2', ['c2.pyx']), - Extension('c3', ['c3.pyx']) - ] +from Cython.Build import cythonize setup( - name = 'pyramex', - cmdclass = {'build_ext': build_ext}, - ext_modules = get_extensions() + name = "My hello app", + ext_modules = cythonize('hello.pyx'), # accepts a glob pattern ) \ No newline at end of file diff --git a/src/pyramid/phasemap.py b/src/pyramid/phasemap.py index 6096799..e828560 100644 --- a/src/pyramid/phasemap.py +++ b/src/pyramid/phasemap.py @@ -112,34 +112,44 @@ def real_space(mag_data, method, b_0=1, jacobi=None): phi_sin = phi_pixel(method, yy_big, xx_big, res, b_0) def phi_mag(i, j): # TODO: rename - return (np.cos(beta[j,i])*phi_cos[y_dim-1-j:(2*y_dim-1)-j, - x_dim-1-i:(2*x_dim-1)-i] - -np.sin(beta[j,i])*phi_sin[y_dim-1-j:(2*y_dim-1)-j, - x_dim-1-i:(2*x_dim-1)-i]) + return (np.cos(beta[j,i])*phi_cos[y_dim-1-j:(2*y_dim-1)-j, x_dim-1-i:(2*x_dim-1)-i] + -np.sin(beta[j,i])*phi_sin[y_dim-1-j:(2*y_dim-1)-j, x_dim-1-i:(2*x_dim-1)-i]) def phi_mag_deriv(i, j): # TODO: rename - return -(np.sin(beta[j,i])*phi_cos[y_dim-1-j:(2*y_dim-1)-j, - x_dim-1-i:(2*x_dim-1)-i] - +np.cos(beta[j,i])*phi_sin[y_dim-1-j:(2*y_dim-1)-j, - x_dim-1-i:(2*x_dim-1)-i]) + return -(np.sin(beta[j,i])*phi_cos[y_dim-1-j:(2*y_dim-1)-j, x_dim-1-i:(2*x_dim-1)-i] + +np.cos(beta[j,i])*phi_sin[y_dim-1-j:(2*y_dim-1)-j, x_dim-1-i:(2*x_dim-1)-i]) + + def phi_mag_fd(i, j, h): # TODO: rename + return ((np.cos(beta[j,i]+h) - np.cos(beta[j,i])) / h + * phi_cos[y_dim-1-j:(2*y_dim-1)-j, x_dim-1-i:(2*x_dim-1)-i] + -(np.sin(beta[j,i]+h) - np.sin(beta[j,i])) / h + * phi_sin[y_dim-1-j:(2*y_dim-1)-j, x_dim-1-i:(2*x_dim-1)-i]) '''CALCULATE THE PHASE''' phase = np.zeros((y_dim, x_dim)) # TODO: only iterate over pixels that have a magn. > threshold (first >0) + jacobi_fd = jacobi.copy() + h = 0.0001 + for j in range(y_dim): for i in range(x_dim): - if (mag[j, i] != 0 ):#or jacobi is not None): # TODO: same result with or without? + #if (mag[j,i] != 0 ):#or jacobi is not None): # TODO: same result with or without? phi_mag_cache = phi_mag(i, j) phase += mag[j,i] * phi_mag_cache if jacobi is not None: jacobi[:,i+x_dim*j] = phi_mag_cache.reshape(-1) - jacobi[:,x_dim*y_dim+i+x_dim*j] = (mag[j,i]*phi_mag_deriv(i, j)).reshape(-1) + jacobi[:,x_dim*y_dim+i+x_dim*j] = (mag[j,i]*phi_mag_deriv(i,j)).reshape(-1) + + jacobi_fd[:,i+x_dim*j] = phi_mag_cache.reshape(-1) + jacobi_fd[:,x_dim*y_dim+i+x_dim*j] = (mag[j,i]*phi_mag_fd(i,j,h)).reshape(-1) + + - return phase - + jacobi_diff = jacobi_fd - jacobi + assert (np.abs(jacobi_diff) < 1.0E-8).all(), 'jacobi matrix is not the same' - + return phase def phase_elec(mag_data, b_0=1, v_0=0, v_acc=30000): -- GitLab