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       SequenceStats 
 19   
 20  from mvpa.misc.data_generators import normalFeatureDataset 
 21   
22 -class MiscDatasetFxTests(unittest.TestCase):
23
24 - def testInvarFeaturesRemoval(self):
25 r = N.random.normal(size=(3,1)) 26 ds = Dataset(samples=N.hstack((N.zeros((3,2)), r)), 27 labels=1) 28 29 self.failUnless(ds.nfeatures == 3) 30 31 dsc = removeInvariantFeatures(ds) 32 33 self.failUnless(dsc.nfeatures == 1) 34 self.failUnless((dsc.samples == r).all())
35 36
37 - def testCoarsenChunks(self):
38 """Just basic testing for now""" 39 chunks = [1,1,2,2,3,3,4,4] 40 ds = Dataset(samples=N.arange(len(chunks)).reshape( 41 (len(chunks),1)), labels=[1]*8, chunks=chunks) 42 coarsenChunks(ds, nchunks=2) 43 chunks1 = coarsenChunks(chunks, nchunks=2) 44 self.failUnless((chunks1 == ds.chunks).all()) 45 self.failUnless((chunks1 == N.asarray([0,0,0,0,1,1,1,1])).all()) 46 47 ds2 = Dataset(samples=N.arange(len(chunks)).reshape( 48 (len(chunks),1)), labels=[1]*8) 49 coarsenChunks(ds2, nchunks=2) 50 self.failUnless((chunks1 == ds.chunks).all())
51
52 - def testBinds(self):
53 ds = normalFeatureDataset() 54 ds_data = ds.samples.copy() 55 ds_chunks = ds.chunks.copy() 56 self.failUnless(N.all(ds.samples == ds_data)) # sanity check 57 58 funcs = ['zscore', 'coarsenChunks'] 59 if externals.exists('scipy'): 60 funcs.append('detrend') 61 62 for f in funcs: 63 eval('ds.%s()' % f) 64 self.failUnless(N.any(ds.samples != ds_data) or 65 N.any(ds.chunks != ds_chunks), 66 msg="We should have modified original dataset with %s" % f) 67 ds.samples = ds_data.copy() 68 ds.chunks = ds_chunks.copy() 69 70 # and some which should just return results 71 for f in ['aggregateFeatures', 'removeInvariantFeatures', 72 'getSamplesPerChunkLabel']: 73 res = eval('ds.%s()' % f) 74 self.failUnless(res is not None, 75 msg='We should have got result from function %s' % f) 76 self.failUnless(N.all(ds.samples == ds_data), 77 msg="Function %s should have not modified original dataset" % f)
78
79 - def testSequenceStat(self):
80 """Test sequence statistics 81 """ 82 order = 3 83 # Close to perfectly balanced one 84 sp = N.array([-1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 85 -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 86 -1, -1, -1, 1, 1, 1, 1, 1, -1], dtype=int) 87 rp = SequenceStats(sp, order=order) 88 self.failUnlessAlmostEqual(rp['sumabscorr'], 1.0) 89 self.failUnlessAlmostEqual(N.max(rp['corrcoef'] * (len(sp)-1) + 1.0), 0.0) 90 91 # Now some random but loong one still binary (boolean) 92 sb = (N.random.random_sample((1000,)) >= 0.5) 93 rb = SequenceStats(sb, order=order) 94 95 # Now lets do multiclass with literal labels 96 s5 = N.random.permutation(['f', 'bu', 'd', 0, 'zz']*200) 97 r5 = SequenceStats(s5, order=order) 98 99 # Degenerate one but still should be valid 100 s1 = ['aaa']*100 101 r1 = SequenceStats(s1, order=order) 102 103 # Generic conformance tests 104 for r in (rp, rb, r5, r1): 105 ulabels = r['ulabels'] 106 nlabels = len(r['ulabels']) 107 cbcounts = r['cbcounts'] 108 self.failUnlessEqual(len(cbcounts), order) 109 for cb in cbcounts: 110 self.failUnlessEqual(N.asarray(cb).shape, (nlabels, nlabels)) 111 # Check if str works fine 112 sr = str(r)
113 # TODO: check the content 114
115 -def suite():
116 return unittest.makeSuite(MiscDatasetFxTests)
117 118 119 if __name__ == '__main__': 120 import runner 121