[SciPy-user] Subclass of TimeSeries
Thu May 21 10:39:27 CDT 2009
On May 21, 2009, at 9:13 AM, Pierre GM wrote:
> 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
>>> your case should be straightforward. Nevertheless, don't hesitate to
>>> ask for more details/info as needed.
>> 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'].
Thanks so much for the detailed solution. Works like a charm, and
surely saved me much agony sometime in the future, wondering why
myts['a'] .label wasn't working. I like the _optinfo solution.
More information about the SciPy-user