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

Source Code for Module mvpa.tests.test_hamster

  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 Hamster""" 
 10   
 11  import os 
 12  import unittest 
 13  from tempfile import mktemp 
 14  import numpy as N 
 15   
 16  from mvpa.misc.io.hamster import * 
 17   
18 -class HamsterHelperTests(unittest.TestCase):
19
20 - def testSpecification(self):
21 22 # more than 1 positional 23 self.failUnlessRaises(ValueError, Hamster, "1", 2) 24 # do not mix positional 25 self.failUnlessRaises(ValueError, Hamster, "1", bu=123) 26 # need to be a string 27 self.failUnlessRaises(ValueError, Hamster, 1) 28 # dump cannot be assigned 29 self.failUnlessRaises(ValueError, Hamster, dump=123) 30 # need to be an existing file 31 self.failUnlessRaises(IOError, Hamster, "/dev/ZUMBARGAN123") 32 33 hh=Hamster(budda=1, z=[123], fuga="123"); hh.h1=123; 34 delattr(hh, 'budda') 35 self.failUnless(`hh` == "Hamster(fuga='123', h1=123, z=[123])")
36 37
38 - def testSimpleStorage(self):
39 ex1 = """eins zwei drei 40 0 1 2 41 3 4 5 42 """ 43 ex2 = {'d1': N.random.normal(size=(4,4))} 44 45 hamster = Hamster(ex1=ex1) 46 hamster.d = ex2 47 hamster.boo = HamsterHelperTests 48 49 total_dict = {'ex1' : ex1, 50 'd' : ex2, 51 'boo' : HamsterHelperTests} 52 self.failUnless(hamster.asdict() == total_dict) 53 self.failUnless(set(hamster.registered) == set(['ex1', 'd', 'boo'])) 54 55 filename = mktemp('mvpa', 'test') 56 filename_gz = filename + '.gz' 57 filename_bogusgz = filename + '_bogus.gz' 58 59 # dump 60 hamster.dump(filename) 61 hamster.dump(filename_gz) 62 # allow to shoot yourself in the head 63 hamster.dump(filename_bogusgz, compresslevel=0) 64 self.failUnless(hamster.asdict() == total_dict) 65 66 # We should have stored plain and gzipped versions 67 gzplain = gzip.open(filename) 68 self.failUnlessRaises(IOError, gzplain.readlines) 69 gzipped = gzip.open(filename_gz) 70 discard = gzipped.readlines() 71 gzbogus = gzip.open(filename_bogusgz) 72 self.failUnlessRaises(IOError, gzbogus.readlines) 73 74 # load plain 75 hamster2 = Hamster(filename) 76 77 # check if we re-stored all the keys 78 k = hamster.__dict__.keys(); 79 k2 = hamster2.__dict__.keys(); 80 self.failUnless(set(k) == set(k2)) 81 82 # identity should be lost 83 self.failUnless(hamster.ex1 is hamster.ex1) 84 self.failUnless(not (hamster.ex1 is hamster2.ex1)) 85 86 # lets compare 87 self.failUnless(hamster.ex1 == hamster2.ex1) 88 89 self.failUnless(hamster.d.keys() == hamster2.d.keys()) 90 self.failUnless((hamster.d['d1'] == hamster2.d['d1']).all()) 91 92 93 self.failUnless(hamster.boo == hamster2.boo) 94 # not sure if that is a feature or a bug 95 self.failUnless(hamster.boo is hamster2.boo) 96 97 # cleanup 98 os.remove(filename) 99 os.remove(filename_gz) 100 os.remove(filename_bogusgz)
101
102 - def testAssignment(self):
103 ex1 = """eins zwei drei 104 0 1 2 105 3 4 5 106 """ 107 ex2 = {'d1': N.random.normal(size=(4,4))} 108 109 h = Hamster(ex1=ex1) 110 h.ex2 = ex2 111 self.failUnless(hasattr(h, 'ex2')) 112 h.ex2 = None 113 self.failUnless(h.ex2 is None) 114 h.ex2 = 123 115 self.failUnless(h.ex2 == 123) 116 h.has_key = 123
117 118 119
120 -def suite():
121 return unittest.makeSuite(HamsterHelperTests)
122 123 124 if __name__ == '__main__': 125 import runner 126