Beispiel #1
0
 def test_fill_missing_dates(self):
     """Test fill_missing_dates function"""
     _start = Date(freq='m', year=2005, month=1)
     _end = Date(freq='m', year=2005, month=4)
     #
     dates = date_array([_start, _end], freq='M')
     series = time_series([1, 2], dates)
     filled_ser = fill_missing_dates(series)
     #
     assert_equal(filled_ser.start_date, _start)
     assert_equal(filled_ser.end_date, _end)
     assert(filled_ser.isfull())
     assert(not filled_ser.has_duplicated_dates())
     assert_equal(filled_ser.size, _end - _start + 1)
     #
     data = N.arange(5*24).reshape(5,24)
     datelist = ['2007-07-01','2007-07-02','2007-07-03','2007-07-05','2007-07-06']
     dates = date_array_fromlist(datelist, 'D')
     dseries = time_series(data, dates)
     ndates = date_array_fromrange(start_date=dates[0],end_date=dates[-2])
     #
     fseries = fill_missing_dates(dseries)
     assert_equal(fseries.shape, (6,24))
     assert_equal(fseries._mask[:,0], [0,0,0,1,0,0])
     #
     fseries = fill_missing_dates(dseries[:,0])
     assert_equal(fseries.shape, (6,))
     assert_equal(fseries._mask, [0,0,0,1,0,0])
     #
     series = time_series(data.ravel()[:4].reshape(2,2),dates=dates[:-1])
     fseries = fill_missing_dates(series)
     assert_equal(fseries.shape, (5,))
     assert_equal(fseries._mask, [0,0,0,1,0,])
Beispiel #2
0
 def test_on2d(self):
     "Tests getitem on a 2D series"
     (a,b,d) = ([1,2,3],[3,2,1], date_array(thisday('M'),length=3))
     ser_x = time_series(N.column_stack((a,b)), dates=d)
     assert_equal(ser_x[0,0], time_series(a[0],d[0]))
     assert_equal(ser_x[0,:], time_series([(a[0],b[0])], d[0]))
     assert_equal(ser_x[:,0], time_series(a, d))
     assert_equal(ser_x[:,:], ser_x)
Beispiel #3
0
 def test_fromseries (self):
     "Base data definition."
     (dlist, dates, data) = self.d
     series = time_series(data, dlist)
     dates = dates+15
     series = time_series(series, dates)
     assert(isinstance(series, TimeSeries))
     assert_equal(series._mask, [1,0,0,0,0]*3)
     assert_equal(series._series, data)
     assert_equal(series._dates, dates)
     assert_equal(series.freqstr, 'D')
Beispiel #4
0
    def test_tshift(self):
        "Test tshift function"
        series = self.d[0]
        shift_negative = series.tshift(-1)
        result_data = [999] + [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
        result_mask = [1  ] + [1,0,0,0,0,1,0,0,0,0,1, 0, 0, 0 ]
        shift_negative_result = time_series(result_data, series._dates, mask=result_mask)

        shift_positive = series.tshift(1)
        result_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] + [999]
        result_mask = [0,0,0,0,1,0,0,0,0,1, 0, 0, 0, 0 ] + [1  ]
        shift_positive_result = time_series(result_data, series._dates, mask=result_mask)

        assert_array_equal(shift_negative, shift_negative_result)
        assert_array_equal(shift_positive, shift_positive_result)
Beispiel #5
0
    def test__timeseriescompat_multiple(self):
        "Tests the compatibility of multiple time series."
        seriesM_10 = time_series(N.arange(10),
                                    date_array(
                                      start_date=Date(freq='m', year=2005, month=1),
                                      length=10)
                                )

        seriesD_10 = time_series(N.arange(10),
                                    date_array(
                                      start_date=Date(freq='d', year=2005, month=1, day=1),
                                      length=10)
                                )

        seriesD_5 = time_series(N.arange(5),
                                    date_array(
                                      start_date=Date(freq='d', year=2005, month=1, day=1),
                                      length=5)
                                )

        seriesD_5_apr = time_series(N.arange(5),
                                    date_array(
                                      start_date=Date(freq='d', year=2005, month=4, day=1),
                                      length=5)
                                )

        assert(tseries._timeseriescompat_multiple(seriesM_10, seriesM_10, seriesM_10))

        try:
            tseries._timeseriescompat_multiple(seriesM_10, seriesD_10)
            exception = False
        except:
            exception = True
        assert(exception)

        try:
            tseries._timeseriescompat_multiple(seriesD_5, seriesD_10)
            exception = False
        except:
            exception = True
        assert(exception)

        try:
            tseries._timeseriescompat_multiple(seriesD_5, seriesD_5_apr)
            exception = False
        except:
            exception = True
        assert(exception)
Beispiel #6
0
 def test_alignseries(self):
     "Tests align_series & align_with"
     (series, data, dates) = self.d
     #
     empty_series = time_series([], freq='d')
     a, b = align_series(series, empty_series)
     assert_equal(a.start_date, b.start_date)
     assert_equal(a.end_date, b.end_date)
     #
     aseries = time_series(data, dates+10)
     bseries = time_series(data, dates-10)
     (a, b) = align_with(series, aseries, bseries)
     assert_equal(a._dates, series._dates)
     assert_equal(b._dates, series._dates)
     assert_equal(a[-5:], series[:5])
     assert_equal(b[:5], series[-5:])
Beispiel #7
0
 def test_empty_timeseries(self):
     "Tests that empty TimeSeries are  handled properly"
     empty_ts = time_series([], freq='b')
     assert_array_equal(empty_ts, empty_ts + 1)
     assert_array_equal(empty_ts, empty_ts + empty_ts)
     assert_equal(empty_ts.start_date, None)
     assert_equal(empty_ts.end_date, None)
Beispiel #8
0
    def test_convert(self):
        """Test convert function

Just check basic functionality. The details of the actual
date conversion algorithms already tested by asfreq in the
test_dates test suite.
        """
        lowFreqSeries = time_series(N.arange(10),
                                    start_date=Date(freq='m', year=2005, month=6))
        highFreqSeries = time_series(N.arange(100),
                                    start_date=Date(freq='b', year=2005, month=6, day=1))
        ndseries = time_series(N.arange(124).reshape(62,2), 
                             start_date=Date(freq='d', year=2005, month=7, day=1))

        lowToHigh_start = lowFreqSeries.convert('B', position='START')

        assert_equal(lowToHigh_start.start_date,
                     Date(freq='m', year=2005, month=6).asfreq("B", relation="BEFORE"))
        assert_equal(lowToHigh_start.end_date,
                     (Date(freq='m', year=2005, month=6) + 9).asfreq("B", relation="AFTER"))

        assert_equal(lowToHigh_start._mask[0], False)
        assert_equal(lowToHigh_start._mask[-1], True)

        lowToHigh_end = lowFreqSeries.convert('B', position='END')

        assert_equal(lowToHigh_end.start_date,
                     Date(freq='m', year=2005, month=6).asfreq("B", relation="BEFORE"))
        assert_equal(lowToHigh_end.end_date,
                     (Date(freq='m', year=2005, month=6) + 9).asfreq("B", relation="AFTER"))

        assert_equal(lowToHigh_end._mask[0], True)
        assert_equal(lowToHigh_end._mask[-1], False)


        highToLow = highFreqSeries.convert('M', func=None)

        assert_equal(highToLow.ndim, 2)
        assert_equal(highToLow.shape[1], 23)
        assert_equal(highToLow.start_date,
                     Date(freq='b', year=2005, month=6, day=1).asfreq('M'))
        assert_equal(highToLow.end_date,
                     (Date(freq='b', year=2005, month=6, day=1) + 99).asfreq('M'))

        assert_array_equal(lowFreqSeries, lowFreqSeries.convert("M"))
                
        assert_equal(ndseries.convert('M',sum), [[930,961],[2852,2883]])
Beispiel #9
0
 def test_fromrange (self):
     "Base data definition."
     (dlist, dates, data) = self.d
     series = time_series(data, start_date=dates[0], length=15)
     assert(isinstance(series, TimeSeries))
     assert_equal(series._mask, [1,0,0,0,0]*3)
     assert_equal(series._series, data)
     assert_equal(series._dates, dates)
     assert_equal(series.freqstr, 'D')
 def test_ontimeseries(self):
     data = time_series(self.maskeddata, start_date=thisday('D'))
     for width in [3,5,7]:
         k = (width-1)/2
         ravg = MF.cmov_average(data,width)
         assert(isinstance(ravg, MaskedArray))
         assert_equal(ravg, data)
         m = N.zeros(len(data), N.bool_)
         m[:k] = m[-k:] = m[10-k:10+k+1] = True
         assert_equal(ravg._mask, m)
         assert_equal(ravg._dates, data._dates)
Beispiel #11
0
 def test_compressed(self):
     "Tests compress"
     dlist = ['2007-01-%02i' % i for i in range(1,16)]
     dates = date_array_fromlist(dlist)
     data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3, dtype=float_)
     series = time_series(data, dlist)
     #
     keeper = N.array([0,1,1,1,1]*3, dtype=bool_)
     c_series = series.compressed()
     assert_equal(c_series._data, [1,2,3,4,6,7,8,9,11,12,13,14])
     assert_equal(c_series._mask, nomask)
     assert_equal(c_series._dates, dates[keeper])
     #
     series_st = time_series(MA.column_stack((data,data[::-1])),
                             dates=dates)
     c_series = series_st.compressed()
     d = [1,2,3,6,7,8,11,12,13]
     assert_equal(c_series._data, N.c_[(d,list(reversed(d)))])
     assert_equal(c_series._mask, nomask)
     assert_equal(c_series._dates, dates[d])
Beispiel #12
0
    def test_split(self):
        """Test the split function."""
        ms = time_series(N.arange(62).reshape(31,2),
                         start_date=Date(freq='d', year=2005, month=7, day=1))
        d1,d2 = split(ms)
        assert_array_equal(d1.data, ms.data[:,0])
        assert_array_equal(d1.dates, ms.dates)
        assert_array_equal(d2.data, ms.data[:,1])

        series = self.d[0]
        ss = split(series)[0]
        assert_array_equal(series, ss)
 def tests_onmultitimeseries(self):
     maskeddata = MaskedArray(N.random.random(75).reshape(25,3))
     maskeddata[10] = masked
     data = time_series(maskeddata, start_date=thisday('D'))
     for width in [3,5,7]:
         k = (width-1)/2
         ravg = MF.cmov_average(data,width)
         assert(isinstance(ravg, MaskedArray))
         assert_almost_equal(ravg[18].squeeze(), data[18-k:18+k+1].mean(0))
         m = N.zeros(data.shape, N.bool_)
         m[:k] = m[-k:] = m[10-k:10+k+1] = True
         assert_equal(ravg._mask, m)
         assert_equal(ravg._dates, data._dates)
Beispiel #14
0
 def test_concatenate(self):
     "Tests concatenate"
     dlist = ['2007-%02i' % i for i in range(1,6)]
     dates = date_array_fromlist(dlist)
     data = masked_array(numeric.arange(5), mask=[1,0,0,0,0], dtype=float_)
     #
     ser_1 = time_series(data, dates)
     ser_2 = time_series(data, dates=dates+10)
     newseries = concatenate_series(ser_1, ser_2)
     assert_equal(newseries._data,[0,1,2,3,4,0,0,0,0,0,0,1,2,3,4])
     assert_equal(newseries._mask,[1,0,0,0,0]+[1]*5+[1,0,0,0,0])
      #
     ser_1 = time_series(data, dates)
     ser_2 = time_series(data, dates=dates+10)
     newseries = concatenate_series(ser_1, ser_2, keep_gap=False)
     assert_equal(newseries._data,[0,1,2,3,4,0,1,2,3,4])
     assert_equal(newseries._mask,[1,0,0,0,0]+[1,0,0,0,0])
     assert newseries.has_missing_dates()
     #
     ser_2 = time_series(data, dates=dates+3)
     newseries = concatenate_series(ser_1, ser_2)
     assert_equal(newseries._data,[0,1,2,0,1,2,3,4])
     assert_equal(newseries._mask,[1,0,0,1,0,0,0,0])
Beispiel #15
0
 def test_onnd(self):
     "Tests getitem on a nD series"
     hodie = thisday('D')
     # Case 1D
     series = time_series(N.arange(5), mask=[1,0,0,0,0], start_date=hodie)
     assert_equal(series[0], 0)
     # Case 1D + mask
     series = time_series(N.arange(5), mask=[1,0,0,0,0], start_date=hodie)
     assert series[0] is tsmasked
     # Case 2D
     series = time_series(N.arange(10).reshape(5,2), start_date=hodie)
     assert_equal(len(series), 5)
     assert_equal(series[0], [[0,1]])
     assert_equal(series[0]._dates[0], (hodie))
     assert_equal(series[:,0], [0,2,4,6,8])
     assert_equal(series[:,0]._dates, series._dates)
     # Case 2D + mask
     series = time_series(N.arange(10).reshape(5,2), start_date=hodie,
                          mask=[[1,1],[0,0],[0,0],[0,0],[0,0]])
     assert_equal(len(series), 5)
     assert_equal(series[0], [[0,1]])
     assert_equal(series[0]._mask, [[1,1]])
     assert_equal(series[0]._dates[0], (hodie))
     assert_equal(series[:,0]._data, [0,2,4,6,8])
     assert_equal(series[:,0]._mask, [1,0,0,0,0])
     assert_equal(series[:,0]._dates, series._dates)
     # Case 3D
     series = time_series(N.arange(30).reshape(5,3,2), start_date=hodie)
     x = series[0]
     assert_equal(len(series), 5)
     assert_equal(series[0], [[[0,1],[2,3],[4,5]]])
     assert_equal(series[0]._dates[0], (hodie))
     assert_equal(series[:,0], series._data[:,0])
     assert_equal(series[:,0]._dates, series._dates)
     x = series[:,:,0]
     assert_equal(series[:,:,0], series._data[:,:,0])
     assert_equal(series[:,:,0]._dates, series._dates)
Beispiel #16
0
 def test_unsorted(self):
     "Tests that the data are properly sorted along the dates."
     dlist = ['2007-01-%02i' % i for i in (3,2,1)]
     data = [10,20,30]
     series = time_series(data,dlist)
     assert_equal(series._data,[30,20,10])
     #
     series = TimeSeries(data, dlist)
     assert_equal(series._data,[30,20,10])
     #
     series = TimeSeries(data, dlist, mask=[1,0,0])
     assert_equal(series._mask,[0,0,1])
     #
     data = masked_array([10,20,30],mask=[1,0,0])
     series = TimeSeries(data, dlist)
     assert_equal(series._mask,[0,0,1])
Beispiel #17
0
 def test_fromdatearray(self):
     "Tests the creation of a series from a datearray"
     _, dates, _ = self.d
     data = dates
     #
     series = time_series(data, dates)
     assert(isinstance(series, TimeSeries))
     assert_equal(series._dates, dates)
     assert_equal(series._data, data)
     assert_equal(series.freqstr, 'D')
     #
     series[5] = MA.masked
     # ensure that series can be represented by a string after masking a value
     # (there was a bug before that prevented this from working when using a
     # DateArray for the data)
     strrep = str(series)
Beispiel #18
0
 def test_pickling(self):
     "Tests pickling/unpickling"
     (series, data, dates) = self.d
     import cPickle
     series_pickled = cPickle.loads(series.dumps())
     assert_equal(series_pickled._dates, series._dates)
     assert_equal(series_pickled._data, series._data)
     assert_equal(series_pickled._mask, series._mask)
     #
     data = masked_array(N.matrix(range(10)).T, mask=[1,0,0,0,0]*2)
     dates = date_array(start_date=thisday('D'), length=10)
     series = time_series(data,dates=dates)
     series_pickled = cPickle.loads(series.dumps())
     assert_equal(series_pickled._dates, series._dates)
     assert_equal(series_pickled._data, series._data)
     assert_equal(series_pickled._mask, series._mask)
     assert(isinstance(series_pickled._data, N.matrix))
def getplot():
    # with open("data.json", "rb") as f:
    #     raw = f.read()

    # data = json.loads(raw)
    # metadata = data['meta'][0]
    # data = data['data']
    if not 'layer' in request.args:
        return render_template('404.html'), 404

    layer = request.args['layer']

    data, dates, results = time_series([layer],
                                       '1',
                                       '2012-01-01',
                                       '2014-01-1',
                                       product="gibs",
                                       increment='monthly',
                                       typ="global")

    # N = len(data)
    # x = [entry[0]['iso_time'].split("T")[0] for entry in data] # np.linspace(0, 1, N)
    # y = np.array([entry[0]['mean'] for entry in data])

    # name = metadata['short_name'].replace("_", " ")

    x = dates
    y = data[0, :, 0]  # mean
    name = layer.replace('_', " ")

    layout = dict(
        title='Image Analytics for {}'.format(name),
        xaxis=dict(title='Date'),
        yaxis=dict(title=name),
        height=300,
    )

    # Create a trace
    trace = go.Scatter(x=x, y=y)

    data = [trace]
    fig = dict(data=data, layout=layout)

    html = Markup(
        plotly.offline.plot(fig, include_plotlyjs=False, output_type='div'))
    return html, {'Access-Control-Allow-Origin': '*'}
    def test_ontimeseries(self):

        data = time_series(self.maskeddata, start_date=thisday('D'))

        for Mfunc, Nfunc in self.func_pairs:
            for k in [3,4,5]:
                result = Mfunc(data, k)
                assert(isinstance(result, MaskedArray))
                for x in range(len(data)-k+1):
                    if result[x+k-1] is not TS.tsmasked:
                        assert_almost_equal(
                                N.asarray(result[x+k-1]),
                                N.asarray(Nfunc(data[x:x+k])))
                result_mask = N.array([1]*(k-1)+[0]*(len(data)-k+1))
                result_mask[10:10+k] = 1
                assert_equal(result._mask, result_mask)
                assert_equal(result._dates, data._dates)
Beispiel #21
0
 def test_maskperiod(self):
     "Test mask_period"
     (series, data, dates) = self.d
     series.mask = nomask
     (start, end) = ('2007-01-06', '2007-01-12')
     mask = mask_period(series, start, end, inside=True, include_edges=True,
                        inplace=False)
     assert_equal(mask._mask, N.array([0,0,0,0,0,1,1,1,1,1,1,1,0,0,0]))
     mask = mask_period(series, start, end, inside=True, include_edges=False,
                        inplace=False)
     assert_equal(mask._mask, [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0])
     mask = mask_period(series, start, end, inside=False, include_edges=True,
                        inplace=False)
     assert_equal(mask._mask, [1,1,1,1,1,1,0,0,0,0,0,1,1,1,1])
     mask = mask_period(series, start, end, inside=False, include_edges=False,
                        inplace=False)
     assert_equal(mask._mask, [1,1,1,1,1,0,0,0,0,0,0,0,1,1,1])
     # Now w/ multivariables
     data = masked_array(numeric.arange(30).reshape(-1,2), dtype=float_)
     series = time_series(data, dates=dates)
     mask = mask_period(series, start, end, inside=True, include_edges=True,
                        inplace=False)
     result = N.array([0,0,0,0,0,1,1,1,1,1,1,1,0,0,0])
     assert_equal(mask._mask, result.repeat(2).reshape(-1,2))
Beispiel #22
0
data['dates']['s'] = ts.Date(freq='s', year=2004, month=1, day=1, hour=0, minute=0, second=0)

for freq in data['dates']:
    data['darrays'][freq] = ts.date_array(start_date=data['dates'][freq], length=10)
    data['cser']['date_'+freq] = data['darrays'][freq]

data['cser']['bool'] = [True, False, True, False, True, True]
data['cser']['int32'] = np.arange(6).astype(np.int32)
data['cser']['int64'] = np.arange(6).astype(np.int64)
data['cser']['float32'] = np.arange(6).astype(np.float32)
data['cser']['float64'] = np.arange(6).astype(np.float64)
data['cser']['str'] = ["asdf", "aasssssssss", "zzzzzzzzzzzz", "", "blah"]

for x in data['cser']:
    data['cser'][x] = ma.masked_array(data['cser'][x])
    data['tser'][x] = ts.time_series(data['cser'][x], start_date=data['dates']['a'])

for freq in data['dates']:
    data['freqs'][freq] = ts.time_series(np.arange(20).astype(np.float32), start_date=data['dates'][freq])

# test writing for all data types as time series and as case series
for x in data['tser']:
    data['tser'][x][1] = ma.masked
    data['cser'][x][1] = ma.masked

# series for testing appending data to an existing series
appendTSer = ts.time_series(np.arange(10, 15).astype(np.float32), freq='A', start_date=ts.Date(freq='A', year=2007))
appendCSer = np.arange(10, 15).astype(np.float32)

# series for testing writing over a specified range
rangeTSer = ts.time_series(np.arange(20).astype(np.float32), freq='A', start_date=ts.Date(freq='A', year=2004))
02.02.2004;24;110.84}}}

'''* END SAMPLE DATA *'''

{{{
#!python
import numpy as N
import maskedarray as M
import timeseries as ts
data = N.loadtxt("tmp.txt", dtype='|S10', skiprows=2)
dates = ts.date_array([ts.Date(freq='H',string="%s %s:00" %
(d[0],int(d[1])-1))
                       for d in data],
                      freq='H')
series = ts.time_series(data[:,-1].astype(N.float_),
                        dates,
                        mask=(data[:,-1]=='-999'))

# <markdowncell>

# ### frequencies
# 
# #### Question
# 
# Is there a example data set for at least one year on a high temporal
# resolution: 15min or at least 1h. Having such a common data set one
# could set up tutorials examples and debug or ask questions easier
# because all will have the same (non-confidetial) data on the disk.
# 
# #### Answer
# 
Beispiel #24
0
    else:
        fig = tsfigure(series=series)
    # Get the current axe, or create one
    sub = fig._axstack()
    if sub is None:
        sub = fig.add_tsplot(111,series=series,**kwargs)
    try:
        ret = sub.tsplot(series, *args, **kwargs)
        pylab.draw_if_interactive()
    except:
        pylab.hold(b)
        raise
    pylab.hold(b)
    return ret

################################################################################
if __name__ == '__main__':

    da = date_array(start_date=Date(freq='B', year=2003, quarter=3, month=1, day=17),
                    length=10)
    ser = timeseries.time_series(MA.arange(len(da)), dates=da)
#    ser[4] = MA.masked
#    ser_2 = timeseries.time_series(MA.arange(len(da)), dates=da.asfreq('Q'))

    pylab.figure()
    pylab.gcf().add_tsplot(111)
    pylab.gca().tsplot(ser, 'ko-')
    pylab.gca().format_dateaxis()
#    pylab.gca().tsplot(ser_2, 'rs')
    pylab.show()
Beispiel #25
0
# generate function doc strings
for fn in (x for x in __all__ if x[:4] == 'mov_' and x[4:] != 'mean'):
    fdoc = _g[fn].func_doc
    for prm, dc in param_doc.iteritems():
        fdoc = fdoc.replace('$$'+prm+'$$', dc)
    fdoc += mov_result_doc
    _g[fn].func_doc = fdoc


###############################################################################
if __name__ == '__main__':
    from timeseries import time_series, today
    from maskedarray.testutils import assert_equal, assert_almost_equal
    #
    series = time_series(N.arange(10),start_date=today('D'))
    #
    filtered = mov_sum(series,3)
    assert_equal(filtered, [0,1,3,6,9,12,15,18,21,24])
    assert_equal(filtered._mask, [1,1,0,0,0,0,0,0,0,0])
    assert_equal(filtered._dates, series._dates)
    assert_equal(series, N.arange(10))
    #
    filtered = mov_average(series,3)
    assert_equal(filtered, [0,1,1,2,3,4,5,6,7,8])
    assert_equal(filtered._mask, [1,1,0,0,0,0,0,0,0,0])
    assert_equal(filtered._dates, series._dates)
    assert_equal(series, N.arange(10))
    #
    filtered = mov_average(series._data,3)
    assert_equal(filtered, [0,1,1,2,3,4,5,6,7,8])
Beispiel #26
0
 def __init__(self, *args, **kwds):
     NumpyTestCase.__init__(self, *args, **kwds)
     dlist = ['2007-01-%02i' % i for i in range(1,16)]
     dates = date_array_fromlist(dlist)
     data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
     self.d = (time_series(data, dlist), data, dates)
Beispiel #27
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
Beispiel #28
0
 def test_datafromlist(self):
     "Check the creation of a time series from a list of data."
     (_, dates, _) = self.d
     data = list(range(15))
     series = time_series(data, dates)
     assert_equal(series._data.size, 15)
Beispiel #29
0
 def _test_write_empty_tser(self):
     "test writing a time series with no data"
     emptySer = ts.time_series([], freq='A')
     self.db.write_tser('$emptyTSer', emptySer)