Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd595cf

Browse files
committed
RF: refactor import_array as documented
Instead of calling `import_array()` in every C function, use the method of sharing the numpy API pointer table documented here: http://docs.scipy.org/doc/numpy/reference/c-api.array.html#importing-the-api Also, deal with different return types from `import_array()` in Python 3 and Python 2 - see http://mail.scipy.org/pipermail/numpy-discussion/2010-December/054345.html
1 parent d5b81f6 commit cd595cf

File tree

14 files changed

+53
-52
lines changed

14 files changed

+53
-52
lines changed

lib/fff_python_wrapper/fffpy.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
because PyArray_API is defined static, in order not to share that symbol
1010
within the dso. (import_array() asks the pointer value to the python process)
1111
*/
12-
void fffpy_import_array(void) {
13-
import_array();
14-
return;
12+
/*
13+
* deal with differences in macro return result between Python 2 and 3
14+
* http://mail.scipy.org/pipermail/numpy-discussion/2010-December/054350.html
15+
*/
16+
IMP_OUT fffpy_import_array(void) {
17+
import_array();
1518
}
1619

17-
1820
/* Static functions */
1921
static npy_intp _PyArray_main_axis(const PyArrayObject* x, int* ok);
2022
static fff_vector* _fff_vector_new_from_buffer(const char* data, npy_intp dim, npy_intp stride, int type, int itemsize);

lib/fff_python_wrapper/fffpy.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,17 @@
3131
static, in order not to share that symbol within the
3232
dso. (import_array() asks the pointer value to the python process)
3333
*/
34-
extern void fffpy_import_array(void);
34+
/*
35+
* deal with differences in macro return result between Python 2 and 3
36+
* http://mail.scipy.org/pipermail/numpy-discussion/2010-December/054350.html
37+
*/
38+
#if PY_MAJOR_VERSION >= 3
39+
typedef int IMP_OUT;
40+
#else
41+
typedef void IMP_OUT;
42+
#endif
3543

44+
extern IMP_OUT fffpy_import_array(void);
3645

3746
/*!
3847
\brief Convert \c PyArrayObject to \c fff_vector
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define PY_ARRAY_UNIQUE_SYMBOL _registration_ARRAY_API

nipy/algorithms/registration/_registration.pyx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ transformations.
88

99
__version__ = '0.3'
1010

11+
# Set symbol for array_import; must come before cimport numpy
12+
cdef extern from "_registration.h":
13+
int PY_ARRAY_UNIQUE_SYMBOL
14+
1115

1216
# Includes
1317
from numpy cimport (import_array, ndarray, flatiter, broadcast,
1418
PyArray_MultiIterNew, PyArray_MultiIter_DATA,
1519
PyArray_MultiIter_NEXT)
1620

1721

18-
# Externals
1922
cdef extern from "joint_histogram.h":
20-
void joint_histogram_import_array()
2123
int joint_histogram(ndarray H, unsigned int clampI, unsigned int clampJ,
2224
flatiter iterI, ndarray imJ_padded,
2325
ndarray Tvox, int interp)
2426
int L1_moments(double* n, double* median, double* dev, ndarray H)
2527

2628
cdef extern from "cubic_spline.h":
27-
void cubic_spline_import_array()
2829
void cubic_spline_transform(ndarray res, ndarray src)
2930
double cubic_spline_sample1d(double x, ndarray coef,
3031
int mode)
@@ -39,14 +40,10 @@ cdef extern from "cubic_spline.h":
3940
int mode_x, int mode_y, int mode_z)
4041

4142
cdef extern from "polyaffine.h":
42-
void polyaffine_import_array()
4343
void apply_polyaffine(ndarray XYZ, ndarray Centers, ndarray Affines, ndarray Sigma)
4444

4545

4646
# Initialize numpy
47-
joint_histogram_import_array()
48-
cubic_spline_import_array()
49-
polyaffine_import_array()
5047
import_array()
5148
import numpy as np
5249

nipy/algorithms/registration/cubic_spline.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ static inline void _apply_affine_transform(double* Tx, double* Ty, double* Tz,
5454
size_t x, size_t y, size_t z);
5555

5656

57-
/* Numpy import */
58-
void cubic_spline_import_array(void) {
59-
import_array();
60-
return;
61-
}
62-
63-
6457
/* Returns the value of the cubic B-spline function at x */
6558
double cubic_spline_basis (double x)
6659
{

nipy/algorithms/registration/cubic_spline.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ extern "C" {
2525
#endif
2626

2727
#include <Python.h>
28-
#include <numpy/arrayobject.h>
2928

30-
/* Numpy import */
31-
extern void cubic_spline_import_array(void);
29+
/*
30+
* Use extension numpy symbol table
31+
*/
32+
#define NO_IMPORT_ARRAY
33+
#include "_registration.h"
34+
35+
#include <numpy/arrayobject.h>
3236

3337
/*!
3438
\brief Cubic spline basis function

nipy/algorithms/registration/joint_histogram.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ static inline void _rand_interpolation(unsigned int i,
3434
int nn,
3535
void* params);
3636

37-
38-
/* Numpy import */
39-
void joint_histogram_import_array(void) {
40-
import_array();
41-
return;
42-
}
43-
44-
4537
/*
4638
4739
JOINT HISTOGRAM COMPUTATION.

nipy/algorithms/registration/joint_histogram.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ extern "C" {
1717
#endif
1818

1919
#include <Python.h>
20-
#include <numpy/arrayobject.h>
21-
2220

23-
/* Numpy import */
24-
extern void joint_histogram_import_array(void);
21+
/*
22+
* Use extension numpy symbol table
23+
*/
24+
#define NO_IMPORT_ARRAY
25+
#include "_registration.h"
2526

27+
#include <numpy/arrayobject.h>
2628

2729
/*
2830
Update a pre-allocated joint histogram. Important notice: in all

nipy/algorithms/registration/polyaffine.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
#define TINY 1e-200
77

88

9-
/* Numpy import */
10-
void polyaffine_import_array(void) {
11-
import_array();
12-
return;
13-
}
14-
15-
169
static double _gaussian(double* xyz, double* center, double* sigma)
1710
{
1811
double aux, d2 = 0.0;

nipy/algorithms/registration/polyaffine.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ extern "C" {
66
#endif
77

88
#include <Python.h>
9-
#include <numpy/arrayobject.h>
109

11-
extern void polyaffine_import_array(void);
10+
/*
11+
* Use extension numpy symbol table
12+
*/
13+
#define NO_IMPORT_ARRAY
14+
#include "_registration.h"
15+
16+
#include <numpy/arrayobject.h>
1217

1318
extern void apply_polyaffine(PyArrayObject* XYZ,
1419
const PyArrayObject* Centers,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define PY_ARRAY_UNIQUE_SYMBOL _segmentation_ARRAY_API

nipy/algorithms/segmentation/_segmentation.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ Author: Alexis Roche, 2010.
88

99
__version__ = '0.2'
1010

11+
# Set symbol for array_import; must come before cimport numpy
12+
cdef extern from "_segmentation.h":
13+
int PY_ARRAY_UNIQUE_SYMBOL
14+
1115
# Includes
1216
from numpy cimport import_array, ndarray
1317

1418
# Externals
1519
cdef extern from "mrf.h":
16-
void mrf_import_array()
1720
void ve_step(ndarray ppm,
1821
ndarray ref,
1922
ndarray XYZ,
@@ -30,7 +33,6 @@ cdef extern from "mrf.h":
3033

3134

3235
# Initialize numpy
33-
mrf_import_array()
3436
import_array()
3537
import numpy as np
3638

nipy/algorithms/segmentation/mrf.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
#endif
99

1010

11-
/* Numpy import */
12-
void mrf_import_array(void) {
13-
import_array();
14-
return;
15-
}
16-
1711
/* Encode neighborhood systems using static arrays */
1812
int ngb6 [] = {1,0,0,
1913
-1,0,0,

nipy/algorithms/segmentation/mrf.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ extern "C" {
66
#endif
77

88
#include <Python.h>
9+
10+
/*
11+
* Use extension numpy symbol table
12+
*/
13+
#define NO_IMPORT_ARRAY
14+
#include "_segmentation.h"
15+
916
#include <numpy/arrayobject.h>
1017

11-
extern void mrf_import_array(void);
1218

1319
extern void ve_step(PyArrayObject* ppm,
1420
const PyArrayObject* ref,

0 commit comments

Comments
 (0)