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

Source Code for Module mvpa.tests.test_arraymapper

  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 dense array mapper""" 
 10   
 11   
 12  from mvpa.mappers.array import DenseArrayMapper 
 13  from mvpa.mappers.metric import * 
 14  import unittest 
 15  import numpy as N 
 16   
17 -class DenseArrayMapperTests(unittest.TestCase):
18
20 mask = N.ones((3,2)) 21 map_ = DenseArrayMapper(mask) 22 23 # test shape reports 24 self.failUnless( map_.nfeatures == 6 ) 25 26 # test 1sample mapping 27 self.failUnless( ( map_.forward( N.arange(6).reshape(3,2) ) \ 28 == [0,1,2,3,4,5]).all() ) 29 30 # test 4sample mapping 31 foursample = map_.forward( N.arange(24).reshape(4,3,2)) 32 self.failUnless( ( foursample \ 33 == [[0,1,2,3,4,5], 34 [6,7,8,9,10,11], 35 [12,13,14,15,16,17], 36 [18,19,20,21,22,23]]).all() ) 37 38 # check incomplete masks 39 mask[1,1] = 0 40 map_ = DenseArrayMapper(mask) 41 self.failUnless( map_.nfeatures == 5 ) 42 self.failUnless( ( map_.forward( N.arange(6).reshape(3,2) ) \ 43 == [0,1,2,4,5]).all() ) 44 45 # check that it doesn't accept wrong dataspace 46 self.failUnlessRaises( ValueError, 47 map_.forward, 48 N.arange(4).reshape(2,2) ) 49 50 # check fail if neither mask nor shape 51 self.failUnlessRaises(ValueError, DenseArrayMapper) 52 53 # check that a full mask is automatically created when providing shape 54 m = DenseArrayMapper(shape=(2, 3, 4)) 55 mp = m.forward(N.arange(24).reshape(2, 3, 4)) 56 self.failUnless((mp == N.arange(24)).all())
57 58
60 mask = N.ones((3,2)) 61 mask[1,1] = 0 62 map_ = DenseArrayMapper(mask) 63 64 rmapped = map_.reverse(N.arange(1,6)) 65 self.failUnless( rmapped.shape == (3,2) ) 66 self.failUnless( rmapped[1,1] == 0 ) 67 self.failUnless( rmapped[2,1] == 5 ) 68 69 70 # check that it doesn't accept wrong dataspace 71 self.failUnlessRaises( ValueError, 72 map_, 73 N.arange(6)) 74 75 rmapped2 = map_.reverse(N.arange(1,11).reshape(2,5)) 76 self.failUnless( rmapped2.shape == (2,3,2) ) 77 self.failUnless( rmapped2[0,1,1] == 0 ) 78 self.failUnless( rmapped2[1,1,1] == 0 ) 79 self.failUnless( rmapped2[0,2,1] == 5 ) 80 self.failUnless( rmapped2[1,2,1] == 10 )
81 82
84 """ Test DenseArrayMapperMetric 85 """ 86 mask = N.ones((3,2)) 87 mask[1,1] = 0 88 89 # take space with non-square elements 90 neighborFinder = DescreteMetric([0.5, 2]) 91 map_ = DenseArrayMapper(mask, neighborFinder) 92 93 # test getNeighbors 94 # now it returns list of arrays 95 #target = [N.array([0, 0]), N.array([0, 1]), 96 # N.array([1, 0]), N.array([2, 0])] 97 #result = map_.getNeighborIn([0, 0], 2) 98 #self.failUnless(N.array(map(lambda x,y:(x==y).all(), result, target)).all()) 99 100 # check by providing outId 101 target = [0,1,2,3] 102 result = map_.getNeighbors(0, 2.1) 103 self.failUnless( result == target ) 104 105 map__ = DenseArrayMapper(mask, elementsize=[0.5, 2]) 106 self.failUnless( map__.getNeighbors(0, 2.1) == target, 107 msg="DenseArrayMapper must accept elementsize parameter and set" + 108 " DescreteMetric accordingly") 109 110 self.failUnlessRaises(ValueError, DenseArrayMapper, 111 mask, elementsize=[0.5]*3) 112 """DenseArrayMapper must raise exception when not appropriatly sized 113 elementsize was provided"""
114 115 116
117 - def testMapperAliases(self):
118 mm=DenseArrayMapper(N.ones((3,4,2))) 119 # We decided to don't have alias for reverse 120 #self.failUnless((mm(N.arange(24)) == mm.reverse(N.arange(24))).all()) 121 self.failUnless((mm(N.ones((3,4,2))) \ 122 == mm.forward(N.ones((3,4,2)))).all())
123 124
125 - def testGetInOutIdBehaviour(self):
126 mask=N.zeros((3,4,2)) 127 mask[0,0,1]=1 128 mask[2,1,0]=1 129 mask[0,3,1]=1 130 131 mm=DenseArrayMapper(mask) 132 133 self.failUnless(mm.nfeatures==3) 134 135 # 'In' 136 self.failUnless((mm.getInIds() \ 137 == N.array([[0, 0, 1],[0, 3, 1],[2, 1, 0]])).all()) 138 self.failUnless((mm.getInId(1) == [0,3,1]).all()) 139 # called with list gives nonzero() like output 140 self.failUnless((mm.getInId(range(mm.nfeatures)) \ 141 == mm.getInIds().T).all()) 142 143 # 'Out' 144 self.failUnlessRaises( ValueError, 145 mm.getOutId, 146 (0,0,0)) 147 self.failUnless(mm.getOutId((0,0,1)) == 0 148 and mm.getOutId((0,3,1)) == 1 149 and mm.getOutId((2,1,0)) == 2)
150 151
152 - def testSelects(self):
153 mask = N.ones((3,2)) 154 mask[1,1] = 0 155 mask0 = mask.copy() 156 data = N.arange(6).reshape(mask.shape) 157 map_ = DenseArrayMapper(mask) 158 159 # check if any exception is thrown if we get 160 # out of the outIds 161 self.failUnlessRaises(IndexError, map_.selectOut, [0,1,2,6]) 162 163 # remove 1,2 164 map_.selectOut([0,3,4]) 165 self.failUnless((map_.forward(data)==[0, 4, 5]).all()) 166 # remove 1 more 167 map_.selectOut([0,2]) 168 self.failUnless((map_.forward(data)==[0, 5]).all()) 169 170 # check if original mask wasn't perturbed 171 self.failUnless((mask == mask0).all()) 172 173 # do the same but using discardOut 174 map_ = DenseArrayMapper(mask) 175 map_.discardOut([1,2]) 176 self.failUnless((map_.forward(data)==[0, 4, 5]).all()) 177 map_.discardOut([1]) 178 self.failUnless((map_.forward(data)==[0, 5]).all()) 179 180 # check if original mask wasn't perturbed 181 self.failUnless((mask == mask0).all())
182 183
184 - def _testSelectReOrder(self):
185 """ 186 Test is desabled for now since if order is incorrect in 187 __debug__ we just spit out a warning - no exception 188 """ 189 mask = N.ones((3,3)) 190 mask[1,1] = 0 191 192 data = N.arange(9).reshape(mask.shape) 193 map_ = DenseArrayMapper(mask) 194 oldneighbors = map_.forward(data)[map_.getNeighbors(0, radius=2)] 195 196 # just do place changes 197 # by default - we don't sort/check order so it would screw things 198 # up 199 map_.selectOut([7, 1, 2, 3, 4, 5, 6, 0]) 200 # we check if an item new outId==7 still has proper neighbors 201 newneighbors = map_.forward(data)[map_.getNeighbors(7, radius=2)] 202 self.failUnless( (oldneighbors != newneighbors ).any())
203 204 # disable since selectOut does not have 'sort' anymore 205 # def testSelectOrder(self): 206 # """ 207 # Test if changing the order by doing selectOut preserves 208 # neighborhood information -- but also apply sort in difference 209 # to testSelectReOrder 210 # """ 211 # mask = N.ones((3,3)) 212 # mask[1,1] = 0 213 # 214 # data = N.arange(9).reshape(mask.shape) 215 # map_ = DenseArrayMapper(mask) 216 # oldneighbors = map_.forward(data)[map_.getNeighbors(0, radius=2)] 217 # 218 # map_ = DenseArrayMapper(mask) 219 # map_.selectOut([7, 1, 2, 3, 4, 5, 6, 0], sort=True) 220 # # we check if an item new outId==0 still has proper neighbors 221 # newneighbors = map_.forward(data)[map_.getNeighbors(0, radius=2)] 222 # self.failUnless( (oldneighbors == newneighbors ).all()) 223 224
225 -def suite():
226 return unittest.makeSuite(DenseArrayMapperTests)
227 228 229 if __name__ == '__main__': 230 import runner 231