Exemplo n.º 1
0
 def test_ONI_standard(self):
     "Test the 'standard' option of load_oni"
     if self.indices is None:
         return
     ONI = load_oni('standard')
     ONI.set_indices(full_year=False, minimum_size=5, reference_season=None)
     assert_equal(*ts.align_series(ONI.indices, self.indices))
Exemplo n.º 2
0
 def test_ONI_standard(self):
     "Test the 'standard' option of load_oni"
     if self.indices is None:
         return
     ONI = load_oni('standard')
     ONI.set_indices(full_year=False, minimum_size=5, reference_season=None)
     assert_equal(*ts.align_series(ONI.indices, self.indices))
Exemplo n.º 3
0
def cvf(x,y,periodogram=True):
    """Computes the cross-covariance function of two series x and y.
The computations are performed on anomalies (deviations from average).
Gaps in the series are filled first, anomalies are then computed and missing
values filled with 0.
If x and y are valid TimeSeries object, they are aligned so that their starting
and ending point match.

    The crosscovariance at lag k, $\hat{R_{x,y}}(k)$, of 2 series {x_1,...,x_n}
and {y_1,...,y_n} with mean 0 is defined as:
\hat{R_{x,y}(k) = \sum_{t=1}^{n-k}{x_t y_{t+k}} / \sum_{t=1}^{n-k}{a_t b_{t+k}}
where x_k (y_k) is set to 0 if x_k (y_k) is initially masked, where a_k = 1 if
x_k is not masked, a_k = 0 if x_k is masked, b_k = 1 if y_k is not masked and
b_k = 0 if y_k is masked.

If the optional parameter `periodogram` is True, the denominator of the previous
expression is $\sum_{t=1}^{n-k}{a_t a_{t+k}} + k$.

Parameters
----------
    x : sequence
        Input data.
    y : sequence
        Input data.
        If y is longer than x, it is truncated to match the length of x.
        If y is shorter than x, x is truncated.
    periodogram : {True, False} optional
        Whether to return a periodogram or a standard estimate of the autocovariance.

Returns
-------
    cvf : ma.array
        Cross-covariance at lags [0,1,...,n,n-1,...,-1]

    """
    #
    x = ma.array(x, copy=False, subok=True, dtype=float)
    y = ma.array(y, copy=False, subok=True, dtype=float)
    if (x.ndim > 1) or (y.ndim > 1):
        raise ValueError("Input arrays should be 1D! (got %iD-%iD)" % \
                         (x.ndim, y.ndim))
    # Make sure the series have the same size .............
    if isinstance(x, TimeSeries):
        if not isinstance(y, TimeSeries):
            raise TypeError("The second input is NOT a valid TimeSeries")
        (x,y) = ts.align_series(x,y)
    elif isinstance(y, TimeSeries) and not isinstance(x, TimeSeries):
        raise TypeError("The first input is NOT a valid TimeSeries")
    else:
        if len(y) > len(x):
            y = y[:len(x)]
        else:
            x = x[:len(y)]
    # Get the masks .......................................
    mx = np.logical_not(ma.getmaskarray(x)).astype(int)
    my = np.logical_not(ma.getmaskarray(y)).astype(int)
    # Get the anomalies ...................................
    x = x.anom().filled(0).view(ndarray)
    y = y.anom().filled(0).view(ndarray)
    n = len(x)
    cvf_ = np.correlate(x, y, 'full')
    dnm_ = np.correlate(mx, my, 'full')
    if periodogram:
        dnm_ += np.concatenate([np.arange(n-1,0,-1), np.arange(n)])
    cvf_ /= dnm_
    return ma.fix_invalid(np.concatenate([cvf_[n-1:],cvf_[:n-1]]))
Exemplo n.º 4
0
def cvf(x, y, periodogram=True):
    """Computes the cross-covariance function of two series x and y.
The computations are performed on anomalies (deviations from average).
Gaps in the series are filled first, anomalies are then computed and missing
values filled with 0.
If x and y are valid TimeSeries object, they are aligned so that their starting
and ending point match.

    The crosscovariance at lag k, $\hat{R_{x,y}}(k)$, of 2 series {x_1,...,x_n}
and {y_1,...,y_n} with mean 0 is defined as:
\hat{R_{x,y}(k) = \sum_{t=1}^{n-k}{x_t y_{t+k}} / \sum_{t=1}^{n-k}{a_t b_{t+k}}
where x_k (y_k) is set to 0 if x_k (y_k) is initially masked, where a_k = 1 if
x_k is not masked, a_k = 0 if x_k is masked, b_k = 1 if y_k is not masked and
b_k = 0 if y_k is masked.

If the optional parameter `periodogram` is True, the denominator of the previous
expression is $\sum_{t=1}^{n-k}{a_t a_{t+k}} + k$.

Parameters
----------
    x : sequence
        Input data.
    y : sequence
        Input data.
        If y is longer than x, it is truncated to match the length of x.
        If y is shorter than x, x is truncated.
    periodogram : {True, False} optional
        Whether to return a periodogram or a standard estimate of the autocovariance.

Returns
-------
    cvf : ma.array
        Cross-covariance at lags [0,1,...,n,n-1,...,-1]

    """
    #
    x = ma.array(x, copy=False, subok=True, dtype=float)
    y = ma.array(y, copy=False, subok=True, dtype=float)
    if (x.ndim > 1) or (y.ndim > 1):
        raise ValueError("Input arrays should be 1D! (got %iD-%iD)" % \
                         (x.ndim, y.ndim))
    # Make sure the series have the same size .............
    if isinstance(x, TimeSeries):
        if not isinstance(y, TimeSeries):
            raise TypeError("The second input is NOT a valid TimeSeries")
        (x, y) = ts.align_series(x, y)
    elif isinstance(y, TimeSeries) and not isinstance(x, TimeSeries):
        raise TypeError("The first input is NOT a valid TimeSeries")
    else:
        if len(y) > len(x):
            y = y[:len(x)]
        else:
            x = x[:len(y)]
    # Get the masks .......................................
    mx = np.logical_not(ma.getmaskarray(x)).astype(int)
    my = np.logical_not(ma.getmaskarray(y)).astype(int)
    # Get the anomalies ...................................
    x = x.anom().filled(0).view(ndarray)
    y = y.anom().filled(0).view(ndarray)
    n = len(x)
    cvf_ = np.correlate(x, y, 'full')
    dnm_ = np.correlate(mx, my, 'full')
    if periodogram:
        dnm_ += np.concatenate([np.arange(n - 1, 0, -1), np.arange(n)])
    cvf_ /= dnm_
    return ma.fix_invalid(np.concatenate([cvf_[n - 1:], cvf_[:n - 1]]))
    def __call__(self, *tseries, **kwargs):
        """
        generate a report. Parameter values are not saved to the Report instance.

        Parameters
        ----------
        Accepts same parameters as __init__ method of Report class
        """

        option_dict = copy.copy(self.options)
        option_dict.update(self.__make_dict(**kwargs))
        if len(tseries) == 0:
            tseries = self.tseries

        def option(kw):
            return option_dict.get(kw, _default_options[kw])

        dates = option('dates')
        header_row = option('header_row')
        header_char = option('header_char')
        header_justify = option('header_justify')
        row_char = option('row_char')
        footer_label = option('footer_label')
        footer_char = option('footer_char')
        footer_func = option('footer_func')
        delim = option('delim')
        justify = option('justify')
        prefix = option('prefix')
        postfix = option('postfix')
        mask_rep = option('mask_rep')
        datefmt = option('datefmt')
        fmt_func = option('fmt_func')
        wrap_func = option('wrap_func')
        col_width = option('col_width')
        nls=option('nls')
        output=option('output')
        fixed_width=option('fixed_width')

        if header_row is not None:
            has_header=True
            if len(header_row) == len(tseries)+1:
                # label for date column included
                rows = [header_row]
            elif len(header_row) == len(tseries):
                # label for date column not included
                rows = [['']+header_row]
            else:
                raise ValueError("mismatch with number of headers and series")
        else:
            has_header=False
            rows=[]

        if fixed_width:

            def _standardize_justify(userspec):
                if isinstance(userspec, str):
                    # justify all columns the the same way
                    return [userspec for x in range(len(tseries)+1)]
                elif isinstance(userspec, list):
                    if len(userspec) == len(tseries):
                        # justification for date column not included, so set that
                        # to left by default
                        return ['left'] + userspec
                else:
                    raise ValueError("invalid `justify` specification")

            if justify is not None:
                justify = _standardize_justify(justify)
            else:
                # default column justification
                justify = ['left']
                for ser in tseries:
                    if ser.dtype.char in 'SUO': justify.append('left')
                    else: justify.append('right')


            if header_justify is not None:
                header_justify = _standardize_justify(header_justify)
            else:
                # default column justification
                header_justify = ['left' for x in range(len(tseries)+1)]
        else:
            justify = ['none' for x in range(len(tseries)+1)]
            header_justify = justify

        if datefmt is None:
            def datefmt_func(date): return str(date)
        else:
            def datefmt_func(date): return date.strftime(datefmt)

        if dates is None:
            tseries = ts.align_series(*tseries)
            dates = ts.date_array(start_date=tseries[0].start_date,
                                  end_date=tseries[0].end_date)
        else:
            tseries = ts.align_series(start_date=dates[0], end_date=dates[-1], *tseries)

        if isinstance(fmt_func, list):
            fmt_func = [fmt_func_wrapper(f, mask_rep) for f in fmt_func]
        else:
            fmt_func = [fmt_func_wrapper(fmt_func, mask_rep)]*len(tseries)

        def wrap_func_default(func):
            if func is None: return lambda x:x
            else: return func

        if isinstance(wrap_func, list):
            if len(wrap_func) == len(tseries):
                wrap_func = [lambda x: x] + wrap_func
            wrap_func = [wrap_func_default(func) for func in wrap_func]
        else:
            wrap_func = [wrap_func_default(wrap_func) for x in range(len(tseries)+1)]


        if isinstance(col_width, list):
            if len(col_width) == len(tseries):
                col_width = [None] + col_width
        else:
            col_width = [col_width for x in range(len(tseries)+1)]

        _sd = dates[0]

        for d in dates:
            rows.append(
                [datefmt_func(d)] + \
                [fmt_func[i](ser.series[d - _sd]) \
                 for i, ser in enumerate(tseries)]
            )

        if footer_func is not None:
            has_footer=True
            if not isinstance(footer_func, list):
                footer_func = [footer_func]*len(tseries)

            if footer_label is None: footer_label = ['']
            else: footer_label = [footer_label]

            footer_data = []
            has_missing = dates.has_missing_dates()

            for i, ser in enumerate(tseries):
                if footer_func[i] is None:
                    footer_data.append('')
                else:
                    if has_missing: _input = ser[dates]
                    else:           _input = ser.series
                    footer_data.append(fmt_func[i](footer_func[i](_input)))

            rows.append(footer_label + footer_data)
        else:
            has_footer=False


        def rowWrapper(row):
            newRows = [wrap_func[i](item).split('\n') for i, item in enumerate(row)]
            return [[(substr or '') for substr in item] for item in map(None, *newRows)]
        # break each logical row into one or more physical ones
        logicalRows = [rowWrapper(row) for row in rows]
        numLogicalRows = len(logicalRows)
        # columns of physical rows
        columns = map(None,*reduce(operator.add,logicalRows))
        numCols = len(columns)
        colNums = list(range(numCols))

        # get the maximum of each column by the string length of its items
        maxWidths = [max(col_width[i], *[len(str(item)) for item in column])
                        for i, column in enumerate(columns)]

        def getSeparator(char, separate):
            if char is not None and separate:
                return char * (len(prefix) + len(postfix) + sum(maxWidths) + \
                                             len(delim)*(len(maxWidths)-1))
            else:
                return None

        header_separator = getSeparator(header_char, has_header)
        footer_separator = getSeparator(footer_char, has_footer)
        row_separator = getSeparator(row_char, True)

        # select the appropriate justify method
        justify_funcs = {'center':str.center, 'right':str.rjust, 'left':str.ljust,
                          'none':(lambda text, width: text)}

        if has_header and has_footer:
            data_start = 1
            data_end = numLogicalRows-3
        elif has_header:
            data_start = 1
            data_end = numLogicalRows-2
        elif has_footer:
            data_start = 0
            data_end = numLogicalRows-3
        else:
            data_start = 0
            data_end = numLogicalRows-2

        for rowNum, physicalRows in enumerate(logicalRows):

            if rowNum == 0 and header_separator:
                _justify = header_justify
            else:
                _justify = justify

            def apply_justify(colNum, item, width):
                jfunc_key = str(_justify[colNum]).lower()
                jfunc = justify_funcs[jfunc_key]
                return jfunc(str(item), width)

            for row in physicalRows:

                output.write(
                    prefix + \
                    delim.join([
                        apply_justify(cn, item, width) \
                        for (cn, item, width) in zip(colNums, row, maxWidths)
                    ]) + \
                    postfix + nls)

            if row_separator and (data_start <= rowNum <= data_end):
                output.write(row_separator + nls)
            elif header_separator and rowNum < data_start:
                output.write(header_separator + nls)
            elif footer_separator and rowNum == data_end + 1:
                output.write(footer_separator + nls)
Exemplo n.º 6
0
Arquivo: xls.py Projeto: exedre/e4t
        for k,v in t.items():
            write_to_xls_book(book,k,*v,names=n[k],metadata=m[k],cfg=cfg)
        return 

    logger.debug('MAKE SHEET %s',sheetname)
    sheet1 = book.add_sheet(sheetname)

    datefmt=None
    if kwargs.has_key('datefmt'): datefmt = kwargs['datefmt']
    if datefmt is None:
        def datefmt_func(date): return str(date)
    else:
        def datefmt_func(date): return date.strftime(datefmt)

    try:
        tseries = ts.align_series(*tseries)
    except:
        print tseries
        raise ts.TimeSeriesError, "Cannot align time series!" 
    dates = ts.date_array(start_date=tseries[0].start_date,
                          end_date=tseries[0].end_date)

    N = 1
    panels,titles = [],[]

    if _metadata:
        _exists_one_panel_name = False
        _exists_one_title = False
        for i,n in enumerate(_metadata):
            panel = n['panel']
            _exists_one_panel_name = False if panel is None else True