|
|
|
@ -1,29 +1,15 @@
|
|
|
|
|
#include "ClusterReader.h"
|
|
|
|
|
#include "arr_desc.h"
|
|
|
|
|
#include "cluster_reader.h"
|
|
|
|
|
#include "data_types.h"
|
|
|
|
|
#include "arr_desc.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//clang-format off
|
|
|
|
|
typedef struct {
|
|
|
|
|
PyObject_HEAD
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int n_left;
|
|
|
|
|
} ClusterFileReader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//clang-format on
|
|
|
|
|
|
|
|
|
|
// Constructor: sets the fp to NULL then tries to open the file
|
|
|
|
|
// raises python exception if something goes wrong
|
|
|
|
@ -74,10 +60,9 @@ static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
|
|
|
|
|
|
|
|
|
// Create an uninitialized numpy array
|
|
|
|
|
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, cluster_dt());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fill with zeros
|
|
|
|
|
PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
|
|
|
|
0);
|
|
|
|
|
PyArray_FILLWBYTE((PyArrayObject *)clusters, 0);
|
|
|
|
|
|
|
|
|
|
// Get a pointer to the array memory
|
|
|
|
|
void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
|
|
|
@ -105,71 +90,6 @@ static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
|
|
|
|
return clusters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// // 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();
|
|
|
|
|
|
|
|
|
|
// 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 *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 (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);
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
// // Cluster *clusters = reinterpret_cast<Cluster *>(
|
|
|
|
|
// // PyArray_DATA(reinterpret_cast<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));
|
|
|
|
|
|
|
|
|
|
// // 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);
|
|
|
|
|
|
|
|
|
|
// if (nc != size) {
|
|
|
|
|
|
|
|
|
|
// PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return clustersA;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// List all methods in our ClusterFileReader class
|
|
|
|
|
static PyMethodDef ClusterFileReader_methods[] = {
|
|
|
|
|
{"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
|
|
|
@ -192,8 +112,7 @@ static PyTypeObject ClusterFileReaderType = {
|
|
|
|
|
.tp_methods = ClusterFileReader_methods,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void init_ClusterFileReader(PyObject *m){
|
|
|
|
|
|
|
|
|
|
PyObject *init_ClusterFileReader(PyObject *m) {
|
|
|
|
|
import_array();
|
|
|
|
|
if (PyType_Ready(&ClusterFileReaderType) < 0)
|
|
|
|
|
return NULL;
|
|
|
|
@ -205,4 +124,5 @@ void init_ClusterFileReader(PyObject *m){
|
|
|
|
|
Py_DECREF(m);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return m;
|
|
|
|
|
}
|