Skip to content

Commit

Permalink
update to numpy 1.7 API
Browse files Browse the repository at this point in the history
  • Loading branch information
dstndstn committed Jul 10, 2017
1 parent 6e87c48 commit fbdd2f4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 64 deletions.
11 changes: 6 additions & 5 deletions tractor/ceres-tractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// http://docs.scipy.org/doc/numpy/reference/c-api.array.html#import_array
#define PY_ARRAY_UNIQUE_SYMBOL tractorceres_ARRAY_API
#define NO_IMPORT_ARRAY
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>

#include <stdio.h>
Expand Down Expand Up @@ -183,7 +184,7 @@ template class ForcedPhotCostFunction<double>;

ImageCostFunction::ImageCostFunction(PyObject* tractor,
int imagei, int nparams,
PyObject* np_params) :
PyArrayObject* np_params) :
_tractor(tractor), _imagei(imagei), _image(NULL),
_npix(0), _nparams(nparams), _W(0), _H(0), _np_params(np_params) {

Expand Down Expand Up @@ -273,10 +274,10 @@ bool ImageCostFunction::_Evaluate(double const* const* parameters,
}

// Get residuals (chi image)
PyObject* np_chi;
PyArrayObject* np_chi;
//printf("Calling getChiImage(%i)\n", _imagei);
np_chi = PyObject_CallMethod(_tractor, (char*)"getChiImage",
(char*)"i", _imagei);
np_chi = (PyArrayObject*)PyObject_CallMethod(
_tractor, (char*)"getChiImage", (char*)"i", _imagei);
if (!np_chi) {
printf("getChiImage() failed\n");
return false;
Expand Down Expand Up @@ -341,7 +342,7 @@ bool ImageCostFunction::_Evaluate(double const* const* parameters,

int x0 = PyInt_AsLong(PyTuple_GetItem(deriv, 1));
int y0 = PyInt_AsLong(PyTuple_GetItem(deriv, 2));
PyObject* np_deriv = PyTuple_GetItem(deriv, 3);
PyArrayObject* np_deriv = (PyArrayObject*)PyTuple_GetItem(deriv, 3);
if (!PyArray_Check(np_deriv)) {
printf("Expected third element of allderivs element %i to be an array\n",
ideriv);
Expand Down
5 changes: 3 additions & 2 deletions tractor/ceres-tractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class ImageCostFunction : public CostFunction {
public:
virtual ~ImageCostFunction();

ImageCostFunction(PyObject* tractor, int imagei, int nparams, PyObject* np_params);
ImageCostFunction(PyObject* tractor, int imagei, int nparams,
PyArrayObject* np_params);

virtual bool Evaluate(double const* const* parameters,
double* residuals,
Expand All @@ -80,5 +81,5 @@ class ImageCostFunction : public CostFunction {
int _nparams;
int _W;
int _H;
PyObject* _np_params;
PyArrayObject* _np_params;
};
32 changes: 17 additions & 15 deletions tractor/ceres.i
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

%{
#define PY_ARRAY_UNIQUE_SYMBOL tractorceres_ARRAY_API
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>

#include <Python.h>
Expand Down Expand Up @@ -52,13 +53,13 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
sources: [ (index, x0, y0, np_img), ... ]
*/
npy_intp Nblocks;
assert(PyList_Check(blocks));
assert(PyList_Check(blocks));
Nblocks = PyList_Size(blocks);
//printf("N blocks: %i\n", (int)Nblocks);
assert(PyArray_Check(np_fluxes));
assert(PyArray_TYPE(np_fluxes) == NPY_DOUBLE);
int Nfluxes = (int)PyArray_Size(np_fluxes);
double* realfluxes = (double*)PyArray_DATA(np_fluxes);
double* realfluxes = (double*)PyArray_DATA((PyArrayObject*)np_fluxes);
T* mod0data;
Problem problem;
int totaldatapix = 0;
Expand All @@ -77,7 +78,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
PyObject* srclist;
PyObject* obj;
int x0, y0;
PyObject *img, *mod0, *ierr;
PyArrayObject *img, *mod0, *ierr;
int w, h;
int Nsources;

Expand All @@ -92,12 +93,12 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
assert(PyTuple_Size(obj) == 5);
x0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 0));
y0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 1));
img = PyTuple_GET_ITEM(obj, 2);
img = (PyArrayObject*)PyTuple_GET_ITEM(obj, 2);
assert(PyArray_Check(img));
h = PyArray_DIM(img, 0);
w = PyArray_DIM(img, 1);
mod0 = PyTuple_GET_ITEM(obj, 3);
if (mod0 == Py_None) {
mod0 = (PyArrayObject*)PyTuple_GET_ITEM(obj, 3);
if ((PyObject*)mod0 == Py_None) {
mod0data = NULL;
} else {
assert(PyArray_Check(mod0));
Expand All @@ -106,7 +107,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
assert(PyArray_TYPE(mod0) == npy_type);
mod0data = (T*)PyArray_DATA(mod0);
}
ierr = PyTuple_GET_ITEM(obj, 4);
ierr = (PyArrayObject*)PyTuple_GET_ITEM(obj, 4);
assert(PyArray_Check(ierr));
assert(PyArray_DIM(ierr, 0) == h);
assert(PyArray_DIM(ierr, 1) == w);
Expand All @@ -124,7 +125,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
int nderivpix = 0;
for (int j=0; j<Nsources; j++) {
int index, uh, uw;
PyObject* uimg;
PyArrayObject* uimg;
obj = PyList_GET_ITEM(srclist, j);
assert(PyTuple_Check(obj));
assert(PyTuple_Size(obj) == 4);
Expand All @@ -136,7 +137,7 @@ static PyObject* real_ceres_forced_phot(PyObject* blocks,
assert(index < Nfluxes);
x0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 1));
y0 = PyInt_AsLong(PyTuple_GET_ITEM(obj, 2));
uimg = PyTuple_GET_ITEM(obj, 3);
uimg = (PyArrayObject*)PyTuple_GET_ITEM(obj, 3);
assert(PyArray_Check(uimg));
uh = PyArray_DIM(uimg, 0);
uw = PyArray_DIM(uimg, 1);
Expand Down Expand Up @@ -291,8 +292,8 @@ static PyObject* ceres_forced_phot(PyObject* blocks,
obj = PyTuple_GET_ITEM(block, 0);
assert(PyTuple_Check(obj));
assert(PyTuple_Size(obj) == 5);
PyObject* img;
img = PyTuple_GET_ITEM(obj, 2);
PyArrayObject* img;
img = (PyArrayObject*)PyTuple_GET_ITEM(obj, 2);
assert(PyArray_Check(img));

if (PyArray_TYPE(img) == NPY_FLOAT) {
Expand Down Expand Up @@ -355,7 +356,8 @@ protected:


static PyObject* ceres_opt(PyObject* tractor, int nims,
PyObject* np_params, PyObject* np_variance,
PyArrayObject* np_params,
PyArrayObject* np_variance,
int scale_columns,
int numeric,
float numeric_stepsize,
Expand Down Expand Up @@ -396,10 +398,10 @@ static PyObject* ceres_opt(PyObject* tractor, int nims,
printf("ceres_opt: wrong type for params variable\n");
return NULL;
}
nparams = (int)PyArray_Size(np_params);
nparams = (int)PyArray_SIZE(np_params);
params = (double*)PyArray_DATA(np_params);

get_variance = (np_variance != Py_None);
get_variance = ((PyObject*)np_variance != Py_None);

//printf("ceres_opt, nims %i, nparams %i, get_variance %i\n",
// nims, nparams, get_variance);
Expand Down Expand Up @@ -580,7 +582,7 @@ static PyObject* ceres_opt(PyObject* tractor, int nims,
printf("ceres_opt: wrong type for variance variable\n");
return NULL;
}
if (PyArray_Size(np_variance) != PyArray_Size(np_params)) {
if (PyArray_SIZE(np_variance) != PyArray_SIZE(np_params)) {
printf("ceres_opt: wrong size for variance variable\n");
return NULL;
}
Expand Down
49 changes: 27 additions & 22 deletions tractor/emfit.i
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
%include <typemaps.i>

%{
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include <math.h>
#include <assert.h>
Expand All @@ -28,10 +29,10 @@

// _reg: Inverse-Wishart prior on variance (with hard-coded
// variance prior I), with strength alpha.
static int em_fit_1d_samples_reg(PyObject* np_x,
PyObject* np_amp,
PyObject* np_mean,
PyObject* np_var,
static int em_fit_1d_samples_reg(PyObject* po_x,
PyObject* po_amp,
PyObject* po_mean,
PyObject* po_var,
double alpha,
int steps) {
npy_intp i, N, K, k;
Expand All @@ -42,9 +43,11 @@ static int em_fit_1d_samples_reg(PyObject* np_x,
double tpd;
int result;

PyArray_Descr* dtype = PyArray_DescrFromType(PyArray_DOUBLE);
int req = NPY_C_CONTIGUOUS | NPY_ALIGNED;
int reqout = req | NPY_WRITEABLE | NPY_UPDATEIFCOPY;
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_DOUBLE);
int req = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED;
int reqout = req | NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY;

PyArrayObject *np_x, *np_amp, *np_mean, *np_var;

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

Py_INCREF(dtype);
np_x = PyArray_FromAny(np_x, dtype, 1, 1, req, NULL);
np_x = (PyArrayObject*)PyArray_FromAny(po_x, dtype, 1, 1, req, NULL);
if (!np_x) {
ERR("x wasn't the type expected");
Py_DECREF(dtype);
return -1;
}
Py_INCREF(dtype);
np_amp = PyArray_FromAny(np_amp, dtype, 1, 1, reqout, NULL);
np_amp = (PyArrayObject*)PyArray_FromAny(po_amp, dtype, 1, 1, reqout, NULL);
if (!np_amp) {
ERR("amp wasn't the type expected");
Py_DECREF(np_x);
Py_DECREF(dtype);
return -1;
}
Py_INCREF(dtype);
np_mean = PyArray_FromAny(np_mean, dtype, 1, 1, reqout, NULL);
np_mean = (PyArrayObject*)PyArray_FromAny(po_mean, dtype, 1, 1, reqout, NULL);
if (!np_mean) {
ERR("mean wasn't the type expected");
Py_DECREF(np_x);
Expand All @@ -78,7 +81,7 @@ static int em_fit_1d_samples_reg(PyObject* np_x,
return -1;
}
Py_INCREF(dtype);
np_var = PyArray_FromAny(np_var, dtype, 1, 1, reqout, NULL);
np_var = (PyArrayObject*)PyArray_FromAny(po_var, dtype, 1, 1, reqout, NULL);
if (!np_var) {
ERR("var wasn't the type expected");
Py_DECREF(np_x);
Expand Down Expand Up @@ -243,10 +246,10 @@ static int em_fit_1d_samples(PyObject* np_x,

// _reg: Inverse-Wishart prior on variance (with hard-coded
// variance prior I), with strength alpha.
static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
PyObject* np_amp,
PyObject* np_mean,
PyObject* np_var,
static int em_fit_2d_reg(PyObject* po_img, int x0, int y0,
PyObject* po_amp,
PyObject* po_mean,
PyObject* po_var,
double alpha,
int steps) {
npy_intp i, N, K, k;
Expand All @@ -259,9 +262,11 @@ static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
double tpd;
int result;

PyArray_Descr* dtype = PyArray_DescrFromType(PyArray_DOUBLE);
int req = NPY_C_CONTIGUOUS | NPY_ALIGNED;
int reqout = req | NPY_WRITEABLE | NPY_UPDATEIFCOPY;
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_DOUBLE);
int req = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED;
int reqout = req | NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY;

PyArrayObject *np_img, *np_amp, *np_mean, *np_var;

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

Py_INCREF(dtype);
np_img = PyArray_FromAny(np_img, dtype, 2, 2, req, NULL);
np_img = (PyArrayObject*)PyArray_FromAny(po_img, dtype, 2, 2, req, NULL);
if (!np_img) {
ERR("img wasn't the type expected");
Py_DECREF(dtype);
return -1;
}
Py_INCREF(dtype);
np_amp = PyArray_FromAny(np_amp, dtype, 1, 1, reqout, NULL);
np_amp = (PyArrayObject*)PyArray_FromAny(po_amp, dtype, 1, 1, reqout, NULL);
if (!np_amp) {
ERR("amp wasn't the type expected");
Py_DECREF(np_img);
Py_DECREF(dtype);
return -1;
}
Py_INCREF(dtype);
np_mean = PyArray_FromAny(np_mean, dtype, 2, 2, reqout, NULL);
np_mean = (PyArrayObject*)PyArray_FromAny(po_mean, dtype, 2, 2, reqout, NULL);
if (!np_mean) {
ERR("mean wasn't the type expected");
Py_DECREF(np_img);
Expand All @@ -295,7 +300,7 @@ static int em_fit_2d_reg(PyObject* np_img, int x0, int y0,
return -1;
}
Py_INCREF(dtype);
np_var = PyArray_FromAny(np_var, dtype, 3, 3, reqout, NULL);
np_var = (PyArrayObject*)PyArray_FromAny(po_var, dtype, 3, 3, reqout, NULL);
if (!np_var) {
ERR("var wasn't the type expected");
Py_DECREF(np_img);
Expand Down
24 changes: 13 additions & 11 deletions tractor/emfit2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
PyObject* np_amp,
PyObject* np_mean,
PyObject* np_var,
static int em_fit_2d_reg2(PyObject* po_img, int x0, int y0,
PyObject* po_amp,
PyObject* po_mean,
PyObject* po_var,
double alpha,
int steps,
double approx,
Expand All @@ -13,9 +13,9 @@ static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
double tpd;
int result;

PyArray_Descr* dtype = PyArray_DescrFromType(PyArray_DOUBLE);
int req = NPY_C_CONTIGUOUS | NPY_ALIGNED;
int reqout = req | NPY_WRITEABLE | NPY_UPDATEIFCOPY;
PyArray_Descr* dtype = PyArray_DescrFromType(NPY_DOUBLE);
int req = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED;
int reqout = req | NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY;

double* amp;
double* mean;
Expand All @@ -26,27 +26,29 @@ static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
double skyamp;
double imgsum;

PyArrayObject *np_img, *np_amp, *np_mean, *np_var;

int nexp = 0;

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

Py_INCREF(dtype);
np_img = PyArray_FromAny(np_img, dtype, 2, 2, req, NULL);
np_img = (PyArrayObject*)PyArray_FromAny(po_img, dtype, 2, 2, req, NULL);
if (!np_img) {
ERR("img wasn't the type expected");
Py_DECREF(dtype);
return -1;
}
Py_INCREF(dtype);
np_amp = PyArray_FromAny(np_amp, dtype, 1, 1, reqout, NULL);
np_amp = (PyArrayObject*)PyArray_FromAny(po_amp, dtype, 1, 1, reqout, NULL);
if (!np_amp) {
ERR("amp wasn't the type expected");
Py_DECREF(np_img);
Py_DECREF(dtype);
return -1;
}
Py_INCREF(dtype);
np_mean = PyArray_FromAny(np_mean, dtype, 2, 2, reqout, NULL);
np_mean = (PyArrayObject*)PyArray_FromAny(po_mean, dtype, 2, 2, reqout, NULL);
if (!np_mean) {
ERR("mean wasn't the type expected");
Py_DECREF(np_img);
Expand All @@ -55,7 +57,7 @@ static int em_fit_2d_reg2(PyObject* np_img, int x0, int y0,
return -1;
}
Py_INCREF(dtype);
np_var = PyArray_FromAny(np_var, dtype, 3, 3, reqout, NULL);
np_var = (PyArrayObject*)PyArray_FromAny(po_var, dtype, 3, 3, reqout, NULL);
if (!np_var) {
ERR("var wasn't the type expected");
Py_DECREF(np_img);
Expand Down
Loading

0 comments on commit fbdd2f4

Please sign in to comment.