Home | Trees | Indices | Help |
|
---|
|
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 transformers.""" 10 11 import unittest 12 import numpy as N 13 14 from mvpa.base import externals 15 16 from mvpa.misc.transformers import Absolute, OneMinus, RankOrder, \ 17 ReverseRankOrder, L1Normed, L2Normed, OverAxis, \ 18 DistPValue, FirstAxisSumNotZero 19 20 from tests_warehouse import sweepargs, datasets 21 22 from mvpa.base import cfg 2325 29129 13031 # generate 100 values (gaussian noise mean -1000 -> all negative) 32 out = Absolute(N.random.normal(-1000, size=100)) 33 34 self.failUnless(out.min() >= 0) 35 self.failUnless(len(out) == 100)36 4143 src = [[ 1, -22.9, 6.8, 0], 44 [ -.8, 7, 0, 0.0], 45 [88, 0, 0.0, 0], 46 [0, 0, 0, 0.0]] 47 target = N.array([ 3, 2, 1, 0]) 48 out = FirstAxisSumNotZero(src) 49 self.failUnless((out == target).all())5052 nelements = len(self.d2) 53 out = RankOrder(self.d2) 54 outr = ReverseRankOrder(self.d2) 55 uout = N.unique(out) 56 uoutr = N.unique(outr) 57 self.failUnless((uout == N.arange(nelements)).all(), 58 msg="We should get all indexes. Got just %s" % uout) 59 self.failUnless((uoutr == N.arange(nelements)).all(), 60 msg="We should get all indexes. Got just %s" % uoutr) 61 self.failUnless((out+outr+1 == nelements).all()) 62 self.failUnless((out == [ 0, 3, 4, 1, 5, 2]).all())63 67 71 7274 data = datasets['uni4large'].samples[:120,0].reshape((2,3,4,5)) 75 # Simple transformer/combiner which collapses across given 76 # dimension, e.g. sum 77 for axis in [None, 0, 1, 2]: 78 oversum = OverAxis(N.sum, axis=axis)(data) 79 sum_ = N.sum(data, axis=axis) 80 self.failUnless(N.all(sum_ == oversum)) 81 82 # Transformer which doesn't modify dimensionality of the data 83 data = data.reshape((6, -1)) 84 overnorm = OverAxis(L2Normed, axis=1)(data) 85 self.failUnless(N.linalg.norm(overnorm)!=1.0) 86 for d in overnorm: 87 self.failUnless(N.abs(N.linalg.norm(d) - 1.0)<0.00001) 88 89 overnorm = OverAxis(L2Normed, axis=0)(data) 90 self.failUnless(N.linalg.norm(overnorm)!=1.0) 91 for d in overnorm.T: 92 self.failUnless(N.abs(N.linalg.norm(d) - 1.0)<0.00001)93 9496 """Basic testing of DistPValue""" 97 if not externals.exists('scipy'): 98 return 99 ndb = 200 100 ndu = 20 101 nperd = 2 102 pthr = 0.05 103 Nbins = 400 104 105 # Lets generate already normed data (on sphere) and add some nonbogus features 106 datau = (N.random.normal(size=(nperd, ndb))) 107 dist = N.sqrt((datau * datau).sum(axis=1)) 108 109 datas = (datau.T / dist.T).T 110 tn = datax = datas[0, :] 111 dataxmax = N.max(N.abs(datax)) 112 113 # now lets add true positive features 114 tp = [-dataxmax * 1.1] * (ndu/2) + [dataxmax * 1.1] * (ndu/2) 115 x = N.hstack((datax, tp)) 116 117 # lets add just pure normal to it 118 x = N.vstack((x, N.random.normal(size=x.shape))).T 119 for distPValue in (DistPValue(), DistPValue(fpp=0.05)): 120 result = distPValue(x) 121 self.failUnless((result>=0).all) 122 self.failUnless((result<=1).all) 123 124 if cfg.getboolean('tests', 'labile', default='yes'): 125 self.failUnless(distPValue.positives_recovered[0] > 10) 126 self.failUnless((N.array(distPValue.positives_recovered) + 127 N.array(distPValue.nulldist_number) == ndb + ndu).all()) 128 self.failUnless(distPValue.positives_recovered[1] == 0)132 return unittest.makeSuite(TransformerTests)133 134 135 if __name__ == '__main__': 136 import runner 137
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Mon Apr 23 23:09:46 2012 | http://epydoc.sourceforge.net |