Skip to content

Commit fbdd2f4

Browse files
committed
update to numpy 1.7 API
1 parent 6e87c48 commit fbdd2f4

File tree

6 files changed

+84
-64
lines changed

6 files changed

+84
-64
lines changed

tractor/ceres-tractor.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// http://docs.scipy.org/doc/numpy/reference/c-api.array.html#import_array
1010
#define PY_ARRAY_UNIQUE_SYMBOL tractorceres_ARRAY_API
1111
#define NO_IMPORT_ARRAY
12+
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
1213
#include <numpy/arrayobject.h>
1314

1415
#include <stdio.h>
@@ -183,7 +184,7 @@ template class ForcedPhotCostFunction<double>;
183184

184185
ImageCostFunction::ImageCostFunction(PyObject* tractor,
185186
int imagei, int nparams,
186-
PyObject* np_params) :
187+
PyArrayObject* np_params) :
187188
_tractor(tractor), _imagei(imagei), _image(NULL),
188189
_npix(0), _nparams(nparams), _W(0), _H(0), _np_params(np_params) {
189190

@@ -273,10 +274,10 @@ bool ImageCostFunction::_Evaluate(double const* const* parameters,
273274
}
274275

275276
// Get residuals (chi image)
276-
PyObject* np_chi;
277+
PyArrayObject* np_chi;
277278
//printf("Calling getChiImage(%i)\n", _imagei);
278-
np_chi = PyObject_CallMethod(_tractor, (char*)"getChiImage",
279-
(char*)"i", _imagei);
279+
np_chi = (PyArrayObject*)PyObject_CallMethod(
280+
_tractor, (char*)"getChiImage", (char*)"i", _imagei);
280281
if (!np_chi) {
281282
printf("getChiImage() failed\n");
282283
return false;
@@ -341,7 +342,7 @@ bool ImageCostFunction::_Evaluate(double const* const* parameters,
341342

342343
int x0 = PyInt_AsLong(PyTuple_GetItem(deriv, 1));
343344
int y0 = PyInt_AsLong(PyTuple_GetItem(deriv, 2));
344-
PyObject* np_deriv = PyTuple_GetItem(deriv, 3);
345+
PyArrayObject* np_deriv = (PyArrayObject*)PyTuple_GetItem(deriv, 3);
345346
if (!PyArray_Check(np_deriv)) {
346347
printf("Expected third element of allderivs element %i to be an array\n",
347348
ideriv);

tractor/ceres-tractor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class ImageCostFunction : public CostFunction {
5858
public:
5959
virtual ~ImageCostFunction();
6060

61-
ImageCostFunction(PyObject* tractor, int imagei, int nparams, PyObject* np_params);
61+
ImageCostFunction(PyObject* tractor, int imagei, int nparams,
62+
PyArrayObject* np_params);
6263

6364
virtual bool Evaluate(double const* const* parameters,
6465
double* residuals,
@@ -80,5 +81,5 @@ class ImageCostFunction : public CostFunction {
8081
int _nparams;
8182
int _W;
8283
int _H;
83-
PyObject* _np_params;
84+
PyArrayObject* _np_params;
8485
};

tractor/ceres.i

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
%{
66
#define PY_ARRAY_UNIQUE_SYMBOL tractorceres_ARRAY_API
7+
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
78
#include <numpy/arrayobject.h>
89

910
#include <Python.h>
@@ -52,13 +53,13 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
5253
sources: [ (index, x0, y0, np_img), ... ]
5354
*/
5455
npy_intp Nblocks;
55-
assert(PyList_Check(blocks));
56+
assert(PyList_Check(blocks));
5657
Nblocks = PyList_Size(blocks);
5758
//printf("N blocks: %i\n", (int)Nblocks);
5859
assert(PyArray_Check(np_fluxes));
5960
assert(PyArray_TYPE(np_fluxes) == NPY_DOUBLE);
6061
int Nfluxes = (int)PyArray_Size(np_fluxes);
61-
double* realfluxes = (double*)PyArray_DATA(np_fluxes);
62+
double* realfluxes = (double*)PyArray_DATA((PyArrayObject*)np_fluxes);
6263
T* mod0data;
6364
Problem problem;
6465
int totaldatapix = 0;
@@ -77,7 +78,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
7778
PyObject* srclist;
7879
PyObject* obj;
7980
int x0, y0;
80-
PyObject *img, *mod0, *ierr;
81+
PyArrayObject *img, *mod0, *ierr;
8182
int w, h;
8283
int Nsources;
8384

@@ -92,12 +93,12 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
9293
assert(PyTuple_Size(obj) == 5);
9394
x0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 0));
9495
y0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 1));
95-
img = PyTuple_GET_ITEM(obj, 2);
96+
img = (PyArrayObject*)PyTuple_GET_ITEM(obj, 2);
9697
assert(PyArray_Check(img));
9798
h = PyArray_DIM(img, 0);
9899
w = PyArray_DIM(img, 1);
99-
mod0 = PyTuple_GET_ITEM(obj, 3);
100-
if (mod0 == Py_None) {
100+
mod0 = (PyArrayObject*)PyTuple_GET_ITEM(obj, 3);
101+
if ((PyObject*)mod0 == Py_None) {
101102
mod0data = NULL;
102103
} else {
103104
assert(PyArray_Check(mod0));
@@ -106,7 +107,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
106107
assert(PyArray_TYPE(mod0) == npy_type);
107108
mod0data = (T*)PyArray_DATA(mod0);
108109
}
109-
ierr = PyTuple_GET_ITEM(obj, 4);
110+
ierr = (PyArrayObject*)PyTuple_GET_ITEM(obj, 4);
110111
assert(PyArray_Check(ierr));
111112
assert(PyArray_DIM(ierr, 0) == h);
112113
assert(PyArray_DIM(ierr, 1) == w);
@@ -124,7 +125,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
124125
int nderivpix = 0;
125126
for (int j=0; j<Nsources; j++) {
126127
int index, uh, uw;
127-
PyObject* uimg;
128+
PyArrayObject* uimg;
128129
obj = PyList_GET_ITEM(srclist, j);
129130
assert(PyTuple_Check(obj));
130131
assert(PyTuple_Size(obj) == 4);
@@ -136,7 +137,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
136137
assert(index < Nfluxes);
137138
x0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 1));
138139
y0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 2));
139-
uimg = PyTuple_GET_ITEM(obj, 3);
140+
uimg = (PyArrayObject*)PyTuple_GET_ITEM(obj, 3);
140141
assert(PyArray_Check(uimg));
141142
uh = PyArray_DIM(uimg, 0);
142143
uw = PyArray_DIM(uimg, 1);
@@ -291,8 +292,8 @@ static PyObject* ceres_forced_phot(PyObject* blocks,
291292
obj = PyTuple_GET_ITEM(block, 0);
292293
assert(PyTuple_Check(obj));
293294
assert(PyTuple_Size(obj) == 5);
294-
PyObject* img;
295-
img = PyTuple_GET_ITEM(obj, 2);
295+
PyArrayObject* img;
296+
img = (PyArrayObject*)PyTuple_GET_ITEM(obj, 2);
296297
assert(PyArray_Check(img));
297298

298299
if (PyArray_TYPE(img) == NPY_FLOAT) {
@@ -355,7 +356,8 @@ protected:
355356

356357

357358
static PyObject* ceres_opt(PyObject* tractor, int nims,
358-
PyObject* np_params, PyObject* np_variance,
359+
PyArrayObject* np_params,
360+
PyArrayObject* np_variance,
359361
int scale_columns,
360362
int numeric,
361363
float numeric_stepsize,
@@ -396,10 +398,10 @@ static PyObject* ceres_opt(PyObject* tractor, int nims,
396398
printf("ceres_opt: wrong type for params variable\n");
397399
return NULL;
398400
}
399-
nparams = (int)PyArray_Size(np_params);
401+
nparams = (int)PyArray_SIZE(np_params);
400402
params = (double*)PyArray_DATA(np_params);
401403

402-
get_variance = (np_variance != Py_None);
404+
get_variance = ((PyObject*)np_variance != Py_None);
403405

404406
//printf("ceres_opt, nims %i, nparams %i, get_variance %i\n",
405407
// nims, nparams, get_variance);
@@ -580,7 +582,7 @@ static PyObject* ceres_opt(PyObject* tractor, int nims,
580582
printf("ceres_opt: wrong type for variance variable\n");
581583
return NULL;
582584
}
583-
if (PyArray_Size(np_variance) != PyArray_Size(np_params)) {
585+
if (PyArray_SIZE(np_variance) != PyArray_SIZE(np_params)) {
584586
printf("ceres_opt: wrong size for variance variable\n");
585587
return NULL;
586588
}

tractor/emfit.i

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
%include <typemaps.i>
44

55
%{
6+
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
67
#include <numpy/arrayobject.h>
78
#include <math.h>
89
#include <assert.h>
@@ -28,10 +29,10 @@
2829

2930
// _reg: Inverse-Wishart prior on variance (with hard-coded
3031
// variance prior I), with strength alpha.
31-
static int em_fit_1d_samples_reg(PyObject* np_x,
32-
PyObject* np_amp,
33-
PyObject* np_mean,
34-
PyObject* np_var,
32+
static int em_fit_1d_samples_reg(PyObject* po_x,
33+
PyObject* po_amp,
34+
PyObject* po_mean,
35+
PyObject* po_var,
3536
double alpha,
3637
int steps) {
3738
npy_intp i, N, K, k;
@@ -42,9 +43,11 @@ static int em_fit_1d_samples_reg(PyObject* np_x,
4243
double tpd;
4344
int result;
4445

45-
PyArray_Descr* dtype = PyArray_DescrFromType(PyArray_DOUBLE);
46-
int req = NPY_C_CONTIGUOUS | NPY_ALIGNED;
47-
int reqout = req | NPY_WRITEABLE | NPY_UPDATEIFCOPY;
46+
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_DOUBLE);
47+
int req = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED;
48+
int reqout = req | NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY;
49+
50+
PyArrayObject *np_x, *np_amp, *np_mean, *np_var;
4851

4952
double* amp;
5053
double* mean;
@@ -54,22 +57,22 @@ static int em_fit_1d_samples_reg(PyObject* np_x,
5457
tpd = pow(2.*M_PI, D);
5558

5659
Py_INCREF(dtype);
57-
np_x = PyArray_FromAny(np_x, dtype, 1, 1, req, NULL);
60+
np_x = (PyArrayObject*)PyArray_FromAny(po_x, dtype, 1, 1, req, NULL);
5861
if (!np_x) {
5962
ERR("x wasn't the type expected");
6063
Py_DECREF(dtype);
6164
return -1;
6265
}
6366
Py_INCREF(dtype);
64-
np_amp = PyArray_FromAny(np_amp, dtype, 1, 1, reqout, NULL);
67+
np_amp = (PyArrayObject*)PyArray_FromAny(po_amp, dtype, 1, 1, reqout, NULL);
6568
if (!np_amp) {
6669
ERR("amp wasn't the type expected");
6770
Py_DECREF(np_x);
6871
Py_DECREF(dtype);
6972
return -1;
7073
}
7174
Py_INCREF(dtype);
72-
np_mean = PyArray_FromAny(np_mean, dtype, 1, 1, reqout, NULL);
75+
np_mean = (PyArrayObject*)PyArray_FromAny(po_mean, dtype, 1, 1, reqout, NULL);
7376
if (!np_mean) {
7477
ERR("mean wasn't the type expected");
7578
Py_DECREF(np_x);
@@ -78,7 +81,7 @@ static int em_fit_1d_samples_reg(PyObject* np_x,
7881
return -1;
7982
}
8083
Py_INCREF(dtype);
81-
np_var = PyArray_FromAny(np_var, dtype, 1, 1, reqout, NULL);
84+
np_var = (PyArrayObject*)PyArray_FromAny(po_var, dtype, 1, 1, reqout, NULL);
8285
if (!np_var) {
8386
ERR("var wasn't the type expected");
8487
Py_DECREF(np_x);
@@ -243,10 +246,10 @@ static int em_fit_1d_samples(PyObject* np_x,
243246

244247
// _reg: Inverse-Wishart prior on variance (with hard-coded
245248
// variance prior I), with strength alpha.
246-
static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
247-
PyObject* np_amp,
248-
PyObject* np_mean,
249-
PyObject* np_var,
249+
static int em_fit_2d_reg(PyObject* po_img, int x0, int y0,
250+
PyObject* po_amp,
251+
PyObject* po_mean,
252+
PyObject* po_var,
250253
double alpha,
251254
int steps) {
252255
npy_intp i, N, K, k;
@@ -259,9 +262,11 @@ static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
259262
double tpd;
260263
int result;
261264

262-
PyArray_Descr* dtype = PyArray_DescrFromType(PyArray_DOUBLE);
263-
int req = NPY_C_CONTIGUOUS | NPY_ALIGNED;
264-
int reqout = req | NPY_WRITEABLE | NPY_UPDATEIFCOPY;
265+
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_DOUBLE);
266+
int req = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED;
267+
int reqout = req | NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY;
268+
269+
PyArrayObject *np_img, *np_amp, *np_mean, *np_var;
265270

266271
double* amp;
267272
double* mean;
@@ -271,22 +276,22 @@ static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
271276
tpd = pow(2.*M_PI, D);
272277

273278
Py_INCREF(dtype);
274-
np_img = PyArray_FromAny(np_img, dtype, 2, 2, req, NULL);
279+
np_img = (PyArrayObject*)PyArray_FromAny(po_img, dtype, 2, 2, req, NULL);
275280
if (!np_img) {
276281
ERR("img wasn't the type expected");
277282
Py_DECREF(dtype);
278283
return -1;
279284
}
280285
Py_INCREF(dtype);
281-
np_amp = PyArray_FromAny(np_amp, dtype, 1, 1, reqout, NULL);
286+
np_amp = (PyArrayObject*)PyArray_FromAny(po_amp, dtype, 1, 1, reqout, NULL);
282287
if (!np_amp) {
283288
ERR("amp wasn't the type expected");
284289
Py_DECREF(np_img);
285290
Py_DECREF(dtype);
286291
return -1;
287292
}
288293
Py_INCREF(dtype);
289-
np_mean = PyArray_FromAny(np_mean, dtype, 2, 2, reqout, NULL);
294+
np_mean = (PyArrayObject*)PyArray_FromAny(po_mean, dtype, 2, 2, reqout, NULL);
290295
if (!np_mean) {
291296
ERR("mean wasn't the type expected");
292297
Py_DECREF(np_img);
@@ -295,7 +300,7 @@ static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
295300
return -1;
296301
}
297302
Py_INCREF(dtype);
298-
np_var = PyArray_FromAny(np_var, dtype, 3, 3, reqout, NULL);
303+
np_var = (PyArrayObject*)PyArray_FromAny(po_var, dtype, 3, 3, reqout, NULL);
299304
if (!np_var) {
300305
ERR("var wasn't the type expected");
301306
Py_DECREF(np_img);

tractor/emfit2.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
2-
PyObject* np_amp,
3-
PyObject* np_mean,
4-
PyObject* np_var,
1+
static int em_fit_2d_reg2(PyObject* po_img, int x0, int y0,
2+
PyObject* po_amp,
3+
PyObject* po_mean,
4+
PyObject* po_var,
55
double alpha,
66
int steps,
77
double approx,
@@ -13,9 +13,9 @@ static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
1313
double tpd;
1414
int result;
1515

16-
PyArray_Descr* dtype = PyArray_DescrFromType(PyArray_DOUBLE);
17-
int req = NPY_C_CONTIGUOUS | NPY_ALIGNED;
18-
int reqout = req | NPY_WRITEABLE | NPY_UPDATEIFCOPY;
16+
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_DOUBLE);
17+
int req = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED;
18+
int reqout = req | NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY;
1919

2020
double* amp;
2121
double* mean;
@@ -26,27 +26,29 @@ static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
2626
double skyamp;
2727
double imgsum;
2828

29+
PyArrayObject *np_img, *np_amp, *np_mean, *np_var;
30+
2931
int nexp = 0;
3032

3133
tpd = pow(2.*M_PI, D);
3234

3335
Py_INCREF(dtype);
34-
np_img = PyArray_FromAny(np_img, dtype, 2, 2, req, NULL);
36+
np_img = (PyArrayObject*)PyArray_FromAny(po_img, dtype, 2, 2, req, NULL);
3537
if (!np_img) {
3638
ERR("img wasn't the type expected");
3739
Py_DECREF(dtype);
3840
return -1;
3941
}
4042
Py_INCREF(dtype);
41-
np_amp = PyArray_FromAny(np_amp, dtype, 1, 1, reqout, NULL);
43+
np_amp = (PyArrayObject*)PyArray_FromAny(po_amp, dtype, 1, 1, reqout, NULL);
4244
if (!np_amp) {
4345
ERR("amp wasn't the type expected");
4446
Py_DECREF(np_img);
4547
Py_DECREF(dtype);
4648
return -1;
4749
}
4850
Py_INCREF(dtype);
49-
np_mean = PyArray_FromAny(np_mean, dtype, 2, 2, reqout, NULL);
51+
np_mean = (PyArrayObject*)PyArray_FromAny(po_mean, dtype, 2, 2, reqout, NULL);
5052
if (!np_mean) {
5153
ERR("mean wasn't the type expected");
5254
Py_DECREF(np_img);
@@ -55,7 +57,7 @@ static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
5557
return -1;
5658
}
5759
Py_INCREF(dtype);
58-
np_var = PyArray_FromAny(np_var, dtype, 3, 3, reqout, NULL);
60+
np_var = (PyArrayObject*)PyArray_FromAny(po_var, dtype, 3, 3, reqout, NULL);
5961
if (!np_var) {
6062
ERR("var wasn't the type expected");
6163
Py_DECREF(np_img);

0 commit comments

Comments
 (0)