From 06b5e97111a43c13ba118b74e40801aa8fe2db6a Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 30 May 2023 16:53:01 +0200 Subject: [PATCH] cleanup --- src/creader_module.c | 196 +------------------------------------------ 1 file changed, 3 insertions(+), 193 deletions(-) diff --git a/src/creader_module.c b/src/creader_module.c index baebb3c..6fe04df 100644 --- a/src/creader_module.c +++ b/src/creader_module.c @@ -1,203 +1,13 @@ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #define PY_SSIZE_T_CLEAN -#include "cluster_reader.h" -#include "data_types.h" #include "ClusterReader.h" - #include -#include -#include - - - -// 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; -// } - - - -// // Constructor: sets the fp to NULL then tries to open the file -// // raises python exception if something goes wrong -// // returned object should mean file is open and ready to read -// static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args, -// PyObject *Py_UNUSED(kwds)) { - -// // Parse file name -// const char *fname = NULL; -// if (!PyArg_ParseTuple(args, "s", &fname)) -// return -1; - -// self->fp = fopen(fname, "rb"); -// self->n_left = 0; - -// // Raise python exception using information from errno -// if (self->fp == NULL) { -// PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname); -// return -1; -// } - -// // Success -// return 0; -// } - -// // Custom destructor to make sure we close the file -// static void ClusterFileReader_dealloc(ClusterFileReader *self) { -// if (self->fp) { -// #ifdef CR_VERBOSE -// printf("Closing file\n"); -// #endif -// fclose(self->fp); -// self->fp = NULL; -// } -// Py_TYPE(self)->tp_free((PyObject *)self); -// } - -// // read method -// static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args, -// PyObject *Py_UNUSED(kwds)) { - -// const int ndim = 1; -// Py_ssize_t size = 0; -// if (!PyArg_ParseTuple(args, "|n", &size)) -// return NULL; - -// 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); -// PyArray_FILLWBYTE((PyArrayObject *)clusters, -// 0); // zero initialization can be removed later - -// // Get a pointer to the array memory -// void *buf = PyArray_DATA((PyArrayObject *)clusters); - -// // Call the standalone C code to read clusters from file -// // Here goes the looping, removing frame numbers etc. -// int n_read = read_clusters(self->fp, size, buf, &self->n_left); - -// if (n_read != size) { -// // resize the array to match the number of read photons -// // this will reallocate memory - -// // create a new_shape struct on the stack -// PyArray_Dims new_shape; - -// // reuse dims for the shape -// dims[0] = n_read; -// new_shape.ptr = dims; -// new_shape.len = 1; - -// // resize the array to match the number of clusters read -// PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER); -// } - -// 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 *c_obj; -// if (!PyArg_ParseTuple(args, "O", &c_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); - -// // 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; -// } - -// 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( -// // PyArray_DATA(reinterpret_cast(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, -// "Read clusters"}, -// {"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS, -// "Analyze clusters"}, -// {NULL, NULL, 0, NULL} /* Sentinel */ -// }; - -// // Class defenition -// static PyTypeObject ClusterFileReaderType = { -// PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader", -// .tp_doc = PyDoc_STR("ClusterFileReader implemented in C"), -// .tp_basicsize = sizeof(ClusterFileReader), -// .tp_itemsize = 0, -// .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, -// .tp_new = PyType_GenericNew, -// .tp_dealloc = (destructor)ClusterFileReader_dealloc, -// .tp_init = (initproc)ClusterFileReader_init, -// .tp_methods = ClusterFileReader_methods, -// }; - -//------------------------------------------- Module stuff from here - -// Docstring +//Module docstring, shown as a part of help(creader) static char module_docstring[] = "C functions to read cluster files"; +//Module defenition static struct PyModuleDef creader_def = { PyModuleDef_HEAD_INIT, "creader", @@ -210,12 +20,12 @@ static struct PyModuleDef creader_def = { NULL // m_free }; +//Initialize module and add classes PyMODINIT_FUNC PyInit_creader(void) { PyObject *m = PyModule_Create(&creader_def); if (m == NULL) return NULL; init_ClusterFileReader(m); - return m; }