Commit 1fb12c9f authored by Purves, Murray's avatar Purves, Murray
Browse files

Adding print statements for error tracking

parent 9693647f
Pipeline #34883 failed with stages
in 4 minutes and 38 seconds
......@@ -9,29 +9,36 @@
*/
static inline std::vector<double> pyObjectToDoubleVector(PyObject* object)
{
std::cout << "object to vector called" << std::endl;
std::vector<double> vector;
// Different conversion methods for list and tuple objects
if (PyTuple_Check(object))
if (PyList_Check(object))
{
for(Py_ssize_t i = 0; i < PyTuple_Size(object); ++i)
std::cout << "Object is list" << std::endl;
for (Py_ssize_t i = 0; i < PyList_Size(object); ++i)
{
PyObject* value = PyTuple_GetItem(object, i);
std::cout << i << "; ";
PyObject* value = PyList_GetItem(object, i);
std::cout << PyFloat_AsDouble(value) << std::endl;
vector.push_back(PyFloat_AsDouble(value));
}
}
else if (PyList_Check(object))
else if (PyTuple_Check(object))
{
for(Py_ssize_t i = 0; i < PyList_Size(object); ++i)
std::cout << "Object is tuple" << std::endl;
for (Py_ssize_t i = 0; i < PyTuple_Size(object); ++i)
{
PyObject* value = PyList_GetItem(object, i);
PyObject* value = PyTuple_GetItem(object, i);
vector.push_back(PyFloat_AsDouble(value));
}
}
else
{
std::cout << "exception" << std::endl;
throw std::logic_error(
"Input PyObject was not a list or tuple - cannot convert");
"Input PyObject was not a list or tuple - cannot convert");
}
return vector;
......@@ -41,7 +48,8 @@ static inline std::vector<double> pyObjectToDoubleVector(PyObject* object)
* Conversion from double vector to PyObject list.
* Adapted from https://gist.github.com/rjzak/5681680
*/
static inline PyObject* doubleVectorToListPyObject(const std::vector<double> &vector)
static inline PyObject* doubleVectorToListPyObject(
const std::vector<double>& vector)
{
PyObject* list = PyList_New(vector.size());
if (!list)
......@@ -49,10 +57,10 @@ static inline PyObject* doubleVectorToListPyObject(const std::vector<double> &ve
throw std::logic_error("Unable to initialise Python list - cannot convert");
}
for(size_t i = 0; i < vector.size(); ++i)
for (size_t i = 0; i < vector.size(); ++i)
{
PyObject* value = PyFloat_FromDouble(vector[i]);
if(!value)
if (!value)
{
Py_DECREF(list);
throw std::logic_error("Unable to convert list member - cannot convert");
......
#include <Python.h>
#include <iostream>
#include <vector>
#include "../../radixmath/interpolate.hh"
......@@ -13,6 +13,8 @@ static PyObject* interpolate_values(PyObject* self, PyObject* args)
bool circular;
double missingValue;
std::cout << "Getting arguments from call" << std::endl;
// Get the arguments from the call
if (!PyArg_ParseTuple(args, "OOpd", &baseValuesObj, &valuesToInterpObj,
&circular, &missingValue))
......@@ -20,20 +22,33 @@ static PyObject* interpolate_values(PyObject* self, PyObject* args)
return NULL;
}
// Parse the vector arguments
std::cout << "circular: " << circular << std::endl;
std::cout << "missingValue: " << missingValue << std::endl;
// std::cout << "converting array" << std::endl;
// PyObject* objectsRepresentation = PyObject_Repr(baseValuesObj);
// std::cout << "converting array 2" << std::endl;
// PyObject* str = PyUnicode_AsEncodedString(objectsRepresentation, "utf-8",
// "~E~"); std::cout << "converting array 3" << std::endl; const char *bytes =
// PyBytes_AS_STRING(str); std::cout << bytes << std::endl;
std::cout << "Parsing arguments" << std::endl;
// Parse the vector argument s
std::vector<double> baseValuesVec = pyObjectToDoubleVector(baseValuesObj);
std::cout << "Parsing arguments 2" << std::endl;
std::vector<double> valuesToInterpVec =
pyObjectToDoubleVector(valuesToInterpObj);
pyObjectToDoubleVector(valuesToInterpObj);
std::cout << "Carrying out interpolation" << std::endl;
// Carry out the interpolation
std::vector<double> interpolatedValuesVec =
radix::interpolateValues(baseValuesVec, valuesToInterpVec, circular,
missingValue);
std::vector<double> interpolatedValuesVec = radix::interpolateValues(
baseValuesVec, valuesToInterpVec, circular, missingValue);
std::cout << "Converting results" << std::endl;
// Convert the result back into a Python object
PyObject* interpolatedValuesObj =
doubleVectorToListPyObject(interpolatedValuesVec);
doubleVectorToListPyObject(interpolatedValuesVec);
std::cout << "Returning" << std::endl;
return interpolatedValuesObj;
}
......@@ -56,37 +71,39 @@ static PyObject* interpolate_to_other_base_values(PyObject* self,
// Parse the vector arguments
std::vector<double> baseValuesVec = pyObjectToDoubleVector(baseValuesObj);
std::vector<double> newBaseValuesVec =
pyObjectToDoubleVector(newBaseValuesObj);
pyObjectToDoubleVector(newBaseValuesObj);
std::vector<double> valuesToInterpVec =
pyObjectToDoubleVector(valuesToInterpObj);
pyObjectToDoubleVector(valuesToInterpObj);
// Carry out the interpolation
std::vector<double> interpolatedValuesVec =
radix::interpolateToOtherBaseValues(baseValuesVec, newBaseValuesVec,
valuesToInterpVec, circular,
missingValue);
radix::interpolateToOtherBaseValues(baseValuesVec, newBaseValuesVec,
valuesToInterpVec, circular,
missingValue);
// Convert the result back into a Python object
PyObject* interpolatedValuesObj =
doubleVectorToListPyObject(interpolatedValuesVec);
doubleVectorToListPyObject(interpolatedValuesVec);
return interpolatedValuesObj;
}
static PyMethodDef interpolate_methods[] = {
{"interpolate_values", interpolate_values, METH_VARARGS,
"Interpolate/extrapolate missing values from a data vector"},
{"interpolate_to_other_base_values", interpolate_to_other_base_values,
METH_VARARGS, "Interpolate/extrapolate missing values from a data vector to a new set of base values"},
{NULL, NULL, 0, NULL},
{"interpolate_values", interpolate_values, METH_VARARGS,
"Interpolate/extrapolate missing values from a data vector"},
{"interpolate_to_other_base_values", interpolate_to_other_base_values,
METH_VARARGS,
"Interpolate/extrapolate missing values from a data vector to a new set "
"of base values"},
{NULL, NULL, 0, NULL},
};
static struct PyModuleDef interpolate_definition = {
PyModuleDef_HEAD_INIT,
"interpolate",
"Python bindings for radixmath/interpolate methods",
-1,
interpolate_methods,
PyModuleDef_HEAD_INIT,
"interpolate",
"Python bindings for radixmath/interpolate methods",
-1,
interpolate_methods,
};
PyMODINIT_FUNC PyInit_interpolate(void)
......
Supports Markdown
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