added make test and cleanup

This commit is contained in:
Erik Frojdh 2023-05-31 13:47:24 +02:00
parent 05822e1c0c
commit 6013f94d01
5 changed files with 15 additions and 91 deletions

View File

@ -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}'

View File

@ -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;
}

View File

@ -1,7 +1,7 @@
# pragma once
#pragma once
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <numpy/arrayobject.h>
void init_ClusterFileReader(PyObject *m);
PyObject* init_ClusterFileReader(PyObject *m);

View File

@ -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;

View File

@ -100,6 +100,6 @@ PyMODINIT_FUNC PyInit_creader(void) {
return NULL;
import_array();
init_ClusterFileReader(m);
m = init_ClusterFileReader(m);
return m;
}