Package mvpa :: Package tests :: Module test_datasetfx
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_datasetfx

 1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  # vi: set ft=python sts=4 ts=4 sw=4 et: 
 3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 4  # 
 5  #   See COPYING file distributed along with the PyMVPA package for the 
 6  #   copyright and license terms. 
 7  # 
 8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 9  """Unit tests for PyMVPA miscelaneouse functions operating on datasets""" 
10   
11  import unittest 
12   
13  import numpy as N 
14   
15  from mvpa.base import externals 
16  from mvpa.datasets import Dataset 
17  from mvpa.datasets.miscfx import removeInvariantFeatures, coarsenChunks 
18   
19  from mvpa.misc.data_generators import normalFeatureDataset 
20   
21 -class MiscDatasetFxTests(unittest.TestCase):
22
24 r = N.random.normal(size=(3,1)) 25 ds = Dataset(samples=N.hstack((N.zeros((3,2)), r)), 26 labels=1) 27 28 self.failUnless(ds.nfeatures == 3) 29 30 dsc = removeInvariantFeatures(ds) 31 32 self.failUnless(dsc.nfeatures == 1) 33 self.failUnless((dsc.samples == r).all())
34 35
36 - def testCoarsenChunks(self):
37 """Just basic testing for now""" 38 chunks = [1,1,2,2,3,3,4,4] 39 ds = Dataset(samples=N.arange(len(chunks)).reshape( 40 (len(chunks),1)), labels=[1]*8, chunks=chunks) 41 coarsenChunks(ds, nchunks=2) 42 chunks1 = coarsenChunks(chunks, nchunks=2) 43 self.failUnless((chunks1 == ds.chunks).all()) 44 self.failUnless((chunks1 == N.asarray([0,0,0,0,1,1,1,1])).all()) 45 46 ds2 = Dataset(samples=N.arange(len(chunks)).reshape( 47 (len(chunks),1)), labels=[1]*8) 48 coarsenChunks(ds2, nchunks=2) 49 self.failUnless((chunks1 == ds.chunks).all())
50
51 - def testBinds(self):
52 ds = normalFeatureDataset() 53 ds_data = ds.samples.copy() 54 ds_chunks = ds.chunks.copy() 55 self.failUnless(N.all(ds.samples == ds_data)) # sanity check 56 57 funcs = ['zscore', 'coarsenChunks'] 58 if externals.exists('scipy'): 59 funcs.append('detrend') 60 61 for f in funcs: 62 eval('ds.%s()' % f) 63 self.failUnless(N.any(ds.samples != ds_data) or 64 N.any(ds.chunks != ds_chunks), 65 msg="We should have modified original dataset with %s" % f) 66 ds.samples = ds_data.copy() 67 ds.chunks = ds_chunks.copy() 68 69 # and some which should just return results 70 for f in ['aggregateFeatures', 'removeInvariantFeatures', 71 'getSamplesPerChunkLabel']: 72 res = eval('ds.%s()' % f) 73 self.failUnless(res is not None, 74 msg='We should have got result from function %s' % f) 75 self.failUnless(N.all(ds.samples == ds_data), 76 msg="Function %s should have not modified original dataset" % f)
77 78
79 -def suite():
80 return unittest.makeSuite(MiscDatasetFxTests)
81 82 83 if __name__ == '__main__': 84 import runner 85