1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA ZScore mapper"""
10
11
12 import unittest
13
14 from mvpa.base import externals
15 externals.exists('scipy', raiseException=True)
16
17 from mvpa.support.copy import deepcopy
18 import numpy as N
19
20 from mvpa.datasets import Dataset
21 from mvpa.mappers.zscore import ZScoreMapper
22 from mvpa.datasets.miscfx import zscore
23
24 from tests_warehouse import datasets
25
27 """Test simple ZScoreMapper
28 """
29
31 """Setup sample datasets
32 """
33
34 self.dss = [
35 Dataset(samples=N.concatenate(
36 [N.arange(40) for i in range(20)]).reshape(20,-1).T,
37 labels=1, chunks=1),
38 ] + datasets.values()
39
40
42 """Test by comparing to results of elderly z-score function
43 """
44 for ds in self.dss:
45 ds1 = deepcopy(ds)
46 ds2 = deepcopy(ds)
47
48 zsm = ZScoreMapper()
49 zsm.train(ds1)
50 ds1z = zsm.forward(ds1.samples)
51
52 zscore(ds2, perchunk=False)
53 self.failUnless(N.linalg.norm(ds1z - ds2.samples) < 1e-12)
54 self.failUnless((ds1.samples == ds.samples).all(),
55 msg="It seems we modified original dataset!")
56
57 ds0 = zsm.reverse(ds1z)
58 self.failUnless(N.linalg.norm(ds0 - ds.samples) < 1e-12,
59 msg="Can't reconstruct from z-scores")
60
61
64
65
66 if __name__ == '__main__':
67 import runner
68