diff --git a/setup.py b/setup.py index ae39134..1df9cde 100644 --- a/setup.py +++ b/setup.py @@ -8,10 +8,10 @@ c_ext = setuptools.Extension("creader", include_dirs=[ np.get_include(),"src/" ], - extra_compile_args=['-std=c++11', '-Wall', '-Wextra'] ) + extra_compile_args=['-std=c99', '-Wall', '-Wextra'] ) -c_ext.language = 'c++' +c_ext.language = 'c' setuptools.setup( name= 'creader', version = '0.1', diff --git a/src/cluster_reader.c b/src/cluster_reader.c index c64961e..0d737fd 100644 --- a/src/cluster_reader.c +++ b/src/cluster_reader.c @@ -1,7 +1,7 @@ #include "cluster_reader.h" int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){ - printf("Item size: %d n_clusters: %d, 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); int iframe=0, nph=*n_left; size_t n_read=0, nph_read=0, nn=*n_left, nr=0; //n_left=n_clusters; @@ -39,7 +39,7 @@ 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); - printf("Read: %d items %d left %d\n", nph_read, n_read,*n_left); + printf("Read: %zu items %zu left %d\n", nph_read, n_read,*n_left); return nph_read; } diff --git a/src/creader_module.c b/src/creader_module.c index 31a75ab..f5477e0 100644 --- a/src/creader_module.c +++ b/src/creader_module.c @@ -67,26 +67,15 @@ static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args, return 0; } -// Close file on clearing the object -static int -ClusterFileReader_clear(ClusterFileReader *self) +// Custom destructor to make sure we close the file +static void +ClusterFileReader_dealloc(ClusterFileReader *self) { if(self->fp){ printf("Closing file\n"); fclose(self->fp); self->fp = NULL; - self->n_left=0; } - return 0; -} - -// Custom destructor, not sure this is needed -// might be an easier way to access the fclose on clear -static void -ClusterFileReader_dealloc(ClusterFileReader *self) -{ - PyObject_GC_UnTrack(self); - ClusterFileReader_clear(self); Py_TYPE(self)->tp_free((PyObject *) self); } @@ -106,6 +95,7 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args, //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 @@ -117,7 +107,22 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args, // Here goes the looping, removing frame numbers etc. self->n_read = read_clusters(self->fp, size, buf, &self->n_left); - //TODO! All kinds of error checking here!!! Resize array etc. + + if(self->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] = self->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; } @@ -137,9 +142,8 @@ static PyTypeObject ClusterFileReaderType = { .tp_doc = PyDoc_STR("ClusterFileReader implemented in C"), .tp_basicsize = sizeof(ClusterFileReader), .tp_itemsize = 0, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_new = PyType_GenericNew, - .tp_clear = (inquiry) ClusterFileReader_clear, .tp_dealloc = (destructor) ClusterFileReader_dealloc, .tp_init = (initproc)ClusterFileReader_init, .tp_methods = ClusterFileReader_methods, diff --git a/test.py b/test.py index acb5d95..4d5c44b 100644 --- a/test.py +++ b/test.py @@ -2,7 +2,13 @@ from creader import ClusterFileReader import numpy as np maxph=100000000 +maxph=100 + +from pathlib import Path +fpath = Path('/Users/erik/data/clusters/beam_En700eV_-40deg_300V_10us_d0_f0_100.clust') +# r = ClusterFileReader("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust") + +r = ClusterFileReader(fpath.as_posix()) -r = ClusterFileReader("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust") a=r.read(maxph) -print(a[::100000]) +# print(a[::100000])