From 6013f94d01127236f1375f38601a35eb9d8954d6 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Wed, 31 May 2023 13:47:24 +0200 Subject: [PATCH] added make test and cleanup --- Makefile | 3 ++ src/ClusterReader.c | 94 ++++---------------------------------------- src/ClusterReader.h | 4 +- src/arr_desc.c | 3 +- src/creader_module.c | 2 +- 5 files changed, 15 insertions(+), 91 deletions(-) diff --git a/Makefile b/Makefile index 4c745b5..ec3bd70 100644 --- a/Makefile +++ b/Makefile @@ -9,5 +9,8 @@ debug: ## Build extension with debug prints and assertions clean: ## Remove the build folder and the shared library rm -rf build/ creader.cpython* +test: ## Run unit tests using pytest + python -m pytest + help: # from compiler explorer @grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' \ No newline at end of file diff --git a/src/ClusterReader.c b/src/ClusterReader.c index 8ecb336..db9be16 100644 --- a/src/ClusterReader.c +++ b/src/ClusterReader.c @@ -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( -// // 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, @@ -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; } \ No newline at end of file diff --git a/src/ClusterReader.h b/src/ClusterReader.h index 54f3a91..d6a7be2 100644 --- a/src/ClusterReader.h +++ b/src/ClusterReader.h @@ -1,7 +1,7 @@ -# pragma once +#pragma once #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #define PY_SSIZE_T_CLEAN #include #include -void init_ClusterFileReader(PyObject *m); \ No newline at end of file +PyObject* init_ClusterFileReader(PyObject *m); \ No newline at end of file diff --git a/src/arr_desc.c b/src/arr_desc.c index ee45234..8186d36 100644 --- a/src/arr_desc.c +++ b/src/arr_desc.c @@ -1,17 +1,18 @@ #include "arr_desc.h" +// Numpy data type holding a cluster 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; } +// Numpy data type holding the result of a cluster analysis PyArray_Descr *cluster_analysis_dt() { import_array(); //TODO! Correct placement for this? PyObject *dict; diff --git a/src/creader_module.c b/src/creader_module.c index caae5b3..8339288 100644 --- a/src/creader_module.c +++ b/src/creader_module.c @@ -100,6 +100,6 @@ PyMODINIT_FUNC PyInit_creader(void) { return NULL; import_array(); - init_ClusterFileReader(m); + m = init_ClusterFileReader(m); return m; }