Exemple #1
0
    def _test_read_range_cser(self):
        "test reading case series over specified ranges"
        src = data['cser']['float32']
        s1 = 3
        s2 = 1
        e1 = 8
        e2 = 4

        caseList = [(s1, e1),
                    (s1, e2),
                    (s2, e1),
                    (s2, e2)]

        for s, e in caseList:
            size = (e - s + 1)
            res = ma.array([0]*size , np.float32, mask=[1]*size )

            if e < src.size: _e = size
            else: _e = size - max(e-size, 0, size - src.size)

            res[0:_e] = src[s-1:min(e, src.size)]
            ser = self.db.read('$cser_float32', start_case=s, end_case=e)

            assert_array_equal(res, ser)
Exemple #2
0
    def read(self, name,
             start_date=None, end_date=None,
             start_case=None, end_case=None, max_string_len=65):

        """read specified object(s) from database

:Parameters:
    - `name` (string or list of strings) : names of objects that will be
      read from the database

    - `start_date` (int, *[None]*) : Applies only when reading time series.
      If specified, only data points on or after `start_date` will be read.
      If None, data will be read from the first value of the series.
    - `end_date` (int, *[None]*) : Applies only when reading time series.
      If specified, only data points on or before `end_date` will be read.
      If None, data will be read to the last value of the series.
    - `start_case` (int, *[None]*) : Applies only when reading case series.
      If specified, only data points on or after `start_case` will be read.
      If None, data will be read starting from case index 1
    - `end_case` (int, *[None]*) : Applies only when reading case series.
      If specified, only data points on or before `end_case` will be read.
      If None, data will be read to the last value of the series.
    - `max_string_len` (int, *[65]*) : Applies only when readings strings
       or series of strings. This is the maximum length of string that can
       be read. Lower values result in less memory usage, so you should
       specify this as low as is reasonable for your data.

:Return:
    if `name` is a list of strings:
        case insensitive dictionary of the objects
    if `name` is a single string:
        object from database that is stored as `name`"""

        isSingle = False
        if isinstance(name, str):
            names = [name]
            isSingle = True
        else:
            names = name

        items = CaseInsensitiveDict()

        #default to -1. This will get the entire range
        _start_case = _end_case = -1
        _start_date = _end_date = -1

        range_freq = None
        if start_date is not None:
            _start_date = start_date.value - date_value_adjust[start_date.freq]
            range_freq = freq_revmap[start_date.freq]

        if end_date is not None:
            if start_date is not None and start_date.freq != end_date.freq:
                raise ValueError("start_date and end_date must be same frequency")
            _end_date = end_date.value - date_value_adjust[end_date.freq]
            if range_freq is None:
                range_freq = freq_revmap[end_date.freq]

        if start_case is not None: _start_case = start_case
        if end_case is not None: _end_case = end_case

        if len(set([_start_case, _end_case, _start_date, _end_date, -1])) != 1:
            checkFreq = True
        else:
            checkFreq = False

        for objName in names:
            objName = objName.upper()

            if checkFreq:
                objFreq = self.obj_size(objName)['freq']

                if objFreq == range_freq:
                    start_index, end_index = _start_date, _end_date
                elif objFreq == HCASEX:
                    start_index, end_index = _start_case, _end_case
                else:
                    start_index, end_index = -1, -1
            else:
                start_index, end_index = -1, -1

            result = cf_read(self.dbkey, objName, start_index,
                             end_index, max_string_len)

            if result['type'] == HBOOLN:
                numpyType = numpy.bool_
            else:
                numpyType = fametype_tonumpy(result['type'])

            if result['type'] == HNAMEL:
                pyObj = [x for x in result['data'][1:-1].split(", ") \
                         if x != '']

            elif result['class'] == HSCALA:
                if isinstance(result['data'], str):
                    if result['mask']:
                        pyObj = None
                    else:
                        pyObj = result['data']
                else:
                    if result['mask'][0]:
                        pyObj = None
                    else:
                        pyObj = result['data'][0]
                        if result['type'] >= 8: # date type
                            value = pyObj+ \
                               date_value_adjust[freq_map[result['type']]]
                            pyObj = ts.Date(
                                        freq=freq_map[result['type']],
                                        value=value)
                        else:
                            pyObj = numpyType(pyObj)

            elif result['class'] == HSERIE:

                if 'data' in result:
                    vals = result['data']
                    mask = result['mask']
                    if not mask.any(): mask = ma.nomask
                else:
                    vals = []
                    mask = ma.nomask

                if result['type'] >= 8: # date type
                    valadj = date_value_adjust[freq_map[result['type']]]
                    if len(vals) > 0: vals += valadj
                    data = ts.DateArray(vals,
                                        freq=freq_map[result['type']])
                else:
                    data = numpy.array(vals, dtype=numpyType)

                if result['freq'] == HCASEX:
                    pyObj = ma.array(data, mask=mask)
                else:
                    observed = observed_map[result['observed']]
                    freq = freq_map[result['freq']]

                    if 'data' in result:
                        start_date = ts.Date(
                              freq=freq,
                              value=result['startindex']+date_value_adjust[freq])
                    else:
                        start_date = None

                    pyObj = ts.time_series(data, freq=freq,
                                           start_date=start_date,
                                           observed=observed, mask=mask)

            items[objName] = pyObj

        if isSingle:
            return items.values()[0]

        return items
Exemple #3
0
 def _test_write_empty_cser(self):
     "test writing a case series with no data"
     self.db.write_cser('$emptyCSer', ma.array([]))
Exemple #4
0
    #
    newdates = __getdates(dates=dates, newdates=newdates, length=nvars, freq=None, start_date=None)
    return MultiTimeSeries(_datalist, dates=newdates, dtype=mdescr)


################################################################################
if __name__ == "__main__":
    import numpy as N
    from maskedarray.testutils import assert_equal

    if 1:
        d = N.arange(5)
        m = MA.make_mask([1, 0, 0, 1, 1])
        base_d = N.r_[d, d[::-1]].reshape(2, -1).T
        base_m = N.r_[[m, m[::-1]]].T
        base = MA.array(base_d, mask=base_m)
        mrec = MR.fromarrays(base.T)
        dlist = ["2007-%02i" % (i + 1) for i in d]
        dates = date_array(dlist)
        ts = time_series(mrec, dates)
        mts = MultiTimeSeries(mrec, dates)
        self_data = [d, m, mrec, dlist, dates, ts, mts]

        assert isinstance(mts.f0, TimeSeries)

    if 0:
        mts[:2] = 5
        assert_equal(mts.f0._data, [5, 5, 2, 3, 4])
        assert_equal(mts.f1._data, [5, 5, 2, 1, 0])
        assert_equal(mts.f0._mask, [0, 0, 0, 1, 1])
        assert_equal(mts.f1._mask, [0, 0, 0, 0, 1])