[Scipy-svn] r2482 - trunk/Lib/sandbox/timeseries/mtimeseries

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Jan 3 14:26:32 CST 2007


Author: pierregm
Date: 2007-01-03 14:26:24 -0600 (Wed, 03 Jan 2007)
New Revision: 2482

Modified:
   trunk/Lib/sandbox/timeseries/mtimeseries/CHANGELOG
   trunk/Lib/sandbox/timeseries/mtimeseries/tsdate.py
   trunk/Lib/sandbox/timeseries/mtimeseries/tseries.py
Log:
cf changelog

Modified: trunk/Lib/sandbox/timeseries/mtimeseries/CHANGELOG
===================================================================
--- trunk/Lib/sandbox/timeseries/mtimeseries/CHANGELOG	2007-01-03 08:12:10 UTC (rev 2481)
+++ trunk/Lib/sandbox/timeseries/mtimeseries/CHANGELOG	2007-01-03 20:26:24 UTC (rev 2482)
@@ -3,4 +3,5 @@
 #           : - Put the estimation of the data length in its own function.
 #           : - Added a timestep compatibility check.
 #           : - The variables in a multi-series correspond now to the last axis.
-#           : - Blocked transpose/swapaxes, temporarily.
\ No newline at end of file
+#           : - Blocked transpose/swapaxes, temporarily.
+#           : - Speed-up fill_missing_dates
\ No newline at end of file

Modified: trunk/Lib/sandbox/timeseries/mtimeseries/tsdate.py
===================================================================
--- trunk/Lib/sandbox/timeseries/mtimeseries/tsdate.py	2007-01-03 08:12:10 UTC (rev 2481)
+++ trunk/Lib/sandbox/timeseries/mtimeseries/tsdate.py	2007-01-03 20:26:24 UTC (rev 2482)
@@ -24,7 +24,7 @@
 
 import tscore as corelib
 #reload(corelib)
-from tscore import isDateType
+#from tscore import isDateType
 
 #from corelib import fmtFreq, freqToType
 import mx.DateTime as mxD
@@ -412,7 +412,8 @@
     else:
         before = False
 
-    if not isDateType(date):
+#    if not isDateType(date):
+    if not isinstance(date, Date):
         raise DateError, "Date should be a valid Date instance!"
 
     if date.freq == toFreq:
@@ -628,7 +629,7 @@
     
     def __getitem__(self, index):
         #dalog.info("__getitem__ got  index %s (%s)"%(index, type(index)))
-        if isDateType(index):
+        if isinstance(index, Date):
             index = self.find_dates(index)
         elif numeric.asarray(index).dtype.kind == 'O':
             try:
@@ -762,8 +763,9 @@
         if self.freq == 'U':
             warnings.warn("Undefined frequency: assuming daily!")
         if self.__steps is None:
-            steps = numeric.asarray(numpy.diff(self))
-            if steps.size > 0:
+            val = numeric.asarray(self).ravel()
+            if val.size > 0:
+                steps = val[1:] - val[:-1]
                 if self.__full is None:
                     self.__full = (steps.max() == 1)
                 if self.__hasdups is None:
@@ -831,7 +833,7 @@
                 raise FrequencyDateError("Cannot operate on dates", \
                                          freq, other.freq)
 #            other = 
-        elif isDateType(other):
+        elif isinstance(other, Date):
             if other.freq != freq:
                 raise FrequencyDateError("Cannot operate on dates", \
                                          freq, other.freq)
@@ -923,7 +925,7 @@
     elif dlist.dtype.kind == 'O':
         template = dlist[0]
         #...as Date objects
-        if isDateType(template):
+        if isinstance(template, Date):
             dates = numpy.fromiter((d.value for d in dlist), float_)
         #...as mx.DateTime objects
         elif hasattr(template,'absdays'):
@@ -963,15 +965,18 @@
     # Case #2: we have a starting date ..........
     if start_date is None:
         raise InsufficientDateError
-    if not isDateType(start_date):
+#    if not isDateType(start_date):
+    if not isinstance(start_date, Date):
         raise DateError, "Starting date should be a valid Date instance!"
     # Check if we have an end_date
     if end_date is None:
         if length is None:
             raise ValueError,"No length precised!"
     else:
-        assert(isDateType(end_date),
-               "Starting date should be a valid Date instance!")
+        if not isinstance(end_date, Date):
+            raise DateError, "Ending date should be a valid Date instance!"
+#        assert(isDateType(end_date),
+#               "Starting date should be a valid Date instance!")
         length = end_date - start_date
         if include_last:
             length += 1

Modified: trunk/Lib/sandbox/timeseries/mtimeseries/tseries.py
===================================================================
--- trunk/Lib/sandbox/timeseries/mtimeseries/tseries.py	2007-01-03 08:12:10 UTC (rev 2481)
+++ trunk/Lib/sandbox/timeseries/mtimeseries/tseries.py	2007-01-03 20:26:24 UTC (rev 2482)
@@ -877,7 +877,7 @@
     return isinstance(series, TimeSeries)
 
 ##### --------------------------------------------------------------------------
-##--- ... Additional functions ...
+#---- ... Additional functions ...
 ##### --------------------------------------------------------------------------
 def mask_period(data, start_date=None, end_date=None, 
                 inside=True, include_edges=True, inplace=True):
@@ -1102,8 +1102,8 @@
                            observed=series.observed, 
                            start_date=newStart)
     return adjust_endpoints(newseries, end_date=newEnd)
+TimeSeries.convert = convert
 
-
 def fill_missing_dates(data, dates=None, freq=None,fill_value=None):
     """Finds and fills the missing dates in a time series.
 The data corresponding to the initially missing dates are masked, or filled to 
@@ -1127,11 +1127,20 @@
         if not isTimeSeries(data):
             raise InsufficientDateError
         dates = data._dates
-        data = data._series
         freq = dates.freq
+        datad = data._series._data
+        datam = data._series._mask
+#        if fill_value is None:
+#            fill_value = data._fill_value
     elif not isinstance(dates, DateArray):
         dates = DateArray(dates, freq)
-    dflat = dates.ravel().asfreq(freq)
+        if isinstance(data, MaskedArray):
+            datad = data._data
+            datam = data._mask
+        else:
+            datad = data
+            datam = nomask
+    dflat = dates.asfreq(freq).ravel()
     n = len(dflat)
     if not dflat.has_missing_dates():
         return time_series(data, dflat)
@@ -1153,22 +1162,31 @@
                      ]
     #.............................
     # Just a quick check
+    vdflat = numeric.asarray(dflat)
+    vnewdates = numeric.asarray(newdates)
     for (osl,nsl) in zip(oldslc,newslc):
-        assert numpy.equal(dflat[osl],newdates[nsl]).all(),\
+        assert numpy.equal(vdflat[osl],vnewdates[nsl]).all(),\
             "Slicing mishap ! Please check %s (old) and %s (new)" % (osl,nsl)
     #.............................
     data = MA.asarray(data)
-    newdata = MA.masked_array(numeric.empty(nsize, data.dtype),
-                              mask=numeric.ones(nsize, bool_))
-    if fill_value is None:
-        if hasattr(data,'fill_value'):
-            fill_value = data.fill_value
-        else:
-            fill_value = MA.default_fill_value(data)
+    newdatad = numeric.empty(nsize, data.dtype)
+    newdatam = numeric.ones(nsize, bool_)
+#    if fill_value is None:
+#        if hasattr(data,'fill_value'):
+#            fill_value = data.fill_value
+#        else:
+#            fill_value = MA.default_fill_value(data)
     #data = data.filled(fill_value)
     #....
-    for (new,old) in zip(newslc,oldslc):
-        newdata[new] = data[old]
+    if datam is nomask:
+        for (new,old) in zip(newslc,oldslc):
+            newdatad[new] = datad[old]
+            newdatam[new] = False
+    else:
+        for (new,old) in zip(newslc,oldslc):
+            newdatad[new] = datad[old]
+            newdatam[new] = datam[old]
+    newdata = MA.masked_array(newdatad, mask=newdatam, fill_value=fill_value)    
     # Get new shape ..............
     if data.ndim == 1:
         nshp = (newdates.size,)
@@ -1229,7 +1247,7 @@
         assert_equal(tseries._mask, [1,0,0,0,0,0,1])
                 
     if 1:
-        mser3 = time_series(MA.mr_[malg1._series, malg2._series].reshape(-1,2), 
+        mser3 = time_series(MA.mr_[malg1._series, 100+malg2._series].reshape(2,-1).T, 
                             dates=malg1.dates)
- 
+        data = mser3._series._data
 



More information about the Scipy-svn mailing list