Menu

[r4678]: / trunk / py4science / examples / pyrex / trailstats / README  Maximize  Restore  History

Download this file

83 lines (57 with data), 2.8 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
Introduction
============

This exercise introduces pyrex to wrap a C library for trailing
statistics.

Computation of trailing windowed statistics is common in many
quantitative data driven disciplines, particularly where there is
noisy data.  Common uses of windowed statistics are the trailing
moving average, standard deviation, minumum and maximum.  Two common
use cases which pose computational challenges for python: real time
updating of trailing statistics as live data comes in, and posthoc
computation of trailing statistics over a large data array.  In the
second case, for some statistics we can use convolution and related
techniques for efficient computation, eg of the trailing 30 sample
average

    numpy.convolve(x, numpy.ones(30), mode=valid')[:len(x)]

but for other statistics like the trailing 30 day maximum at each
point, efficient routines like convolution are of no help.

This exercise introduces pyrex to efficiently solve the problem of
trailing statistics over arrays as well as for a live, incoming data
stream. A pure C library, ringbuf, defines a circular C buffer and
attached methods for efficiently computing trailing averages, and
pyrex is used to provide a pythonic API on top of this extension code.
The rigid segregation between the C library and the python wrappers
insures that the C code can be used in other projects, be it a matlab
(TM) extension or some other C library.  The goal of the exericse is
to compute the trailing statistics mean, median, stddev, min and max
using three approaches:

  - with brute force using numpy arrays, slices and methods
    (movavg_bruteforce.py)

  - with python bindings to the ringbuf code ringbuf.Rinbuf
    (movavg_ringbuf.py).  See ringbuf_demo.py for an example of how to
    use the ringbuf module

  - using a pyrex extension to the ringbuf runstats code
    (movavg_fast.py)

pyrex module support
====================

  - Makefile : simple interface to setup.py so you can just 'make'

  - setup.py : configure and build the python modules

  - c_numpy.pxd : the numpy C API for pyrex

  - c_python.pxd : the python C API for pyrex

  - c_ringbuf.pxi : the ringbuf C API for pyrex

  - ringbuf.pyx : python interface to the ringbuf C API

examples
========

  - ringbuf_demo.py : basic demo of the python bindings to basic
    Ringbuf class

  - movavg_bruteforce.py : do the trailing stats with brute force
    numpy slices and methods

  - movavg_ringbuf.py : do the trailing stats with the Ringbuf code

  - movavg_fast.py : do the trailing stats with the ringbuf runstats wrapper

ringbuf C code
==============

  - ringbuf.h : pure C ringbuf API headers

  - ringbufnan.c : pure C ringbuf library

Acknowledgements
================

Thanks to Eric Firing for the ringbuf and runstats code!
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.