def force_reference(series, ref, func=ma.mean): """ Force a timeseries to match a reference, in terms of frequency and first and last dates. Parameters ---------- series : TimeSeries Input time series ref : TimeSeries Reference time series. func : function Function used for the conversion of series to ref.freq, if necessary. """ assert(isinstance(ref, TimeSeries) and isinstance(series, TimeSeries), "Inputs should be valid TimeSeries object ! (got '%s' and '%s')" % \ (type(ref), type(series))) (sfreq, rfreq) = (series.freq, ref.freq) if sfreq > rfreq: series = series.convert(rfreq, func=func) elif sfreq < rfreq: series = backward_fill(series.convert(rfreq)) if (series.dates[0] != ref.dates[0]) or (series.dates[-1] != ref.dates[-1]): return ts.adjust_endpoints(series, ref.dates[0], ref.dates[-1]) return series
def set_ensoindicator(self, indicator): if indicator is not None: if not isinstance(indicator, ENSOIndicator): raise TypeError("The input is not a valid ENSOIndicator ! " "(got '%s' instead)" % type(indicator)) # Reset the frequency (sfreq, ifreq) = (self._dates.freq, indicator.freq) if ifreq != sfreq: if sfreq < ifreq: indicator = indicator.convert(sfreq, func=ma.mean) elif sfreq > ifreq: indicator = backward_fill(indicator.convert(sfreq)) # Set the ENSO indices to the default (if we don't have any...) if indicator._cachedcurrent is None: indicator.set_indices() # Reset the dates dates = self._dates if dates.isfull(): if len(dates) > 0: (start, end) = dates.flat[[0, -1]] if tuple(indicator._dates.flat[[0, -1]]) != (start, end): indicator = ts.adjust_endpoints(indicator, start, end) else: indicator = None else: indicator = indicator[self._dates] self._optinfo["ensoindicator"] = indicator
def _fix_cachedcurrent(self, ensoindices): """ Private function to convert cached ENSO indices. This function is used to ensure that: * the frequency of the indices matches the frequency of the indicator. * the date range of the indices matches the date range of the indicator. * the shape of the indices matches the shape of the indicator. """ _currentfreq = ensoindices._dates.freq _freq = self._dates.freq # Check the frequency, and convert if needed if _currentfreq > _freq: # To a lower frequency ('Q'/'A') if self.ndim == 2: conversionfunc = None else: conversionfunc = lambda x: mode(x)[0].squeeze() ensoindices = ensoindices.convert(_freq, func=conversionfunc) elif _currentfreq < _freq: # To a higher frequency (eg, 'D') ensoindices = backward_fill(ensoindices.convert(_freq)) # Reset to the original frequency if needed... (start, end) = self._dates.flat[[0, -1]] if tuple(ensoindices._dates.flat[[0, -1]]) != (start, end): ensoindices = ts.adjust_endpoints(ensoindices, start_date=start, end_date=end) # Reset the shape of the indices if ensoindices.shape != self.shape: ensoindices.shape = self.shape return ensoindices
def set_ensoindicator(self, indicator): if indicator is not None: if not isinstance(indicator, ENSOIndicator): raise TypeError("The input is not a valid ENSOIndicator ! "\ "(got '%s' instead)" % type(indicator)) # Reset the frequency (sfreq, ifreq) = (self._dates.freq, indicator.freq) if ifreq != sfreq: if sfreq < ifreq: indicator = indicator.convert(sfreq, func=ma.mean) elif sfreq > ifreq: indicator = backward_fill(indicator.convert(sfreq)) # Set the ENSO indices to the default (if we don't have any...) if indicator._cachedcurrent is None: indicator.set_indices() # Reset the dates dates = self._dates if dates.isfull(): if len(dates) > 0: (start, end) = dates.flat[[0, -1]] if tuple(indicator._dates.flat[[0, -1]]) != (start, end): indicator = ts.adjust_endpoints(indicator, start, end) else: indicator = None else: indicator = indicator[self._dates] self._optinfo['ensoindicator'] = indicator
def same_tsrange_of(self,_ints,*_args): args=_args[0] logger.debug('{SAME_TSRANGE_OF} _ints=%s %s',_ints,args) args=args[0] if type(args) in (ets.Timeseries,): freq = _ints._data.freqstr start_date = args._data.start_date.asfreq(freq) end_date = args._data.end_date.asfreq(freq) e = min(_ints.data.end_date,end_date) _outs = ts.adjust_endpoints(_ints.data, start_date=start_date, end_date=e, copy=True) _res = ets.Timeseries(data=_outs) return _res
def _set_annual_indices(self, minimum_size=None, reference_season=None): """ Sets the ENSO indices per periods of 12 months, starting at the first month of the reference season if any, otherwise at October. The same steps are followed as for :meth:`set_monthly_indices`. Parameters ---------- minimum_size : {None, int}, optional Minimum size for the groups of consecutive values. If None, defaults to :attr:`minimum_size`. reference_season : {None, string or sequence}, optional Reference season. If None, defaults to :attr:`reference_season`. See Also -------- :meth:`set_monthly_indices` Sets the ENSO indices for each month. """ # Get the monthly indices ..... _monthly = self.set_monthly_indices(minimum_size=minimum_size, reference_season=reference_season) # Make sure we reset the full_year flag to True (we lost it w/ set_monthly self.full_year = True # Get the annual indices refseason = self.refseason if refseason: _annual = _monthly[self.months == refseason[0]] refseason = months2code(refseason) else: _annual = _monthly[self.months == 10] _annual = adjust_endpoints(forward_fill(fill_missing_dates(_annual)), self._dates[0], self._dates[-1]) # Cache the results ........... self._cachedmonthly['indices_annual'] = _annual return _annual
def _set_annual_indices(self, minimum_size=None, reference_season=None): """ Sets the ENSO indices per periods of 12 months, starting at the first month of the reference season if any, otherwise at October. The same steps are followed as for :meth:`set_monthly_indices`. Parameters ---------- minimum_size : {None, int}, optional Minimum size for the groups of consecutive values. If None, defaults to :attr:`minimum_size`. reference_season : {None, string or sequence}, optional Reference season. If None, defaults to :attr:`reference_season`. See Also -------- :meth:`set_monthly_indices` Sets the ENSO indices for each month. """ # Get the monthly indices ..... _monthly = self.set_monthly_indices(minimum_size=minimum_size, reference_season=reference_season) # Make sure we reset the full_year flag to True (we lost it w/ set_monthly self.full_year = True # Get the annual indices refseason = self.refseason if refseason: _annual = _monthly[self.months == refseason[0]] refseason = months2code(refseason) else: _annual = _monthly[self.months == 10] _annual = adjust_endpoints(forward_fill(fill_missing_dates(_annual)), self._dates[0], self._dates[-1]) # Cache the results ........... self._cachedmonthly["indices_annual"] = _annual return _annual
def weighted(self,*args): R = None (weights,[data,series]) = args logger.debug("Params are (w=%s,d=%s,s=%s)" % (weights,data,series)) if check_type(weights,str,unicode) and check_type(data,str,unicode) and check_type(series,str,unicode): logger.debug("Params are (w=%s,d=%s,s=%s)" % (weights,data,series)) Ws = weights if ',' in series: Ss = series.split(',') else: raise ValueError,"La stringa di definizione delle serie è errata" if ',' in weights: Ws = weights.split(',') else: if '$$' not in weights: raise ValueError,"La stringa di definizione dei pesi non permette la generazione di un elenco" Ws = [ weights.replace('$$',S) for S in Ss ] if ',' in data: Ds = data.split(',') else: if '$$' not in data: raise ValueError,"La stringa di definizione dei dati non permette la generazione di un elenco" Ds = [ data.replace('$$',S) for S in Ss ] if len(Ws)==len(Ds): W = 0.0 S = 0.0 for d,w in zip(Ds,Ws): wb=IS.has_key(w) db=IS.has_key(d) if wb and db: Wn = IS[w].data[0] Dn = IS[d].data try: # if isinstance(Dn,ts.TimeSeries): # Dn = np.nan_to_num(Dn) # if isinstance(W,ts.TimeSeries): # W = np.nan_to_num(W) # if isinstance(W,ts.TimeSeries): # ts.align_series(W,Dn) if isinstance(Dn,ts.TimeSeries): Dn = ts.time_series(Dn,copy=True) if isinstance(W,ts.TimeSeries): Dn = ts.convert(Dn,W.freq) ts.fill_missing_dates(Dn,dates=W.dates,fill_value=np.nan) Dn = ts.adjust_endpoints(Dn, start_date=W.start_date, end_date=W.end_date, copy=True) # _ts = np.ma.masked_invalid(Dn) # Dn = _ts.filled(0.0) # if isinstance(W,ts.TimeSeries): # print "GOò" # _report(W,Dn*Wn) W += Dn*Wn # if isinstance(W,ts.TimeSeries): # print "=" # _report(W) except Exception, exc: # if isinstance(W,ts.TimeSeries): # _report(W) # print "DN" # _report(Dn) # print w,d,"W",type(W),W,W.shape,"D",type(Dn),Dn,Dn.shape,"Wn",type(Wn),Wn raise logger.error('Non posso comporre %s * %s | %s', d,w) ValueError, "%s * %s" % (d,w) S += Wn else: if not wb: logger.error(u'la serie %s non è presente nell\'IS. ATTENZIONE i risultati dell\'aggregazione possono non essere sono attendibili',w) if not db: logger.error(u'la serie %s non è presente nell\'IS. ATTENZIONE i risultati dell\'aggregazione possono non essere sono attendibili',d) try: R = ets.Timeseries(data=W / S,name="WEIGHTED(\"%s\",\"%s\")" % (','.join(Ws),','.join(Ds))) except ZeroDivisionError, exc: logger.warn('ZeroDivisionError') R = None