[Scipy-svn] r3474 - in trunk/scipy/sandbox/timeseries: include lib src

scipy-svn@scip... scipy-svn@scip...
Tue Oct 30 17:44:03 CDT 2007


Author: mattknox_ca
Date: 2007-10-30 17:43:58 -0500 (Tue, 30 Oct 2007)
New Revision: 3474

Modified:
   trunk/scipy/sandbox/timeseries/include/c_tseries.h
   trunk/scipy/sandbox/timeseries/lib/moving_funcs.py
   trunk/scipy/sandbox/timeseries/src/c_tseries.c
   trunk/scipy/sandbox/timeseries/src/cseries.c
Log:
added mov_max and mov_min functions to moving_funcs

Modified: trunk/scipy/sandbox/timeseries/include/c_tseries.h
===================================================================
--- trunk/scipy/sandbox/timeseries/include/c_tseries.h	2007-10-29 15:18:13 UTC (rev 3473)
+++ trunk/scipy/sandbox/timeseries/include/c_tseries.h	2007-10-30 22:43:58 UTC (rev 3474)
@@ -7,6 +7,8 @@
 
 PyObject *MaskedArray_mov_sum(PyObject *, PyObject *, PyObject *);
 PyObject *MaskedArray_mov_median(PyObject *, PyObject *, PyObject *);
+PyObject *MaskedArray_mov_min(PyObject *, PyObject *, PyObject *);
+PyObject *MaskedArray_mov_max(PyObject *, PyObject *, PyObject *);
 PyObject *MaskedArray_mov_average(PyObject *, PyObject *, PyObject *);
 PyObject *MaskedArray_mov_stddev(PyObject *, PyObject *, PyObject *);
 

Modified: trunk/scipy/sandbox/timeseries/lib/moving_funcs.py
===================================================================
--- trunk/scipy/sandbox/timeseries/lib/moving_funcs.py	2007-10-29 15:18:13 UTC (rev 3473)
+++ trunk/scipy/sandbox/timeseries/lib/moving_funcs.py	2007-10-30 22:43:58 UTC (rev 3474)
@@ -11,6 +11,12 @@
 __revision__ = "$Revision: 2819 $"
 __date__     = '$Date: 2007-03-03 18:00:20 -0500 (Sat, 03 Mar 2007) $'
 
+__all__ = ['mov_sum', 'mov_median', 'mov_min', 'mov_max',
+           'mov_average', 'mov_mean', 'mov_average_expw',
+           'mov_stddev', 'mov_var', 'mov_covar', 'mov_corr',
+           'cmov_average', 'cmov_mean', 'cmov_window'
+           ]
+
 import numpy as N
 from numpy import bool_, float_
 narray = N.array
@@ -22,14 +28,8 @@
 marray = MA.array
 
 from timeseries.cseries import MA_mov_stddev, MA_mov_sum, MA_mov_average, \
-                               MA_mov_median
+                               MA_mov_median, MA_mov_min, MA_mov_max
 
-__all__ = ['mov_sum', 'mov_median',
-           'mov_average', 'mov_mean', 'mov_average_expw',
-           'mov_stddev', 'mov_var', 'mov_covar', 'mov_corr',
-           'cmov_average', 'cmov_mean', 'cmov_window'
-           ]
-
 def _process_result_dict(orig_data, result_dict):
     "process the results from the c function"
 
@@ -101,6 +101,34 @@
 
     return _moving_func(data, MA_mov_median, kwargs)
 #...............................................................................
+def mov_min(data, span, dtype=None):
+    """Calculates the moving minimum of a series.
+
+*Parameters*:
+    $$data$$
+    $$span$$
+    $$dtype$$"""
+
+    kwargs = {'span':span}
+    if dtype is not None:
+        kwargs['dtype'] = dtype
+
+    return _moving_func(data, MA_mov_min, kwargs)
+#...............................................................................
+def mov_max(data, span, dtype=None):
+    """Calculates the moving max of a series.
+
+*Parameters*:
+    $$data$$
+    $$span$$
+    $$dtype$$"""
+
+    kwargs = {'span':span}
+    if dtype is not None:
+        kwargs['dtype'] = dtype
+
+    return _moving_func(data, MA_mov_max, kwargs)
+#...............................................................................
 def mov_average(data, span, dtype=None):
     """Calculates the moving average of a series.
 

Modified: trunk/scipy/sandbox/timeseries/src/c_tseries.c
===================================================================
--- trunk/scipy/sandbox/timeseries/src/c_tseries.c	2007-10-29 15:18:13 UTC (rev 3473)
+++ trunk/scipy/sandbox/timeseries/src/c_tseries.c	2007-10-30 22:43:58 UTC (rev 3474)
@@ -552,17 +552,10 @@
 }
 
 
-/* computation portion of moving median. Appropriate mask is overlayed on top
-   afterwards.
 
-   The algorithm used here is based on the code found at:
-    http://cran.r-project.org/src/contrib/Devel/runStat_1.1.tar.gz
-
-   This code was originally released under the GPL, but the author
-   (David Brahm) has granted me (and scipy) permission to use it under the BSD
-   license. */
+//calc_mov_median(PyArrayObject *orig_ndarray, int span, int rtype)
 PyObject*
-calc_mov_median(PyArrayObject *orig_ndarray, int span, int rtype)
+calc_mov_ranked(PyArrayObject *orig_ndarray, int span, int rtype, char rank_type)
 {
     PyArrayObject *result_ndarray=NULL;
     PyObject **result_array, **ref_array, **even_array=NULL;
@@ -604,13 +597,29 @@
             r[i] = 1;
         }
 
-        if ((span % 2) == 0) {
+        if (rank_type == 'E' && ((span % 2) == 0)) {
             // array to store two median values when span is an even #
             even_array = calloc(2, sizeof(PyObject*));
             MEM_CHECK(even_array)
         }
 
-        R = (span + 1)/2;
+		switch(rank_type) {
+			case 'E': // median
+				R = (span + 1)/2;
+				break;
+			case 'I': // min
+				R = 1;
+				break;
+			case 'A': // max
+				R = span;
+				break;
+			default:
+			{
+				PyErr_SetString(PyExc_RuntimeError, "unexpected rank type");
+		        return NULL;
+			}
+		}
+
         one_half = PyFloat_FromDouble(0.5);
 
         z = arr_size - span;
@@ -751,8 +760,8 @@
         rtype = _get_type_num(((PyArrayObject*)orig_ndarray)->descr, dtype);
     }
 
-    result_ndarray = calc_mov_median((PyArrayObject*)orig_ndarray,
-                                     span, rtype);
+    result_ndarray = calc_mov_ranked((PyArrayObject*)orig_ndarray,
+                                     span, rtype, 'E');
     ERR_CHECK(result_ndarray)
 
     result_dict = PyDict_New();
@@ -765,8 +774,77 @@
     return result_dict;
 }
 
+PyObject *
+MaskedArray_mov_min(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *orig_arrayobj=NULL, *orig_ndarray=NULL,
+             *result_ndarray=NULL, *result_mask=NULL, *result_dict=NULL;
+    PyArray_Descr *dtype=NULL;
 
+    int rtype, span;
+
+    static char *kwlist[] = {"array", "span", "dtype", NULL};
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds,
+                "Oi|O&:mov_min(array, span, dtype)", kwlist,
+                &orig_arrayobj, &span,
+                PyArray_DescrConverter2, &dtype)) return NULL;
+
+    check_mov_args(orig_arrayobj, span, 1,
+                   &orig_ndarray, &result_mask);
+
+    rtype = _get_type_num(((PyArrayObject*)orig_ndarray)->descr, dtype);
+
+    result_ndarray = calc_mov_ranked((PyArrayObject*)orig_ndarray,
+                                     span, rtype, 'I');
+    ERR_CHECK(result_ndarray)
+
+    result_dict = PyDict_New();
+    MEM_CHECK(result_dict)
+    PyDict_SetItemString(result_dict, "array", result_ndarray);
+    PyDict_SetItemString(result_dict, "mask", result_mask);
+
+    Py_DECREF(result_ndarray);
+    Py_DECREF(result_mask);
+    return result_dict;
+}
+
 PyObject *
+MaskedArray_mov_max(PyObject *self, PyObject *args, PyObject *kwds)
+{
+    PyObject *orig_arrayobj=NULL, *orig_ndarray=NULL,
+             *result_ndarray=NULL, *result_mask=NULL, *result_dict=NULL;
+    PyArray_Descr *dtype=NULL;
+
+    int rtype, span;
+
+    static char *kwlist[] = {"array", "span", "dtype", NULL};
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds,
+                "Oi|O&:mov_max(array, span, dtype)", kwlist,
+                &orig_arrayobj, &span,
+                PyArray_DescrConverter2, &dtype)) return NULL;
+
+    check_mov_args(orig_arrayobj, span, 1,
+                   &orig_ndarray, &result_mask);
+
+    rtype = _get_type_num(((PyArrayObject*)orig_ndarray)->descr, dtype);
+
+    result_ndarray = calc_mov_ranked((PyArrayObject*)orig_ndarray,
+                                     span, rtype, 'A');
+    ERR_CHECK(result_ndarray)
+
+    result_dict = PyDict_New();
+    MEM_CHECK(result_dict)
+    PyDict_SetItemString(result_dict, "array", result_ndarray);
+    PyDict_SetItemString(result_dict, "mask", result_mask);
+
+    Py_DECREF(result_ndarray);
+    Py_DECREF(result_mask);
+    return result_dict;
+}
+
+PyObject *
 MaskedArray_mov_stddev(PyObject *self, PyObject *args, PyObject *kwds)
 {
 

Modified: trunk/scipy/sandbox/timeseries/src/cseries.c
===================================================================
--- trunk/scipy/sandbox/timeseries/src/cseries.c	2007-10-29 15:18:13 UTC (rev 3473)
+++ trunk/scipy/sandbox/timeseries/src/cseries.c	2007-10-30 22:43:58 UTC (rev 3474)
@@ -8,6 +8,10 @@
      METH_VARARGS | METH_KEYWORDS, ""},
     {"MA_mov_median", (PyCFunction)MaskedArray_mov_median,
      METH_VARARGS | METH_KEYWORDS, ""},
+    {"MA_mov_min", (PyCFunction)MaskedArray_mov_min,
+     METH_VARARGS | METH_KEYWORDS, ""},
+    {"MA_mov_max", (PyCFunction)MaskedArray_mov_max,
+     METH_VARARGS | METH_KEYWORDS, ""},
     {"MA_mov_average", (PyCFunction)MaskedArray_mov_average,
      METH_VARARGS | METH_KEYWORDS, ""},
     {"MA_mov_stddev", (PyCFunction)MaskedArray_mov_stddev,



More information about the Scipy-svn mailing list