本文整理汇总了Python中mvpa.datasets.base.Dataset类的典型用法代码示例。如果您正苦于以下问题:Python Dataset类的具体用法?Python Dataset怎么用?Python Dataset使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dataset类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_query_engine
def test_query_engine():
data = np.arange(54)
# indices in 3D
ind = np.transpose((np.ones((3, 3, 3)).nonzero()))
# sphere generator for 3 elements diameter
sphere = ne.Sphere(1)
# dataset with just one "space"
ds = Dataset([data, data], fa={'s_ind': np.concatenate((ind, ind))})
# and the query engine attaching the generator to the "index-space"
qe = ne.IndexQueryEngine(s_ind=sphere)
# cannot train since the engine does not know about the second space
assert_raises(ValueError, qe.train, ds)
# now do it again with a full spec
ds = Dataset([data, data], fa={'s_ind': np.concatenate((ind, ind)),
't_ind': np.repeat([0,1], 27)})
qe = ne.IndexQueryEngine(s_ind=sphere, t_ind=None)
qe.train(ds)
# internal representation check
# YOH: invalid for new implementation with lookup tables (dictionaries)
#assert_array_equal(qe._searcharray,
# np.arange(54).reshape(qe._searcharray.shape) + 1)
# should give us one corner, collapsing the 't_ind'
assert_array_equal(qe(s_ind=(0, 0, 0)),
[0, 1, 3, 9, 27, 28, 30, 36])
# directly specifying an index for 't_ind' without having an ROI
# generator, should give the same corner, but just once
assert_array_equal(qe(s_ind=(0, 0, 0), t_ind=0), [0, 1, 3, 9])
# just out of the mask -- no match
assert_array_equal(qe(s_ind=(3, 3, 3)), [])
# also out of the mask -- but single match
assert_array_equal(qe(s_ind=(2, 2, 3), t_ind=1), [53])
# query by id
assert_array_equal(qe(s_ind=(0, 0, 0), t_ind=0), qe[0])
assert_array_equal(qe(s_ind=(0, 0, 0), t_ind=[0, 1]),
qe(s_ind=(0, 0, 0)))
# should not fail if t_ind is outside
assert_array_equal(qe(s_ind=(0, 0, 0), t_ind=[0, 1, 10]),
qe(s_ind=(0, 0, 0)))
# should fail if asked about some unknown thing
assert_raises(ValueError, qe.__call__, s_ind=(0, 0, 0), buga=0)
# Test by using some literal feature atttribute
ds.fa['lit'] = ['roi1', 'ro2', 'r3']*18
# should work as well as before
assert_array_equal(qe(s_ind=(0, 0, 0)), [0, 1, 3, 9, 27, 28, 30, 36])
# should fail if asked about some unknown (yet) thing
assert_raises(ValueError, qe.__call__, s_ind=(0,0,0), lit='roi1')
# Create qe which can query literals as well
qe_lit = ne.IndexQueryEngine(s_ind=sphere, t_ind=None, lit=None)
qe_lit.train(ds)
# should work as well as before
assert_array_equal(qe_lit(s_ind=(0, 0, 0)), [0, 1, 3, 9, 27, 28, 30, 36])
# and subselect nicely -- only /3 ones
assert_array_equal(qe_lit(s_ind=(0, 0, 0), lit='roi1'),
[0, 3, 9, 27, 30, 36])
assert_array_equal(qe_lit(s_ind=(0, 0, 0), lit=['roi1', 'ro2']),
[0, 1, 3, 9, 27, 28, 30, 36])
开发者ID:B-Rich,项目名称:PyMVPA,代码行数:59,代码来源:test_neighborhood.py
示例2: test_mergeds
def test_mergeds():
data0 = Dataset.from_wizard(np.ones((5, 5)), targets=1)
data0.fa['one'] = np.ones(5)
data1 = Dataset.from_wizard(np.ones((5, 5)), targets=1, chunks=1)
data1.fa['one'] = np.zeros(5)
data2 = Dataset.from_wizard(np.ones((3, 5)), targets=2, chunks=1)
data3 = Dataset.from_wizard(np.ones((4, 5)), targets=2)
data4 = Dataset.from_wizard(np.ones((2, 5)), targets=3, chunks=2)
data4.fa['test'] = np.arange(5)
# cannot merge if there are attributes missing in one of the datasets
assert_raises(DatasetError, data1.append, data0)
merged = data1.copy()
merged.append(data2)
ok_( merged.nfeatures == 5 )
l12 = [1]*5 + [2]*3
l1 = [1]*8
ok_((merged.targets == l12).all())
ok_((merged.chunks == l1).all())
data_append = data1.copy()
data_append.append(data2)
ok_(data_append.nfeatures == 5)
ok_((data_append.targets == l12).all())
ok_((data_append.chunks == l1).all())
#
# appending
#
# we need the same samples attributes in both datasets
assert_raises(DatasetError, data2.append, data3)
#
# vstacking
#
if __debug__:
# tested only in __debug__
assert_raises(ValueError, vstack, (data0, data1, data2, data3))
datasets = (data1, data2, data4)
merged = vstack(datasets)
assert_equal(merged.shape,
(np.sum([len(ds) for ds in datasets]), data1.nfeatures))
assert_true('test' in merged.fa)
assert_array_equal(merged.sa.targets, [1]*5 + [2]*3 + [3]*2)
#
# hstacking
#
assert_raises(ValueError, hstack, datasets)
datasets = (data0, data1)
merged = hstack(datasets)
assert_equal(merged.shape,
(len(data1), np.sum([ds.nfeatures for ds in datasets])))
assert_true('chunks' in merged.sa)
assert_array_equal(merged.fa.one, [1]*5 + [0]*5)
开发者ID:geeragh,项目名称:PyMVPA,代码行数:59,代码来源:test_datasetng.py
示例3: test_labelpermutation_randomsampling
def test_labelpermutation_randomsampling():
ds = Dataset.from_wizard(np.ones((5, 1)), targets=range(5), chunks=1)
ds.append(Dataset.from_wizard(np.ones((5, 1)) + 1, targets=range(5), chunks=2))
ds.append(Dataset.from_wizard(np.ones((5, 1)) + 2, targets=range(5), chunks=3))
ds.append(Dataset.from_wizard(np.ones((5, 1)) + 3, targets=range(5), chunks=4))
ds.append(Dataset.from_wizard(np.ones((5, 1)) + 4, targets=range(5), chunks=5))
# use subclass for testing if it would survive
ds.samples = ds.samples.view(myarray)
ok_(ds.get_nsamples_per_attr('targets') == {0:5, 1:5, 2:5, 3:5, 4:5})
sample = ds.random_samples(2)
ok_(sample.get_nsamples_per_attr('targets').values() == [ 2, 2, 2, 2, 2 ])
ok_((ds.sa['chunks'].unique == range(1, 6)).all())
# keep the orig labels
orig_labels = ds.targets[:]
# also keep the orig dataset, but SHALLOW copy and leave everything
# else as a view!
ods = copy.copy(ds)
ds.permute_targets()
# some permutation should have happened
assert_false((ds.targets == orig_labels).all())
# but the original dataset should be uneffected
assert_array_equal(ods.targets, orig_labels)
# array subclass survives
ok_(isinstance(ods.samples, myarray))
# samples are really shared
ds.samples[0, 0] = 123456
assert_array_equal(ds.samples, ods.samples)
# and other samples attributes too
ds.chunks[0] = 9876
assert_array_equal(ds.chunks, ods.chunks)
# try to permute on custom target
ds = ods.copy()
otargets = ods.sa.targets.copy()
ds.sa['custom'] = ods.sa.targets.copy()
assert_array_equal(ds.sa.custom, otargets)
assert_array_equal(ds.sa.targets, otargets)
ds.permute_targets(targets_attr='custom')
# original targets should still match
assert_array_equal(ds.sa.targets, otargets)
# but custom should get permuted
assert_false((ds.sa.custom == otargets).all())
开发者ID:geeragh,项目名称:PyMVPA,代码行数:50,代码来源:test_datasetng.py
示例4: test_labelpermutation_randomsampling
def test_labelpermutation_randomsampling():
ds = Dataset.from_wizard(np.ones((5, 10)), targets=range(5), chunks=1)
for i in xrange(1, 5):
ds.append(Dataset.from_wizard(np.ones((5, 10)) + i,
targets=range(5), chunks=i+1))
# assign some feature attributes
ds.fa['roi'] = np.repeat(np.arange(5), 2)
ds.fa['lucky'] = np.arange(10)%2
# use subclass for testing if it would survive
ds.samples = ds.samples.view(myarray)
ok_(ds.get_nsamples_per_attr('targets') == {0:5, 1:5, 2:5, 3:5, 4:5})
sample = ds.random_samples(2)
ok_(sample.get_nsamples_per_attr('targets').values() == [ 2, 2, 2, 2, 2 ])
ok_((ds.sa['chunks'].unique == range(1, 6)).all())
开发者ID:esc,项目名称:PyMVPA,代码行数:15,代码来源:test_datasetng.py
示例5: test_icamapper
def test_icamapper():
# data: 40 sample feature line in 2d space (40x2; samples x features)
samples = np.vstack([np.arange(40.) for i in range(2)]).T
samples -= samples.mean()
samples += np.random.normal(size=samples.shape, scale=0.1)
ndlin = Dataset(samples)
pm = ICAMapper()
pm.train(ndlin.copy())
assert_equal(pm.proj.shape, (2, 2))
p = pm.forward(ndlin.copy())
assert_equal(p.shape, (40, 2))
# check that the mapped data can be fully recovered by 'reverse()'
assert_array_almost_equal(pm.reverse(p), ndlin)
开发者ID:arokem,项目名称:PyMVPA,代码行数:15,代码来源:test_mdp.py
示例6: _call
def _call(self, dataset):
# just for the beauty of it
X = self._design
# precompute transformation is not yet done
if self._inv_design is None:
self._inv_ip = (X.T * X).I
self._inv_design = self._inv_ip * X.T
# get parameter estimations for all features at once
# (betas x features)
betas = self._inv_design * dataset.samples
# charge state
self.ca.pe = pe = betas.T.A
# if betas and no z-stats are desired return them right away
if not self._voi == 'pe' or self.ca.is_enabled('zstat'):
# compute residuals
residuals = X * betas
residuals -= dataset.samples
# estimates of the parameter variance and compute zstats
# assumption of mean(E) == 0 and equal variance
# XXX next lines ignore off-diagonal elements and hence covariance
# between regressors. The humble being writing these lines asks the
# god of statistics for forgives, because it knows not what it does
diag_ip = np.diag(self._inv_ip)
# (features x betas)
beta_vars = np.array([ r.var() * diag_ip for r in residuals.T ])
# (parameter x feature)
zstat = pe / np.sqrt(beta_vars)
# charge state
self.ca.zstat = zstat
if self._voi == 'pe':
# return as (beta x feature)
result = Dataset(pe.T)
elif self._voi == 'zstat':
# return as (zstat x feature)
result = Dataset(zstat.T)
else:
# we shall never get to this point
raise ValueError, \
"Unknown variable of interest '%s'" % str(self._voi)
result.sa['regressor'] = np.arange(len(result))
return result
开发者ID:B-Rich,项目名称:PyMVPA,代码行数:48,代码来源:glm.py
示例7: test_h5py_io
def test_h5py_io():
skip_if_no_external('h5py')
tempdir = tempfile.mkdtemp()
# store random dataset to file
ds = datasets['3dlarge']
ds.save(os.path.join(tempdir, 'plain.hdf5'))
# reload and check for identity
ds2 = Dataset.from_hdf5(os.path.join(tempdir, 'plain.hdf5'))
assert_array_equal(ds.samples, ds2.samples)
for attr in ds.sa:
assert_array_equal(ds.sa[attr].value, ds2.sa[attr].value)
for attr in ds.fa:
assert_array_equal(ds.fa[attr].value, ds2.fa[attr].value)
assert_true(len(ds.a.mapper), 2)
# since we have no __equal__ do at least some comparison
if __debug__:
# debug mode needs special test as it enhances the repr output
# with module info and id() appendix for objects
assert_equal('#'.join(repr(ds.a.mapper).split('#')[:-1]),
'#'.join(repr(ds2.a.mapper).split('#')[:-1]))
else:
assert_equal(repr(ds.a.mapper), repr(ds2.a.mapper))
#cleanup temp dir
shutil.rmtree(tempdir, ignore_errors=True)
开发者ID:esc,项目名称:PyMVPA,代码行数:29,代码来源:test_datasetng.py
示例8: test_multidim_attrs
def test_multidim_attrs():
samples = np.arange(24).reshape(2, 3, 4)
# have a dataset with two samples -- mapped from 2d into 1d
# but have 2d labels and 3d chunks -- whatever that is
ds = Dataset.from_wizard(samples.copy(),
targets=samples.copy(),
chunks=np.random.normal(size=(2,10,4,2)))
assert_equal(ds.nsamples, 2)
assert_equal(ds.nfeatures, 12)
assert_equal(ds.sa.targets.shape, (2,3,4))
assert_equal(ds.sa.chunks.shape, (2,10,4,2))
# try slicing
subds = ds[0]
assert_equal(subds.nsamples, 1)
assert_equal(subds.nfeatures, 12)
assert_equal(subds.sa.targets.shape, (1,3,4))
assert_equal(subds.sa.chunks.shape, (1,10,4,2))
# add multidim feature attr
fattr = ds.mapper.forward(samples)
assert_equal(fattr.shape, (2,12))
# should puke -- first axis is #samples
assert_raises(ValueError, ds.fa.__setitem__, 'moresamples', fattr)
# but that should be fine
ds.fa['moresamples'] = fattr.T
assert_equal(ds.fa.moresamples.shape, (12,2))
开发者ID:geeragh,项目名称:PyMVPA,代码行数:27,代码来源:test_datasetng.py
示例9: test_labelschunks_access
def test_labelschunks_access():
samples = np.arange(12).reshape((4, 3)).view(myarray)
labels = range(4)
chunks = [1, 1, 2, 2]
ds = Dataset.from_wizard(samples, labels, chunks)
# array subclass survives
ok_(isinstance(ds.samples, myarray))
assert_array_equal(ds.targets, labels)
assert_array_equal(ds.chunks, chunks)
# moreover they should point to the same thing
ok_(ds.targets is ds.sa.targets)
ok_(ds.targets is ds.sa['targets'].value)
ok_(ds.chunks is ds.sa.chunks)
ok_(ds.chunks is ds.sa['chunks'].value)
# assignment should work at all levels including 1st
ds.targets = chunks
assert_array_equal(ds.targets, chunks)
ok_(ds.targets is ds.sa.targets)
ok_(ds.targets is ds.sa['targets'].value)
# test broadcasting
# but not for plain scalars
assert_raises(ValueError, ds.set_attr, 'sa.bc', 5)
# and not for plain plain str
assert_raises(TypeError, ds.set_attr, 'sa.bc', "mike")
# but for any iterable of len == 1
ds.set_attr('sa.bc', (5,))
ds.set_attr('sa.dc', ["mike"])
assert_array_equal(ds.sa.bc, [5] * len(ds))
assert_array_equal(ds.sa.dc, ["mike"] * len(ds))
开发者ID:esc,项目名称:PyMVPA,代码行数:34,代码来源:test_datasetng.py
示例10: test_origmask_extraction
def test_origmask_extraction():
origdata = np.random.standard_normal((10, 2, 4, 3))
data = Dataset.from_wizard(origdata, targets=2, chunks=2)
# check with custom mask
sel = data[:, 5]
ok_(sel.samples.shape[1] == 1)
开发者ID:geeragh,项目名称:PyMVPA,代码行数:7,代码来源:test_datasetng.py
示例11: get_data
def get_data(self):
data = np.random.standard_normal(( 100, 2, 2, 2 ))
labels = np.concatenate( ( np.repeat( 0, 50 ),
np.repeat( 1, 50 ) ) )
chunks = np.repeat( range(5), 10 )
chunks = np.concatenate( (chunks, chunks) )
return Dataset.from_wizard(samples=data, targets=labels, chunks=chunks)
开发者ID:esc,项目名称:PyMVPA,代码行数:7,代码来源:test_ifs.py
示例12: test_feature_masking
def test_feature_masking():
mask = np.zeros((5, 3), dtype='bool')
mask[2, 1] = True
mask[4, 0] = True
data = Dataset.from_wizard(np.arange(60).reshape((4, 5, 3)),
targets=1, chunks=1, mask=mask)
# check simple masking
ok_(data.nfeatures == 2)
# selection should be idempotent
ok_(data[:, mask].nfeatures == data.nfeatures)
# check that correct feature get selected
assert_array_equal(data[:, 1].samples[:, 0], [12, 27, 42, 57])
# XXX put back when coord -> fattr is implemented
#ok_(tuple(data[:, 1].a.mapper.getInId(0)) == (4, 0))
ok_(data[:, 1].a.mapper.forward1(mask).shape == (1,))
# check sugarings
# XXX put me back
#self.failUnless(np.all(data.I == data.origids))
assert_array_equal(data.C, data.chunks)
assert_array_equal(data.UC, np.unique(data.chunks))
assert_array_equal(data.T, data.targets)
assert_array_equal(data.UT, np.unique(data.targets))
assert_array_equal(data.S, data.samples)
assert_array_equal(data.O, data.mapper.reverse(data.samples))
开发者ID:geeragh,项目名称:PyMVPA,代码行数:27,代码来源:test_datasetng.py
示例13: test_samples_shape
def test_samples_shape():
ds = Dataset.from_wizard(np.ones((10, 2, 3, 4)), targets=1, chunks=1)
ok_(ds.samples.shape == (10, 24))
# what happens to 1D samples
ds = Dataset(np.arange(5))
assert_equal(ds.shape, (5, 1))
assert_equal(ds.nfeatures, 1)
开发者ID:geeragh,项目名称:PyMVPA,代码行数:8,代码来源:test_datasetng.py
示例14: test_ex_from_masked
def test_ex_from_masked():
ds = Dataset.from_wizard(samples=np.atleast_2d(np.arange(5)).view(myarray),
targets=1, chunks=1)
# simple sequence has to be a single pattern
assert_equal(ds.nsamples, 1)
# array subclass survives
ok_(isinstance(ds.samples, myarray))
# check correct pattern layout (1x5)
assert_array_equal(ds.samples, [[0, 1, 2, 3, 4]])
# check for single label and origin
assert_array_equal(ds.targets, [1])
assert_array_equal(ds.chunks, [1])
# now try adding pattern with wrong shape
assert_raises(DatasetError, ds.append,
Dataset.from_wizard(np.ones((2,3)), targets=1, chunks=1))
# now add two real patterns
ds.append(Dataset.from_wizard(np.random.standard_normal((2, 5)),
targets=2, chunks=2))
assert_equal(ds.nsamples, 3)
assert_array_equal(ds.targets, [1, 2, 2])
assert_array_equal(ds.chunks, [1, 2, 2])
# test unique class labels
ds.append(Dataset.from_wizard(np.random.standard_normal((2, 5)),
targets=3, chunks=5))
assert_array_equal(ds.sa['targets'].unique, [1, 2, 3])
# test wrong attributes length
assert_raises(ValueError, Dataset.from_wizard,
np.random.standard_normal((4,2,3,4)), targets=[1, 2, 3],
chunks=2)
assert_raises(ValueError, Dataset.from_wizard,
np.random.standard_normal((4,2,3,4)), targets=[1, 2, 3, 4],
chunks=[2, 2, 2])
# no test one that is using from_masked
ds = datasets['3dlarge']
for a in ds.sa:
assert_equal(len(ds.sa[a].value), len(ds))
for a in ds.fa:
assert_equal(len(ds.fa[a].value), ds.nfeatures)
开发者ID:geeragh,项目名称:PyMVPA,代码行数:45,代码来源:test_datasetng.py
示例15: test_shape_conversion
def test_shape_conversion():
ds = Dataset.from_wizard(np.arange(24).reshape((2, 3, 4)).view(myarray),
targets=1, chunks=1)
# array subclass survives
ok_(isinstance(ds.samples, myarray))
assert_equal(ds.nsamples, 2)
assert_equal(ds.samples.shape, (2, 12))
assert_array_equal(ds.samples, [range(12), range(12, 24)])
开发者ID:geeragh,项目名称:PyMVPA,代码行数:9,代码来源:test_datasetng.py
示例16: test_featuregroup_mapper
def test_featuregroup_mapper():
ds = Dataset(np.arange(24).reshape(3,8))
ds.fa['roi'] = [0, 1] * 4
# just to check
ds.sa['chunks'] = np.arange(3)
# correct results
csamples = [[3, 4], [11, 12], [19, 20]]
croi = [0, 1]
cchunks = np.arange(3)
m = mean_group_feature(['roi'])
mds = m.forward(ds)
assert_equal(mds.shape, (3, 2))
assert_array_equal(mds.samples, csamples)
assert_array_equal(mds.fa.roi, np.unique([0, 1] * 4))
# FAs should simply remain the same
assert_array_equal(mds.sa.chunks, np.arange(3))
开发者ID:geeragh,项目名称:PyMVPA,代码行数:18,代码来源:test_fxmapper.py
示例17: test_icamapper
def test_icamapper():
# data: 40 sample feature line in 2d space (40x2; samples x features)
samples = np.vstack([np.arange(40.) for i in range(2)]).T
samples -= samples.mean()
samples += np.random.normal(size=samples.shape, scale=0.1)
ndlin = Dataset(samples)
pm = ICAMapper()
try:
pm.train(ndlin.copy())
assert_equal(pm.proj.shape, (2, 2))
p = pm.forward(ndlin.copy())
assert_equal(p.shape, (40, 2))
# check that the mapped data can be fully recovered by 'reverse()'
assert_array_almost_equal(pm.reverse(p), ndlin)
except mdp.NodeException:
# do not puke if the ICA did not converge at all -- that is not our
# fault but MDP's
pass
开发者ID:B-Rich,项目名称:PyMVPA,代码行数:19,代码来源:test_mdp.py
示例18: setUp
def setUp(self):
data = np.random.standard_normal(( 100, 3, 4, 2 ))
labels = np.concatenate( ( np.repeat( 0, 50 ),
np.repeat( 1, 50 ) ) )
chunks = np.repeat( range(5), 10 )
chunks = np.concatenate( (chunks, chunks) )
mask = np.ones( (3, 4, 2), dtype='bool')
mask[0,0,0] = 0
mask[1,3,1] = 0
self.dataset = Dataset.from_wizard(samples=data, targets=labels,
chunks=chunks, mask=mask)
开发者ID:esc,项目名称:PyMVPA,代码行数:11,代码来源:test_perturbsensana.py
示例19: test_pcamapper
def test_pcamapper():
# data: 40 sample feature line in 20d space (40x20; samples x features)
ndlin = Dataset(np.concatenate([np.arange(40)
for i in range(20)]).reshape(20,-1).T)
pm = PCAMapper()
# train PCA
assert_raises(mdp.NodeException, pm.train, ndlin)
ndlin.samples = ndlin.samples.astype('float')
ndlin_noise = ndlin.copy()
ndlin_noise.samples += np.random.random(size=ndlin.samples.shape)
# we have no variance for more than one PCA component, hence just one
# actual non-zero eigenvalue
assert_raises(mdp.NodeException, pm.train, ndlin)
pm.train(ndlin_noise)
assert_equal(pm.proj.shape, (20, 20))
# now project data into PCA space
p = pm.forward(ndlin.samples)
assert_equal(p.shape, (40, 20))
# check that the mapped data can be fully recovered by 'reverse()'
assert_array_almost_equal(pm.reverse(p), ndlin)
开发者ID:arokem,项目名称:PyMVPA,代码行数:21,代码来源:test_mdp.py
示例20: test_basic_datamapping
def test_basic_datamapping():
samples = np.arange(24).reshape((4, 3, 2)).view(myarray)
ds = Dataset.from_wizard(samples)
# array subclass survives
ok_(isinstance(ds.samples, myarray))
# mapper should end up in the dataset
ok_(ds.a.has_key('mapper'))
# check correct mapping
ok_(ds.nsamples == 4)
ok_(ds.nfeatures == 6)
开发者ID:geeragh,项目名称:PyMVPA,代码行数:14,代码来源:test_datasetng.py
注:本文中的mvpa.datasets.base.Dataset类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论