From 9887e2d3749b0e2df53570490a04709c6af2dfbb Mon Sep 17 00:00:00 2001 From: Eduardo Gonzalez Solares <eglez@ast.cam.ac.uk> Date: Thu, 9 May 2019 16:03:21 +0100 Subject: [PATCH] Cythonize get_coords function --- .gitignore | 1 + setup.py | 3 ++ stpt_pipeline/settings.py | 2 +- stpt_pipeline/stpt_displacement.py | 46 ------------------------------ stpt_pipeline/utils.pyx | 37 ++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 47 deletions(-) create mode 100644 stpt_pipeline/utils.pyx diff --git a/.gitignore b/.gitignore index 4542d77..d4f6506 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ __pycache__/ # C extensions *.so +utils.c # Distribution / packaging .Python diff --git a/setup.py b/setup.py index 8d9e046..4f2224a 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +from Cython.Build import cythonize +from setuptools.extension import Extension from setuptools import find_packages, setup with open('README.rst') as readme_file: @@ -32,6 +34,7 @@ setup( keywords='stpt_pipeline', name='stpt_pipeline', packages=find_packages(include=['stpt_pipeline']), + ext_modules=cythonize(Extension('stpt_pipeline.utils', ['stpt_pipeline/utils.pyx'])), setup_requires=setup_requirements, test_suite='tests', tests_require=test_requirements, diff --git a/stpt_pipeline/settings.py b/stpt_pipeline/settings.py index 0df7039..fe613b0 100644 --- a/stpt_pipeline/settings.py +++ b/stpt_pipeline/settings.py @@ -25,4 +25,4 @@ class Settings: do_flat = True do_defringe = False channel_to_use = 4 - cof_dist = np.array([3.93716645e-05, -7.37696218e-02, 2.52457306e01]) / 2.0 + cof_dist = (3.93716645e-05 / 2, -7.37696218e-02 / 2, 2.52457306e01 / 2) diff --git a/stpt_pipeline/stpt_displacement.py b/stpt_pipeline/stpt_displacement.py index 1f7b53b..91d0b1d 100644 --- a/stpt_pipeline/stpt_displacement.py +++ b/stpt_pipeline/stpt_displacement.py @@ -60,52 +60,6 @@ def defringe(img): return fr_img -# -# Â get_coords is the function that feeds geometric_transform -# in order to correct for the optical distortion of the detector. -# -def get_coords(coords, cof, center_x, max_x, direct=True): - """[summary] - - Parameters - ---------- - coords : [type] - [description] - cof : [type] - [description] - center_x : [type] - [description] - max_x : [type] - [description] - direct : bool, optional - [description], by default True - - Returns - ------- - [type] - [description] - """ - max_desp = cof[0] * coords[1] ** 2 + cof[1] * coords[1] + cof[2] - dy_cof = max_desp / (max_x - center_x) ** 2 - if direct: - if coords[0] > center_x: - sign = 1 - else: - sign = -1 - xi = np.abs(coords[0] - center_x) - return (center_x + sign * (xi + dy_cof * xi ** 2), coords[1]) - else: - if coords[0] > center_x + cof[2]: - sign = 1 - else: - sign = -1 - xi = np.abs(coords[0] - center_x - cof[2]) - return ( - center_x + sign * (np.sqrt(1 + 4 * dy_cof * xi) - 1) / (2 * dy_cof), - coords[1], - ) - - def magic_function(x, flat=1): # TODO: Call this some other name """[summary] diff --git a/stpt_pipeline/utils.pyx b/stpt_pipeline/utils.pyx new file mode 100644 index 0000000..d54b9cc --- /dev/null +++ b/stpt_pipeline/utils.pyx @@ -0,0 +1,37 @@ +from libc.math cimport abs + +def get_coords(tuple coords, tuple cof, float center_x, float max_x): + """[summary] + + Parameters + ---------- + coords : [type] + [description] + cof : [type] + [description] + center_x : [type] + [description] + max_x : [type] + [description] + + Returns + ------- + [type] + [description] + """ + cdef float max_desp, dy_cof, xi, r0, r1 + cdef int c0, c1, sign + cdef tuple res + c0 = coords[0] + c1 = coords[1] + max_desp = cof[0] * c1 ** 2 + cof[1] * c1 + cof[2] + dy_cof = max_desp / (max_x - center_x) ** 2 + if c0 > center_x: + sign = 1 + else: + sign = -1 + xi = abs(c0 - center_x) + r0 = center_x + sign * (xi + dy_cof * xi ** 2) + res = (r0, c1) + return res + \ No newline at end of file -- GitLab