def adeltap(self,tsl,N): _n = int(N) logger.debug("L'esponente = %d",_n) # if _n not in (4,12): # logger.error("L'esponente della funzione ADELTAP può essere solo 12 o 4 invece è %d",_n) # return tsl _ts = tsl._data _t = ts.tshift(_ts,-1,True) _v = (((_ts/_t)**_n)-1.0)*100.0 _v[_v.mask] = np.nan tsl._data = _v tsl._data.mask = None return tsl
def shift(self,tsl,N): """Shift the time-series (lag/lead) :param tsl: the input time-series :type tsl: a time-series :param N: N>0 lag N<0 lead :type N: integer """ _n = int(N) _ts = tsl._data _t = ts.tshift(_ts,_n,True) tsl._data = _t return tsl
def set_indices(self, full_year=None, reference_season=None, minimum_size=None, lag=0): """ Sets the ENSO indices for the ENSO indicator. Results are cached for convenience: if the method is called without arguments, the monthly indices are returned, provided they had been already computed. Parameters ---------- full_year : {None, False, True}, optional Whether the ENSO indices are set for a period of 12 months (True) or by valid episodes (False). If None, defaults to :attr:`full_year`. reference_season : {None, string or sequence}, optional Reference season. If None, defaults to :attr:`reference_season`. minimum_size : {None, int}, optional Minimum size for the groups of consecutive values. If None, defaults to :attr:`minimum_size`. lag : {integer}, optional Number of months of lag for ENSO indices. For example, if lag=6, the ENSO phase starting in Oct. 2001 is applied starting on Apr. 2002. Notes ----- * If ``full_year`` is False, this method calls :meth:`set_monthly_indices`. Otherwise, it calls :meth:`set_annual_indices`. * Results are cached for convenience. The cached results are always with a monthly frequency, and converted to the current frequency ``freq`` as needed, with the command >>> scikits.timeseries.lib.backward_fill(_cached.convert(freq, func=ma.mean)) * The cached indices have a monthly frequency by default. When the ENSOIndicator is converted to a lower frequency with a given conversion function, the ENSO indices are converted using :func:`scipy.stats.mstats.mode` as conversion function. """ optinfo = self._optinfo # Check the full_year flag: reset _cachedcurrent if it changed.... if full_year is None: full_year = optinfo.get("full_year", False) else: full_year = bool(full_year) if optinfo.get("full_year", False) != full_year: optinfo["full_year"] = full_year self._cachedcurrent = None # Check the reference season ..................................... if reference_season is None: reference_season = optinfo.get("reference_season", None) else: optreference_season = optinfo.get("reference_season", ()) if tuple(reference_season) != tuple(optreference_season): self.refseason = reference_season # Check the minimum_size.......................................... if minimum_size is None: minimum_size = optinfo.get("minimum_size", None) else: self.minimum_size = minimum_size # Check the current lag .......................................... if lag != optinfo.get("current_lag", 0): optinfo["current_lag"] = lag self._cachedcurrent = None # Define what we need to get...................................... if full_year: _cached = self._cachedmonthly.get("indices_annual", None) func = self._set_annual_indices else: _cached = self._cachedmonthly.get("indices_monthly", None) func = self._set_monthly_indices # ............... if _cached is None: if (minimum_size is None) and (reference_season is None): err_msg = ( "The ENSO indices have not been initialized!\n" "Please define a reference season with `self.refseason`" " and a minimum cluster size with `self.minsize`" ) raise ValueError(err_msg) _cached = func(reference_season=reference_season, minimum_size=minimum_size) # Make sure we didn't zap full_year optinfo["full_year"] = full_year # Check whether we need to take a lag into account if lag: _cached = ts.tshift(func(reference_season=reference_season, minimum_size=minimum_size), -lag, copy=False) # Fix the frequency/date range/shape of _cached _cached = self._fix_cachedcurrent(_cached) self._cachedcurrent = _cached return _cached
def set_indices(self, full_year=None, reference_season=None, minimum_size=None, lag=0): """ Sets the ENSO indices for the ENSO indicator. Results are cached for convenience: if the method is called without arguments, the monthly indices are returned, provided they had been already computed. Parameters ---------- full_year : {None, False, True}, optional Whether the ENSO indices are set for a period of 12 months (True) or by valid episodes (False). If None, defaults to :attr:`full_year`. reference_season : {None, string or sequence}, optional Reference season. If None, defaults to :attr:`reference_season`. minimum_size : {None, int}, optional Minimum size for the groups of consecutive values. If None, defaults to :attr:`minimum_size`. lag : {integer}, optional Number of months of lag for ENSO indices. For example, if lag=6, the ENSO phase starting in Oct. 2001 is applied starting on Apr. 2002. Notes ----- * If ``full_year`` is False, this method calls :meth:`set_monthly_indices`. Otherwise, it calls :meth:`set_annual_indices`. * Results are cached for convenience. The cached results are always with a monthly frequency, and converted to the current frequency ``freq`` as needed, with the command >>> scikits.timeseries.lib.backward_fill(_cached.convert(freq, func=ma.mean)) * The cached indices have a monthly frequency by default. When the ENSOIndicator is converted to a lower frequency with a given conversion function, the ENSO indices are converted using :func:`scipy.stats.mstats.mode` as conversion function. """ optinfo = self._optinfo # Check the full_year flag: reset _cachedcurrent if it changed.... if full_year is None: full_year = optinfo.get('full_year', False) else: full_year = bool(full_year) if optinfo.get('full_year', False) != full_year: optinfo['full_year'] = full_year self._cachedcurrent = None # Check the reference season ..................................... if reference_season is None: reference_season = optinfo.get('reference_season', None) else: optreference_season = optinfo.get('reference_season', ()) if tuple(reference_season) != tuple(optreference_season): self.refseason = reference_season # Check the minimum_size.......................................... if minimum_size is None: minimum_size = optinfo.get("minimum_size", None) else: self.minimum_size = minimum_size # Check the current lag .......................................... if lag != optinfo.get('current_lag', 0): optinfo['current_lag'] = lag self._cachedcurrent = None # Define what we need to get...................................... if full_year: _cached = self._cachedmonthly.get('indices_annual', None) func = self._set_annual_indices else: _cached = self._cachedmonthly.get('indices_monthly', None) func = self._set_monthly_indices #............... if _cached is None: if (minimum_size is None) and (reference_season is None): err_msg = "The ENSO indices have not been initialized!\n"\ "Please define a reference season with `self.refseason`"\ " and a minimum cluster size with `self.minsize`" raise ValueError(err_msg) _cached = func(reference_season=reference_season, minimum_size=minimum_size) # Make sure we didn't zap full_year optinfo['full_year'] = full_year # Check whether we need to take a lag into account if lag: _cached = ts.tshift(func(reference_season=reference_season, minimum_size=minimum_size), - lag, copy=False) # Fix the frequency/date range/shape of _cached _cached = self._fix_cachedcurrent(_cached) self._cachedcurrent = _cached return _cached
def deltap(self,tsl,n=12): _ts = tsl._data _n = int(n) _t = ts.tshift(_ts,-_n,False) tsl._data = (_ts/_t-1.0)*100.0 return tsl