def test_permutation(): """Test permutation""" v = Var(np.arange(6)) res = np.empty((5, 6)) for i, y in enumerate(resample(v, samples=5)): res[i] = y.x logging.info('Standard Permutation:\n%s' % res) # with unit s = Factor('abc', tile=2) for i, y in enumerate(resample(v, samples=5, unit=s)): res[i] = y.x logging.info('Permutation with Unit:\n%s' % res) # check we have only appropriate cells cols = [np.unique(res[:, i]) for i in xrange(res.shape[1])] for i in xrange(3): eq_(len(np.setdiff1d(cols[i], [i, i + 3])), 0) for i in xrange(3, 6): eq_(len(np.setdiff1d(cols[i], [i, i - 3])), 0) # check we have some variability eq_(max(map(len, cols)), 2) # with sign flip v = Var(np.arange(1, 7)) res = np.empty((2**6 - 1, 6)) for i, y in enumerate(resample(v, samples=-1, sign_flip=True)): res[i] = y.x logging.info('Permutation with sign_flip:\n%s' % res) ok_(np.all(res.min(1) < 0), "Not all permutations have a sign flip")
def test_anova_crawley(): y = Var([2, 3, 3, 4, 3, 4, 5, 6, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 3], name="Growth Rate") genot = Factor(range(6), repeat=4, name="Genotype") hrs = Var([8, 12, 16, 24], tile=6, name="Hours") aov = test.anova(y, hrs * genot) assert f'\n{aov}\n' == """
def test_equality(): u = Var(np.arange(5.)) v = Var(np.arange(5.)) ok_(all_equal(u, v)) u[-1] = np.nan assert_false(all_equal(u, v)) v[-1] = np.nan assert_false(all_equal(u, v)) ok_(all_equal(u, v, True))
def test_var(): "Test Var objects" base = Factor('aabbcde') Y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(Y.x, [5, 5, 0, 0, 0, 0, 8]) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) assert_equal(len(split.cells), i)
def gen_triggers(): raw = Var([], info={'sfreq': SAMPLINGRATE}) ds = Dataset(info={ 'subject': SUBJECT, 'session': 'cheese', 'raw': raw, 'sfreq': SAMPLINGRATE }) ds['trigger'] = Var(TRIGGERS) ds['i_start'] = Var(I_START) return ds
def t_stop_ds(ds: Dataset, t: float): "Dummy-event for the end of the last step" t_stop = ds.info['tstop'] + t out = {} for k, v in ds.items(): if k == 'time': out['time'] = Var([t_stop]) elif isinstance(v, Var): out[k] = Var(numpy.asarray([0], v.x.dtype)) elif isinstance(v, Factor): out[k] = Factor(['']) else: raise ValueError(f"{k!r} in predictor: {v!r}") return Dataset(out)
def test_longname(): "Test info['longname'] entry" ds = Dataset() u = Var([2], 'u') v = Var([1], 'v') # simple operations, also tested in test_var() eq_(longname(v.abs()), 'abs(v)') eq_(longname(u * v), "u * v") eq_(longname(u * v.abs()), "u * abs(v)") # Dataset assigning ds['abs_v'] = v.abs() eq_(longname(ds['abs_v']), 'abs_v')
def test_permutation(): """Test permutation""" v = Var(np.arange(6)) res = np.empty((5, 6)) for i, y in enumerate(resample(v, samples=5)): res[i] = y.x # with unit s = Factor('abc', tile=2) for i, y in enumerate(resample(v, samples=5, unit=s)): res[i] = y.x # check we have only appropriate cells cols = [np.unique(res[:, i]) for i in range(res.shape[1])] for i in range(3): eq_(len(np.setdiff1d(cols[i], [i, i + 3])), 0) for i in range(3, 6): eq_(len(np.setdiff1d(cols[i], [i, i - 3])), 0) # check we have some variability eq_(max(map(len, cols)), 2) # make sure sequence is stable eq_(list(map(tuple, permute_order(4, 3))), [(2, 3, 1, 0), (2, 1, 3, 0), (0, 2, 3, 1)])
def test_timeplot(): "Test plot.uv.timeplot" plot.configure_backend(False, False) ds = datasets.get_rand() ds['seq'] = Var(np.arange(2).repeat(30)) plot.uv.Timeplot('Y', 'B', 'seq', match='rm', ds=ds) plt.close('all')
def test_coercion(): "Test data class coercion" ds = datasets.get_uts() ds['avar'] = Var.from_dict(ds['A'], {'a0': 0, 'a1': 1}) assert_array_equal(assub("A == 'a0'", ds), ds['A'] == 'a0') assert_array_equal(assub("avar == 0", ds), ds['avar'] == 0) assert_raises(TypeError, assub, "avar == '0'", ds)
def test_coercion(): "Test data class coercion" ds = datasets.get_uts() ds['avar'] = Var.from_dict(ds['A'], {'a0': 0, 'a1': 1}) assert_array_equal(assub("A == 'a0'", ds), ds['A'] == 'a0') assert_array_equal(assub("avar == 0", ds), ds['avar'] == 0) with warnings.catch_warnings(): # element-wise comparison warnings.simplefilter("ignore") assert_raises(TypeError, assub, "avar == '0'", ds)
def align_word_dataset( self, ds: Dataset, words: FactorArg = 'word', ) -> Dataset: """Align ``ds`` to the TextGrid Parameters ---------- ds Dataset with data to align. words Words in ``ds`` to use to align to the TextGrid words. Returns ------- aligned_ds Dataset with the variables in ``ds`` aligned to the TextGrid, including time stamps and TextGrid words. """ words_ = asfactor(words, ds=ds) index = self._align_index(words_, silence=-1, missing=-2) out = Dataset( { 'time': Var([r.times[0] for r in self.realizations]), 'grid_word': Factor([r.graphs for r in self.realizations]), }, info={'tstop': self.realizations[-1].tstop}) for key, variable in ds.items(): if isinstance(variable, (Var, Factor)): values = dict(enumerate(variable)) if isinstance(variable, Var): values[-1] = values[ -2] = False # coerced to 0 unless all values are boolean out[key] = Var([values[i] for i in index]) else: values[-1] = values[-2] = '' out[key] = Factor([values[i] for i in index], random=variable.random) return out
def test_dataset_sorting(): "Test Dataset sorting methods" test_array = np.arange(10) ds = Dataset() ds['v'] = Var(test_array) ds['f'] = Factor(test_array) # shuffle the Dataset rand_idx = test_array.copy() np.random.shuffle(rand_idx) ds_shuffled = ds[rand_idx] # ascending, Var, copy dsa = ds_shuffled.sorted('v') assert_dataset_equal(dsa, ds, "Copy sorted by Var, ascending") # descending, Factor, in-place ds_shuffled.sort('f', descending=True) assert_dataset_equal(ds_shuffled, ds[::-1], "In-place sorted by Factor, " "descending")
def test_permutation(): """Test permutation""" v = Var(np.arange(6)) res = np.empty((5, 6)) for i, y in enumerate(resample(v, samples=5)): res[i] = y.x logging.info('Standard Permutation:\n%s' % res) # with unit s = Factor('abc', tile=2) for i, y in enumerate(resample(v, samples=5, unit=s)): res[i] = y.x logging.info('Permutation with Unit:\n%s' % res) # check we have only appropriate cells cols = [np.unique(res[:, i]) for i in range(res.shape[1])] for i in range(3): eq_(len(np.setdiff1d(cols[i], [i, i + 3])), 0) for i in range(3, 6): eq_(len(np.setdiff1d(cols[i], [i, i - 3])), 0) # check we have some variability eq_(max(list(map(len, cols))), 2)
def test_ndvar(): "Test the NDVar class" ds = datasets.get_uts(utsnd=True) x = ds['utsnd'] # meaningful slicing assert_raises(KeyError, x.sub, sensor='5') assert_equal(x.sub(sensor='4'), x.x[:, 4]) assert_equal(x.sub(sensor=['4', '3', '2']), x.x[:, [4, 3, 2]]) assert_equal(x.sub(sensor=['4']), x.x[:, [4]]) assert_equal(x.sub(case=1, sensor='4'), x.x[1, 4]) # setup indices s_case = slice(10, 13) s_sensor = slice(2, 4) s_time = x.time._slice(0.1, 0.2) b_case = np.zeros(ds.n_cases, dtype=bool) b_case[s_case] = True b_sensor = np.array([False, False, True, True, False]) b_time = np.arange(s_time.start, s_time.stop) a_case = np.arange(10, 13) a_sensor = np.arange(2, 4) a_time = np.arange(x.time.dimindex(0.1), x.time.dimindex(0.2)) # slicing with different index kinds tgt = x.x[s_case, s_sensor, s_time] eq_(tgt.shape, (3, 2, 10)) # single assert_equal(x.sub(case=s_case, sensor=s_sensor, time=s_time), tgt) assert_equal(x.sub(case=a_case, sensor=a_sensor, time=a_time), tgt) assert_equal(x.sub(case=b_case, sensor=b_sensor, time=b_time), tgt) # bool & slice assert_equal(x.sub(case=b_case, sensor=s_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=b_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=s_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=b_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=b_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=s_sensor, time=b_time), tgt) # bool & array assert_equal(x.sub(case=b_case, sensor=a_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=b_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=a_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=b_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=b_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=a_sensor, time=b_time), tgt) # slice & array assert_equal(x.sub(case=s_case, sensor=a_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=s_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=a_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=s_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=s_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=a_sensor, time=s_time), tgt) # all three assert_equal(x.sub(case=a_case, sensor=b_sensor, time=s_time), tgt) assert_equal(x.sub(case=a_case, sensor=s_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=a_sensor, time=s_time), tgt) assert_equal(x.sub(case=b_case, sensor=s_sensor, time=a_time), tgt) assert_equal(x.sub(case=s_case, sensor=a_sensor, time=b_time), tgt) assert_equal(x.sub(case=s_case, sensor=b_sensor, time=a_time), tgt) # Var v_case = Var(b_case) assert_equal(x.sub(case=v_case, sensor=b_sensor, time=a_time), tgt) # baseline correction x_bl = x - x.summary(time=(None, 0)) # assert that the baseline is 0 bl = x_bl.summary('case', 'sensor', time=(None, 0)) ok_(abs(bl) < 1e-10, "Baseline correction") # NDVar as index sens_mean = x.mean(('case', 'time')) idx = sens_mean > 0 pos = sens_mean[idx] assert_array_equal(pos.x > 0, True)
def test_isin(): "Test .isin() methods" values = np.array([ 6, -6, 6, -2, -1, 0, -10, -5, -10, -6]) v = values[0] v2 = values[:2] labels = {i: c for i, c in enumerate(ascii_lowercase, -10)} vl = labels[v] v2l = [labels[v_] for v_ in v2] target = np.logical_or(values == v2[0], values == v2[1]) inv_target = np.invert(target) index_target = np.flatnonzero(values == v) empty = np.array([]) var = Var(values) assert_array_equal(var.index(v), index_target) assert_array_equal(var.isin(v2), target) assert_array_equal(var.isany(*v2), target) assert_array_equal(var.isnot(*v2), inv_target) assert_array_equal(var.isnotin(v2), inv_target) var0 = Var([]) assert_array_equal(var0.isin(v2), empty) assert_array_equal(var0.isany(*v2), empty) assert_array_equal(var0.isnot(*v2), empty) assert_array_equal(var0.isnotin(v2), empty) f = Factor(values, labels=labels) assert_array_equal(f.index(vl), index_target) assert_array_equal(f.isin(v2l), target) assert_array_equal(f.isany(*v2l), target) assert_array_equal(f.isnot(*v2l), inv_target) assert_array_equal(f.isnotin(v2l), inv_target) f0 = Factor([]) assert_array_equal(f0.isin(v2l), empty) assert_array_equal(f0.isany(*v2l), empty) assert_array_equal(f0.isnot(*v2l), empty) assert_array_equal(f0.isnotin(v2l), empty)
def test_factor(): "Test basic Factor functionality" # initializing assert_array_equal(Factor('ab'), ['a', 'b']) assert_array_equal(Factor('ab', repeat=2), ['a', 'a', 'b', 'b']) assert_array_equal(Factor('ab', repeat=np.array([2, 1])), ['a', 'a', 'b']) empty_factor = Factor([]) eq_(len(empty_factor), 0) assert_dataobj_equal(Factor(np.empty(0)), empty_factor) # from Factor f = Factor('aabbcc') assert_array_equal(Factor(f), f) assert_array_equal(Factor(f, labels={'a': 'b'}), Factor('bbbbcc')) # removing a cell f = Factor('aabbcc') eq_(f.cells, ('a', 'b', 'c')) eq_(f.n_cells, 3) f[f == 'c'] = 'a' eq_(f.cells, ('a', 'b')) eq_(f.n_cells, 2) # cell order a = np.tile(np.arange(3), 3) # alphabetical f = Factor(a, labels={0: 'c', 1: 'b', 2: 'a'}) eq_(f.cells, ('a', 'b', 'c')) # ordered f = Factor(a, labels=((0, 'c'), (1, 'b'), (2, 'a'))) eq_(f.cells, ('c', 'b', 'a')) eq_(f[:2].cells, ('c', 'b')) f[f == 'b'] = 'c' eq_(f.cells, ('c', 'a')) # label length lens = [2, 5, 32, 2, 32, 524] f = Factor(['a' * l for l in lens], 'f') fl = f.label_length() assert_array_equal(fl, lens) eq_(fl.info['longname'], 'f.label_length()') lens2 = [3, 5, 32, 2, 32, 523] f2 = Factor(['b' * l for l in lens2], 'f2') assert_array_equal(fl - f2.label_length(), [a - b for a, b in zip(lens, lens2)]) # equality f = Factor('aabbcc') assert_equal(f == Factor('aabbcc'), True) assert_equal(f == Factor('bbccaa'), False) assert_equal(f == Factor('aabxxx'), (True, True, True, False, False, False)) assert_equal(f == Var(np.ones(6)), False) # Factor.as_var() assert_array_equal(f.as_var(dict(list(zip('abc', list(range(3)))))), [0, 0, 1, 1, 2, 2]) assert_array_equal(f.as_var({'a': 1}, 2), [1, 1, 2, 2, 2, 2]) assert_raises(KeyError, f.as_var, {'a': 1}) # Factor.floodfill() f = Factor([' ', ' ', '1', '2', ' ', ' ', '3', ' ', ' ', '2', ' ', ' ', '1']) regions = [ 1, 1, 1, 2, 2, 2, 3, 3, 3, 2, 2, 1, 1] regions2 = [ 1, 1, 1, 2, 2, 3, 3, 2, 2, 2, 2, 1, 1] regions3 = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2] target3 = ['1', '1', '1', '2', '2', '2', '3', '3', '2', '2', '2', '2', '1'] target_p = [' ', ' ', '1', '2', '2', '2', '3', '3', '3', '2', '2', '2', '1'] assert_array_equal(f.floodfill(regions, ' '), Var(regions).as_factor()) assert_array_equal(f.floodfill(regions2, ' '), Var(regions2).as_factor()) assert_array_equal(f.floodfill(regions3, ' '), target3) assert_array_equal(f.floodfill('previous', ' '), target_p) f = Factor(['', '', 'a', '', 'e', 'r', '']) assert_array_equal(f.floodfill([1, 1, 1, 11, 11, 11, 11]), Factor('aaaeerr'))
def test_var(): "Test Var objects" base = Factor('aabbcde') # initialization x = np.arange(4) y = Var(x) assert_array_equal(y, x) y = Var(x, repeat=2) assert_array_equal(y, x.repeat(2)) y = Var(x, repeat=x) assert_array_equal(y, x.repeat(x)) y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) assert_raises(TypeError, Var, x, info=1) # basic operations info = {'a': 1} v = Var([1., 2., 3., -4.], 'v', info=info) c = 2 v2 = Var([2., 2., 3., 3.], 'w', info=info) eq_(v.info, info) for op, iop, desc in OPERATORS: target = op(v.x, c) vtarget = op(v.x, v2.x) # op if desc == '+': w = v.copy() w.x = iop(w.x, c) else: w = op(v, c) eq_(w.info, {'a': 1, 'longname': 'v %s %s' % (desc, c)}) assert_array_equal(w, target) # with Var w = op(v, v2) eq_(w.info, {'a': 1, 'longname': 'v %s w' % desc}) assert_array_equal(w, vtarget) # i-op w = v.copy() w = iop(w, c) assert_array_equal(w, target) # i-op with Var w = v.copy() w = iop(w, v2) assert_array_equal(w, vtarget) # methods w = v.abs() eq_(w.info, {'a': 1, 'longname': 'abs(v)'}) assert_array_equal(w, np.abs(v.x)) x = w.log() eq_(x.info, {'a': 1, 'longname': 'log(abs(v))'}) assert_array_equal(x, np.log(w.x)) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .count() v = Var([1., 2., 1.11, 2., 1.11, 4.]) assert_array_equal(v.count(), [0, 0, 0, 1, 1, 0]) # .split() y = Var(np.arange(16)) for i in range(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_dataobj_equal(v.as_factor(), Factor('0123')) assert_dataobj_equal(v.as_factor({0: 'a'}), Factor(['a', '', '', ''])) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', 2: 'b', 'default': 'c'}), Factor('aabc')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 'default'): 'b'}), Factor('aabb'))
def test_var(): "Test Var objects" base = Factor('aabbcde') y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) # basic operations info = {'a': 1} v = Var(np.arange(4.), info=info) eq_(v.info, info) w = v - 1 eq_(w.info, info) assert_array_equal(w.x, v.x - 1) w = v + 1 eq_(w.info, info) assert_array_equal(w.x, v.x + 1) w = v * 2 eq_(w.info, info) assert_array_equal(w.x, v.x * 2) w = v / 2 eq_(w.info, info) assert_array_equal(w.x, v.x / 2) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_array_equal(v.as_factor(), Factor('0123')) assert_array_equal(v.as_factor({0: 'a'}), Factor('a123')) assert_array_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_array_equal(v.as_factor({(0, 1): 'a', 2: 'b', 'default': 'c'}), Factor('aabc')) assert_array_equal(v.as_factor({(0, 1): 'a', (2, 'default'): 'b'}), Factor('aabb'))
def test_celltable(): "Test the Celltable class." ds = datasets.get_uts() ds['cat'] = Factor('abcd', repeat=15) ct = Celltable('Y', 'A', ds=ds) eq_(ct.n_cases, 60) eq_(ct.n_cells, 2) eq_(repr(ct), "Celltable(Y, A)") eq_(repr(Celltable(ds['Y'].x, 'A', ds=ds)), "Celltable(<ndarray>, A)") eq_(repr(Celltable(ds['Y'].x, ds['A'].x, ds=ds)), "Celltable(<ndarray>, <Factor>)") ct = Celltable('Y', 'A', match='rm', ds=ds) eq_(ct.n_cases, 30) eq_(ct.n_cells, 2) # cat argument ct = Celltable('Y', 'cat', cat=('c', 'b'), ds=ds) eq_(ct.n_cases, 30) eq_(ct.X[0], 'c') eq_(ct.X[-1], 'b') assert_raises(ValueError, Celltable, 'Y', 'cat', cat=('c', 'e'), ds=ds) ct = Celltable('Y', 'A', match='rm', ds=ds) eq_(ct.n_cases, 30) assert np.all(ct.groups['a0'] == ct.groups['a1']) ct = Celltable('Y', 'cat', match='rm', cat=('c', 'b'), ds=ds) eq_(ct.n_cases, 30) eq_(ct.X[0], 'c') eq_(ct.X[-1], 'b') # catch unequal length assert_raises(ValueError, Celltable, ds['Y', :-1], 'cat', ds=ds) assert_raises(ValueError, Celltable, ds['Y', :-1], 'cat', match='rm', ds=ds) # coercion of numerical X X = ds.eval("A == 'a0'") ct = Celltable('Y', X, cat=(None, None), ds=ds) eq_(('False', 'True'), ct.cat) assert_array_equal(ct.data['True'], ds['Y', X]) ct = Celltable('Y', X, cat=('True', 'False'), ds=ds) eq_(('True', 'False'), ct.cat) assert_array_equal(ct.data['True'], ds['Y', X]) # test coercion of Y ct = Celltable(ds['Y'].x, 'A', ds=ds) assert_is_instance(ct.Y, np.ndarray) ct = Celltable(ds['Y'].x, 'A', ds=ds, coercion=asvar) assert_is_instance(ct.Y, Var) # test sub ds_sub = ds.sub("A == 'a0'") ct_sub = Celltable('Y', 'B', ds=ds_sub) ct = Celltable('Y', 'B', sub="A == 'a0'", ds=ds) assert_dataobj_equal(ct_sub.Y, ct.Y) # test sub with rm ct_sub = Celltable('Y', 'B', match='rm', ds=ds_sub) ct = Celltable('Y', 'B', match='rm', sub="A == 'a0'", ds=ds) assert_dataobj_equal(ct_sub.Y, ct.Y) # Interaction match ct = Celltable('Y', 'A', match='B % rm', ds=ds) ok_(ct.all_within) assert_dataobj_equal(combine((ct.data['a0'], ct.data['a1'])), ds['Y']) # test rm sorting ds = Dataset() ds['rm'] = Factor('abc', repeat=4) ds['Y'] = Var(np.arange(3.).repeat(4)) ds['X'] = Factor('ab', repeat=2, tile=3) idx = np.arange(12) np.random.shuffle(idx) ds = ds[idx] ct = Celltable('Y', 'X', 'rm', ds=ds) assert_array_equal(ct.match, Factor('abc', tile=2)) assert_array_equal(ct.Y, np.tile(np.arange(3.), 2)) assert_array_equal(ct.X, Factor('ab', repeat=3))
def test_ols(): "Test NDVar.ols() method" from rpy2.robjects import r # simulate data ds = datasets.get_uts(True) n_times = len(ds['uts'].time) x = np.zeros(n_times) x[20:40] = np.hanning(20) utsc = ds.eval("uts.copy()") utsc.x += ds['Y'].x[:, None] * x[None, :] ds_ = Dataset() ds_['x'] = Var(ds['Y'].x) ds_['x2'] = ds_['x'] + np.random.normal(0, 1, ds.n_cases) # ols regression m1 = ds_['x'] b1 = utsc.ols(m1) res1 = utsc.residuals(m1) t1 = utsc.ols_t(m1) m2 = ds_.eval("x + x2") b2 = utsc.ols(m2) res2 = utsc.residuals(m2) t2 = utsc.ols_t(m2) # compare with R for i in range(n_times): ds_['y'] = Var(utsc.x[:, i]) ds_.to_r('ds') # 1 predictor r('lm1 <- lm(y ~ x, ds)') beta = r('coef(lm1)')[1] assert_almost_equal(b1.x[0, i], beta) res = r('residuals(lm1)') assert_array_almost_equal(res1.x[:, i], res) t = r('coef(summary(lm1))')[5] assert_almost_equal(t1.x[0, i], t) # 2 predictors r('lm2 <- lm(y ~ x + x2, ds)') beta = r('coef(lm2)')[1:] assert_array_almost_equal(b2.x[:, i], beta) res = r('residuals(lm2)') assert_array_almost_equal(res2.x[:, i], res) lm2_coefs = r('coef(summary(lm2))') t = [lm2_coefs[7], lm2_coefs[8]] assert_array_almost_equal(t2.x[:, i], t) # 3d utsnd = ds['utsnd'] ds_['utsnd'] = utsnd b1 = ds_.eval("utsnd.ols(x)") res1 = ds_.eval("utsnd.residuals(x)") t1 = ds_.eval("utsnd.ols_t(x)") for i in range(len(b1.time)): ds_['y'] = Var(utsnd.x[:, 1, i]) ds_.to_r('ds') # 1 predictor r('lm1 <- lm(y ~ x, ds)') beta = r('coef(lm1)')[1] assert_almost_equal(b1.x[0, 1, i], beta) res = r('residuals(lm1)') assert_array_almost_equal(res1.x[:, 1, i], res) t = r('coef(summary(lm1))')[5] assert_almost_equal(t1.x[0, 1, i], t)
def test_timeplot(): "Test plot.Timeplot" ds = datasets.get_uts() ds['seq'] = Var(np.arange(2).repeat(30)) plot.Timeplot('Y', 'B', 'seq', match='rm', ds=ds, show=False)
def test_var(): "Test Var objects" base = Factor('aabbcde') # initialization x = np.arange(4) y = Var(x) assert_array_equal(y, x) y = Var(x, repeat=2) assert_array_equal(y, x.repeat(2)) y = Var(x, repeat=x) assert_array_equal(y, x.repeat(x)) y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) assert_raises(TypeError, Var, x, info=1) # basic operations info = {'a': 1} v = Var([1., 2., 3., -4.], 'v', info=info) c = 2 v2 = Var([2., 2., 3., 3.], 'w', info=info) eq_(v.info, info) for op, iop, desc in OPERATORS: target = op(v.x, c) vtarget = op(v.x, v2.x) # op if desc == '+': w = v.copy() w.x = iop(w.x, c) else: w = op(v, c) eq_(w.info, {'a': 1, 'longname': 'v %s %s' % (desc, c)}) assert_array_equal(w, target) # with Var w = op(v, v2) eq_(w.info, {'a': 1, 'longname': 'v %s w' % desc}) assert_array_equal(w, vtarget) # i-op w = v.copy() w = iop(w, c) assert_array_equal(w, target) # i-op with Var w = v.copy() w = iop(w, v2) assert_array_equal(w, vtarget) # methods w = v.abs() eq_(w.info, {'a': 1, 'longname': 'abs(v)'}) assert_array_equal(w, np.abs(v.x)) x = w.log() eq_(x.info, {'a': 1, 'longname': 'log(abs(v))'}) assert_array_equal(x, np.log(w.x)) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .count() v = Var([1., 2., 1.11, 2., 1.11, 4.]) assert_array_equal(v.count(), [0, 0, 0, 1, 1, 0]) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_dataobj_equal(v.as_factor(), Factor('0123')) assert_dataobj_equal(v.as_factor({0: 'a'}), Factor(['a', '', '', ''])) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', 2: 'b', 'default': 'c'}), Factor('aabc')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 'default'): 'b'}), Factor('aabb'))
def test_model(): "Test Model class" a = Factor('ab', repeat=3, name='a') b = Factor('ab', tile=3, name='b') u = Var([1, 1, 1, -1, -1, -1], 'u') v = Var([1., 2., 3., 4., 5., 6.], 'v') w = Var([1., 0., 0., 1., 1., 0.], 'w') # model repr m = a * b + v eq_(repr(m), "a + b + a % b + v") lines = ("intercept a b a x b v", "-----------------------------", "1 1 1 1 1", "1 1 0 0 2", "1 1 1 1 3", "1 0 0 0 4", "1 0 1 0 5", "1 0 0 0 6") eq_(str(m), '\n'.join(lines)) eq_(str(m.head(2)), '\n'.join(lines[:4])) eq_(str(m.tail(2)), '\n'.join(lines[:2] + lines[-2:])) # model without explicit names x1 = Factor('ab', repeat=2) x2 = Factor('ab', tile=2) m = x1 * x2 eq_(repr(m), "<?> + <?> + <?> % <?>") # catch explicit intercept intercept = Factor('i', repeat=4, name='intercept') assert_raises(ValueError, a.__mul__, intercept) # different var/factor combinations eq_(a * b, a + b + a % b) eq_(a * v, a + v + a % v) eq_(a * (v + w), a + v + w + a % v + a % w) # parametrization m = v + w + v * w p = m._parametrize('dummy') eq_(p.column_names, ['intercept', 'v', 'w', 'v * w']) assert_array_equal(p.x[:, p.terms['intercept']], 1) assert_array_equal(p.x[:, p.terms['v']], v.x[:, None]) assert_array_equal(p.x[:, p.terms['w']], w.x[:, None]) assert_array_equal(p.x[:, p.terms['v * w']], (v * w).x[:, None]) # persistence mp = pickle.loads(pickle.dumps(m, pickle.HIGHEST_PROTOCOL)) assert_array_equal(m.full, mp.full) # nested Vars m = (v + w) * u assert_dataobj_equal(m.effects[2], u) assert_dataobj_equal(m.effects[3], v * u) assert_dataobj_equal(m.effects[4], w * u) m = u * (v + w) assert_dataobj_equal(m.effects[0], u) assert_dataobj_equal(m.effects[3], u * v) assert_dataobj_equal(m.effects[4], u * w) m = (v + w) % u assert_dataobj_equal(m.effects[0], v * u) assert_dataobj_equal(m.effects[1], w * u) m = u % (v + w) assert_dataobj_equal(m.effects[0], u * v) assert_dataobj_equal(m.effects[1], u * w)
def test_ndvar(): "Test the NDVar class" ds = datasets.get_uts(utsnd=True) x = ds['utsnd'] # meaningful slicing assert_raises(KeyError, x.sub, sensor='5') assert_equal(x.sub(sensor='4'), x.x[:, 4]) assert_equal(x.sub(sensor=['4', '3', '2']), x.x[:, [4, 3, 2]]) assert_equal(x.sub(sensor=['4']), x.x[:, [4]]) assert_equal(x.sub(case=1, sensor='4'), x.x[1, 4]) # setup indices s_case = slice(10, 13) s_sensor = slice('2', '4') s_time = slice(0.1, 0.2) b_case = np.bincount([10, 11, 12], minlength=len(x)).astype(bool) b_sensor = np.array([False, False, True, True, False]) b_time = np.bincount(range(30, 40), minlength=len(x.time)).astype(bool) a_case = np.arange(10, 13) a_sensor = ['2', '3'] a_time = np.arange(0.1, 0.2, 0.01) # slicing with different index kinds tgt = x.x[s_case, 2:4, 30:40] eq_(tgt.shape, (3, 2, 10)) # single assert_equal(x.sub(case=s_case, sensor=s_sensor, time=s_time), tgt) assert_equal(x.sub(case=a_case, sensor=a_sensor, time=a_time), tgt) assert_equal(x.sub(case=b_case, sensor=b_sensor, time=b_time), tgt) # bool & slice assert_equal(x.sub(case=b_case, sensor=s_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=b_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=s_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=b_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=b_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=s_sensor, time=b_time), tgt) # bool & array assert_equal(x.sub(case=b_case, sensor=a_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=b_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=a_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=b_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=b_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=a_sensor, time=b_time), tgt) # slice & array assert_equal(x.sub(case=s_case, sensor=a_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=s_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=a_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=s_sensor, time=a_time), tgt) assert_equal(x.sub(case=a_case, sensor=s_sensor, time=s_time), tgt) assert_equal(x.sub(case=s_case, sensor=a_sensor, time=s_time), tgt) # all three assert_equal(x.sub(case=a_case, sensor=b_sensor, time=s_time), tgt) assert_equal(x.sub(case=a_case, sensor=s_sensor, time=b_time), tgt) assert_equal(x.sub(case=b_case, sensor=a_sensor, time=s_time), tgt) assert_equal(x.sub(case=b_case, sensor=s_sensor, time=a_time), tgt) assert_equal(x.sub(case=s_case, sensor=a_sensor, time=b_time), tgt) assert_equal(x.sub(case=s_case, sensor=b_sensor, time=a_time), tgt) # norm y = x / x.norm('sensor') assert_allclose(y.norm('sensor'), 1.) y = ds['uts'].mean('case').norm('time') assert_is_instance(y, float) # Var v_case = Var(b_case) assert_equal(x.sub(case=v_case, sensor=b_sensor, time=a_time), tgt) # univariate result assert_dataobj_equal(x.sub(sensor='2', time=0.1), Var(x.x[:, 2, 30], x.name)) eq_(x.sub(case=0, sensor='2', time=0.1), x.x[0, 2, 30]) # baseline correction x_bl = x - x.summary(time=(None, 0)) # assert that the baseline is 0 bl = x_bl.summary('case', 'sensor', time=(None, 0)) ok_(abs(bl) < 1e-10, "Baseline correction") # NDVar as index sens_mean = x.mean(('case', 'time')) idx = sens_mean > 0 pos = sens_mean[idx] assert_array_equal(pos.x > 0, True) # NDVar as index along one dimension x_tc = x.sub(sensor='1') x_time = NDVar(x_tc.time.times >= 0.3, dims=(x_tc.time,)) assert_dataobj_equal(x_tc[x_time], x_tc.sub(time=(0.3, None))) # out of range index assert_raises(ValueError, x.sub, time=(0.1, 0.81)) assert_raises(IndexError, x.sub, time=(-0.25, 0.1)) # iteration for i, xi in enumerate(x): assert_dataobj_equal(xi, x[i]) if i > 4: break
def test_var(): "Test Var objects" base = Factor('aabbcde') y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) # basic operations info = {'a': 1} v = Var(np.arange(4.), info=info) eq_(v.info, info) w = v - 1 eq_(w.info, info) assert_array_equal(w.x, v.x - 1) w = v + 1 eq_(w.info, info) assert_array_equal(w.x, v.x + 1) w = v * 2 eq_(w.info, info) assert_array_equal(w.x, v.x * 2) w = v / 2 eq_(w.info, info) assert_array_equal(w.x, v.x / 2) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_array_equal(v.as_factor(), Factor('0123')) assert_array_equal(v.as_factor({0: 'a'}), Factor('a123')) assert_array_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_array_equal(v.as_factor({ (0, 1): 'a', 2: 'b', 'default': 'c' }), Factor('aabc')) assert_array_equal(v.as_factor({ (0, 1): 'a', (2, 'default'): 'b' }), Factor('aabb'))
def test_anova_rutherford(): # ANOVA 1 y = Var([7, 3, 6, 6, 5, 8, 6, 7, 7, 11, 9, 11, 10, 10, 11, 11, 8, 14, 10, 11, 12, 10, 11, 12], name='y') a = Factor('abc', repeat=8, name='A') subject = Factor(list(range(24)), name='subject', random=True) aov = test.anova(y, a + subject(a)) assert f'\n{aov}\n' == """ SS df MS MS(denom) df(denom) F p ----------------------------------------------------------------------- A 112.00 2 56.00 2.48 21 22.62*** < .001 ----------------------------------------------------------------------- Total 164.00 23 """ aov = test.anova(y, a) assert f'\n{aov}\n' == """ SS df MS F p --------------------------------------------------- A 112.00 2 56.00 22.62*** < .001 Residuals 52.00 21 2.48 --------------------------------------------------- Total 164.00 23 """ subject = Factor(range(8), tile=3, name='subject', random=True) aov = test.anova(y, a * subject) assert f'\n{aov}\n' == """ SS df MS MS(denom) df(denom) F p ----------------------------------------------------------------------- A 112.00 2 56.00 2.71 14 20.63*** < .001 ----------------------------------------------------------------------- Total 164.00 23 """ # ANCOVA y = Var([16, 7, 11, 9, 10, 11, 8, 8, 16, 10, 13, 10, 10, 14, 11, 12, 24, 29, 10, 22, 25, 28, 22, 24]) cov = Var([9, 5, 6, 4, 6, 8, 3, 5, 8, 5, 6, 5, 3, 6, 4, 6, 5, 8, 3, 4, 6, 9, 4, 5], name='cov') a = Factor([1, 2, 3], repeat=8, name='A') aov = test.anova(y, a + cov) assert f'\n{aov}\n' == """ SS df MS F p ----------------------------------------------------- A 807.82 2 403.91 62.88*** < .001 cov 199.54 1 199.54 31.07*** < .001 Residuals 128.46 20 6.42 ----------------------------------------------------- Total 1112.00 23 """ aov = test.anova(y, cov * a) assert f'\n{aov}\n' == """ SS df MS F p ----------------------------------------------------- cov 199.54 1 199.54 32.93*** < .001 A 807.82 2 403.91 66.66*** < .001 cov x A 19.39 2 9.70 1.60 .229 Residuals 109.07 18 6.06 ----------------------------------------------------- Total 1112.00 23 """ # ANOVA 2 y = Var([7, 3, 6, 6, 5, 8, 6, 7, 7, 11, 9, 11, 10, 10, 11, 11, 8, 14, 10, 11, 12, 10, 11, 12, 16, 7, 11, 9, 10, 11, 8, 8, 16, 10, 13, 10, 10, 14, 11, 12, 24, 29, 10, 22, 25, 28, 22, 24]) a = Factor([1, 0], repeat=3 * 8, name='A') b = Factor(list(range(3)), tile=2, repeat=8, name='B') # Independent Measures: subject = Factor(list(range(8 * 6)), name='subject', random=True) aov = test.anova(y, a * b + subject(a % b)) assert f'\n{aov}\n' == """ SS df MS MS(denom) df(denom) F p ------------------------------------------------------------------------- A 432.00 1 432.00 9.05 42 47.75*** < .001 B 672.00 2 336.00 9.05 42 37.14*** < .001 A x B 224.00 2 112.00 9.05 42 12.38*** < .001 ------------------------------------------------------------------------- Total 1708.00 47 """ subject = Factor(list(range(8)), tile=6, name='subject', random=True) aov = test.anova(y, a * b * subject) assert f'\n{aov}\n' == """