Package mvpa :: Package tests :: Module test_iohelpers

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.tests.test_iohelpers

      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 IO helpers"""
     10
     11  import re
     12  import os
     13  import unittest
     14  from tempfile import mkstemp
     15  import numpy as N
     16
     17  from nose.tools import ok_, assert_equal
     18
     19  from mvpa import pymvpa_dataroot
     20  from mvpa.misc.io import *
     21  from mvpa.misc.fsl import *
     22  from mvpa.misc.bv import BrainVoyagerRTC
     23
     24
     25 -class IOHelperTests(unittest.TestCase):
     26
     27 -    def testColumnDataFromFile(self):
     28          ex1 = """eins zwei drei
     29          0 1 2
     30          3 4 5
     31          """
     32          file, fpath = mkstemp('mvpa', 'test')
     33          file = open(fpath, 'w')
     34          file.write(ex1)
     35          file.close()
     36
     37          # intentionally rely on defaults
     38          d = ColumnData(fpath, header=True)
     39
     40          # check header (sort because order in dict is unpredictable)
     41          self.failUnless(sorted(d.keys()) == ['drei','eins','zwei'])
     42
     43          self.failUnless(d['eins'] == [0, 3])
     44          self.failUnless(d['zwei'] == [1, 4])
     45          self.failUnless(d['drei'] == [2, 5])
     46
     47          # make a copy
     48          d2 = ColumnData(d)
     49
     50          # check if identical
     51          self.failUnless(sorted(d2.keys()) == ['drei','eins','zwei'])
     52          self.failUnless(d2['eins'] == [0, 3])
     53          self.failUnless(d2['zwei'] == [1, 4])
     54          self.failUnless(d2['drei'] == [2, 5])
     55
     56          # now merge back
     57          d += d2
     58
     59          # same columns?
     60          self.failUnless(sorted(d.keys()) == ['drei','eins','zwei'])
     61
     62          # but more data
     63          self.failUnlessEqual(d['eins'], [0, 3, 0, 3])
     64          self.failUnlessEqual(d['zwei'], [1, 4, 1, 4])
     65          self.failUnlessEqual(d['drei'], [2, 5, 2, 5])
     66
     67          # test file write
     68          # TODO: check if correct
     69          header_order = ['drei', 'zwei', 'eins']
     70          d.tofile(fpath, header_order=header_order)
     71
     72          # test sample selection
     73          dsel = d.selectSamples([0, 2])
     74          self.failUnlessEqual(dsel['eins'], [0, 0])
     75          self.failUnlessEqual(dsel['zwei'], [1, 1])
     76          self.failUnlessEqual(dsel['drei'], [2, 2])
     77
     78          # test if order is read from file when available
     79          d3 = ColumnData(fpath)
     80          self.failUnlessEqual(d3._header_order, header_order)
     81
     82          # add another column -- should be appended as the last column
     83          # while storing
     84          d3['four'] = [0.1] * len(d3['eins'])
     85          d3.tofile(fpath)
     86
     87          d4 = ColumnData(fpath)
     88          self.failUnlessEqual(d4._header_order, header_order + ['four'])
     89
     90          # cleanup and ignore stupidity
     91          try:
     92              os.remove(fpath)
     93          except WindowsError:
     94              pass
     95
     96
     97 -    def testSamplesAttributes(self):
     98          sa = SampleAttributes(os.path.join(pymvpa_dataroot,
     99                                             'attributes_literal.txt'),
    100                                literallabels=True)
    101
    102          ok_(sa.nrows == 1452, msg='There should be 1452 samples')
    103
    104          # convert to event list, with some custom attr
    105          ev = sa.toEvents(funky='yeah')
    106          ok_(len(ev) == 17 * (max(sa.chunks) + 1),
    107              msg='Not all events got detected.')
    108
    109          ok_(len([e for e in ev if e.has_key('funky')]) == len(ev),
    110              msg='All events need to have to custom arg "funky".')
    111
    112          ok_(ev[0]['label'] == ev[-1]['label'] == 'rest',
    113              msg='First and last event are rest condition.')
    114
    115          ok_(ev[-1]['onset'] + ev[-1]['duration'] == sa.nrows,
    116              msg='Something is wrong with the timiing of the events')
    117
    118
    119 -    def testFslEV(self):
    120          ex1 = """0.0 2.0 1
    121          13.89 2 1
    122          16 2.0 0.5
    123          """
    124          file, fpath = mkstemp('mvpa', 'test')
    125          file = open(fpath, 'w')
    126          file.write(ex1)
    127          file.close()
    128
    129          # intentionally rely on defaults
    130          d = FslEV3(fpath)
    131
    132          # check header (sort because order in dict is unpredictable)
    133          self.failUnless(sorted(d.keys()) == \
    134              ['durations','intensities','onsets'])
    135
    136          self.failUnless(d['onsets'] == [0.0, 13.89, 16.0])
    137          self.failUnless(d['durations'] == [2.0, 2.0, 2.0])
    138          self.failUnless(d['intensities'] == [1.0, 1.0, 0.5])
    139
    140          self.failUnless(d.getNEVs() == 3)
    141          self.failUnless(d.getEV(1) == (13.89, 2.0, 1.0))
    142          # cleanup and ignore stupidity
    143          try:
    144              os.remove(fpath)
    145          except WindowsError:
    146              pass
    147
    148          d = FslEV3(os.path.join(pymvpa_dataroot, 'fslev3.txt'))
    149          ev = d.toEvents()
    150          self.failUnless(len(ev) == 3)
    151          self.failUnless([e['duration'] for e in ev] == [9] * 3)
    152          self.failUnless([e['onset'] for e in ev] == [6, 21, 35])
    153          self.failUnless([e['features'] for e in ev] == [[1],[1],[1]])
    154
    155          ev = d.toEvents(label='face', chunk=0, crap=True)
    156          ev[0]['label'] = 'house'
    157          self.failUnless(len(ev) == 3)
    158          self.failUnless([e['duration'] for e in ev] == [9] * 3)
    159          self.failUnless([e['onset'] for e in ev] == [6, 21, 35])
    160          self.failUnless([e['features'] for e in ev] == [[1],[1],[1]])
    161          self.failUnless([e['label'] for e in ev] == ['house', 'face', 'face'])
    162          self.failUnless([e['chunk'] for e in ev] == [0]*3)
    163          self.failUnless([e['crap'] for e in ev] == [True]*3)
    164
    165
    166 -    def testFslEV2(self):
    167          attr = SampleAttributes(os.path.join(pymvpa_dataroot, 'smpl_attr.txt'))
    168
    169          # check header (sort because order in dict is unpredictable)
    170          self.failUnless(sorted(attr.keys()) == \
    171              ['chunks','labels'])
    172
    173          self.failUnless(attr.nsamples == 3)
    174
    175 -    def testBVRTC(self):
    176          """Simple testing of reading RTC files from BrainVoyager"""
    177
    178          attr = BrainVoyagerRTC(os.path.join(pymvpa_dataroot, 'bv', 'smpl_model.rtc'))
    179          self.failUnlessEqual(attr.ncolumns, 4, "We must have 4 colums")
    180          self.failUnlessEqual(attr.nrows, 147, "We must have 147 rows")
    181
    182          self.failUnlessEqual(attr._header_order,
    183                  ['l_60 B', 'r_60 B', 'l_80 B', 'r_80 B'],
    184                  "We must got column names correctly")
    185          self.failUnless(len(attr.r_60_B) == attr.nrows,
    186                  "We must have got access to column by property")
    187          self.failUnless(attr.toarray() != None,
    188                  "We must have got access to column by property")
    189
    190 -    def testdesign2labels(self):
    191          """Simple testing of helper Design2Labels"""
    192
    193          attr = BrainVoyagerRTC(os.path.join(pymvpa_dataroot, 'bv', 'smpl_model.rtc'))
    194          labels0 = design2labels(attr, baseline_label='silence')
    195          labels = design2labels(attr, baseline_label='silence',
    196                                  func=lambda x:x>0.5)
    197          Nsilence = lambda x:len(N.where(N.array(x) == 'silence')[0])
    198
    199          nsilence0 = Nsilence(labels0)
    200          nsilence = Nsilence(labels)
    201          self.failUnless(nsilence0 < nsilence,
    202                          "We must have more silence if thr is higher")
    203          self.failUnlessEqual(len(labels), attr.nrows,
    204                          "We must have the same number of labels as rows")
    205          self.failUnlessRaises(ValueError, design2labels, attr,
    206                          baseline_label='silence', func=lambda x:x>-1.0)
    207
    208
    209 -    def testlabels2chunks(self):
    210          attr = BrainVoyagerRTC(os.path.join(pymvpa_dataroot, 'bv', 'smpl_model.rtc'))
    211          labels = design2labels(attr, baseline_label='silence')
    212          self.failUnlessRaises(ValueError, labels2chunks, labels, 'bugga')
    213          chunks = labels2chunks(labels)
    214          self.failUnlessEqual(len(labels), len(chunks))
    215          # we must got them in sorted order
    216          chunks_sorted = N.sort(chunks)
    217          self.failUnless((chunks == chunks_sorted).all())
    218          # for this specific one we must have just 4 chunks
    219          self.failUnless((N.unique(chunks) == range(4)).all())
    220
    221
    222 -    def testSensorLocations(self):
    223          sl = XAVRSensorLocations(os.path.join(pymvpa_dataroot, 'xavr1010.dat'))
    224
    225          for var in ['names', 'pos_x', 'pos_y', 'pos_z']:
    226              self.failUnless(len(eval('sl.' + var)) == 31)
    227
    228
    229 -    def testFslGLMDesign(self):
    230          glm = FslGLMDesign(os.path.join(pymvpa_dataroot, 'glm.mat'))
    231
    232          self.failUnless(glm.mat.shape == (850, 6))
    233          self.failUnless(len(glm.ppheights) == 6)
    234
    235 -    def test_read_fsl_design(self):
    236          fname = os.path.join(pymvpa_dataroot,
    237                               'sample_design.fsf')
    238          # use our function
    239          design = read_fsl_design(fname)
    240          # and just load manually to see either we match fine
    241          set_lines = [x for x in open(fname).readlines()
    242                       if x.startswith('set ')]
    243          assert_equal(len(set_lines), len(design))
    244
    245          # figure out which one is missing
    246          """TODO: would require the same special treatment for _files fields
    247          re_set = re.compile("set ([^)]*\)).*")
    248          for line in set_lines:
    249              key = re_set.search(line).groups()[0]
    250              if not key in design:
    251                  raise AssertionError(
    252                      "Key %s was not found in read FSL design" % key)
    253          key_list = [' '.join(l.split(None,2)[1:2]) for l in set_lines]
    254          for k in set(key_list):
    255              if len([key for key in key_list if key == k]) == 2:
    256                  raise AssertionError(
    257                      "Got the non-unique beast %s" % k)
    258                      """
    259
    260 -def suite():
    261      return unittest.makeSuite(IOHelperTests)
    262
    263
    264  if __name__ == '__main__':
    265      import runner
    266