|
From: <jd...@us...> - 2008-12-19 19:31:29
|
Revision: 6683
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6683&view=rev
Author: jdh2358
Date: 2008-12-19 19:31:23 +0000 (Fri, 19 Dec 2008)
Log Message:
-----------
fixed a min/max bug when initial values all nan
Modified Paths:
--------------
trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi
trunk/py4science/examples/pyrex/trailstats/movavg_fast.py
trunk/py4science/examples/pyrex/trailstats/ringbuf.h
trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx
trunk/py4science/examples/pyrex/trailstats/ringbufnan.c
trunk/py4science/examples/pyrex/trailstats/setup.py
Modified: trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi 2008-12-19 12:40:09 UTC (rev 6682)
+++ trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi 2008-12-19 19:31:23 UTC (rev 6683)
@@ -18,6 +18,7 @@
double ringbuf_min(ringbuf_t *rb_ptr)
double ringbuf_max(ringbuf_t *rb_ptr)
double ringbuf_median(ringbuf_t *rb_ptr)
+ double ringbuf_ptile(ringbuf_t *rb_ptr, double x)
int ringbuf_N_good(ringbuf_t *rb_ptr)
int ringbuf_N_added(ringbuf_t *rb_ptr)
int ringbuf_N_filled(ringbuf_t *rb_ptr)
@@ -28,8 +29,12 @@
double *dstd,
double *dmin,
double *dmax,
- double *dmed, int *ng)
+ double *dmed,
+ double *dptile5,
+ double *dptile95,
+ int *ng)
void c_runstats2(int nrb, int nd, int step, int ofs,
double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, int *ng)
+ double *dmin, double *dmax, double *dmed, double *dptile5,
+ double *dptile95, int *ng)
Modified: trunk/py4science/examples/pyrex/trailstats/movavg_fast.py
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/movavg_fast.py 2008-12-19 12:40:09 UTC (rev 6682)
+++ trunk/py4science/examples/pyrex/trailstats/movavg_fast.py 2008-12-19 19:31:23 UTC (rev 6683)
@@ -5,10 +5,10 @@
import ringbuf
x = numpy.random.rand(10000)
-dmean, dstd, dmin, dmax, dmedian, ng = ringbuf.runstats(x, 30)
+dmean, dstd, dmin, dmax, dmedian, ptile5, ptile95, ng = ringbuf.runstats(x, 30)
r = numpy.rec.fromarrays([dmean, dstd, dmin, dmax, dmedian, ng],
- names='dmean,dstd,dmin,dmax,dmedian,ngood')
+ names='dmean,dstd,dmin,dmax,dmedian,ptile5,ptile95,ngood')
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf.h
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf.h 2008-12-19 12:40:09 UTC (rev 6682)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf.h 2008-12-19 19:31:23 UTC (rev 6683)
@@ -25,6 +25,7 @@
double ringbuf_min(ringbuf_t *rb_ptr);
double ringbuf_max(ringbuf_t *rb_ptr);
double ringbuf_median(ringbuf_t *rb_ptr);
+double ringbuf_ptile(ringbuf_t *rb_ptr, double val);
int ringbuf_N_added(ringbuf_t *rb_ptr);
int ringbuf_N_filled(ringbuf_t *rb_ptr);
int ringbuf_N_good(ringbuf_t *rb_ptr);
@@ -32,10 +33,10 @@
double ringbuf_sd(ringbuf_t *rb_ptr);
void c_runstats(int nrb, int nd, double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, int *ng);
+ double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *ng);
void c_runstats2(int nrb, int nd, int step, int ofs,
double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, int *ng);
+ double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *ng);
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx 2008-12-19 12:40:09 UTC (rev 6682)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx 2008-12-19 19:31:23 UTC (rev 6683)
@@ -14,7 +14,7 @@
cdef class Ringbuf:
cdef ringbuf_t *rb_ptr
- def __new__(self, N):
+ def __cinit__(self, N):
self.rb_ptr = new_ringbuf(N)
def __dealloc__(self):
@@ -55,6 +55,9 @@
def median(self):
return ringbuf_median(self.rb_ptr)
+ def ptile(self, x):
+ return ringbuf_ptile(self.rb_ptr, x)
+
def N_added(self):
return ringbuf_N_added(self.rb_ptr)
@@ -73,7 +76,7 @@
cdef object records
- def __new__(self, N):
+ def __cinit__(self, N):
self.rb_ptr = new_ringbuf(N)
self.records = []
@@ -122,6 +125,9 @@
def median(self):
return ringbuf_median(self.rb_ptr)
+ def ptile(self, x):
+ return ringbuf_ptile(self.rb_ptr, x)
+
def N_added(self):
return ringbuf_N_added(self.rb_ptr)
@@ -160,6 +166,8 @@
cdef c_numpy.ndarray c_dmin
cdef c_numpy.ndarray c_dmax
cdef c_numpy.ndarray c_dmedian
+ cdef c_numpy.ndarray c_dptile5
+ cdef c_numpy.ndarray c_dptile95
cdef c_numpy.ndarray c_ng
# make sure that the input array is a 1D numpy array of floats.
@@ -181,6 +189,8 @@
dmin = numpy.empty_like(data)
dmax = numpy.empty_like(data)
dmedian = numpy.empty_like(data)
+ dptile5 = numpy.empty_like(data)
+ dptile95 = numpy.empty_like(data)
ng = numpy.empty(data.shape, dtype=numpy.int_)
# now we have to assign the c_data structures and friends to their
@@ -191,6 +201,8 @@
c_dmin = dmin
c_dmax = dmax
c_dmedian = dmedian
+ c_dptile5 = dptile5
+ c_dptile95 = dptile95
c_ng = ng
# now we call the function and pass in the c data pointers to the
@@ -202,7 +214,9 @@
<double *>c_dmin.data,
<double *>c_dmax.data,
<double *>c_dmedian.data,
+ <double *>c_dptile5.data,
+ <double *>c_dptile95.data,
<int *>c_ng.data)
# all done, return the arrays
- return dmean, dstd, dmin, dmax, dmedian, ng
+ return dmean, dstd, dmin, dmax, dmedian, dptile5, dptile95, ng
Modified: trunk/py4science/examples/pyrex/trailstats/ringbufnan.c
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbufnan.c 2008-12-19 12:40:09 UTC (rev 6682)
+++ trunk/py4science/examples/pyrex/trailstats/ringbufnan.c 2008-12-19 19:31:23 UTC (rev 6683)
@@ -121,7 +121,7 @@
d_old = rb_ptr->data[rb_ptr->i_oldest];
rb_ptr->data[i_new] = d;
good_new = !isnan(d);
-#if 0
+#if 1
printf("new value: %lf good_new: %d\n", d, good_new);
printf("i_next: %d i_oldest: %d N_filled: %d N_good: %d\n",
rb_ptr->i_next, rb_ptr->i_oldest,
@@ -183,7 +183,7 @@
{
resum_ringbuf(rb_ptr);
}
-#if 0
+#if 1
printf("i_next: %d i_oldest: %d N_filled: %d N_good: %d\n",
rb_ptr->i_next, rb_ptr->i_oldest,
rb_ptr->N_filled, rb_ptr->N_good);
@@ -221,13 +221,21 @@
double ringbuf_min(ringbuf_t *rb_ptr)
{
+ if (rb_ptr->N_good==0)
+ return NaN;
return rb_ptr->data[rb_ptr->i_sorted[0]];
}
double ringbuf_max(ringbuf_t *rb_ptr)
{
- int i_end;
+
+ int i_end;
+
+ if (rb_ptr->N_good==0)
+ return NaN;
+
i_end = rb_ptr->N_good - 1;
+
return rb_ptr->data[rb_ptr->i_sorted[i_end]];
}
@@ -248,6 +256,16 @@
}
}
+double ringbuf_ptile(ringbuf_t *rb_ptr, double ptile)
+{
+ int i, N;
+
+ N = rb_ptr->N_good;
+ if (N == 0) return NaN;
+ i = (int)(ptile*N);
+ return rb_ptr->data[rb_ptr->i_sorted[i]];
+}
+
int ringbuf_N_good(ringbuf_t *rb_ptr)
{
return rb_ptr->N_good;
@@ -272,6 +290,7 @@
{
int N;
+
N = rb_ptr->N_good;
if (N > 0)
{
@@ -286,6 +305,7 @@
double ringbuf_sd(ringbuf_t *rb_ptr)
{
double m, s;
+
int N;
N = rb_ptr->N_good;
@@ -304,7 +324,7 @@
}
void c_runstats(int nrb, int nd, double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, int *ng)
+ double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *ng)
{
int i, j;
ringbuf_t *rb_ptr;
@@ -319,6 +339,8 @@
dmin[j] = ringbuf_min(rb_ptr);
dmax[j] = ringbuf_max(rb_ptr);
dmed[j] = ringbuf_median(rb_ptr);
+ dptile5[j] = ringbuf_ptile(rb_ptr, 0.05);
+ dptile95[j] = ringbuf_ptile(rb_ptr, 0.95);
ng[j] = rb_ptr->N_good;
}
delete_ringbuf(rb_ptr);
@@ -327,7 +349,8 @@
void c_runstats2(int nrb, int nd, int step, int ofs,
double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, int *ng)
+ double *dmin, double *dmax, double *dmed, double
+*dptile5, double *dptile95, int *ng)
{
int i, j;
int npad = (nrb - 1) / 2;
@@ -339,6 +362,8 @@
dmin += ofs;
dmax += ofs;
dmed += ofs;
+ dptile5 += ofs;
+ dptile95 += ofs;
ng += ofs;
rb_ptr = new_ringbuf(nrb);
@@ -355,6 +380,8 @@
dmin[j*step] = ringbuf_min(rb_ptr);
dmax[j*step] = ringbuf_max(rb_ptr);
dmed[j*step] = ringbuf_median(rb_ptr);
+ dptile5[j*step] = ringbuf_ptile(rb_ptr, 0.05);
+ dptile95[j*step] = ringbuf_ptile(rb_ptr, 0.95);
ng[j*step] = rb_ptr->N_good;
}
delete_ringbuf(rb_ptr);
Modified: trunk/py4science/examples/pyrex/trailstats/setup.py
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/setup.py 2008-12-19 12:40:09 UTC (rev 6682)
+++ trunk/py4science/examples/pyrex/trailstats/setup.py 2008-12-19 19:31:23 UTC (rev 6683)
@@ -4,11 +4,8 @@
# Make this usable by people who don't have pyrex installed (I've committed
# the generated C sources to SVN).
-try:
- from Pyrex.Distutils import build_ext
- has_pyrex = True
-except ImportError:
- has_pyrex = False
+from Cython.Distutils import build_ext
+has_pyrex = True
import numpy
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jd...@us...> - 2008-12-19 20:27:23
|
Revision: 6684
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6684&view=rev
Author: jdh2358
Date: 2008-12-19 20:27:18 +0000 (Fri, 19 Dec 2008)
Log Message:
-----------
added ringbuf license
Modified Paths:
--------------
trunk/py4science/examples/pyrex/trailstats/ringbuf.h
trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx
trunk/py4science/examples/pyrex/trailstats/ringbufnan.c
Added Paths:
-----------
trunk/py4science/examples/pyrex/trailstats/LICENSE.ringbuf
Added: trunk/py4science/examples/pyrex/trailstats/LICENSE.ringbuf
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/LICENSE.ringbuf (rev 0)
+++ trunk/py4science/examples/pyrex/trailstats/LICENSE.ringbuf 2008-12-19 20:27:18 UTC (rev 6684)
@@ -0,0 +1,32 @@
+Copyright (c) 2008, Eric Firing
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+ * Neither the name of the copyright holder nor the names of
+ subsequent contributors may be used to endorse or promote
+ products derived from this software without specific prior
+ written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf.h
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf.h 2008-12-19 19:31:23 UTC (rev 6683)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf.h 2008-12-19 20:27:18 UTC (rev 6684)
@@ -1,5 +1,5 @@
+//See LICENSE.ringbuf for license (BSD)
-
typedef struct {
int N_size;
int N_filled;
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx 2008-12-19 19:31:23 UTC (rev 6683)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx 2008-12-19 20:27:18 UTC (rev 6684)
@@ -3,6 +3,8 @@
Ringbuf, on which various statistics are calculated as each entry is
added and a method runstats for computing a host of trailing
statistics over a numpy array
+
+See LICENSE.ringbuf for license (BSD)
"""
include "c_ringbuf.pxi"
Modified: trunk/py4science/examples/pyrex/trailstats/ringbufnan.c
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbufnan.c 2008-12-19 19:31:23 UTC (rev 6683)
+++ trunk/py4science/examples/pyrex/trailstats/ringbufnan.c 2008-12-19 20:27:18 UTC (rev 6684)
@@ -10,6 +10,7 @@
2003/07/28 EF
+See LICENSE.ringbuf for license (BSD)
*/
#include <stdlib.h>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jd...@us...> - 2008-12-22 16:50:15
|
Revision: 6692
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6692&view=rev
Author: jdh2358
Date: 2008-12-22 16:50:12 +0000 (Mon, 22 Dec 2008)
Log Message:
-----------
added nsorted attr
Modified Paths:
--------------
trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi
trunk/py4science/examples/pyrex/trailstats/movavg_fast.py
trunk/py4science/examples/pyrex/trailstats/movavg_ringbuf.py
trunk/py4science/examples/pyrex/trailstats/ringbuf.h
trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx
trunk/py4science/examples/pyrex/trailstats/ringbuf_demo.py
trunk/py4science/examples/pyrex/trailstats/ringbufnan.c
Modified: trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/c_ringbuf.pxi 2008-12-22 16:50:12 UTC (rev 6692)
@@ -13,7 +13,7 @@
ringbuf_t *new_ringbuf(int N)
void zero_ringbuf(ringbuf_t *rb_ptr)
void delete_ringbuf(ringbuf_t *rb_ptr)
- void ringbuf_add(ringbuf_t *rb_ptr, double d)
+ int ringbuf_add(ringbuf_t *rb_ptr, double d)
double ringbuf_getitem(ringbuf_t *rb_ptr, int i)
double ringbuf_min(ringbuf_t *rb_ptr)
double ringbuf_max(ringbuf_t *rb_ptr)
@@ -29,12 +29,12 @@
double *dstd,
double *dmin,
double *dmax,
- double *dmed,
- double *dptile5,
- double *dptile95,
+ double *dmed,
+ double *dptile5,
+ double *dptile95,
+ int *nsorted,
int *ng)
void c_runstats2(int nrb, int nd, int step, int ofs,
double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, double *dptile5,
- double *dptile95, int *ng)
-
+ double *dmin, double *dmax, double *dmed, double *dptile5,
+ double *dptile95, int *nsorted, int *ng)
Modified: trunk/py4science/examples/pyrex/trailstats/movavg_fast.py
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/movavg_fast.py 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/movavg_fast.py 2008-12-22 16:50:12 UTC (rev 6692)
@@ -5,10 +5,10 @@
import ringbuf
x = numpy.random.rand(10000)
-dmean, dstd, dmin, dmax, dmedian, ptile5, ptile95, ng = ringbuf.runstats(x, 30)
+dmean, dstd, dmin, dmax, dmedian, ptile5, ptile95, nsorted, ng = ringbuf.runstats(x, 30)
r = numpy.rec.fromarrays([dmean, dstd, dmin, dmax, dmedian, ng],
- names='dmean,dstd,dmin,dmax,dmedian,ptile5,ptile95,ngood')
+ names='dmean,dstd,dmin,dmax,dmedian,ptile5,ptile95,nsorted,ngood')
Modified: trunk/py4science/examples/pyrex/trailstats/movavg_ringbuf.py
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/movavg_ringbuf.py 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/movavg_ringbuf.py 2008-12-22 16:50:12 UTC (rev 6692)
@@ -9,8 +9,8 @@
data = []
for thisx in x:
- r.add(thisx)
- data.append([thisx, r.N_good(), r.min(), r.max(), r.mean(), r.sd()])
+ nsorted = r.add(thisx)
+ data.append([thisx, r.N_good(), nsorted,r.min(), r.max(), r.mean(), r.sd()])
-r = numpy.rec.fromarrays(data,names='x,ngood,min30,max30,mean30,sd30')
+r = numpy.rec.fromarrays(data,names='x,ngood,nsorted,min30,max30,mean30,sd30')
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf.h
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf.h 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf.h 2008-12-22 16:50:12 UTC (rev 6692)
@@ -21,7 +21,7 @@
int ringbuf_slice_i(ringbuf_t *rb_ptr, int i);
double ringbuf_getitem(ringbuf_t *rb_ptr, int i);
-void ringbuf_add(ringbuf_t *rb_ptr, double d);
+int ringbuf_add(ringbuf_t *rb_ptr, double d);
double ringbuf_min(ringbuf_t *rb_ptr);
double ringbuf_max(ringbuf_t *rb_ptr);
double ringbuf_median(ringbuf_t *rb_ptr);
@@ -33,10 +33,10 @@
double ringbuf_sd(ringbuf_t *rb_ptr);
void c_runstats(int nrb, int nd, double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *ng);
+ double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *nsorted, int *ng);
void c_runstats2(int nrb, int nd, int step, int ofs,
double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *ng);
+ double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *nsorted, int *ng);
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf.pyx 2008-12-22 16:50:12 UTC (rev 6692)
@@ -46,7 +46,7 @@
zero_ringbuf(self.rb_ptr)
def add(self, d):
- ringbuf_add(self.rb_ptr, d)
+ return ringbuf_add(self.rb_ptr, d)
def min(self):
return ringbuf_min(self.rb_ptr)
@@ -113,10 +113,11 @@
self.records = []
def add(self, d, r):
- ringbuf_add(self.rb_ptr, d)
+ ret = ringbuf_add(self.rb_ptr, d)
self.records.append(r)
if len(self.records) > self.rb_ptr.N_size:
del self.records[0]
+ return ret
def min(self):
return ringbuf_min(self.rb_ptr)
@@ -170,8 +171,10 @@
cdef c_numpy.ndarray c_dmedian
cdef c_numpy.ndarray c_dptile5
cdef c_numpy.ndarray c_dptile95
+ cdef c_numpy.ndarray c_nsorted
cdef c_numpy.ndarray c_ng
+
# make sure that the input array is a 1D numpy array of floats.
# asarray is used to copy and cast a python sequence or array to
# the approriate type, with the advantage that if the data is
@@ -193,6 +196,7 @@
dmedian = numpy.empty_like(data)
dptile5 = numpy.empty_like(data)
dptile95 = numpy.empty_like(data)
+ nsorted = numpy.empty(data.shape, dtype=numpy.int_)
ng = numpy.empty(data.shape, dtype=numpy.int_)
# now we have to assign the c_data structures and friends to their
@@ -205,8 +209,10 @@
c_dmedian = dmedian
c_dptile5 = dptile5
c_dptile95 = dptile95
+ c_nsorted = nsorted
c_ng = ng
+
# now we call the function and pass in the c data pointers to the
# arrays. The syntax <double *>c_data.data tells pyrex to pass
# the numpy data memory block as a pointer to a float array.
@@ -218,7 +224,8 @@
<double *>c_dmedian.data,
<double *>c_dptile5.data,
<double *>c_dptile95.data,
+ <int *>c_nsorted.data,
<int *>c_ng.data)
# all done, return the arrays
- return dmean, dstd, dmin, dmax, dmedian, dptile5, dptile95, ng
+ return dmean, dstd, dmin, dmax, dmedian, dptile5, dptile95, nsorted, ng
Modified: trunk/py4science/examples/pyrex/trailstats/ringbuf_demo.py
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbuf_demo.py 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/ringbuf_demo.py 2008-12-22 16:50:12 UTC (rev 6692)
@@ -1,14 +1,17 @@
"""
Example code showin how to use the ringbuf extension code from python
"""
+import numpy as np
import random
import ringbuf
r = ringbuf.Ringbuf(30)
for i in range(100):
- r.add(random.random())
- print 'Nadded=%d, Ngood=%d, min=%1.2f, max=%1.2f, mean=%1.2f, std=%1.2f'%(
- r.N_added(), r.N_good(), r.min(), r.max(), r.mean(), r.sd())
+ val = random.random()
+ indsorted = r.add(val)
+ ng = r.N_good()
+ percentile = indsorted/float(ng)
+ print 'val=%1.4f, Nadded=%d, Ngood=%d, Nsorted=%d, percentile=%1.4f, min=%1.2f, max=%1.2f, mean=%1.2f, std=%1.2f'%(val, r.N_added(), r.N_good(), indsorted, percentile, r.min(), r.max(), r.mean(), r.sd())
print 'slice', r[:10]
Modified: trunk/py4science/examples/pyrex/trailstats/ringbufnan.c
===================================================================
--- trunk/py4science/examples/pyrex/trailstats/ringbufnan.c 2008-12-22 16:16:58 UTC (rev 6691)
+++ trunk/py4science/examples/pyrex/trailstats/ringbufnan.c 2008-12-22 16:50:12 UTC (rev 6692)
@@ -20,7 +20,7 @@
static double NaN = 0.0;
-static void sort_ringbuf(ringbuf_t *rb_ptr);
+static int sort_ringbuf(ringbuf_t *rb_ptr);
static void resum_ringbuf(ringbuf_t *rb_ptr);
ringbuf_t *new_ringbuf(int N)
@@ -110,19 +110,21 @@
}
}
-void ringbuf_add(ringbuf_t *rb_ptr, double d)
+// return the index into the sorted ring buffer, -1 if no good points
+int ringbuf_add(ringbuf_t *rb_ptr, double d)
{
double d_old;
- int i, i_new, good_new, N;
+ int i, i_new, good_new, N, indsorted;
+ indsorted = -1;
N = rb_ptr->N_size; /* We need this many times. */
i_new = rb_ptr->i_next;
/* Save the old value; otherwise, it will be overwritten. */
d_old = rb_ptr->data[rb_ptr->i_oldest];
rb_ptr->data[i_new] = d;
good_new = !isnan(d);
-#if 1
+#if 0
printf("new value: %lf good_new: %d\n", d, good_new);
printf("i_next: %d i_oldest: %d N_filled: %d N_good: %d\n",
rb_ptr->i_next, rb_ptr->i_oldest,
@@ -174,7 +176,7 @@
{
rb_ptr->sum += d;
rb_ptr->sumsq += d*d;
- sort_ringbuf(rb_ptr);
+ indsorted = sort_ringbuf(rb_ptr);
}
/* To prevent accumulation of truncation error, we
recalculate the sums periodically.
@@ -184,11 +186,12 @@
{
resum_ringbuf(rb_ptr);
}
-#if 1
+#if 0
printf("i_next: %d i_oldest: %d N_filled: %d N_good: %d\n",
rb_ptr->i_next, rb_ptr->i_oldest,
rb_ptr->N_filled, rb_ptr->N_good);
#endif
+ return indsorted;
}
/* This is not a full sort--it assumes the list is
@@ -196,7 +199,7 @@
pass of bubble sorting to put that entry in its place.
The code could be moved bodily into the function above.
*/
-void sort_ringbuf(ringbuf_t *rb_ptr)
+int sort_ringbuf(ringbuf_t *rb_ptr)
{
int i, i_hold;
int *ip;
@@ -218,6 +221,7 @@
break;
}
}
+ return i;
}
double ringbuf_min(ringbuf_t *rb_ptr)
@@ -325,7 +329,7 @@
}
void c_runstats(int nrb, int nd, double *data, double *dmean, double *dstd,
- double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *ng)
+ double *dmin, double *dmax, double *dmed, double *dptile5, double *dptile95, int *nsorted, int *ng)
{
int i, j;
ringbuf_t *rb_ptr;
@@ -334,7 +338,7 @@
for (j=0; j<nd; i++, j++)
{
- ringbuf_add(rb_ptr, data[j]);
+ nsorted[j] = ringbuf_add(rb_ptr, data[j]);
dmean[j] = ringbuf_mean(rb_ptr);
dstd[j] = ringbuf_sd(rb_ptr);
dmin[j] = ringbuf_min(rb_ptr);
@@ -342,6 +346,7 @@
dmed[j] = ringbuf_median(rb_ptr);
dptile5[j] = ringbuf_ptile(rb_ptr, 0.05);
dptile95[j] = ringbuf_ptile(rb_ptr, 0.95);
+
ng[j] = rb_ptr->N_good;
}
delete_ringbuf(rb_ptr);
@@ -351,7 +356,7 @@
void c_runstats2(int nrb, int nd, int step, int ofs,
double *data, double *dmean, double *dstd,
double *dmin, double *dmax, double *dmed, double
-*dptile5, double *dptile95, int *ng)
+*dptile5, double *dptile95, int *nsorted, int *ng)
{
int i, j;
int npad = (nrb - 1) / 2;
@@ -365,17 +370,18 @@
dmed += ofs;
dptile5 += ofs;
dptile95 += ofs;
+ nsorted += ofs;
ng += ofs;
rb_ptr = new_ringbuf(nrb);
for (i = 0; i < npad; i++)
{
- ringbuf_add(rb_ptr, data[i*step]);
+ nsorted[j*step] = ringbuf_add(rb_ptr, data[i*step]);
}
for (j=0; j<nd; i++, j++)
{
- if (i < nd) {ringbuf_add(rb_ptr, data[i*step]);}
- else {ringbuf_add(rb_ptr, NaN);}
+ if (i < nd) {nsorted[j*step] = ringbuf_add(rb_ptr, data[i*step]);}
+ else {nsorted[j*step] = ringbuf_add(rb_ptr, NaN);}
dmean[j*step] = ringbuf_mean(rb_ptr);
dstd[j*step] = ringbuf_sd(rb_ptr);
dmin[j*step] = ringbuf_min(rb_ptr);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|