-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path_robustats.c
More file actions
158 lines (123 loc) · 4.4 KB
/
_robustats.c
File metadata and controls
158 lines (123 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdint.h>
#include <Python.h>
#include <numpy/arrayobject.h>
#include "robustats.h"
// Docstrings
static char module_docstring[] =
"This module provides an interface for calculating robust statistical estimators using C.";
static char weighted_median_docstring[] =
"Calculate the weighted median of a data sample with respective weights.";
static char medcouple_docstring[] =
"Calculate the medcouple of a data sample.";
static char mode_docstring[] =
"Calculate the mode of a data sample.";
// Available functions
static PyObject *robustats_weighted_median(PyObject *self, PyObject *args);
static PyObject *robustats_medcouple(PyObject *self, PyObject *args);
static PyObject *robustats_mode(PyObject *self, PyObject *args);
// Module specification
static PyMethodDef module_methods[] = {
{"weighted_median", (PyCFunction)robustats_weighted_median, METH_VARARGS, weighted_median_docstring},
{"medcouple", (PyCFunction)robustats_medcouple, METH_VARARGS, medcouple_docstring},
{"mode", (PyCFunction)robustats_mode, METH_VARARGS, mode_docstring},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef robustatsmodule = {
PyModuleDef_HEAD_INIT,
"_robustats", // Module name
module_docstring,
-1, // size of per-interpreter state of the module,
// or -1 if the module keeps state in global variables.
module_methods
};
// Initialize the module
PyMODINIT_FUNC PyInit__robustats(void)
{
PyObject *m;
m = PyModule_Create(&robustatsmodule);
if (m == NULL)
return NULL;
// Load Numpy functionality
import_array();
return m;
}
static PyObject *robustats_weighted_median(PyObject *self, PyObject *args)
{
PyObject *x_obj, *w_obj;
// Parse the input tuple
if (!PyArg_ParseTuple(args, "OO", &x_obj, &w_obj))
return NULL;
// Interpret the input objects as numpy arrays
PyObject *x_array = PyArray_FROM_OTF(x_obj, NPY_DOUBLE, NPY_IN_ARRAY);
PyObject *w_array = PyArray_FROM_OTF(w_obj, NPY_DOUBLE, NPY_IN_ARRAY);
// If that didn't work, throw an exception
if (x_array == NULL || w_array == NULL) {
Py_XDECREF(x_array);
Py_XDECREF(w_array);
return NULL;
}
// Number of data points
int64_t n = (int64_t)PyArray_DIM(x_array, 0);
// Get pointers to the data as C-types
double *x = (double*)PyArray_DATA(x_array);
double *w = (double*)PyArray_DATA(w_array);
// Call the external C function to compute the chi-squared
double value = weighted_median(x, w, 0, n - 1);
// Clean up
Py_DECREF(x_array);
Py_DECREF(w_array);
// Build the output tuple
PyObject *ret = Py_BuildValue("d", value);
return ret;
}
static PyObject *robustats_medcouple(PyObject *self, PyObject *args)
{
double epsilon1, epsilon2;
PyObject *x_obj;
// Parse the input tuple
if (!PyArg_ParseTuple(args, "Odd", &x_obj, &epsilon1, &epsilon2))
return NULL;
// Interpret the input objects as numpy arrays
PyObject *x_array = PyArray_FROM_OTF(x_obj, NPY_DOUBLE, NPY_IN_ARRAY);
// If that didn't work, throw an exception
if (x_array == NULL) {
Py_XDECREF(x_array);
return NULL;
}
// Number of data points
int64_t n = (int64_t)PyArray_DIM(x_array, 0);
// Get pointers to the data as C-types
double *x = (double*)PyArray_DATA(x_array);
// Call the external C function to compute the chi-squared
double value = medcouple(x, n, epsilon1, epsilon2);
// Clean up
Py_DECREF(x_array);
// Build the output tuple
PyObject *ret = Py_BuildValue("d", value);
return ret;
}
static PyObject *robustats_mode(PyObject *self, PyObject *args)
{
PyObject *x_obj;
// Parse the input tuple
if (!PyArg_ParseTuple(args, "O", &x_obj))
return NULL;
// Interpret the input objects as numpy arrays
PyObject *x_array = PyArray_FROM_OTF(x_obj, NPY_DOUBLE, NPY_IN_ARRAY);
// If that didn't work, throw an exception
if (x_array == NULL) {
Py_XDECREF(x_array);
return NULL;
}
// Number of data points
int64_t n = (int64_t)PyArray_DIM(x_array, 0);
// Get pointers to the data as C-types
double *x = (double*)PyArray_DATA(x_array);
// Call the external C function to compute the chi-squared
double value = mode(x, n);
// Clean up
Py_DECREF(x_array);
// Build the output tuple
PyObject *ret = Py_BuildValue("d", value);
return ret;
}