restructure data types
This commit is contained in:
@ -1,38 +1,27 @@
|
||||
#include "ClusterReader.h"
|
||||
#include "cluster_reader.h"
|
||||
#include "data_types.h"
|
||||
#include "arr_desc.h"
|
||||
|
||||
|
||||
#include <numpy/arrayobject.h>
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
FILE *fp;
|
||||
int n_left;
|
||||
} ClusterFileReader;
|
||||
|
||||
// Create a custom numpy data type that should reflect
|
||||
// our cluster data type.
|
||||
// TODO! Update with the actual cluster data type
|
||||
static PyArray_Descr *cluster_dt() {
|
||||
PyObject *dtype_dict;
|
||||
PyArray_Descr *dtype;
|
||||
dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s, (i))]", "x", "u2", "y",
|
||||
"u2", "data", "i4", 9);
|
||||
|
||||
PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
Py_DECREF(dtype_dict);
|
||||
return dtype;
|
||||
}
|
||||
|
||||
static PyArray_Descr *cluster_analysis_dt() {
|
||||
PyObject *dtype_dict;
|
||||
PyArray_Descr *dtype;
|
||||
dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
|
||||
"i4", "corner", "u4");
|
||||
// static PyArray_Descr *cluster_analysis_dt() {
|
||||
// PyObject *dtype_dict;
|
||||
// PyArray_Descr *dtype;
|
||||
// dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
|
||||
// "i4", "corner", "u4");
|
||||
|
||||
PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
Py_DECREF(dtype_dict);
|
||||
return dtype;
|
||||
}
|
||||
// PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
// Py_DECREF(dtype_dict);
|
||||
// return dtype;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
@ -84,12 +73,11 @@ static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
||||
npy_intp dims[] = {size};
|
||||
|
||||
// Create an uninitialized numpy array
|
||||
PyArray_Descr *dtype = cluster_dt();
|
||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// PyArray_Descr *descr)
|
||||
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
||||
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, cluster_dt());
|
||||
|
||||
// Fill with zeros
|
||||
PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
||||
0); // zero initialization can be removed later
|
||||
0);
|
||||
|
||||
// Get a pointer to the array memory
|
||||
void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
||||
@ -117,77 +105,77 @@ static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
||||
return clusters;
|
||||
}
|
||||
|
||||
// read method
|
||||
static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
||||
PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
// // read method
|
||||
// static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
||||
// PyObject *args,
|
||||
// PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
// Create an uninitialized numpy array
|
||||
PyArray_Descr *dtypeIn = cluster_dt();
|
||||
PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||
// // Create an uninitialized numpy array
|
||||
// PyArray_Descr *dtypeIn = cluster_dt();
|
||||
// PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||
|
||||
PyObject *c_obj;
|
||||
if (!PyArg_ParseTuple(args, "O", &c_obj))
|
||||
return NULL;
|
||||
// PyObject *cl_obj;
|
||||
// if (!PyArg_ParseTuple(args, "O", &cl_obj))
|
||||
// return NULL;
|
||||
|
||||
// Create two numpy arrays from the passed objects, if possible numpy will
|
||||
// use the underlying buffer, otherwise it will create a copy, for example
|
||||
// if data type is different or we pass in a list. The
|
||||
// NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
|
||||
PyObject *c_array = PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn,
|
||||
NPY_ARRAY_C_CONTIGUOUS);
|
||||
// // Create two numpy arrays from the passed objects, if possible numpy will
|
||||
// // use the underlying buffer, otherwise it will create a copy, for example
|
||||
// // if data type is different or we pass in a list. The
|
||||
// // // NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
|
||||
// PyObject *cl_array = PyArray_FromArray((PyArrayObject *)cl_obj, cluster_dt,
|
||||
// NPY_ARRAY_C_CONTIGUOUS);
|
||||
|
||||
// If parsing of a or b fails we throw an exception in Python
|
||||
if (c_array == NULL) {
|
||||
PyErr_SetString(
|
||||
PyExc_TypeError,
|
||||
"Could not convert one of the arguments to a numpy array.");
|
||||
return NULL;
|
||||
}
|
||||
// // If parsing of a or b fails we throw an exception in Python
|
||||
// if (cl_array == NULL) {
|
||||
// PyErr_SetString(
|
||||
// PyExc_TypeError,
|
||||
// "Could not convert one of the arguments to a numpy array.");
|
||||
// return NULL;
|
||||
// }
|
||||
|
||||
const int ndim = PyArray_NDIM((PyArrayObject *)c_array);
|
||||
// const int ndim = PyArray_NDIM((PyArrayObject *)c_array);
|
||||
|
||||
npy_intp *dims = PyArray_SHAPE((PyArrayObject *)c_array);
|
||||
// npy_intp *dims = PyArray_SHAPE((PyArrayObject *)c_array);
|
||||
|
||||
Py_ssize_t size = dims[0];
|
||||
// printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
||||
// dims[0]=size;
|
||||
// Py_ssize_t size = dims[0];
|
||||
// // printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
||||
// // dims[0]=size;
|
||||
|
||||
// Cluster *clusters = reinterpret_cast<Cluster *>(
|
||||
// PyArray_DATA(reinterpret_cast<PyArrayObject *>(c_array)));
|
||||
// // Cluster *clusters = reinterpret_cast<Cluster *>(
|
||||
// // PyArray_DATA(reinterpret_cast<PyArrayObject *>(c_array)));
|
||||
|
||||
Cluster *clusters = (Cluster *)(PyArray_DATA((PyArrayObject *)(c_array)));
|
||||
// Cluster *clusters = (Cluster *)(PyArray_DATA((PyArrayObject *)(c_array)));
|
||||
|
||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// PyArray_Descr *descr)
|
||||
PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
||||
// PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
||||
// can be removed later
|
||||
npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
||||
// printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
||||
// // PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// // PyArray_Descr *descr)
|
||||
// PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
||||
// // PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
||||
// // can be removed later
|
||||
// npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
||||
// // printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
||||
|
||||
// Get a pointer to the array memory
|
||||
ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
||||
// // Get a pointer to the array memory
|
||||
// ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
||||
|
||||
// Call the standalone C code to read clusters from file
|
||||
// Here goes the looping, removing frame numbers etc.
|
||||
int nc = analyze_clusters(size, clusters, buf);
|
||||
// printf("%d %d\n",nc,size);
|
||||
// // Call the standalone C code to read clusters from file
|
||||
// // Here goes the looping, removing frame numbers etc.
|
||||
// int nc = analyze_clusters(size, clusters, buf);
|
||||
// // printf("%d %d\n",nc,size);
|
||||
|
||||
if (nc != size) {
|
||||
// if (nc != size) {
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
||||
}
|
||||
// PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
||||
// }
|
||||
|
||||
return clustersA;
|
||||
}
|
||||
// return clustersA;
|
||||
// }
|
||||
|
||||
// List all methods in our ClusterFileReader class
|
||||
static PyMethodDef ClusterFileReader_methods[] = {
|
||||
{"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
||||
"Read clusters"},
|
||||
{"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
||||
"Analyze clusters"},
|
||||
// {"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
||||
// "Analyze clusters"},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
@ -205,6 +193,7 @@ static PyTypeObject ClusterFileReaderType = {
|
||||
};
|
||||
|
||||
void init_ClusterFileReader(PyObject *m){
|
||||
|
||||
import_array();
|
||||
if (PyType_Ready(&ClusterFileReaderType) < 0)
|
||||
return NULL;
|
||||
|
@ -1,5 +1,7 @@
|
||||
# pragma once
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include <Python.h>
|
||||
|
||||
#include <numpy/arrayobject.h>
|
||||
|
||||
void init_ClusterFileReader(PyObject *m);
|
25
src/arr_desc.c
Normal file
25
src/arr_desc.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include "arr_desc.h"
|
||||
|
||||
PyArray_Descr* cluster_dt(){
|
||||
import_array();
|
||||
PyObject *dict;
|
||||
PyArray_Descr *dtype = NULL;
|
||||
dict = Py_BuildValue("[(s, s),(s, s),(s, s, (i))]", "x", "u2", "y",
|
||||
"u2", "data", "i4", 9);
|
||||
// return dict;
|
||||
PyArray_DescrConverter(dict, &dtype);
|
||||
Py_DECREF(dict);
|
||||
return dtype;
|
||||
}
|
||||
|
||||
PyArray_Descr *cluster_analysis_dt() {
|
||||
import_array(); //TODO! Correct placement for this?
|
||||
PyObject *dict;
|
||||
PyArray_Descr *dtype;
|
||||
dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
|
||||
"i4", "corner", "u4");
|
||||
|
||||
PyArray_DescrConverter(dict, &dtype);
|
||||
Py_DECREF(dict);
|
||||
return dtype;
|
||||
}
|
8
src/arr_desc.h
Normal file
8
src/arr_desc.h
Normal file
@ -0,0 +1,8 @@
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include <Python.h>
|
||||
#include <numpy/arrayobject.h>
|
||||
|
||||
PyArray_Descr* cluster_dt();
|
||||
|
||||
PyArray_Descr* cluster_analysis_dt();
|
@ -1,19 +1,89 @@
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
|
||||
#include "ClusterReader.h"
|
||||
#include <Python.h>
|
||||
#include <numpy/arrayobject.h>
|
||||
|
||||
#include "arr_desc.h"
|
||||
#include "data_types.h"
|
||||
#include "ClusterReader.h"
|
||||
|
||||
|
||||
static PyObject *clusterize(PyObject *self, PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
// // Create an uninitialized numpy array
|
||||
// PyArray_Descr *dtypeIn = cluster_dt();
|
||||
// PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||
|
||||
PyObject *cl_obj;
|
||||
if (!PyArg_ParseTuple(args, "O", &cl_obj))
|
||||
return NULL;
|
||||
|
||||
if (cluster_dt==NULL){
|
||||
printf("BYE\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Create a numpy array from the passed object, if possible numpy will
|
||||
// use the underlying buffer, otherwise it will create a copy, for example
|
||||
// if data type is different or we pass in a list. The
|
||||
// NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
|
||||
// function steals a reference to the data type so no need to deallocate
|
||||
PyObject *cl_array = PyArray_FromArray((PyArrayObject *)cl_obj, cluster_dt(),
|
||||
NPY_ARRAY_C_CONTIGUOUS);
|
||||
if (cl_array == NULL) {
|
||||
PyErr_SetString(
|
||||
PyExc_TypeError,
|
||||
"Could not convert first argument to numpy array.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int ndim = PyArray_NDIM((PyArrayObject *)cl_array);
|
||||
npy_intp *dims = PyArray_SHAPE((PyArrayObject *)cl_array);
|
||||
Py_ssize_t size = dims[0];
|
||||
|
||||
|
||||
Cluster *clusters = (Cluster *)(PyArray_DATA((PyArrayObject *)(cl_array)));
|
||||
PyObject *cl_analysis = PyArray_SimpleNewFromDescr(ndim, dims, cluster_analysis_dt());
|
||||
PyArray_FILLWBYTE((PyArrayObject *)cl_analysis, 0); //zero initialization
|
||||
|
||||
// // Get a pointer to the array memory
|
||||
ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)cl_analysis);
|
||||
|
||||
|
||||
int nc = analyze_clusters(size, clusters, buf);
|
||||
if (nc != size) {
|
||||
PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
||||
}
|
||||
return cl_analysis;
|
||||
}
|
||||
|
||||
|
||||
static PyObject *get_cluster_dt(PyObject *self, PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
return cluster_dt();
|
||||
}
|
||||
|
||||
//Module docstring, shown as a part of help(creader)
|
||||
static char module_docstring[] = "C functions to read cluster files";
|
||||
|
||||
//Module methods
|
||||
static PyMethodDef creader_methods[] = {
|
||||
{"clusterize", clusterize, METH_VARARGS,
|
||||
"Do some stuff"},
|
||||
{"cluster_dt", get_cluster_dt, METH_VARARGS,
|
||||
"Do some stuff"},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
|
||||
//Module defenition
|
||||
static struct PyModuleDef creader_def = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"creader",
|
||||
module_docstring,
|
||||
-1,
|
||||
NULL, // m_methods
|
||||
creader_methods, // m_methods
|
||||
NULL, // m_slots
|
||||
NULL, // m_traverse
|
||||
NULL, // m_clear
|
||||
@ -22,9 +92,12 @@ static struct PyModuleDef creader_def = {
|
||||
|
||||
//Initialize module and add classes
|
||||
PyMODINIT_FUNC PyInit_creader(void) {
|
||||
|
||||
|
||||
PyObject *m = PyModule_Create(&creader_def);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
import_array();
|
||||
|
||||
init_ClusterFileReader(m);
|
||||
return m;
|
||||
|
Reference in New Issue
Block a user