moved printouts behind CR_VERBOSE

This commit is contained in:
Erik Frojdh 2023-05-30 14:45:49 +02:00
parent 66195c0313
commit 6aa6673696
3 changed files with 103 additions and 121 deletions

View File

@ -3,6 +3,9 @@ default: ext
ext: ## [DEFAULT] build c extension in place ext: ## [DEFAULT] build c extension in place
python setup.py build_ext --inplace python setup.py build_ext --inplace
debug: ## Build extension with debug prints and assertions
python setup.py build_ext --inplace -UNDEBUG -DCR_VERBOSE
clean: ## Remove the build folder and the shared library clean: ## Remove the build folder and the shared library
rm -rf build/ creader.cpython* rm -rf build/ creader.cpython*

View File

@ -1,7 +1,9 @@
#include "cluster_reader.h" #include "cluster_reader.h"
int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){ int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){
#ifdef CR_VERBOSE
printf("Item size: %lu n_clusters: %lld, n_left: %d\n", sizeof(Cluster), n_clusters,*n_left); printf("Item size: %lu n_clusters: %lld, n_left: %d\n", sizeof(Cluster), n_clusters,*n_left);
#endif
int iframe=0, nph=*n_left; int iframe=0, nph=*n_left;
size_t n_read=0, nph_read=0, nn=*n_left, nr=0; size_t n_read=0, nph_read=0, nn=*n_left, nr=0;
//n_left=n_clusters; //n_left=n_clusters;
@ -39,7 +41,9 @@ int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){
} }
} }
// size_t n_read = fread(buf, sizeof(Cluster), n_clusters, fp); // size_t n_read = fread(buf, sizeof(Cluster), n_clusters, fp);
#ifdef CR_VERBOSE
printf("Read: %zu items %zu left %d\n", nph_read, n_read,*n_left); printf("Read: %zu items %zu left %d\n", nph_read, n_read,*n_left);
#endif
return nph_read; return nph_read;
} }

View File

@ -1,11 +1,13 @@
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_SSIZE_T_CLEAN #define PY_SSIZE_T_CLEAN
#include "cluster_reader.h"
#include "data_types.h"
#include <Python.h> #include <Python.h>
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
#include "data_types.h" #include <stdbool.h>
#include "cluster_reader.h"
// Create a custom numpy data type that should reflect // Create a custom numpy data type that should reflect
@ -14,9 +16,8 @@
static PyArray_Descr *cluster_dt() { static PyArray_Descr *cluster_dt() {
PyObject *dtype_dict; PyObject *dtype_dict;
PyArray_Descr *dtype; PyArray_Descr *dtype;
dtype_dict = Py_BuildValue( dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s, (i))]", "x", "u2", "y",
"[(s, s),(s, s),(s, s, (i))]", "u2", "data", "i4", 9);
"x", "u2", "y", "u2","data","i4",9 );
PyArray_DescrConverter(dtype_dict, &dtype); PyArray_DescrConverter(dtype_dict, &dtype);
Py_DECREF(dtype_dict); Py_DECREF(dtype_dict);
@ -26,30 +27,28 @@ static PyArray_Descr *cluster_dt() {
static PyArray_Descr *cluster_analysis_dt() { static PyArray_Descr *cluster_analysis_dt() {
PyObject *dtype_dict; PyObject *dtype_dict;
PyArray_Descr *dtype; PyArray_Descr *dtype;
dtype_dict = Py_BuildValue( dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
"[(s, s),(s, s),(s, s)]", "tot3", "i4","tot2","i4", "i4", "corner", "u4");
"corner", "u4");
PyArray_DescrConverter(dtype_dict, &dtype); PyArray_DescrConverter(dtype_dict, &dtype);
Py_DECREF(dtype_dict); Py_DECREF(dtype_dict);
return dtype; return dtype;
} }
// clang-format off
// Structure holding our cluster reader class // Structure holding our cluster reader class
typedef struct { typedef struct {
PyObject_HEAD FILE *fp; PyObject_HEAD
FILE *fp;
int n_left; int n_left;
int n_read;
} ClusterFileReader; } ClusterFileReader;
// clang-format on
// Constructor: sets the fp to NULL then tries to open the file // Constructor: sets the fp to NULL then tries to open the file
// raises python exception if something goes wrong // raises python exception if something goes wrong
// returned object should mean file is open and ready to read // returned object should mean file is open and ready to read
static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args, static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
PyObject *Py_UNUSED(kwds)) { PyObject *Py_UNUSED(kwds)) {
self->fp = NULL;
// Parse file name // Parse file name
const char *fname = NULL; const char *fname = NULL;
@ -70,23 +69,20 @@ static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
} }
// Custom destructor to make sure we close the file // Custom destructor to make sure we close the file
static void static void ClusterFileReader_dealloc(ClusterFileReader *self) {
ClusterFileReader_dealloc(ClusterFileReader *self)
{
if (self->fp) { if (self->fp) {
#ifdef CR_VERBOSE
printf("Closing file\n"); printf("Closing file\n");
#endif
fclose(self->fp); fclose(self->fp);
self->fp = NULL; self->fp = NULL;
} }
Py_TYPE(self)->tp_free((PyObject *)self); Py_TYPE(self)->tp_free((PyObject *)self);
} }
// read method // read method
static PyObject * static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
ClusterFileReader_read(ClusterFileReader *self, PyObject *args, PyObject *Py_UNUSED(kwds)) {
PyObject * Py_UNUSED(kwds))
{
const int ndim = 1; const int ndim = 1;
Py_ssize_t size = 0; Py_ssize_t size = 0;
@ -97,20 +93,20 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
// Create an uninitialized numpy array // Create an uninitialized numpy array
PyArray_Descr *dtype = cluster_dt(); PyArray_Descr *dtype = cluster_dt();
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims, PyArray_Descr *descr) // 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, dtype);
PyArray_FILLWBYTE((PyArrayObject *)clusters, 0); //zero initialization can be removed later PyArray_FILLWBYTE((PyArrayObject *)clusters,
0); // zero initialization can be removed later
// Get a pointer to the array memory // Get a pointer to the array memory
void *buf = PyArray_DATA((PyArrayObject *)clusters); void *buf = PyArray_DATA((PyArrayObject *)clusters);
// Call the standalone C code to read clusters from file // Call the standalone C code to read clusters from file
// Here goes the looping, removing frame numbers etc. // Here goes the looping, removing frame numbers etc.
self->n_read = read_clusters(self->fp, size, buf, &self->n_left); int n_read = read_clusters(self->fp, size, buf, &self->n_left);
if (n_read != size) {
if(self->n_read != size){
// resize the array to match the number of read photons // resize the array to match the number of read photons
// this will reallocate memory // this will reallocate memory
@ -118,7 +114,7 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
PyArray_Dims new_shape; PyArray_Dims new_shape;
// reuse dims for the shape // reuse dims for the shape
dims[0] = self->n_read; dims[0] = n_read;
new_shape.ptr = dims; new_shape.ptr = dims;
new_shape.len = 1; new_shape.len = 1;
@ -129,18 +125,10 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
return clusters; return clusters;
} }
// read method // read method
static PyObject * static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
ClusterFileReader_clusterize(ClusterFileReader *self, PyObject *args, PyObject *args,
PyObject * Py_UNUSED(kwds)) PyObject *Py_UNUSED(kwds)) {
{
// Create an uninitialized numpy array // Create an uninitialized numpy array
PyArray_Descr *dtypeIn = cluster_dt(); PyArray_Descr *dtypeIn = cluster_dt();
@ -154,7 +142,8 @@ ClusterFileReader_clusterize(ClusterFileReader *self, PyObject *args,
// use the underlying buffer, otherwise it will create a copy, for example // use the underlying buffer, otherwise it will create a copy, for example
// if data type is different or we pass in a list. The // if data type is different or we pass in a list. The
// NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory. // NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
PyObject *c_array =PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn, NPY_ARRAY_C_CONTIGUOUS); 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 parsing of a or b fails we throw an exception in Python
if (c_array == NULL) { if (c_array == NULL) {
@ -164,7 +153,6 @@ ClusterFileReader_clusterize(ClusterFileReader *self, PyObject *args,
return NULL; 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);
@ -173,17 +161,16 @@ ClusterFileReader_clusterize(ClusterFileReader *self, PyObject *args,
// printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis)); // printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
// dims[0]=size; // 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 *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims, PyArray_Descr *descr)
PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut); PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
//PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization can be removed later // PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
// can be removed later
npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA))); npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
// printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis)); // printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
@ -197,31 +184,21 @@ ClusterFileReader_clusterize(ClusterFileReader *self, PyObject *args,
if (nc != size) { if (nc != size) {
PyErr_SetString( PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
PyExc_TypeError,
"Parsed wrong size array!");
} }
return clustersA; return clustersA;
} }
// List all methods in our ClusterFileReader class // List all methods in our ClusterFileReader class
static PyMethodDef ClusterFileReader_methods[] = { static PyMethodDef ClusterFileReader_methods[] = {
{"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS, {"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
"Read clusters" "Read clusters"},
},
{"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS, {"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
"Analyze clusters" "Analyze clusters"},
},
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };
// Class defenition // Class defenition
static PyTypeObject ClusterFileReaderType = { static PyTypeObject ClusterFileReaderType = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader", PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader",
@ -238,11 +215,11 @@ static PyTypeObject ClusterFileReaderType = {
//------------------------------------------- Module stuff from here //------------------------------------------- Module stuff from here
// Docstring // Docstring
static char module_docstring[] = static char module_docstring[] = "C functions to read cluster files";
"C functions to read cluster files";
static struct PyModuleDef creader_def = { static struct PyModuleDef creader_def = {
PyModuleDef_HEAD_INIT, "creader", PyModuleDef_HEAD_INIT,
"creader",
module_docstring, module_docstring,
-1, -1,
ClusterFileReader_methods, ClusterFileReader_methods,
@ -252,7 +229,6 @@ static struct PyModuleDef creader_def = {
NULL // m_free NULL // m_free
}; };
PyMODINIT_FUNC PyInit_creader(void) { PyMODINIT_FUNC PyInit_creader(void) {
PyObject *m; PyObject *m;
if (PyType_Ready(&ClusterFileReaderType) < 0) if (PyType_Ready(&ClusterFileReaderType) < 0)
@ -272,4 +248,3 @@ PyMODINIT_FUNC PyInit_creader(void) {
} }
return m; return m;
} }