<commit_before>#include "Python.h"

#define PY_ARRAY_UNIQUE_SYMBOL pyopencv_ARRAY_API
#define NO_IMPORT_ARRAY
#include "arrayobject.h"

#include "ml.h"

#include "assert.h"
#include "common.h"
#include "except.h"

extern PyObject *NumpyConversionFailedError;


//#define DEBUG

#define SET_NUMPY_CONVERSION_ERROR(msg) SET_ERROR_STRING(NumpyConversionFailedError, msg)



void print_opencv_matrix(const CvMat* input) {
    float *dataPtr = input->data.fl;
    for (unsigned int r = 0; r < input->rows; r++) {
        for (unsigned int c = 0; c < input->cols; c++) {
            printf("%f ",*dataPtr);
            dataPtr++;
        }
        printf("\n");
    }
}


int parse_numpy_array_to_opencv(PyObject *array, void* out_address) {
    PY_ASSERT(array != NULL, "obj is null");
    PY_ASSERT(out_address != NULL, "address is null");
    
    if (!PyArray_Check(array)) {
        SET_NUMPY_CONVERSION_ERROR("array failed PyArray_Check()");
        return FAILURE;
    }
    if (!PyArray_ISFLOAT(array)) {
        SET_NUMPY_CONVERSION_ERROR("array is not of type float");
        return FAILURE;
    }
    
    PyArrayObject *real_array = (PyArrayObject *) array;
    int ndims = real_array->nd;
    
    if (ndims == 2) {
        int nrows = real_array->dimensions[0];
        int ncols = real_array->dimensions[1];
#ifdef DEBUG
        printf("%dx%d array\n", nrows, ncols);
#endif
        
        CvMat* cvMat = cvCreateMat(nrows, ncols, CV_32F);
#ifdef DEBUG
        printf("done cvCreateMat\n");
#endif
        
        for (unsigned int i=0; i < nrows; i++) {
            float* thisRowOut = cvMat->data.fl + ncols*i;
            for (unsigned int j=0; j < ncols; j++) {
                thisRowOut[j] = NP_ARRAY_DB_2D(real_array, i, j);
            }
        }
#ifdef DEBUG
        printf("parsed matrix:\n");
        print_opencv_matrix(cvMat);
#endif
        CvMat** out = (CvMat **) out_address;
        *out = cvMat;
        return SUCCESS;
    }
    else if (ndims == 1) {
        int sz = real_array->dimensions[0];
#ifdef DEBUG
        printf("%d element vector\n", sz);
#endif        

        CvMat* cvMat = cvCreateMat(ndims, 1, CV_32F);
        for (unsigned int i=0; i<sz; i++) {
            cvMat->data.fl[i] = NP_ARRAY_DB_1D(real_array, i);
        }
#ifdef DEBUG
        printf("parsed matrix:\n");
        print_opencv_matrix(cvMat);
#endif
        CvMat** out = (CvMat **) out_address;
        *out = cvMat;
        return SUCCESS;
    }
    else {
        SET_NUMPY_CONVERSION_ERROR("only support matrices, sorry!");
        return FAILURE;
    }
}

int parse_opencv_array_to_numpy(CvMat* cvmat, void* out_address) {
    PY_ASSERT(cvmat != NULL, "input is null");
    PY_ASSERT(out_address != NULL, "out address is null");

    PY_ASSERT(cvmat->type == CV_32F, "cvmat matrix is of wrong type");

    int nrows = cvmat->rows;
    int ncols = cvmat->cols;

    npy_intp dims[2] = {nrows, ncols};
    PyArrayObject* result = (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_DOUBLE);<commit_msg>Horrible bug in vector parsing.<commit_after>#include "Python.h"

#define PY_ARRAY_UNIQUE_SYMBOL pyopencv_ARRAY_API
#define NO_IMPORT_ARRAY
#include "arrayobject.h"

#include "ml.h"

#include "assert.h"
#include "common.h"
#include "except.h"

extern PyObject *NumpyConversionFailedError;


//#define DEBUG

#define SET_NUMPY_CONVERSION_ERROR(msg) SET_ERROR_STRING(NumpyConversionFailedError, msg)



void print_opencv_matrix(const CvMat* input) {
    float *dataPtr = input->data.fl;
    for (unsigned int r = 0; r < input->rows; r++) {
        for (unsigned int c = 0; c < input->cols; c++) {
            printf("%f ",*dataPtr);
            dataPtr++;
        }
        printf("\n");
    }
}


int parse_numpy_array_to_opencv(PyObject *array, void* out_address) {
    PY_ASSERT(array != NULL, "obj is null");
    PY_ASSERT(out_address != NULL, "address is null");
    
    if (!PyArray_Check(array)) {
        SET_NUMPY_CONVERSION_ERROR("array failed PyArray_Check()");
        return FAILURE;
    }
    if (!PyArray_ISFLOAT(array)) {
        SET_NUMPY_CONVERSION_ERROR("array is not of type float");
        return FAILURE;
    }
    
    PyArrayObject *real_array = (PyArrayObject *) array;
    int ndims = real_array->nd;
    
    if (ndims == 2) {
        int nrows = real_array->dimensions[0];
        int ncols = real_array->dimensions[1];
#ifdef DEBUG
        printf("%dx%d array\n", nrows, ncols);
#endif
        
        CvMat* cvMat = cvCreateMat(nrows, ncols, CV_32F);
#ifdef DEBUG
        printf("done cvCreateMat\n");
#endif
        
        for (unsigned int i=0; i < nrows; i++) {
            float* thisRowOut = cvMat->data.fl + ncols*i;
            for (unsigned int j=0; j < ncols; j++) {
                thisRowOut[j] = NP_ARRAY_DB_2D(real_array, i, j);
            }
        }
#ifdef DEBUG
        printf("parsed matrix:\n");
        print_opencv_matrix(cvMat);
#endif
        CvMat** out = (CvMat **) out_address;
        *out = cvMat;
        return SUCCESS;
    }
    else if (ndims == 1) {
        int sz = real_array->dimensions[0];
#ifdef DEBUG
        printf("%d element vector\n", sz);
#endif        

        CvMat* cvMat = cvCreateMat(sz, 1, CV_32F);
        for (unsigned int i=0; i<sz; i++) {
            cvMat->data.fl[i] = NP_ARRAY_DB_1D(real_array, i);
        }
#ifdef DEBUG
        printf("parsed matrix:\n");
        print_opencv_matrix(cvMat);
#endif
        CvMat** out = (CvMat **) out_address;
        *out = cvMat;
        return SUCCESS;
    }
    else {
        SET_NUMPY_CONVERSION_ERROR("only support matrices, sorry!");
        return FAILURE;
    }
}

int parse_opencv_array_to_numpy(CvMat* cvmat, void* out_address) {
    PY_ASSERT(cvmat != NULL, "input is null");
    PY_ASSERT(out_address != NULL, "out address is null");

    PY_ASSERT(cvmat->type == CV_32F, "cvmat matrix is of wrong type");

    int nrows = cvmat->rows;
    int ncols = cvmat->cols;

    npy_intp dims[2] = {nrows, ncols};
    PyArrayObject* result = (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_DOUBLE);
{'fraction_non_alphanumeric': 0.13180089300479578, 'fraction_numerical': 0.006945592855961634, 'mean_word_length': 2.6654545454545455, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6689956', 'n_tokens_mistral': 2164, 'n_tokens_neox': 2118, 'n_words': 489}