def setUp(self): data = south() data['n_samples'] = 0 np.random.seed(TEST_SEED) self.trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000', multi=True) self.single_trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000_0.csv') self.size_known = json.load(open(FULL_PATH + '/data/effective_size.json', 'r'))
def setUp(self): self.a = {chr(i+97):list(range(10)) for i in range(5)} self.t = Trace(**self.a) self.mt = Trace(self.a,self.a,self.a) self.real_mt = Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000', multi=True) self.real_singles = [Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000_{}.csv' .format(i)) for i in range(4)]
def setUp(self): data = south() data['n_samples'] = 0 np.random.seed(TEST_SEED) self.trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000', multi=True) self.single_trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000_0.csv') self.geweke_known = json.load(open(FULL_PATH + '/data/geweke.json'))
def setUp(self): data = south() data['n_samples'] = 0 np.random.seed(TEST_SEED) test_methods = ['obm', 'bm', 'bartlett', 'hanning', 'tukey'] self.trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000', multi=True) self.single_trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000_0.csv') self.bm = json.load(open(FULL_PATH + '/data/mcse_bm.json', 'r')) self.obm = json.load(open(FULL_PATH + '/data/mcse_obm.json', 'r')) self.tukey = json.load(open(FULL_PATH + '/data/mcse_hanning.json', 'r')) self.bartlett = json.load(open(FULL_PATH + '/data/mcse_bartlett.json', 'r')) self.hanning = self.tukey
def setUp(self): super(Test_Lower_SMA, self).build_self() self.cls = lower.Lower_SMA del self.inputs["M"] self.inputs['n_samples'] = 0 instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/lower_sma.csv')
def setUp(self): super(Test_Lower_SE, self).build_self() self.cls = lower.Lower_SE del self.inputs["M"] self.inputs['n_samples'] = 0 instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/lower_se.csv')
def setUp(self): super(Test_Upper_SE, self).build_self() self.cls = upper.Upper_SE del self.inputs["W"] self.inputs['n_samples'] = 0 instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/upper_se.csv')
def setUp(self): super(Test_Upper_SMA, self).build_self() self.cls = upper.Upper_SMA del self.inputs["W"] self.inputs['n_samples'] = 0 instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/upper_sma.csv')
def setUp(self): data = south() data['n_samples'] = 0 np.random.seed(TEST_SEED) test_methods = ['obm', 'bm', 'bartlett', 'hanning', 'tukey'] self.trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000', multi=True) self.single_trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000_0.csv') self.bm = json.load(open(FULL_PATH + '/data/mcse_bm.json', 'r')) self.obm = json.load(open(FULL_PATH + '/data/mcse_obm.json', 'r')) self.tukey = json.load(open(FULL_PATH + '/data/mcse_hanning.json', 'r')) self.bartlett = json.load( open(FULL_PATH + '/data/mcse_bartlett.json', 'r')) self.hanning = self.tukey
def setUp(self): super(Test_MVCM, self).build_self() self.cls = M.MVCM del self.inputs['M'] del self.inputs['W'] self.inputs['n_samples'] = 0 self.instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/mvcm.csv')
def test_mvcm(self): instance = self.cls(**self.inputs) np.random.seed(TEST_SEED) instance.draw() other_answers = Trace.from_csv(FULL_PATH + '/data/mvcm.csv') strip_out = [col for col in instance.trace.varnames if col not in other_answers.varnames] other_answers._assert_allclose(instance.trace.drop( *strip_out, inplace=False), rtol=RTOL, atol=ATOL)
def setUp(self): data = south() data['n_samples'] = 0 with open(FULL_PATH + '/data/psrf_noburn.json', 'r') as noburn: self.noburn = json.load(noburn) with open(FULL_PATH + '/data/psrf_brooks.json', 'r') as brooks: self.known_brooks = json.load(brooks) with open(FULL_PATH + '/data/psrf_gr.json', 'r') as gr: self.known_gr = json.load(gr) np.random.seed(TEST_SEED) self.trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000', multi=True) self.mockmodel = Hashmap(trace=self.trace)
def test_mvcm(self): instance = self.cls(**self.inputs) np.random.seed(TEST_SEED) instance.draw() other_answers = Trace.from_csv(FULL_PATH + '/data/mvcm.csv') strip_out = [ col for col in instance.trace.varnames if col not in other_answers.varnames ] other_answers._assert_allclose(instance.trace.drop(*strip_out, inplace=False), rtol=RTOL, atol=ATOL)
def setUp(self): self.a = {chr(i + 97): list(range(10)) for i in range(5)} self.t = Trace(**self.a) self.mt = Trace(self.a, self.a, self.a) self.real_mt = Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000', multi=True) self.real_singles = [ Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000_{}.csv'.format(i)) for i in range(4) ]
def test_from_df(self): df = self.t.to_df() new_trace = Trace.from_df(df) assert new_trace == self.t new_mt = Trace.from_df((df, df, df)) assert new_mt == self.t
def setUp(self): super(Test_SMASE, self).build_self() self.cls = M.SMASE self.inputs['n_samples'] = 0 self.instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/smase.csv')
def setUp(self): data = south() data['n_samples'] = 0 np.random.seed(TEST_SEED) self.trace = Trace.from_csv(FULL_PATH + '/data/south_mvcm_5000', multi=True)
def test_from_csv(self): self.t.to_csv('./test_from_csv.csv') new_t = Trace.from_csv('./test_from_csv.csv') assert self.t == new_t os.remove('./test_from_csv.csv')
def test_validate_names(self): b = self.a.copy() try: bad_names = Trace(self.a, b, self.a, self.a) except KeyError: pass
def test_multi_roundtrips(self): dfs = self.real_mt.to_df() new = Trace.from_df(dfs) new._assert_allclose(self.real_mt)
def test_ordering(self): for ch, alone in zip(self.real_mt.chains, self.real_singles): Trace(ch)._assert_allclose(alone)
def setUp(self): super(Test_Generic, self).build_self() self.cls = M.Generic self.inputs['n_samples'] = 0 instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/generic.csv')
def test_single_roundtrips(self): source_from_file = self.real_singles[0] from_df = Trace.from_df(source_from_file.to_df()) source_from_file._assert_allclose(from_df)
def __init__( self, #data parameters Y, X, coordinates, n_samples=1000, n_jobs=1, priors=None, configs=None, starting_values=None, extra_traced_params=None, dmetric='euclidean', correlation_function=nexp, verbose=False, center=True, rescale_dists=True): if center: X = verify_center(X) X = verify_covariates(X) N, p = X.shape Xs = X X = explode(X) self.state = Hashmap(X=X, Y=Y, coordinates=coordinates) self.traced_params = ['Betas', 'Mus', 'T', 'Phi', 'Tau2'] if extra_traced_params is not None: self.traced_params.extend(extra_traced_params) self.trace = Trace(**{param: [] for param in self.traced_params}) st = self.state self.state.correlation_function = correlation_function self.verbose = verbose st.Y = Y st.X = X st.Xs = Xs st.N = N st.p = p st._dmetric = dmetric if isinstance(st._dmetric, str): st.pwds = d.squareform(d.pdist(st.coordinates, metric=st._dmetric)) elif callable(st._dmetric): st.pwds = st._dmetric(st.coordinates) st.max_dist = st.pwds.max() if rescale_dists: st.pwds = st.pwds / st.max_dist st._old_max = st.max_dist st.max_dist = 1. if configs is None: configs = dict() if priors is None: priors = dict() if starting_values is None: starting_values = dict() self._setup_priors(**priors) self._setup_starting_values(**starting_values) self._setup_configs(**configs) self._verbose = verbose self.cycles = 0 if n_samples > 0: try: self.sample(n_samples, n_jobs=n_jobs) except (np.linalg.LinAlgError, ValueError) as e: Warn('Encountered the following LinAlgError. ' 'Model will return for debugging. \n {}'.format(e))
class Test_Trace(ut.TestCase): def setUp(self): self.a = {chr(i+97):list(range(10)) for i in range(5)} self.t = Trace(**self.a) self.mt = Trace(self.a,self.a,self.a) self.real_mt = Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000', multi=True) self.real_singles = [Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000_{}.csv' .format(i)) for i in range(4)] def test_validate_names(self): b = self.a.copy() try: bad_names = Trace(self.a,b,self.a,self.a) except KeyError: pass # tests def test_slicing(self): t = self.t mt = self.mt #1index assert t[1] == {'a':1, 'b':1, 'c':1, 'd':1, 'e':1} assert mt[6] == [{k:6 for k in ['a','b', 'c', 'd', 'e']}]*3 assert t[-1] =={k:9 for k in ['a','b', 'c', 'd', 'e']} assert mt[-1] == [{k:9 for k in ['a','b', 'c', 'd', 'e']}]*3 assert t[2:5] == {k:list(range(2,5)) for k in ['a','b', 'c', 'd', 'e']} assert mt[8:] == [ {k:list(range(8,10)) for k in ['a','b', 'c', 'd', 'e'] }] * 3 assert t[-4::2] == {k:[6,8] for k in ['a','b', 'c', 'd', 'e']} assert (t['a'] == list(range(10))).all() assert (mt['a'] == [list(range(10))]*3).all() assert t[['a','b']] == {'a':list(range(10)), 'b':list(range(10))} assert mt[['a','b']] == [{'a':list(range(10)), 'b':list(range(10))}]*3 #2index assert t['a', 1] == 1 assert t[['a', 'b'], 1] == {'a':1, 'b':1} assert (mt['e', 5] == [5]*3).all() assert mt[['d', 'e'], 8:] == [{'d':[8,9], 'e':[8,9]}]*3 assert (t[0, 'a'] == list(range(10))).all() assert t[0, ['a', 'b']] == {'a':list(range(10)), 'b':list(range(10))} try: t[1, ['a','c']] raise Exception('This did not raise an exception within the slicer!') except IndexError: pass assert mt[1:, ['a','c']] == [{'a':list(range(10)), 'c':list(range(10))}] * 2 assert (mt[2, 'a'] == list(range(10))).all() assert t[0,-1] == {k:9 for k in ['a', 'b', 'c', 'd', 'e']} assert t[0,:] == {k:list(range(10)) for k in ['a', 'b', 'c', 'd', 'e']} assert mt[:, -1:-4:-1] == [{k:[9,8,7] for k in ['a', 'b', 'c', 'd', 'e']}]*3 #3index assert t[0, 'a', -1] == 9 assert t[0, ['a','b'],-3::2] == {'a':[7,9], 'b':[7,9]} assert t[0, : ,-1] == {k:9 for k in ['a','b','c','d','e']} try: t[1, 'a', -1] raise Exception('this did not raise an exception when it should have') except IndexError: pass assert (mt[1:, 'a', -1] == [9]*2).all() assert mt[1:, ['a','b'], -2:] == [{'a':[8,9], 'b':[8,9]}]*2 assert (mt[2, 'a', 5::2] == [5,7,9]).all() assert (mt[1:, 'a', -5::2] == [[5,7,9]]*2).all() assert (mt[:, 'a', -5::2] == [[5,7,9]]*3).all() assert mt[2, :, :] == {k:list(range(10)) for k in ['a','b','c','d','e']} assert mt[:,:,:] == mt.chains assert mt[:,:,:] is not mt.chains def test_to_df(self): df = self.t.to_df() df2 = pd.DataFrame.from_dict(self.t.chains[0]) np.testing.assert_array_equal(df.values, df2.values) mtdf = self.mt.to_df() mtdf2 = [pd.DataFrame.from_dict(chain) for chain in self.mt.chains] for i in range(len(mtdf2)): np.testing.assert_array_equal(mtdf[i].values, mtdf2[i].values) def test_from_df(self): df = self.t.to_df() new_trace = Trace.from_df(df) assert new_trace == self.t new_mt = Trace.from_df((df, df, df)) assert new_mt == self.t def test_to_csv(self): df = self.t.to_df() self.t.to_csv('./test_to_csv.csv') new_df = pd.read_csv('./test_to_csv.csv') np.testing.assert_allclose(df.values, new_df.values, rtol=RTOL, atol=ATOL) os.remove('./test_to_csv.csv') def test_from_csv(self): self.t.to_csv('./test_from_csv.csv') new_t = Trace.from_csv('./test_from_csv.csv') assert self.t == new_t os.remove('./test_from_csv.csv') def test_single_roundtrips(self): source_from_file = self.real_singles[0] from_df = Trace.from_df(source_from_file.to_df()) source_from_file._assert_allclose(from_df) def test_ordering(self): for ch, alone in zip(self.real_mt.chains, self.real_singles): Trace(ch)._assert_allclose(alone) def test_multi_roundtrips(self): dfs = self.real_mt.to_df() new = Trace.from_df(dfs) new._assert_allclose(self.real_mt) @ut.skip def test_from_pymc3(self): raise NotImplementedError @ut.skip def test_plot(self): try: import matplotlib as mpl mpl.use('Agg') self.t.plot() except: raise Exception('Single trace plotting failed!') try: import matplotlib as mpl mpl.use('Agg') self.mt.plot() except: raise Exception('Multi-chain trace plotting failed!')
def setUp(self): super(Test_SESE, self).build_self() self.cls = M.SESE self.inputs['n_samples'] = 0 self.instance = self.cls(**self.inputs) self.answer_trace = Trace.from_csv(FULL_PATH + '/data/sese.csv')
class Test_Trace(ut.TestCase): def setUp(self): self.a = {chr(i + 97): list(range(10)) for i in range(5)} self.t = Trace(**self.a) self.mt = Trace(self.a, self.a, self.a) self.real_mt = Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000', multi=True) self.real_singles = [ Trace.from_csv(FULL_PATH + r'/data/south_mvcm_5000_{}.csv'.format(i)) for i in range(4) ] def test_validate_names(self): b = self.a.copy() try: bad_names = Trace(self.a, b, self.a, self.a) except KeyError: pass # tests def test_slicing(self): t = self.t mt = self.mt #1index assert t[1] == {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1} assert mt[6] == [{k: 6 for k in ['a', 'b', 'c', 'd', 'e']}] * 3 assert t[-1] == {k: 9 for k in ['a', 'b', 'c', 'd', 'e']} assert mt[-1] == [{k: 9 for k in ['a', 'b', 'c', 'd', 'e']}] * 3 assert t[2:5] == { k: list(range(2, 5)) for k in ['a', 'b', 'c', 'd', 'e'] } assert mt[8:] == [{ k: list(range(8, 10)) for k in ['a', 'b', 'c', 'd', 'e'] }] * 3 assert t[-4::2] == {k: [6, 8] for k in ['a', 'b', 'c', 'd', 'e']} assert (t['a'] == list(range(10))).all() assert (mt['a'] == [list(range(10))] * 3).all() assert t[['a', 'b']] == {'a': list(range(10)), 'b': list(range(10))} assert mt[['a', 'b']] == [{ 'a': list(range(10)), 'b': list(range(10)) }] * 3 #2index assert t['a', 1] == 1 assert t[['a', 'b'], 1] == {'a': 1, 'b': 1} assert (mt['e', 5] == [5] * 3).all() assert mt[['d', 'e'], 8:] == [{'d': [8, 9], 'e': [8, 9]}] * 3 assert (t[0, 'a'] == list(range(10))).all() assert t[0, ['a', 'b']] == {'a': list(range(10)), 'b': list(range(10))} try: t[1, ['a', 'c']] raise Exception( 'This did not raise an exception within the slicer!') except IndexError: pass assert mt[1:, ['a', 'c']] == [{ 'a': list(range(10)), 'c': list(range(10)) }] * 2 assert (mt[2, 'a'] == list(range(10))).all() assert t[0, -1] == {k: 9 for k in ['a', 'b', 'c', 'd', 'e']} assert t[0, :] == { k: list(range(10)) for k in ['a', 'b', 'c', 'd', 'e'] } assert mt[:, -1:-4:-1] == [{ k: [9, 8, 7] for k in ['a', 'b', 'c', 'd', 'e'] }] * 3 #3index assert t[0, 'a', -1] == 9 assert t[0, ['a', 'b'], -3::2] == {'a': [7, 9], 'b': [7, 9]} assert t[0, :, -1] == {k: 9 for k in ['a', 'b', 'c', 'd', 'e']} try: t[1, 'a', -1] raise Exception( 'this did not raise an exception when it should have') except IndexError: pass assert (mt[1:, 'a', -1] == [9] * 2).all() assert mt[1:, ['a', 'b'], -2:] == [{'a': [8, 9], 'b': [8, 9]}] * 2 assert (mt[2, 'a', 5::2] == [5, 7, 9]).all() assert (mt[1:, 'a', -5::2] == [[5, 7, 9]] * 2).all() assert (mt[:, 'a', -5::2] == [[5, 7, 9]] * 3).all() assert mt[2, :, :] == { k: list(range(10)) for k in ['a', 'b', 'c', 'd', 'e'] } assert mt[:, :, :] == mt.chains assert mt[:, :, :] is not mt.chains def test_to_df(self): df = self.t.to_df() df2 = pd.DataFrame.from_dict(self.t.chains[0]) np.testing.assert_array_equal(df.values, df2.values) mtdf = self.mt.to_df() mtdf2 = [pd.DataFrame.from_dict(chain) for chain in self.mt.chains] for i in range(len(mtdf2)): np.testing.assert_array_equal(mtdf[i].values, mtdf2[i].values) def test_from_df(self): df = self.t.to_df() new_trace = Trace.from_df(df) assert new_trace == self.t new_mt = Trace.from_df((df, df, df)) assert new_mt == self.t def test_to_csv(self): df = self.t.to_df() self.t.to_csv('./test_to_csv.csv') new_df = pd.read_csv('./test_to_csv.csv') np.testing.assert_allclose(df.values, new_df.values, rtol=RTOL, atol=ATOL) os.remove('./test_to_csv.csv') def test_from_csv(self): self.t.to_csv('./test_from_csv.csv') new_t = Trace.from_csv('./test_from_csv.csv') assert self.t == new_t os.remove('./test_from_csv.csv') def test_single_roundtrips(self): source_from_file = self.real_singles[0] from_df = Trace.from_df(source_from_file.to_df()) source_from_file._assert_allclose(from_df) def test_ordering(self): for ch, alone in zip(self.real_mt.chains, self.real_singles): Trace(ch)._assert_allclose(alone) def test_multi_roundtrips(self): dfs = self.real_mt.to_df() new = Trace.from_df(dfs) new._assert_allclose(self.real_mt) @ut.skip def test_from_pymc3(self): raise NotImplementedError @ut.skip def test_plot(self): try: import matplotlib as mpl mpl.use('Agg') self.t.plot() except: raise Exception('Single trace plotting failed!') try: import matplotlib as mpl mpl.use('Agg') self.mt.plot() except: raise Exception('Multi-chain trace plotting failed!')