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.
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins