##// END OF EJS Templates
Update noise C extension to properly work with python 3
Juan C. Espinoza -
r1286:a59b24777aee
parent child
Show More
@@ -0,0 +1,82
1 #include <Python.h>
2 #include <numpy/arrayobject.h>
3 #include <math.h>
4
5
6 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) {
7 double navg;
8 PyObject *data_obj, *data_array;
9
10 if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) {
11 return NULL;
12 }
13
14 data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_IN_ARRAY);
15
16 if (data_array == NULL) {
17 Py_XDECREF(data_array);
18 Py_XDECREF(data_obj);
19 return NULL;
20 }
21 double *sortdata = (double*)PyArray_DATA(data_array);
22 int lenOfData = (int)PyArray_SIZE(data_array) ;
23 double nums_min = lenOfData*0.2;
24 if (nums_min <= 5) nums_min = 5;
25 double sump = 0;
26 double sumq = 0;
27 int j = 0;
28 int cont = 1;
29 double rtest = 0;
30 while ((cont == 1) && (j < lenOfData)) {
31 sump = sump + sortdata[j];
32 sumq = sumq + pow(sortdata[j], 2);
33 if (j > nums_min) {
34 rtest = (double)j/(j-1) + 1/navg;
35 if ((sumq*j) > (rtest*pow(sump, 2))) {
36 j = j - 1;
37 sump = sump - sortdata[j];
38 sumq = sumq - pow(sortdata[j],2);
39 cont = 0;
40 }
41 }
42 j = j + 1;
43 }
44
45 double lnoise = sump / j;
46
47 Py_DECREF(data_array);
48
49 return PyLong_FromLong(lnoise);
50 //return Py_BuildValue("d", lnoise);
51 }
52
53
54 static PyMethodDef noiseMethods[] = {
55 { "hildebrand_sekhon", hildebrand_sekhon, METH_VARARGS, "Get noise with hildebrand_sekhon algorithm" },
56 { NULL, NULL, 0, NULL }
57 };
58
59 #if PY_MAJOR_VERSION >= 3
60
61 static struct PyModuleDef noisemodule = {
62 PyModuleDef_HEAD_INIT,
63 "_noise",
64 "Get noise with hildebrand_sekhon algorithm",
65 -1,
66 noiseMethods
67 };
68
69 #endif
70
71 #if PY_MAJOR_VERSION >= 3
72 PyMODINIT_FUNC PyInit__noise(void) {
73 Py_Initialize();
74 import_array();
75 return PyModule_Create(&noisemodule);
76 }
77 #else
78 PyMODINIT_FUNC init_noise() {
79 Py_InitModule("_noise", noiseMethods);
80 import_array();
81 }
82 #endif
@@ -12,6 +12,7 import json
12 12 import schainpy.admin
13 13 from schainpy.utils import log
14 14 from .jroheaderIO import SystemHeader, RadarControllerHeader
15 from schainpy.model.data import _noise
15 16
16 17
17 18 def getNumpyDtype(dataTypeCode):
@@ -69,6 +70,7 def hildebrand_sekhon(data, navg):
69 70 """
70 71
71 72 sortdata = numpy.sort(data, axis=None)
73 '''
72 74 lenOfData = len(sortdata)
73 75 nums_min = lenOfData*0.2
74 76
@@ -98,8 +100,8 def hildebrand_sekhon(data, navg):
98 100 j += 1
99 101
100 102 lnoise = sump / j
101
102 return lnoise
103 '''
104 return _noise.hildebrand_sekhon(sortdata, navg)
103 105
104 106
105 107 class Beam:
@@ -2,6 +2,7
2 2 Created on Jul 16, 2014
3 3
4 4 @author: Miguel Urco
5 @author: Juan C. Espinoza
5 6 '''
6 7
7 8 import os
@@ -17,19 +18,20 class build_ext(_build_ext):
17 18 import numpy
18 19 self.include_dirs.append(numpy.get_include())
19 20
20 setup(name = "schainpy",
21 setup(
22 name = "schainpy",
21 23 version = __version__,
22 24 description = "Python tools to read, write and process Jicamarca data",
23 author = "Miguel Urco",
24 author_email = "miguel.urco@jro.igp.gob.pe",
25 url = "http://jro.igp.gob.pe",
26 packages = {'schainpy',
25 author = "Miguel Urco, Juan C. Espinoza",
26 author_email = "juan.espinoza@jro.igp.gob.pe",
27 url = "http://jro-dev.igp.gob.pe/rhodecode/schain",
28 packages = {
29 'schainpy',
27 30 'schainpy.model',
28 31 'schainpy.model.data',
29 32 'schainpy.model.graphics',
30 33 'schainpy.model.io',
31 34 'schainpy.model.proc',
32 'schainpy.model.serializer',
33 35 'schainpy.model.utils',
34 36 'schainpy.utils',
35 37 'schainpy.gui',
@@ -37,8 +39,8 setup(name = "schainpy",
37 39 'schainpy.gui.viewcontroller',
38 40 'schainpy.gui.viewer',
39 41 'schainpy.gui.viewer.windows',
40 'schainpy.cli'},
41 ext_package = 'schainpy',
42 'schainpy.cli',
43 },
42 44 package_data = {'': ['schain.conf.template'],
43 45 'schainpy.gui.figures': ['*.png', '*.jpg'],
44 46 'schainpy.files': ['*.oga']
@@ -51,7 +53,10 setup(name = "schainpy",
51 53 ],
52 54 },
53 55 cmdclass = {'build_ext': build_ext},
54 setup_requires = ["numpy >= 1.11.2"],
56 ext_modules=[
57 Extension("schainpy.model.data._noise", ["schainc/_noise.c"]),
58 ],
59 setup_requires = ["numpy"],
55 60 install_requires = [
56 61 "scipy",
57 62 "h5py",
General Comments 0
You need to be logged in to leave comments. Login now