Ejemplo n.º 1
0
 def test_2d(self):
     "Tests median w/ 2D"
     (n,p) = (101,30)
     x = masked_array(numpy.linspace(-1.,1.,n),)
     x[:10] = x[-10:] = masked
     z = masked_array(numpy.empty((n,p), dtype=numpy.float_))
     z[:,0] = x[:]
     idx = numpy.arange(len(x))
     for i in range(1,p):
         numpy.random.shuffle(idx)
         z[:,i] = x[idx]
     assert_equal(mmedian(z[:,0]), 0)
     assert_equal(mmedian(z), numpy.zeros((p,)))
Ejemplo n.º 2
0
 def test_ranking(self):
     x = masked_array([0,1,1,1,2,3,4,5,5,6,])
     assert_almost_equal(rank_data(x),[1,3,3,3,5,6,7,8.5,8.5,10])
     x[[3,4]] = masked
     assert_almost_equal(rank_data(x),[1,2.5,2.5,0,0,4,5,6.5,6.5,8])
     assert_almost_equal(rank_data(x,use_missing=True),
                         [1,2.5,2.5,4.5,4.5,4,5,6.5,6.5,8])
     x = masked_array([0,1,5,1,2,4,3,5,1,6,])
     assert_almost_equal(rank_data(x),[1,3,8.5,3,5,7,6,8.5,3,10])
     x = masked_array([[0,1,1,1,2], [3,4,5,5,6,]])
     assert_almost_equal(rank_data(x),[[1,3,3,3,5],[6,7,8.5,8.5,10]])
     assert_almost_equal(rank_data(x,axis=1),[[1,3,3,3,5],[1,2,3.5,3.5,5]])
     assert_almost_equal(rank_data(x,axis=0),[[1,1,1,1,1],[2,2,2,2,2,]])
Ejemplo n.º 3
0
 def test_winsorization(self):
     "Tests the Winsorization of the data."
     data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
                          296,299,306,376,428,515,666,1310,2611])
     assert_almost_equal(winsorize(data).varu(), 21551.4, 1)
     data[5] = masked
     winsorized = winsorize(data)
     assert_equal(winsorized.mask, data.mask)
Ejemplo n.º 4
0
 def check_cov(self): 
     "Tests the cov function."
     x = masked_array([[1,2,3],[4,5,6]], mask=[[1,0,0],[0,0,0]])
     c = cov(x[0])
     assert_equal(c, (x[0].anom()**2).sum())
     c = cov(x[1])
     assert_equal(c, (x[1].anom()**2).sum()/2.)
     c = cov(x)
     assert_equal(c[1,0], (x[0].anom()*x[1].anom()).sum())
Ejemplo n.º 5
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])
Ejemplo n.º 6
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))
Ejemplo n.º 7
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])
Ejemplo n.º 8
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])
Ejemplo n.º 9
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))
Ejemplo n.º 10
0
 def test_trimmed_stde(self):
     "Tests the trimmed mean standard error."
     data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
                          296,299,306,376,428,515,666,1310,2611])
     assert_almost_equal(trimmed_stde(data,0.2), 56.1, 1)
Ejemplo n.º 11
0
 def test_trimmedmean(self):
     "Tests the trimmed mean."
     data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
                          296,299,306,376,428,515,666,1310,2611])
     assert_almost_equal(trimmed_mean(data,0.1), 343, 0)
     assert_almost_equal(trimmed_mean(data,0.2), 283, 0)
Ejemplo n.º 12
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 = (dlist, dates, data)
Ejemplo n.º 13
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, dtype=float_)
     self.d = (time_series(data, dlist), data, dates)
Ejemplo n.º 14
0
data['dates']['t'] = ts.Date(freq='t', year=2004, month=1, day=1, hour=0, minute=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
Ejemplo n.º 15
0
 def test_trimmedmeanci(self):
     "Tests the confidence intervals of the trimmed mean."
     data = masked_array([545,555,558,572,575,576,578,580,
                          594,605,635,651,653,661,666])
     assert_almost_equal(trimmed_mean(data,0.2), 596.2, 1)
     assert_equal(numpy.round(trimmed_mean_ci(data,0.2),1), [561.8, 630.6])
Ejemplo n.º 16
0
 def test_mjci(self):
     "Tests the Marits-Jarrett estimator"
     data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
                          296,299,306,376,428,515,666,1310,2611])
     assert_almost_equal(mjci(data),[55.76819,45.84028,198.8788],5)