Example #1
0
    def write_cser(self, name, cser,
                   overwrite=False, assume_exists=False,
                   zero_represents=1, start_case=None, end_case=None):
        """write `cser` to the database as `name` as a case series.

:Parameters:
    - `name` (string) : database key that the object will be written to
    - `cser` (ndarray) : 1-dimensional ndarray (or subclass of ndarray) object to be
       written. If `cser` is a MaskedArray, then masked values will be written as ND.
    - `overwrite (boolean, *[False]*) : If True, if `name` exists in the database it
       will be overwritten. If False, data will be added to series that already exist
       (data in `cser` will be given priority over pre-existing data in the db where
       there is overlap)
    - `assume_exists` (boolean, *[False]*) : If True, an error will be
       raised if the series does not exist. If False, the series will be
       created if it does not exist already.
    - `zero_represents` (int, *[1]*) : the case index for FAME that index zero in
       the array represents
    - `start_case` (int, *[None]*) : If None, data will be written from the start of
       `cser`. If specified, only data points on or after start_case will be written.
    - `end_case` (int, *[None]*) : If None, data will be written until the end of
       `cser`. If specified, only data points on or before end_case will be written.
"""

        if not isinstance(cser, numpy.ndarray):
            raise ValueError("cser is not a valid ndarray")
        elif cser.ndim != 1:
            raise ValueError("FAME db only supports 1-dimensional arrays")

        exists = self.obj_exists(name)
        if assume_exists and not exists:
            raise DBError("%s does not exist" % name)

        if overwrite or not exists: create = True
        else: create = False

        fame_params = _fame_params_from_pyobj_cser(cser)

        fame_cls = fame_params['cls']
        fame_type = fame_params['type']
        fame_freq = fame_params['freq']
        fame_basis = fame_params['basis']
        fame_observed = fame_params['observed']

        if hasattr(cser, "_data"):
            fame_data = cser._data
            if cser._mask is ma.nomask:
                fame_mask = numpy.zeros(fame_data.shape, dtype=numpy.bool_)
            else:
                fame_mask = cser._mask
        else:
            fame_data = cser
            fame_mask = numpy.zeros(fame_data.shape, dtype=numpy.bool_)

        if create:
            if exists: self.delete_obj(name)
            cf_create(self.dbkey, name, fame_cls, fame_freq, fame_type, fame_basis, fame_observed)

        def get_boundary_case(bcase, attr):
            if bcase is not None:
                idx = bcase - zero_represents
                if idx < 0 or idx > cser.size:
                    raise ValueError("%s outside range of series" % attr)
                return bcase
            else:
                if cser.size == 0:
                    return None
                else:
                    if attr == 'start_case':
                        return zero_represents
                    elif attr == 'end_case':
                        return zero_represents + cser.size - 1
                    else:
                        raise ValueError("unexpected argument: %s " % attr)

        start_case = get_boundary_case(start_case, "start_case")
        end_case = get_boundary_case(end_case, "end_case")

        if start_case is not None:
            # convert integer types to floats since FAME does not have an integer type
            s = start_case - zero_represents
            e = end_case - zero_represents

            fame_data = fame_data[s:e+1]
            fame_mask = fame_mask[s:e+1]
            newType = fametype_tonumpy(fame_type)
            if fame_type >= 8:
                # date type
                fame_data = fame_data - date_value_adjust[fame_data.freq]
            elif newType != fame_data.dtype:
                fame_data = fame_data.astype(newType)

            cfame.write_series(self.dbkey, name, fame_data, fame_mask, start_case, end_case, fame_type, fame_freq)
Example #2
0
    def write_tser(self, name, tser,
                   overwrite=False, assume_exists=False,
                   start_date=None, end_date=None):
        """write `tser` to the database as `name` as a time series.

:Parameters:
    - `name` (string) : database key that the object will be written to
    - `tser` (TimeSeries) : TimeSeries object to be written. Cannot have missing dates.
       Use fill_missing_dates first on your series if you suspect this is the situation.
       TimeSeries must be 1-dimensional
    - `overwrite (boolean, *[False]*) : If True, if `name` exists in the database it
       will be overwritten. If False, data will be added to series that already exist
       (data in `tser` will be given priority over pre-existing data in the db where
       there is overlap)
    - `assume_exists` (boolean, *[False]*) : If True, an error will be
       raised if the series does not exist. If False, the series will be
       created if it does not exist already.
    - `start_date` (Date, *[None]*) : If None, data will be written from the start of
       `tser`. If specified, only data points on or after start_date will be written.
    - `end_date` (Date, *[None]*) : If None, data will be written until the end of
       `tser`. If specified, only data points on or before end_date will be written.
"""

        if not isinstance(tser, ts.TimeSeries):
            raise ValueError("tser is not a valid time series")
        elif tser.has_missing_dates():
            raise ValueError("tser must not have any missing dates")
        elif tser.ndim != 1:
            raise ValueError("FAME db only supports 1-dimensional time series")

        exists = self.obj_exists(name)

        if assume_exists and not exists:
            raise DBError("%s does not exist" % name)

        if overwrite or not exists: create = True
        else: create = False

        fame_params = _fame_params_from_pyobj_tser(tser)

        fame_cls = fame_params['cls']
        fame_type = fame_params['type']
        fame_freq = fame_params['freq']
        fame_basis = fame_params['basis']
        fame_observed = fame_params['observed']

        if create:
            if exists: self.delete_obj(name)
            cf_create(self.dbkey, name, fame_cls, fame_freq, fame_type, fame_basis, fame_observed)

        def get_boundary_date(bdate, attr):
            if bdate is not None:
                if bdate.freq != tser.freq:
                    raise ValueError(attr+" frequency must be same as tser frequency")
                if tser.start_date > bdate or tser.end_date < bdate:
                    raise ValueError(attr+" outside range of series")
                return bdate
            else:
                return getattr(tser, attr)

        start_date = get_boundary_date(start_date, "start_date")
        end_date = get_boundary_date(end_date, "end_date")

        if start_date is not None:

            towrite = tser[start_date:end_date+1]

            start_index = start_date.value
            end_index = end_date.value

            # convert integer types to floats since FAME does not have an integer type
            newType = fametype_tonumpy(fame_type)
            if fame_type >= 8:
                # date type
                fame_data = towrite._data - date_value_adjust[towrite._data.freq]
            elif newType != tser._data.dtype:
                fame_data = towrite._data.astype(newType)
            else:
                fame_data = towrite._data

            if towrite._mask is ma.nomask:
                fame_mask = numpy.zeros(towrite._data.shape, dtype=numpy.bool_)
            else:
                fame_mask = towrite._mask

            start_index -= date_value_adjust[towrite.freq]
            end_index   -= date_value_adjust[towrite.freq]

            cfame.write_series(self.dbkey, name, fame_data, fame_mask, start_index, end_index, fame_type, fame_freq)