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)
def _test_db_attribs(self): "test setting and retrieving database attributes" self.db.set_db_desc(_desc) self.db.set_db_doc(_doc) created = self.db.db_created() modified = self.db.db_modified() desc = self.db.db_desc() doc = self.db.db_doc() assert(abs(ts.thisday('s') - created) < 100) assert(abs(ts.thisday('s') - modified) < 100) assert_equal(desc, _desc) assert_equal(doc, _doc) assert(self.db.db_is_open()) self.db.close() assert(not self.db.db_is_open()) self.db = fame.FameDb("testdb.db",'s')
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)
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)
def _test_obj_attribs(self): "test getting and setting object attributes" assert_equal(self.db.obj_freq("$freq_b"), data['freqs']['b'].freq) assert_equal(self.db.obj_start_date("$freq_b"), data['freqs']['b'].start_date) assert_equal(self.db.obj_end_date("$freq_b"), data['freqs']['b'].end_date) created = self.db.obj_created("$freq_b") modified = self.db.obj_modified("$freq_b") assert(abs(ts.thisday('s') - created) < 100) assert(abs(ts.thisday('s') - modified) < 100) self.db.set_obj_desc("$freq_b", _desc) self.db.set_obj_doc("$freq_b", _doc) desc = self.db.obj_desc("$freq_b") doc = self.db.obj_doc("$freq_b") assert_equal(desc, _desc) assert_equal(doc, _doc) self.db.set_obj_basis("$freq_b", const.HBSDAY) assert_equal(self.db.obj_basis("$freq_b"), const.HBSDAY) self.db.set_obj_basis("$freq_b", const.HBSBUS) assert_equal(self.db.obj_basis("$freq_b"), const.HBSBUS) self.db.set_obj_observed("$freq_b", "END") assert_equal(self.db.obj_observed("$freq_b"), "ENDING") self.db.set_obj_observed("$freq_b", "MAX") assert_equal(self.db.obj_observed("$freq_b"), "MAXIMUM") self.db.set_obj_observed("$freq_b", "AVERAGE") assert_equal(self.db.obj_observed("$freq_b"), "AVERAGED")
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)
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 _test_initialize_obj_and_post(self): """test initializing an object and posting of database""" self.db.initialize_obj("$postobj", ts.thisday('B')) exist_script = "from timeseries.io import fame;" exist_script += "db = fame.FameDb('testdb.db', 'r');" exist_script += "print db.obj_exists('$postobj');" proc = os.popen('python -c "'+exist_script+'"') exists = proc.readlines()[0].strip('\n') proc.close() assert_equal(exists, "False") self.db.post() proc = os.popen('python -c "'+exist_script+'"') exists = proc.readlines()[0].strip('\n') proc.close() assert_equal(exists, "True")
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)