##// 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 import schainpy.admin
12 import schainpy.admin
13 from schainpy.utils import log
13 from schainpy.utils import log
14 from .jroheaderIO import SystemHeader, RadarControllerHeader
14 from .jroheaderIO import SystemHeader, RadarControllerHeader
15 from schainpy.model.data import _noise
15
16
16
17
17 def getNumpyDtype(dataTypeCode):
18 def getNumpyDtype(dataTypeCode):
@@ -69,6 +70,7 def hildebrand_sekhon(data, navg):
69 """
70 """
70
71
71 sortdata = numpy.sort(data, axis=None)
72 sortdata = numpy.sort(data, axis=None)
73 '''
72 lenOfData = len(sortdata)
74 lenOfData = len(sortdata)
73 nums_min = lenOfData*0.2
75 nums_min = lenOfData*0.2
74
76
@@ -98,8 +100,8 def hildebrand_sekhon(data, navg):
98 j += 1
100 j += 1
99
101
100 lnoise = sump / j
102 lnoise = sump / j
101
103 '''
102 return lnoise
104 return _noise.hildebrand_sekhon(sortdata, navg)
103
105
104
106
105 class Beam:
107 class Beam:
@@ -2,6 +2,7
2 Created on Jul 16, 2014
2 Created on Jul 16, 2014
3
3
4 @author: Miguel Urco
4 @author: Miguel Urco
5 @author: Juan C. Espinoza
5 '''
6 '''
6
7
7 import os
8 import os
@@ -17,47 +18,51 class build_ext(_build_ext):
17 import numpy
18 import numpy
18 self.include_dirs.append(numpy.get_include())
19 self.include_dirs.append(numpy.get_include())
19
20
20 setup(name = "schainpy",
21 setup(
21 version = __version__,
22 name = "schainpy",
22 description = "Python tools to read, write and process Jicamarca data",
23 version = __version__,
23 author = "Miguel Urco",
24 description = "Python tools to read, write and process Jicamarca data",
24 author_email = "miguel.urco@jro.igp.gob.pe",
25 author = "Miguel Urco, Juan C. Espinoza",
25 url = "http://jro.igp.gob.pe",
26 author_email = "juan.espinoza@jro.igp.gob.pe",
26 packages = {'schainpy',
27 url = "http://jro-dev.igp.gob.pe/rhodecode/schain",
27 'schainpy.model',
28 packages = {
28 'schainpy.model.data',
29 'schainpy',
29 'schainpy.model.graphics',
30 'schainpy.model',
30 'schainpy.model.io',
31 'schainpy.model.data',
31 'schainpy.model.proc',
32 'schainpy.model.graphics',
32 'schainpy.model.serializer',
33 'schainpy.model.io',
33 'schainpy.model.utils',
34 'schainpy.model.proc',
34 'schainpy.utils',
35 'schainpy.model.utils',
35 'schainpy.gui',
36 'schainpy.utils',
36 'schainpy.gui.figures',
37 'schainpy.gui',
37 'schainpy.gui.viewcontroller',
38 'schainpy.gui.figures',
38 'schainpy.gui.viewer',
39 'schainpy.gui.viewcontroller',
39 'schainpy.gui.viewer.windows',
40 'schainpy.gui.viewer',
40 'schainpy.cli'},
41 'schainpy.gui.viewer.windows',
41 ext_package = 'schainpy',
42 'schainpy.cli',
42 package_data = {'': ['schain.conf.template'],
43 },
43 'schainpy.gui.figures': ['*.png', '*.jpg'],
44 package_data = {'': ['schain.conf.template'],
44 'schainpy.files': ['*.oga']
45 'schainpy.gui.figures': ['*.png', '*.jpg'],
45 },
46 'schainpy.files': ['*.oga']
46 include_package_data = False,
47 },
47 scripts = ['schainpy/gui/schainGUI'],
48 include_package_data = False,
48 entry_points = {
49 scripts = ['schainpy/gui/schainGUI'],
49 'console_scripts': [
50 entry_points = {
50 'schain = schainpy.cli.cli:main',
51 'console_scripts': [
51 ],
52 'schain = schainpy.cli.cli:main',
52 },
53 ],
53 cmdclass = {'build_ext': build_ext},
54 },
54 setup_requires = ["numpy >= 1.11.2"],
55 cmdclass = {'build_ext': build_ext},
55 install_requires = [
56 ext_modules=[
56 "scipy",
57 Extension("schainpy.model.data._noise", ["schainc/_noise.c"]),
57 "h5py",
58 ],
58 "matplotlib",
59 setup_requires = ["numpy"],
59 "pyzmq",
60 install_requires = [
60 "fuzzywuzzy",
61 "scipy",
61 "click",
62 "h5py",
62 ],
63 "matplotlib",
64 "pyzmq",
65 "fuzzywuzzy",
66 "click",
67 ],
63 )
68 )
General Comments 0
You need to be logged in to leave comments. Login now