def test_difference(): "Test table.difference" ds = datasets.get_uv() # add a variables that should stay in the dataset labels = {c: c[-1] for c in ds['rm'].cells} ds['rmf'] = Factor(ds['rm'], labels=labels) dds = table.difference('fltvar', 'A', 'a1', 'a2', 'rm', ds=ds) assert repr(dds) == "<Dataset n_cases=20 {'rm':F, 'fltvar':V, 'rmf':F}>" assert_array_equal(dds['rmf'], Factor(dds['rm'], labels=labels)) dds = table.difference('fltvar', 'A', 'a1', 'a2', 'rm', by='B', ds=ds) assert repr(dds) == "<Dataset n_cases=40 {'rm':F, 'fltvar':V, 'B':F, 'rmf':F}>" # difference of the difference ddds = table.difference('fltvar', 'B', 'b1', 'b2', 'rm', ds=dds) assert repr(ddds) == "<Dataset n_cases=20 {'rm':F, 'fltvar':V, 'rmf':F}>" dds = table.difference('fltvar', 'A%B', ('a1', 'b1'), ('a2', 'b2'), 'rm', ds=ds) assert repr(dds) == "<Dataset n_cases=20 {'rm':F, 'fltvar':V, 'rmf':F}>" # create bigger dataset ds2 = ds.copy() ds['C', :] = 'c1' ds2['C', :] = 'c2' ds = combine((ds, ds2)) dds = table.difference('fltvar', 'A', 'a1', 'a2', 'rm', by='B%C', ds=ds) assert repr(dds) == "<Dataset n_cases=80 {'rm':F, 'fltvar':V, 'B':F, 'C':F, 'rmf':F}>" dds = table.difference('fltvar', 'A%B', ('a1', 'b1'), ('a2', 'b2'), 'rm', by='C', ds=ds) assert repr(dds) == "<Dataset n_cases=40 {'rm':F, 'fltvar':V, 'C':F, 'rmf':F}>"
def test_effect(): "Test _Effect class" # .enumerate_cells() f1 = Factor('aabbccaabbcc') f2 = Factor('abababababab') i = f1 % f2 n1 = np.concatenate((np.tile([0, 1], 3), np.tile([2, 3], 3))) assert_array_equal(f1.enumerate_cells(), n1) assert_array_equal(f2.enumerate_cells(), np.arange(6).repeat(2)) assert_array_equal(i.enumerate_cells(), np.arange(2).repeat(6))
def test_test_experiment(root_dir): "Test event labeling with the EventExperiment subclass of MneExperiment" e = EventExperiment() # test defaults assert e.get('session') == 'cheese' assert e.get('model') == 'name' # test event labeling ds = e._label_events(gen_triggers()) name = Factor([e.variables['name'][t] for t in TRIGGERS], name='name') assert_dataobj_equal(ds['name'], name) tgt = ds['trigger'].as_factor(e.variables['backorder'], 'backorder') assert_dataobj_equal(ds['backorder'], tgt) tgt = ds['trigger'].as_factor(e.variables['taste'], 'taste') assert_dataobj_equal(ds['taste'], tgt) assert_array_equal(ds['i_start'], I_START) assert_array_equal(ds['subject'] == SUBJECT, True) # epochs assert e._epochs['cheese'].tmin == -0.2 assert e._epochs['cheese-leicester'].tmin == -0.1 assert e._epochs['cheese-tilsit'].tmin == -0.2 # tests e = EventExperiment(root_dir) # add test EventExperiment.tests['aov'] = ANOVA('backorder * taste * subject') e = EventExperiment(root_dir) e.set(test='aov') assert e.get('model') == 'backorder%taste' # remove test del EventExperiment.tests['aov'] e = EventExperiment(root_dir)
def test_boxplot(): "Test plot.Boxplot" ds = datasets.get_uv() plot.Boxplot('fltvar', 'A%B', match='rm', ds=ds, show=False) # one category plot.Boxplot('fltvar', ds=ds, test=False, show=False) plot.Boxplot('fltvar', ds=ds, show=False) plot.Boxplot('fltvar', match='rm', ds=ds, show=False) # many pairwise significances ds['fltvar'][ds.eval("A%B==('a1','b1')")] += 1 ds['fltvar'][ds.eval("A%B==('a2','b2')")] -= 1 ds['C'] = Factor('qw', repeat=10, tile=4) plot.Boxplot('fltvar', 'A%B%C', ds=ds, show=False) # long labels ds['A'].relabel({ 'a1': 'a very long label', 'a2': 'another very long label' }) p = plot.Barplot('fltvar', 'A%B', ds=ds, show=False) labels = p._ax.get_xticklabels() bbs = [l.get_window_extent() for l in labels] for i in xrange(len(bbs) - 1): ok_(bbs[i].x1 < bbs[i + 1].x0)
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_dataobjects(): "Test handing MNE-objects as data-objects" ds = datasets.get_mne_sample(sns=True) ds['C'] = Factor(ds['index'] > 155, labels={False: 'a', True: 'b'}) sds = ds.sub("side % C != ('L', 'b')") ads = sds.aggregate('side % C') eq_(ads.n_cases, 3)
def test_clusters(): "test plot.uts cluster plotting functions" ds = datasets.get_uts() A = ds['A'] B = ds['B'] Y = ds['uts'] # fixed effects model res = testnd.anova(Y, A * B) p = plot.UTSClusters(res, title="Fixed Effects Model", show=False) p.close() # random effects model: subject = Factor(range(15), tile=4, random=True, name='subject') res = testnd.anova(Y, A * B * subject, samples=2) p = plot.UTSClusters(res, title="Random Effects Model", show=False) p.close() # plot UTSStat p = plot.UTSStat(Y, A % B, match=subject, show=False) p.set_clusters(res.clusters) p.close() p = plot.UTSStat(Y, A, Xax=B, match=subject, show=False) p.close()
def test_dataset(): "Basic dataset operations" ds = Dataset() # naming ds['f'] = Factor('abab') eq_(ds['f'].name, 'f') # ds.add() assert_raises(ValueError, ds.add, Factor('aabb')) ds.add(Factor('aabb', name='g')) eq_(ds['g'].name, 'g') # ds.update() ds = Dataset() ds.update({'f': Factor('abab')}) eq_(ds['f'].name, 'f')
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_test_experiment(): "Test event labeling with the EventExperiment subclass of MneExperiment" e = EventExperiment() # test defaults eq_(e.get('session'), 'cheese') eq_(e.get('model'), 'name') # test event labeling ds = e.label_events(gen_triggers()) name = Factor([e.variables['name'][t] for t in TRIGGERS], name='name') assert_dataobj_equal(ds['name'], name) tgt = ds['trigger'].as_factor(e.variables['backorder'], 'backorder') assert_dataobj_equal(ds['backorder'], tgt) tgt = ds['trigger'].as_factor(e.variables['taste'], 'taste') assert_dataobj_equal(ds['taste'], tgt) assert_array_equal(ds['i_start'], I_START) assert_array_equal(ds['subject'] == SUBJECT, True) # tests disabled (trigger-shift applied in load_events): # --- # assert_equal(ds['i_start'], I_START + round(0.03 * SAMPLINGRATE)) # # test without trigger shift # e.trigger_shift = 0 # ds = e.label_events(gen_triggers()) # assert_equal(ds['i_start'], I_START) # # trigger shift dict # e2 = EventExperimentTriggerShiftDict('', False) # ds = e2.label_events(gen_triggers()) # assert_equal(ds['i_start'], I_START + round(0.04 * SAMPLINGRATE)) # epochs eq_(e._epochs['cheese'].tmin, -0.2) eq_(e._epochs['cheese-leicester'].tmin, -0.1) eq_(e._epochs['cheese-tilsit'].tmin, -0.2)
def test_complement(): """Test design.complement()""" ds = Dataset() ds['A'] = Factor('abcabc') ds['B'] = Factor('bcabca') ds['C'] = Factor('cabcab') # underspecified assert_raises(ValueError, complement, ['A'], ds=ds) # correct comp = complement(['A', 'B'], ds=ds) ok_(np.all(comp == ds['C']), "Complement yielded %s instead of " "%s." % (comp, ds['C'])) # overspecified assert_raises(ValueError, complement, ['A', 'B', 'C'], ds=ds)
def test_model(): "Test Model class" # model repr a = Factor('ab', repeat=2, name='a') b = Factor('ab', tile=2, name='b') m = a * b eq_(repr(m), "a + b + a % b") # 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)
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_complement(): """Test design.complement()""" ds = Dataset() ds['A'] = Factor('abcabc') ds['B'] = Factor('bcabca') ds['C'] = Factor('cabcab') # underspecified with pytest.raises(ValueError): complement(['A'], ds=ds) # correct comp = complement(['A', 'B'], ds=ds) assert np.all(comp == ds['C']), f"Complement yielded {comp} instead of {ds['C']}" # overspecified with pytest.raises(ValueError): complement(['A', 'B', 'C'], ds=ds)
def test_repmeas(): "Test table.repmeas (repeated measures table)" ds = datasets.get_uv() print table.repmeas('fltvar', 'A', 'rm', ds=ds) print table.repmeas('fltvar', 'A%B', 'rm', ds=ds) print table.repmeas('fltvar', 'A', 'B%rm', ds=ds) # test naturalization of cellnames ds['ANum'] = Factor(ds['A'], labels={'a1': '1', 'a2': '2'}) print table.repmeas('fltvar', 'ANum', 'rm', ds=ds)
def test_datalist(): "Test Datalist class" dl = Datalist(list(range(10))) # indexing eq_(dl[3], 3) x = dl[:3] assert_is_instance(x, Datalist) assert_array_equal(x, list(range(3))) assert_array_equal(dl[8:], list(range(8, 10))) x = dl[np.arange(10) < 3] assert_is_instance(x, Datalist) assert_array_equal(x, list(range(3))) assert_array_equal(dl[np.arange(3)], list(range(3))) # __add__ x = dl + list(range(10, 12)) assert_is_instance(x, Datalist) assert_array_equal(x, list(range(12))) # aggregate x = dl.aggregate(Factor('ab', repeat=5)) assert_is_instance(x, Datalist) assert_array_equal(x, [2.0, 7.0]) # repr dl = Datalist([['a', 'b'], [], ['a']]) eq_(str(dl), "[['a', 'b'], [], ['a']]") dl = Datalist([['a', 'b'], [], ['a']], fmt='strlist') eq_(str(dl), '[[a, b], [], [a]]') eq_(str(dl[:2]), '[[a, b], []]') # eq a = Datalist([[], [1], [], [1]]) b = Datalist([[], [], [2], [1]]) assert_array_equal(a == b, [True, False, False, True]) assert_array_equal(a != b, [False, True, True, False]) # deepcopy ac = deepcopy(a) ok_(ac is not a) assert_array_equal(ac, a) ac[0].append(1) assert_array_equal(ac == a, [False, True, True, True]) # __setitem__ ac[:2] = (1, 2) assert_array_equal(ac == [1, 2, [], [1]], True) ac[np.arange(2, 4)] = [3, 4] assert_array_equal(ac == list(range(1, 5)), True) assert_raises(ValueError, ac.__setitem__, np.arange(2), np.arange(3)) # update a._update_listlist(b) assert_array_equal(a, [[], [1], [2], [1]])
def test_dataset_indexing(): """Test Dataset indexing""" ds = datasets.get_uv() # indexing values eq_(ds['A', 1], ds['A'][1]) eq_(ds[1, 'A'], ds['A'][1]) # indexing variables assert_dataobj_equal(ds[:, 'A'], ds['A']) assert_dataobj_equal(ds['A', :], ds['A']) assert_dataobj_equal(ds[:10, 'A'], ds['A'][:10]) assert_dataobj_equal(ds['A', :10], ds['A'][:10]) # new Dataset through indexing ds2 = Dataset() ds2['A'] = ds['A'] assert_dataset_equal(ds[('A',)], ds2) ds2['B'] = ds['B'] assert_dataset_equal(ds['A', 'B'], ds2) assert_dataset_equal(ds[('A', 'B'), :10], ds2[:10]) assert_dataset_equal(ds[:10, ('A', 'B')], ds2[:10]) # assigning value ds[2, 'A'] = 'hello' eq_(ds[2, 'A'], 'hello') ds['A', 2] = 'not_hello' eq_(ds[2, 'A'], 'not_hello') # assigning new factor ds['C', :] = 'c' ok_(np.all(ds.eval("C == 'c'"))) # assigning new Var ds['D1', :] = 5. ds[:, 'D2'] = 5. assert_array_equal(ds['D1'], 5) assert_array_equal(ds['D2'], 5) # test illegal names f = Factor('aaabbb') assert_raises(ValueError, ds.__setitem__, '%dsa', f) assert_raises(ValueError, ds.__setitem__, '432', f) assert_raises(ValueError, ds.__setitem__, ('%dsa', slice(None)), 'value') assert_raises(ValueError, ds.__setitem__, (slice(None), '%dsa'), 'value') assert_raises(ValueError, ds.__setitem__, ('432', slice(None)), 4.) assert_raises(ValueError, ds.__setitem__, (slice(None), '432'), 4.) # deleting items del ds['A'] ok_('A' not in ds) assert_raises(KeyError, ds.__getitem__, 'A') del ds['B', 'rm'] ok_('B' not in ds and 'rm' not in ds)
def test_boxplot(): "Test plot.uv.boxplot" plot.configure_backend(False, False) ds = datasets.get_uv() plot.uv.Boxplot('fltvar', 'A%B', match='rm', ds=ds) plt.close('all') # many pairwise significances ds['fltvar'][ds.eval("A%B==('a1','b1')")] += 1 ds['fltvar'][ds.eval("A%B==('a2','b2')")] -= 1 ds['C'] = Factor('qw', rep=10, tile=4) plot.uv.Boxplot('fltvar', 'A%B%C', ds=ds)
def test_factor_relabel(): "Test Factor.relabel() method" f = Factor('aaabbbccc') f.relabel({'a': 'd'}) assert_array_equal(f, Factor('dddbbbccc')) f.relabel({'d': 'c', 'c': 'd'}) assert_array_equal(f, Factor('cccbbbddd')) f.relabel({'d': 'c'}) assert_array_equal(f, Factor('cccbbbccc')) assert_raises(KeyError, f.relabel, {'a':'c'})
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_factor(): "Test basic Factor functionality" # 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]) assert_array_equal(f.label_length(), lens) # 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)
def test_interaction(): "Test Interaction" ds = datasets.get_uv() A = ds['A'] B = ds['B'] i = A % B # eq for sequence assert_array_equal(i == A % B, True) assert_array_equal(i == B % A, False) assert_array_equal(i == A, False) assert_array_equal(i == ds['fltvar'], False) assert_array_equal(ds.eval("A%B") == Factor(ds['A']) % B, True) # eq for element for a, b in product(A.cells, B.cells): assert_array_equal(i == (a, b), np.logical_and(A == a, B == b)) # Interaction.as_factor() a = Factor('aabb') i = a % Factor('cdcd') assert_dataobj_equal(i.as_factor(), Factor(['a c', 'a d', 'b c', 'b d'])) i = a % Factor(['c', '', 'c', '']) assert_dataobj_equal(i.as_factor(), Factor(['a c', 'a', 'b c', 'b'])) # pickling ip = pickle.loads(pickle.dumps(i)) assert_dataobj_equal(ip, i)
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_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_interaction(): "Test Interaction" ds = datasets.get_uv() A = ds['A'] B = ds['B'] i = A % B # eq for sequence assert_array_equal(i == A % B, True) assert_array_equal(i == B % A, False) assert_array_equal(i == A, False) assert_array_equal(i == ds['fltvar'], False) assert_array_equal(ds.eval("A%B") == Factor(ds['A']) % B, True) # eq for element for a, b in product(A.cells, B.cells): assert_array_equal(i == (a, b), np.logical_and(A == a, B == b))
def test_timeplot(): "Test plot.Timeplot" ds = datasets.get_loftus_masson_1994() ds['cat'] = Factor([int(s) > 5 for s in ds['subject']], labels={ True: 'a', False: 'b' }) plot.Timeplot('n_recalled', 'subject', 'exposure', ds=ds) plot.Timeplot('n_recalled', 'cat', 'exposure', ds=ds) plot.Timeplot('n_recalled', 'cat', 'exposure', 'subject', ds=ds, x_jitter=True)
def test_repmeas(): "Test table.repmeas (repeated measures table)" ds = datasets.get_uv() print(table.repmeas('fltvar', 'A', 'rm', ds=ds)) print(table.repmeas('fltvar', 'A%B', 'rm', ds=ds)) print(table.repmeas('fltvar', 'A', 'B%rm', ds=ds)) # with int model ds['Bv'] = ds['B'].as_var({'b1': 1, 'b2': 2}) print(table.repmeas('fltvar', 'A', 'Bv%rm', ds=ds)) # test naturalization of cellnames ds['ANum'] = Factor(ds['A'], labels={'a1': '1', 'a2': '2'}) print(table.repmeas('fltvar', 'ANum', 'rm', ds=ds)) # with empty cell name ds['A'].update_labels({'a1': ''}) print(table.repmeas('fltvar', 'A', 'rm', ds=ds))
def test_factor(): "Test basic Factor functionality" # removing a cell f = Factor('aabbcc') eq_(f.cells, ('a', 'b', 'c')) f[f == 'c'] = 'a' eq_(f.cells, ('a', 'b')) # 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]) assert_array_equal(f.label_length(), lens)
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_boxplot(): "Test plot.Boxplot" ds = datasets.get_uv(nrm=True) plot.Boxplot('fltvar', 'A%B', match='rm', ds=ds) # boxplot args plot.Boxplot('fltvar', 'A%B', match='rm', ds=ds, showmeans=True) # one category plot.Boxplot('fltvar', ds=ds, test=False) plot.Boxplot('fltvar', ds=ds) plot.Boxplot('fltvar', match='rm', ds=ds) plot.Boxplot('fltvar', 'A%B', match='rm', ds=ds, label_fliers=True) # cells plot.Boxplot('fltvar', 'A%B', cells=(('a2', 'b2'), ('a1', 'b1')), ds=ds) plot.Boxplot('fltvar', 'A%B', match='rm', cells=(('a2', 'b2'), ('a1', 'b1')), ds=ds) # many pairwise significances ds['fltvar'][ds.eval("A%B==('a1','b1')")] += 1 ds['fltvar'][ds.eval("A%B==('a2','b2')")] -= 1 ds['C'] = Factor('qw', repeat=10, tile=4) plot.Boxplot('fltvar', 'A%B%C', ds=ds) # long labels ds['A'].update_labels({ 'a1': 'a very long label', 'a2': 'another very long label' }) p = plot.Boxplot('fltvar', 'A%B', ds=ds) labels = p._ax.get_xticklabels() bbs = [l.get_window_extent() for l in labels] for i in range(len(bbs) - 1): assert bbs[i].x1 < bbs[i + 1].x0 # nested rm plot.Boxplot('fltvar', 'A%B', match='nrm', ds=ds) plot.Boxplot('fltvar', 'A%B', match='nrm', ds=ds, sub="nrm != 's001'")
def test_vec_source(): "Test vector source space" ds = datasets.get_mne_sample(0, 0.1, (0, 0), src='vol', sub="(modality=='A') & (side == 'L')", ori='vector', stc=True) # conversion: vector stc = ds[0, 'stc'] stc2 = load.fiff.stc_ndvar([stc, stc], ds.info['subject'], 'vol-10', ds.info['subjects_dir']) assert_dataobj_equal(stc2[1], ds[0, 'src'], name=False) # non-vector if hasattr(stc, 'magnitude'): # added in mne 0.18 stc = stc.magnitude() ndvar = load.fiff.stc_ndvar(stc, ds.info['subject'], 'vol-10', ds.info['subjects_dir']) assert_dataobj_equal(ndvar, ds[0, 'src'].norm('space'), name=False) # test res = testnd.Vector('src', ds=ds, samples=2) clusters = res.find_clusters() assert_array_equal(clusters['n_sources'], [799, 1, 7, 1, 2, 1]) # NDVar v = ds['src'] assert v.sub(source='lh', time=0).shape == (72, 712, 3) # parc v = ds[0, 'src'] v = set_parc(v, Factor('abcdefg', repeat=227)) v1 = v.sub(source='a') assert len(v1.source) == 227 v2 = v.sub(source=('b', 'c')) assert len(v2.source) == 454 assert 'b' in v2.source.parc assert 'd' not in v2.source.parc with pytest.raises(IndexError): v.sub(source='ab') with pytest.raises(IndexError): v.sub(source=['a', 'bc'])
def test_test_experiment(): "Test event labeling with the EventExperiment subclass of MneExperiment" e = EventExperiment('', False) # test defaults eq_(e.get('experiment'), 'cheese') eq_(e.get('model'), 'name') # test event labeling ds = e.label_events(gen_triggers()) name = Factor([e.variables['name'][t] for t in TRIGGERS], name='name') assert_dataobj_equal(ds['name'], name) tgt = ds['trigger'].as_factor(e.variables['backorder'], 'backorder') assert_dataobj_equal(ds['backorder'], tgt) tgt = ds['trigger'].as_factor(e.variables['taste'], 'taste') assert_dataobj_equal(ds['taste'], tgt) assert_equal(ds['i_start'], I_START + round((0.03 * SAMPLINGRATE))) assert_equal(ds['subject'] == SUBJECT, True) # test without trigger shift e.trigger_shift = 0 ds = e.label_events(gen_triggers()) assert_equal(ds['i_start'], I_START)
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(zip('abc', 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'))