Commit fd4706f5 authored by SPeillet's avatar SPeillet
Browse files

ENH: Add python3 support

parent 51e59ca7
export NUMPY_INCLUDE=/usr/lib64/python2.7/site-packages/numpy/core/include/numpy
export PYTHON_INCLUDE=/usr/include/python2.7
export PYTHON_LIB=python2.7
export PYTHON_LIB_PATH=/usr/lib64
export NUMPY_INCLUDE=/usr/lib/python3/dist-packages/numpy/core/include/numpy
export PYTHON_INCLUDE=/usr/include/python3.6
export PYTHON_LIB=python3.6
export PYTHON_LIB_PATH=/usr/lib
export PYTHON_LD_EXT=so
export PYTHON_LD_INSTALLDIR=/home/raffaele/Chaines/sitsproc/sitsproc_c_modules
export PYTHON_LD_INSTALLDIR=$HOME/moringa/sitsproc_c_modules
export PYTHON_CPY_LINKERFLAGS=
......@@ -8,6 +8,10 @@
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif
#include <arrayobject.h>
#include "dataconverter.h"
......@@ -19,328 +23,173 @@ using namespace std;
static PyObject *
obiatools_version(PyObject *self, PyObject *args) {
PySys_WriteStdout("Hello from OBIATOOLS! Version 0.0.1a");
return Py_None;
PySys_WriteStdout("Hello from OBIATOOLS! Version 0.0.1a");
return Py_None;
}
/*
* TO DELETE!
static PyObject *
obiatools_test(PyObject *self, PyObject *args) {
int b,iR,iC,oR,oC;
PyArrayObject *img;
PyArrayObject *ol;
PyObject *w = 0;
PyArg_ParseTuple(args,"O!O!|O", &PyArray_Type, &img, &PyArray_Type, &ol, &w);
npy_intp *iDims = PyArray_DIMS(img);
b = iDims[0]; iR = iDims[1]; iC = iDims[2];
npy_intp *oDims = PyArray_DIMS(ol);
oR = oDims[0]; oC = oDims[1];
Data* img_vec = (Data*)PyArray_DATA(img);
Data*** image = array_1to3d<Data,npy_intp>(img_vec,iDims);
Label* ol_vec = (Label*)PyArray_DATA(ol);
Label** objlayer = array_1to2d<Label,npy_intp>(ol_vec,oDims);
float **wg;
float *w_vec;
if (!w || w == Py_None) {
wg = 0;
} else {
PyArray_Descr* dtype;
int _ndims;
npy_intp _dims;
PyArrayObject* _w;
PyArray_GetArrayParamsFromObject(w,NULL,1,&dtype,&_ndims,&_dims,&_w,NULL);
w_vec = (float*)PyArray_DATA(_w);
wg = array_1to2d<float,npy_intp>(w_vec,&iDims[1]);
}
RasterStats rs(objlayer,oR,oC);
rs.addRaster(image,b,iR,iC,wg);
rs.computeStatistics(0,OSTAT_MIN);
rs.computeStatistics(0,OSTAT_MAX);
rs.computeStatistics(0,OSTAT_RANGE);
map<Label,float> feat = rs.getStat(0,0,OSTAT_MIN);
PySys_WriteStdout("Min:\n");
map<Label,float>::iterator it;
for (it=feat.begin();it!=feat.end();it++)
PySys_WriteStdout("%d --> %10.5f\n",it->first,it->second);
PySys_WriteStdout("\nMax:\n");
feat = rs.getStat(0,0,OSTAT_MAX);
for (it=feat.begin();it!=feat.end();it++)
PySys_WriteStdout("%d --> %10.5f\n",it->first,it->second);
PySys_WriteStdout("\nRange:\n");
feat = rs.getStat(0,0,OSTAT_RANGE);
for (it=feat.begin();it!=feat.end();it++)
PySys_WriteStdout("%d --> %10.5f\n",it->first,it->second);
rs.computeStatistics(0,OSTAT_MEDIAN);
map<Label,float> feat = rs.getStat(0,3,OSTAT_MEDIAN);
PySys_WriteStdout("\nMedian:\n");
map<Label,float>::iterator it;
for (it=feat.begin();it!=feat.end();it++)
PySys_WriteStdout("%d --> %10.5f\n",it->first,it->second);
PySys_WriteStdout("\n ==> OK from inside! \n\n");
return Py_None;
}
*/
/*
static PyObject *
obiatools_zonalstats(PyObject *self, PyObject *args) {
int t,k,s;
int B,R,C,oR,oC,nStats,nObj;
PyArrayObject *ol, *ras, *ridx, *cidx, *stats;
PyObject *w = 0;
PyArg_ParseTuple(args,"O!O!O!O!O!|O", &PyArray_Type, &ol, &PyArray_Type, &ras, &PyArray_Type, &ridx, &PyArray_Type, &cidx, &PyArray_Type, &stats, &w);
npy_intp nRasDims = PyArray_NDIM(ras);
npy_intp *rasDims = PyArray_DIMS(ras);
if (nRasDims == 2) {
B = 1; R = rasDims[0]; C = rasDims[1];
} else {
B = rasDims[0]; R = rasDims[1]; C = rasDims[2];
}
npy_intp *olDims = PyArray_DIMS(ol);
oR = olDims[0]; oC = olDims[1];
Data* ras_vec = (Data*)PyArray_DATA(ras);
Data*** ras_arr = array_1to3d<Data,npy_intp>(ras_vec,rasDims);
Label* ol_vec = (Label*)PyArray_DATA(ol);
Label** ol_arr = array_1to2d<Label,npy_intp>(ol_vec,olDims);
int* row_idx = (int*)PyArray_DATA(ridx);
int* col_idx = (int*)PyArray_DATA(cidx);
npy_intp *statsDims = PyArray_DIMS(stats);
int* stats_vec = (int*)PyArray_DATA(stats);
nStats = (int)statsDims[0];
float **wg;
float *w_vec;
if (!w || w == Py_None) {
wg = 0;
} else {
PyArray_Descr* dtype;
int _ndims;
npy_intp _dims;
PyArrayObject* _w;
PyArray_GetArrayParamsFromObject(w,NULL,1,&dtype,&_ndims,&_dims,&_w,NULL);
w_vec = (float*)PyArray_DATA(_w);
wg = array_1to2d<float,npy_intp>(w_vec,&rasDims[1]);
}
RasterStats rs(ol_arr,oR,oC);
rs.addRaster(ras_arr,B,R,C,row_idx,col_idx,wg);
nObj = rs.getNObjects();
int *cstats = new int[nStats];
int tot_stats = 0;
for (s=0; s<nStats; s++) {
cstats[s] = rs.computeStatistics(0,stats_vec[s]);
tot_stats += cstats[s];
}
float **zstats = new float*[nObj];
for (t=0; t<nObj; t++) zstats[t] = new float[tot_stats+1];
map<Label,float> tmp;
map<Label,float>::iterator it;
int cnt = 0;
for (s=0; s<nStats; s++) {
for (k=0; k<cstats[s]; k++) {
tmp.clear();
tmp = rs.getStat(0,k,stats_vec[s]);
if (k==0 && s==0) {
t = 0;
for (it=tmp.begin();it!=tmp.end();it++) zstats[t++][cnt] = it->first;
cnt++;
}
t = 0;
for (it=tmp.begin();it!=tmp.end();it++) zstats[t++][cnt] = it->second;
cnt++;
}
}
npy_intp featDims = (npy_intp)tot_stats;
PyObject *out_dict = PyDict_New();
PyObject *key,*val;
for (t=0;t<nObj;t++) {
key = PyInt_FromLong((long)round(zstats[t][0]));
val = PyArray_SimpleNewFromData(1,&featDims,NPY_FLOAT32,&zstats[t][1]);
PyDict_SetItem(out_dict,key,val);
}
npy_intp py_nStats = (npy_intp)nStats;
PyObject *out_cstats = PyArray_SimpleNewFromData(1,&py_nStats,NPY_INT32,cstats);
del_array_3d<Data,npy_intp>(ras_arr,rasDims);
del_array_2d<Label,npy_intp>(ol_arr,olDims);
if (w && w != Py_None) del_array_2d<float,npy_intp>(wg,&rasDims[1]);
delete [] zstats;
return Py_BuildValue("OO",out_dict,out_cstats);
}
*/
//V2
static PyObject *
obiatools_zonalstats(PyObject *self, PyObject *args) {
int t,k,s;
int B,R,C,oR,oC,nStats,nObj;
PyArrayObject *ol, *ras, *ridx, *cidx, *stats;
PyObject *w = 0, *nv = 0;
PyArg_ParseTuple(args,"O!O!O!O!O!|OO", &PyArray_Type, &ol, &PyArray_Type, &ras, &PyArray_Type, &ridx, &PyArray_Type, &cidx, &PyArray_Type, &stats, &nv, &w);
npy_intp nRasDims = PyArray_NDIM(ras);
npy_intp *rasDims = PyArray_DIMS(ras);
if (nRasDims == 2) {
B = 1; R = rasDims[0]; C = rasDims[1];
} else {
B = rasDims[0]; R = rasDims[1]; C = rasDims[2];
}
npy_intp *olDims = PyArray_DIMS(ol);
oR = olDims[0]; oC = olDims[1];
Data* ras_arr = (Data*)PyArray_DATA(ras);
Label* ol_arr = (Label*)PyArray_DATA(ol);
int* row_idx = (int*)PyArray_DATA(ridx);
int* col_idx = (int*)PyArray_DATA(cidx);
npy_intp *statsDims = PyArray_DIMS(stats);
int* stats_vec = (int*)PyArray_DATA(stats);
nStats = (int)statsDims[0];
float *wg;
if (!w || w == Py_None) {
wg = 0;
} else {
PyArray_Descr* dtype;
int _ndims;
npy_intp _dims;
PyArrayObject* _w;
PyArray_GetArrayParamsFromObject(w,NULL,1,&dtype,&_ndims,&_dims,&_w,NULL);
wg = (float*)PyArray_DATA(_w);
}
Data *nvg;
if (!nv || nv == Py_None) {
nvg = new Data[B];
for (k=0; k<B; k++) nvg[k] = -9999.0;
} else {
PyArray_Descr* dtype;
int _ndims;
npy_intp _dims;
PyArrayObject* _nv;
PyArray_GetArrayParamsFromObject(nv,NULL,1,&dtype,&_ndims,&_dims,&_nv,NULL);
nvg = (Data*)PyArray_DATA(_nv);
}
RasterStats1D rs(ol_arr,oR,oC);
rs.addRaster(ras_arr,B,R,C,nvg,row_idx,col_idx,wg);
nObj = rs.getNObjects();
int *cstats = new int[nStats];
int tot_stats = 0;
for (s=0; s<nStats; s++) {
cstats[s] = rs.computeStatistics(0,stats_vec[s]);
tot_stats += cstats[s];
}
float **zstats = new float*[nObj];
for (t=0; t<nObj; t++) zstats[t] = new float[tot_stats+1];
map<Label,float> tmp;
map<Label,float>::iterator it;
int cnt = 0;
for (s=0; s<nStats; s++) {
for (k=0; k<cstats[s]; k++) {
tmp.clear();
tmp = rs.getStat(0,k,stats_vec[s]);
if (k==0 && s==0) {
t = 0;
for (it=tmp.begin();it!=tmp.end();it++) zstats[t++][cnt] = it->first;
cnt++;
}
t = 0;
for (it=tmp.begin();it!=tmp.end();it++) zstats[t++][cnt] = it->second;
cnt++;
}
}
npy_intp featDims = (npy_intp)tot_stats;
PyObject *out_dict = PyDict_New();
PyObject *key,*val;
for (t=0;t<nObj;t++) {
key = PyInt_FromLong((long)round(zstats[t][0]));
val = PyArray_SimpleNewFromData(1,&featDims,NPY_FLOAT32,&zstats[t][1]);
PyDict_SetItem(out_dict,key,val);
}
npy_intp py_nStats = (npy_intp)nStats;
PyObject *out_cstats = PyArray_SimpleNewFromData(1,&py_nStats,NPY_INT32,cstats);
delete [] zstats;
return Py_BuildValue("OO",out_dict,out_cstats);
int t,k,s;
int B,R,C,oR,oC,nStats,nObj;
PyArrayObject *ol, *ras, *ridx, *cidx, *stats;
PyObject *w = 0, *nv = 0;
PyArg_ParseTuple(args,"O!O!O!O!O!|OO", &PyArray_Type, &ol, &PyArray_Type, &ras, &PyArray_Type, &ridx, &PyArray_Type, &cidx, &PyArray_Type, &stats, &nv, &w);
npy_intp nRasDims = PyArray_NDIM(ras);
npy_intp *rasDims = PyArray_DIMS(ras);
if (nRasDims == 2) {
B = 1; R = rasDims[0]; C = rasDims[1];
} else {
B = rasDims[0]; R = rasDims[1]; C = rasDims[2];
}
npy_intp *olDims = PyArray_DIMS(ol);
oR = olDims[0]; oC = olDims[1];
Data* ras_arr = (Data*)PyArray_DATA(ras);
Label* ol_arr = (Label*)PyArray_DATA(ol);
int* row_idx = (int*)PyArray_DATA(ridx);
int* col_idx = (int*)PyArray_DATA(cidx);
npy_intp *statsDims = PyArray_DIMS(stats);
int* stats_vec = (int*)PyArray_DATA(stats);
nStats = (int)statsDims[0];
float *wg;
if (!w || w == Py_None) {
wg = 0;
} else {
PyArray_Descr* dtype;
int _ndims;
npy_intp _dims;
PyArrayObject* _w;
PyArray_GetArrayParamsFromObject(w,NULL,1,&dtype,&_ndims,&_dims,&_w,NULL);
wg = (float*)PyArray_DATA(_w);
}
Data *nvg;
if (!nv || nv == Py_None) {
nvg = new Data[B];
for (k=0; k<B; k++) nvg[k] = -9999.0;
} else {
PyArray_Descr* dtype;
int _ndims;
npy_intp _dims;
PyArrayObject* _nv;
PyArray_GetArrayParamsFromObject(nv,NULL,1,&dtype,&_ndims,&_dims,&_nv,NULL);
nvg = (Data*)PyArray_DATA(_nv);
}
RasterStats1D rs(ol_arr,oR,oC);
rs.addRaster(ras_arr,B,R,C,nvg,row_idx,col_idx,wg);
nObj = rs.getNObjects();
int *cstats = new int[nStats];
int tot_stats = 0;
for (s=0; s<nStats; s++) {
cstats[s] = rs.computeStatistics(0,stats_vec[s]);
tot_stats += cstats[s];
}
float **zstats = new float*[nObj];
for (t=0; t<nObj; t++) zstats[t] = new float[tot_stats+1];
map<Label,float> tmp;
map<Label,float>::iterator it;
int cnt = 0;
for (s=0; s<nStats; s++) {
for (k=0; k<cstats[s]; k++) {
tmp.clear();
tmp = rs.getStat(0,k,stats_vec[s]);
if (k==0 && s==0) {
t = 0;
for (it=tmp.begin();it!=tmp.end();it++) zstats[t++][cnt] = it->first;
cnt++;
}
t = 0;
for (it=tmp.begin();it!=tmp.end();it++) zstats[t++][cnt] = it->second;
cnt++;
}
}
npy_intp featDims = (npy_intp)tot_stats;
PyObject *out_dict = PyDict_New();
PyObject *key,*val;
for (t=0;t<nObj;t++) {
key = PyLong_FromLong((long)round(zstats[t][0]));
val = PyArray_SimpleNewFromData(1,&featDims,NPY_FLOAT32,&zstats[t][1]);
PyDict_SetItem(out_dict,key,val);
}
npy_intp py_nStats = (npy_intp)nStats;
PyObject *out_cstats = PyArray_SimpleNewFromData(1,&py_nStats,NPY_INT32,cstats);
delete [] zstats;
return Py_BuildValue("OO",out_dict,out_cstats);
}
// MODULE INITIALIZATION
static PyMethodDef obiatoolsMethods[] = {
{"version", obiatools_version, METH_VARARGS, "Gives version of the OBIATOOLS module."},
//{"test", obiatools_test, METH_VARARGS, "Generic test function."},
{"zonalstats", obiatools_zonalstats, METH_VARARGS, "Simple object-based zonal statistics extraction from single (possibly multi-band) rasters."},
//{"zonalstatsv2", obiatools_zonalstats, METH_VARARGS, "Simple object-based zonal statistics extraction from single (possibly multi-band) rasters. Optimized."},
{NULL, NULL, 0, NULL}
{"version", obiatools_version, METH_VARARGS, "Gives version of the OBIATOOLS module."},
//{"test", obiatools_test, METH_VARARGS, "Generic test function."},
{"zonalstats", obiatools_zonalstats, METH_VARARGS, "Simple object-based zonal statistics extraction from single (possibly multi-band) rasters."},
//{"zonalstatsv2", obiatools_zonalstats, METH_VARARGS, "Simple object-based zonal statistics extraction from single (possibly multi-band) rasters. Optimized."},
{NULL, NULL, 0, NULL}
};
#ifdef PY3K
static struct PyModuleDef obiatoolsdef = {
PyModuleDef_HEAD_INIT,
"obiatools",
"obia doc",
-1,
obiatoolsMethods
};
// module initializer for python3
PyMODINIT_FUNC PyInit_obiatools()
{
PyObject* mod = PyModule_Create(&obiatoolsdef);
PyModule_AddIntConstant(mod,"OSTAT_COUNT",OSTAT_COUNT);
PyModule_AddIntConstant(mod,"OSTAT_SUM",OSTAT_SUM);
PyModule_AddIntConstant(mod,"OSTAT_MEAN",OSTAT_MEAN);
PyModule_AddIntConstant(mod,"OSTAT_MEDIAN",OSTAT_MEDIAN);
PyModule_AddIntConstant(mod,"OSTAT_STD",OSTAT_STD);
PyModule_AddIntConstant(mod,"OSTAT_MIN",OSTAT_MIN);
PyModule_AddIntConstant(mod,"OSTAT_MAX",OSTAT_MAX);
PyModule_AddIntConstant(mod,"OSTAT_RANGE",OSTAT_RANGE);
PyModule_AddIntConstant(mod,"OSTAT_MINORITY",OSTAT_MINORITY);
PyModule_AddIntConstant(mod,"OSTAT_MAJORITY",OSTAT_MAJORITY);
PyModule_AddIntConstant(mod,"OSTAT_VARIETY",OSTAT_VARIETY);
import_array();
return mod;
}
#else
// module initializer for python2
PyMODINIT_FUNC initobiatools(void) {
//(void) Py_InitModule("obiatools",obiatoolsMethods);
PyObject* mod = Py_InitModule("obiatools",obiatoolsMethods);
PyModule_AddIntConstant(mod,"OSTAT_COUNT",OSTAT_COUNT);
PyModule_AddIntConstant(mod,"OSTAT_SUM",OSTAT_SUM);
PyModule_AddIntConstant(mod,"OSTAT_MEAN",OSTAT_MEAN);
PyModule_AddIntConstant(mod,"OSTAT_MEDIAN",OSTAT_MEDIAN);
PyModule_AddIntConstant(mod,"OSTAT_STD",OSTAT_STD);
PyModule_AddIntConstant(mod,"OSTAT_MIN",OSTAT_MIN);
PyModule_AddIntConstant(mod,"OSTAT_MAX",OSTAT_MAX);
PyModule_AddIntConstant(mod,"OSTAT_RANGE",OSTAT_RANGE);
PyModule_AddIntConstant(mod,"OSTAT_MINORITY",OSTAT_MINORITY);
PyModule_AddIntConstant(mod,"OSTAT_MAJORITY",OSTAT_MAJORITY);
PyModule_AddIntConstant(mod,"OSTAT_VARIETY",OSTAT_VARIETY);
import_array();
//(void) Py_InitModule("obiatools",obiatoolsMethods);
PyObject* mod = Py_InitModule("obiatools",obiatoolsMethods);
PyModule_AddIntConstant(mod,"OSTAT_COUNT",OSTAT_COUNT);
PyModule_AddIntConstant(mod,"OSTAT_SUM",OSTAT_SUM);
PyModule_AddIntConstant(mod,"OSTAT_MEAN",OSTAT_MEAN);
PyModule_AddIntConstant(mod,"OSTAT_MEDIAN",OSTAT_MEDIAN);
PyModule_AddIntConstant(mod,"OSTAT_STD",OSTAT_STD);
PyModule_AddIntConstant(mod,"OSTAT_MIN",OSTAT_MIN);
PyModule_AddIntConstant(mod,"OSTAT_MAX",OSTAT_MAX);
PyModule_AddIntConstant(mod,"OSTAT_RANGE",OSTAT_RANGE);
PyModule_AddIntConstant(mod,"OSTAT_MINORITY",OSTAT_MINORITY);
PyModule_AddIntConstant(mod,"OSTAT_MAJORITY",OSTAT_MAJORITY);
PyModule_AddIntConstant(mod,"OSTAT_VARIETY",OSTAT_VARIETY);
import_array();
}
#endif
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment