[SciPy-user] Subclass of TimeSeries

Pierre GM pgmdevlist@gmail....
Thu May 21 10:13:20 CDT 2009

On May 20, 2009, at 11:56 PM, Robert Ferrell wrote:

> On May 20, 2009, at 5:50 PM, Pierre GM wrote:
>> On May 20, 2009, at 7:12 PM, Robert Ferrell wrote:
>>> How do I derive a subclass from TimeSeries?  I tried

>> The easiest is to follow some of the examples given in the
>> scikits.hydroclimpy package (http://hydroclimpy.sourceforge.net/).
>> For instance, here's a class that attaches a reference period to the
>> series (in scikits/hydroclimpy/core/base.py). Adapting this example  
>> to
>> your case should be straightforward. Nevertheless, don't hesitate to
>> ask for more details/info as needed.
>> Cheers
>> P.
> Thanks for the response.  I think I get the idea.

Almost. Once again, you should not use __init__ to define new  
attributes, as it may fail in some cases. For example, try to get the  
'label' of your myts['a'] object, or pickle myts...

>  For my purposes, it
> seems to work if I just call the TimeSeries.__new__ method.  That way
> I don't have to get into maoptions.

No pb with that.

> I don't think I need to call __array_finalize__.

You may not have to if you use the fact that MaskedArrays and  
TimeSeries always carry a special dictionary (_optinfo) with them,  
which stores various attributes and which is properly taken care of in  
__array_finalize__. You just need to make sure you store your 'label'  
in _optinfo, and provide convenient access methods.
Check that:

class MyTS(ts.TimeSeries):
     def __new__(cls, label, data, dates, mask=nomask, dtype=None,
                 copy=False, fill_value=None, subok=True,  
                 hard_mask=False, autosort=True, **options):
         cls = ts.TimeSeries.__new__(cls, data=data, dates=dates,  
         copy=copy, fill_value=fill_value, subok=subok,  
         hard_mask=hard_mask, autosort=autosort, **options)
         cls._optinfo['label'] = label
         return cls

     def _get_label(self):
         return self._optinfo['label']
     def _set_label(self, label):
         self._optinfo['label'] = label
     label = property(fget=_get_label, fset=_set_label)

It works as you expect, and in cases you didn't think of (try again  
taking the label of myts['a'].

Now, if I remember correctly, Travis O. toyed with the idea of  
implementing _optinfo for basic ndarrays some time ago, sometime in  
the future. No doubt that'll be the way to go when it'll happen.

More information about the SciPy-user mailing list