Exemplo n.º 1
0
    def test_getattr(self):
        # Test the
        # Set up a fake dir structure to generate an aligned fixmat
        img_per_cat = {1: range(1, 11), 2: range(1, 11)}
        features = ['a', 'b']
        path, ftrpath = test_loader.create_tmp_structure(img_per_cat,
                                                         features=features)
        l = loader.LoadFromDisk(impath=path, ftrpath=ftrpath, size=(100, 100))
        inp = stimuli.Categories(l, img_per_cat, features)
        fm = fixmat.TestFixmatFactory(categories=[1, 2],
                                      filenumbers=range(1, 11),
                                      subjectindices=[1, 2, 3, 4, 5, 6],
                                      params={
                                          'pixels_per_degree': 10,
                                          'image_size': [100, 100]
                                      },
                                      categories_obj=inp)

        fm_err = fixmat.TestFixmatFactory(categories=[1, 2],
                                          filenumbers=range(1, 11),
                                          subjectindices=[1, 2, 3, 4, 5, 6],
                                          params={
                                              'pixels_per_degree': 10,
                                              'image_size': [100, 100]
                                          })

        # Now let's check if we can access all the images
        # and all the features.
        fm.add_feature_values(['a', 'b'])
        self.assertRaises(RuntimeError,
                          lambda: fm_err.add_feature_values(['a', 'b']))
        for cat_mat, cat_inp in fm.by_cat():
            self.assertEquals(cat_mat.category[0], cat_inp.category)
            for img_mat, img_inp in cat_mat.by_filenumber():
                self.assertEquals(img_mat.filenumber[0], img_inp.image)
        self.assertEquals(len(fm.a), len(fm.x))
        self.assertEquals(len(fm.b), len(fm.x))
        # Let's also check if make_reg_data works
        a, b = fm.make_reg_data(features)
        self.assertEquals(a.shape[1], len(fm.x))
        self.assertEquals(a.shape[0], len(features))
        self.assertEquals(b.shape[1], len(fm.x))
        self.assertEquals(b.shape[0], len(features))
        self.assertEquals(b.sum(), a.sum())
        a, b = fm.make_reg_data(features, all_controls=True)
        self.assertEquals(a.shape[1], len(fm.x))
        self.assertEquals(a.shape[0], len(features))
        self.assertEquals(b.shape[1], len(fm.x))
        self.assertEquals(b.shape[0], len(features))
        self.assertEquals(b.sum(), a.sum())
        test_loader.rm_tmp_structure(path)
        test_loader.rm_tmp_structure(ftrpath)
Exemplo n.º 2
0
    def test_xvalidation(self):       
        img_per_cat = {}
        [img_per_cat.update({cat: list(range(1,50))}) for cat in range(1,11)]
        fm = fixmat.TestFixmatFactory(categories = list(range(1,11)), subjectindices = list(range(1,11)),
                                      filenumbers = list(range(1,11)))
        l = loader.TestLoader(img_per_cat,size = (10,10))
        stim = stimuli.FixmatStimuliFactory(fm,l) 
        img_ratio = 0.3
        sub_ratio = 0.3
        data_slices = SimpleXValidation(fm, stim,img_ratio,sub_ratio,10)
        for i,(fm_train, cat_train, fm_test, cat_test) in enumerate(data_slices.generate()):
            self.assertEqual (len(np.unique(fm_train.SUBJECTINDEX)), 
               round(len(np.unique(fm.SUBJECTINDEX))*(1-sub_ratio))) 
            self.assertEqual(len(np.unique(fm_test.SUBJECTINDEX)), 
                   round(len(np.unique(fm.SUBJECTINDEX))*(sub_ratio))) 
            for (test,train,reference) in zip(cat_test,
                                               cat_train, 
                                               stim):
                self.assertEqual(test.category, reference.category)
                self.assertEqual(len(test.images()), 
                        len(reference.images())*img_ratio)
                self.assertEqual(len(train.images()),
                        len(reference.images())*(1-img_ratio))

                self.assertTrue((np.sort(np.unique(fm_train[fm_train.category==test.category].filenumber)) == 
                       np.sort( train.images())).all())
Exemplo n.º 3
0
    def test_kldiv(self):
        arr = scipy.random.random((21, 13))
        fm = fixmat.TestFixmatFactory(categories=[1, 2, 3],
                                      filenumbers=[1, 2, 3, 4, 5, 6],
                                      subjectindices=[1, 2, 3, 4, 5, 6],
                                      params={
                                          'pixels_per_degree': 10,
                                          'image_size': [100, 500]
                                      })

        kl = measures.kldiv(arr, arr)
        self.assertEqual(
            kl, 0, "KL Divergence between same distribution should be 0")
        kl = measures.kldiv(None, None, distp=fm, distq=fm, scale_factor=0.25)
        self.assertEqual(
            kl, 0, "KL Divergence between same distribution should be 0")
        fdm = fixmat.compute_fdm(fm)
        kl = measures.kldiv_model(fdm, fm)
        self.assertTrue(
            kl < 10**-13,
            "KL Divergence between same distribution should be almost 0")
        fm.x = np.array([])
        fm.y = np.array([])

        kl = measures.kldiv(None, None, distp=fm, distq=fm, scale_factor=0.25)
        self.assertTrue(np.isnan(kl))
Exemplo n.º 4
0
 def gen_sub(self, subind, numfix):
     fm = fixmat.TestFixmatFactory(
         subjectindices=[subind],
         points=[range(0, numfix), range(0, numfix)],
         categories=[1, 2, 3, 4, 5, 6, 7],
         filenumbers=[1, 2, 3, 4, 5, 6, 7])
     return fm
Exemplo n.º 5
0
 def test_corners(self):
     """ ``test_corners(self)``
     Tests whether handling of fixations in the corners is correct. 
     It manually generates an fdm with four fixations in the corners of a
     922x1272 array and compares it to the map generated by compute_fdm. 
     The difference between the maps must not be larger than the machine
     precision for floats.
     """
     yvec = [922, 922, 0, 0]
     xvec = [1272, 0, 1272, 0]
     self.fm = fixmat.TestFixmatFactory(points=(xvec, yvec))
     fdm = fixmat.compute_fdm(self.fm)
     # manually calculate the fdm
     fdm_man = np.zeros((922, 1272))
     fdm_man[0][0] = 1
     fdm_man[921][1271] = 1
     fdm_man[0][1271] = 1
     fdm_man[921][0] = 1
     # use default settings for fwhm, pixels_per_degree and scale_factor
     kernel_sigma = 2 * 36 * 1
     kernel_sigma = kernel_sigma / (2 * (2 * np.log(2))**.5)
     fdm_man = gaussian_filter(np.array(fdm_man),
                               kernel_sigma,
                               order=0,
                               mode='constant')
     fdm_man = fdm_man / fdm_man.sum()
     diff = fdm - fdm_man
     self.assertFalse((diff > np.finfo('float').eps).any())
Exemplo n.º 6
0
 def setUp(self):
     self.fm = fixmat.TestFixmatFactory(categories=[1, 2, 3],
                                        filenumbers=[1, 2, 3, 4, 5, 6],
                                        subjectindices=[1, 2, 3, 4, 5, 6],
                                        params={
                                            'pixels_per_degree': 1,
                                            'image_size': [100, 500]
                                        })
Exemplo n.º 7
0
 def setUp(self):
     self.fm = fixmat.TestFixmatFactory(categories = [1,2,3], 
                             filenumbers = [1,2,3,4,5,6],
                             subjectindices = [1, 2, 3, 4, 5, 6],
                             params = {'pixels_per_degree':1, 'image_size':[100,500]})
     measures.set_scores([measures.roc_model,
                          measures.kldiv_model,
                          measures.nss_model])
Exemplo n.º 8
0
 def test_relative_bias(self):
     fm = fixmat.TestFixmatFactory(categories=[1, 2],
                                   filenumbers=range(1, 11),
                                   subjectindices=[1, 2, 3, 4, 5, 6],
                                   params={
                                       'pixels_per_degree': 1,
                                       'image_size': [10, 10]
                                   })
     rb = fixmat.relative_bias(fm)
     self.assertEquals(rb.shape, (21, 21))
     self.assertEquals(rb[9][9], 960)
Exemplo n.º 9
0
 def test_nss(self):
     fm = fixmat.TestFixmatFactory(points=zip([0,500,1000],[1,10,10]),
             params = {'image_size':[100,10]})
     fm.SUBJECTINDEX = np.array([1,1,1])
     fm.filenumber = np.array([1,1,1])
     fm.category = np.array([1,1,1])
     fm.x = np.array([0,50,1000])
     fm.y = np.array([1,10,10])
     fm.fix = np.array([1,2,3])
     fm._num_fix = 3
     fdm = fixmat.compute_fdm(fm[(fm.x<10) & (fm.y<10)])
     self.assertRaises(IndexError, lambda: measures.nss(fdm, (fm.y, fm.x))) 
Exemplo n.º 10
0
 def test_copying(self):
     fm = fixmat.TestFixmatFactory(categories=[7],
                                   filenumbers=[10],
                                   subjectindices=[100],
                                   params={
                                       'pixels_per_degree': 10,
                                       'image_size': [100, 500]
                                   })
     fm.x[0] = 18728001
     fm_copied = fm.copy()
     fm_copied.pixels_per_degree = 100
     self.assertFalse(fm.pixels_per_degree == fm_copied.pixels_per_degree)
     fm_copied.x[0] = 1
     self.assertFalse(fm.x[0] == fm_copied.x[0])
Exemplo n.º 11
0
 def test_nss_values(self):
     fm = fixmat.TestFixmatFactory(categories = [1,2,3], 
             filenumbers = [1,2,3,4,5,6],
             subjectindices = [1, 2, 3, 4, 5, 6],
             params = {'pixels_per_degree':0.1, 'image_size':[200,500]})
     # Arr has zero variance, should return nan
     arr = np.ones(fm.image_size)
     nss = measures.nss_model(arr, fm)
     self.assertTrue(np.isnan(nss))
     # With itself should yield a high value 
     fdm = fixmat.compute_fdm(fm)
     nss = measures.nss_model(fdm, fm)
     self.assertTrue(nss>15)
     # Fixations at these locations should give nss < 0
     nss = measures.nss(fdm, [[100, 101, 102, 103, 104, 105],[0, 0, 0, 0, 0, 0]])
     self.assertTrue(nss < 0)
Exemplo n.º 12
0
 def test_correlation(self):
     fm = fixmat.TestFixmatFactory(categories = [1,2,3], 
             filenumbers = [1,2,3,4,5,6],
             subjectindices = [1, 2, 3, 4, 5, 6],
             params = {'pixels_per_degree':1, 'image_size':[100,500]})
     # Arr has zero variance, should return nan
     arr = np.ones(fm.image_size)
     corr = measures.correlation_model(arr, fm)
     self.assertTrue(np.isnan(corr))
     # With itself should give 1
     fdm = fixmat.compute_fdm(fm)
     corr = measures.correlation_model(fdm,fm)
     self.assertEquals(corr,1)
     # Anti-correlation should give -1
     corr = measures.correlation_model(-1*fdm,fm)
     self.assertEquals(corr,-1)
Exemplo n.º 13
0
 def test_emd(self):
    try: 
        import opencv
    except ImportError:
        print "Skipping EMD test - no opencv available"
        return 
    opencv # pyflakes
    fm = fixmat.TestFixmatFactory(categories = [1,2,3], 
        filenumbers = [1,2,3,4,5,6],
        subjectindices = [1, 2, 3, 4, 5, 6],
        params = {'pixels_per_degree':1, 'image_size':[20,50]})
    arr = np.ones(fm.image_size)
    fdm = fixmat.compute_fdm(fm)
    e = measures.emd_model(arr, fm)
    self.assertTrue(e > 0)
    e = measures.emd(fdm, fdm)
    self.assertEquals(e, 0) 
Exemplo n.º 14
0
 def test_prediction_scores(self):
     measures.set_scores([measures.roc_model, 
                          measures.kldiv_model, 
                          measures.nss_model])
     fm = fixmat.TestFixmatFactory(categories = [1,2,3], 
             filenumbers = [1,2,3,4,5,6],
             subjectindices = [1, 2, 3, 4, 5, 6],
             params = {'pixels_per_degree':10, 'image_size':[100,500]})
     fdm = fixmat.compute_fdm(fm)
     measures.set_scores([measures.roc_model, 
                          measures.kldiv_model, 
                          measures.nss_model])
     scores  =  measures.prediction_scores(fdm, fm) 
     self.assertEquals(len(scores), 3)
     measures.set_scores([measures.roc_model])
     scores  =  measures.prediction_scores(fdm, fm) 
     self.assertEquals(len(scores), 1)
     measures.set_scores([measures.roc_model, 
                          measures.kldiv_model, 
                          measures.nss_model])
     scores  =  measures.prediction_scores(fdm, fm) 
     self.assertEquals(len(scores), 3)
Exemplo n.º 15
0
    def test_interface(self):
        fm = fixmat.TestFixmatFactory(categories=[1, 2, 3],
                                      filenumbers=[1, 2, 3, 4, 5, 6],
                                      subjectindices=[1, 2, 3, 4, 5, 6],
                                      params={
                                          'pixels_per_degree': 10,
                                          'image_size': [100, 500]
                                      })
        # We can produce a pretty table with the fixmats parameters by
        # printin it:
        print fm
        # Check parameter access
        self.assertTrue(fm.pixels_per_degree == 10)
        self.assertTrue(fm.image_size[0] == 100 and fm.image_size[1] == 500)

        # Check that all fields can be accessed
        self.assertTrue(
            (np.unique(fm.SUBJECTINDEX) == np.array([1, 2, 3, 4, 5, 6])).all())
        self.assertTrue((np.unique(fm.category) == np.array([1, 2, 3])).all())
        self.assertTrue(
            (np.unique(fm.filenumber) == np.array([1, 2, 3, 4, 5, 6])).all())
        self.assertTrue(
            len(fm.x) == len(fm.y) and len(fm.y) == len(fm.SUBJECTINDEX))
        self.assertTrue(len(fm.SUBJECTINDEX) == len(fm.filenumber))

        # Test filtering
        fm_sub1 = fm[fm.SUBJECTINDEX == 1]
        self.assertTrue(
            (np.unique(fm_sub1.SUBJECTINDEX) == np.array([1])).all())
        self.assertTrue((np.unique(fm_sub1.category) == np.array([1, 2,
                                                                  3])).all())
        self.assertTrue(
            (np.unique(fm_sub1.filenumber) == np.array([1, 2, 3, 4, 5,
                                                        6])).all())
        self.assertTrue(
            len(fm_sub1.x) == len(fm_sub1.y)
            and len(fm_sub1.y) == len(fm_sub1.SUBJECTINDEX))
        self.assertTrue(len(fm_sub1.SUBJECTINDEX) == len(fm_sub1.filenumber))

        fm_cmp = fm.filter(fm.SUBJECTINDEX == 1)
        for (a, b) in zip(fm_cmp.x, fm_sub1.x):
            self.assertTrue(a == b)

        # Test save and load
        fm_sub1.save('/tmp/test_fixmat')
        fm_cmp = fixmat.load('/tmp/test_fixmat')
        self.compare_fixmats(fm_sub1, fm_cmp)

        self.assertTrue('pixels_per_degree' in fm.parameters())
        self.assertTrue('image_size' in fm.parameters())

        # Test iterating over fixmat
        for (img, img_mat) in zip([1, 2, 3, 4, 5, 6],
                                  fm.by_field('filenumber')):
            self.assertEquals(len(np.unique(img_mat.filenumber)), 1)
            self.assertEquals(np.unique(img_mat.filenumber)[0], img)

        # Test adding fields
        fm.add_field('x2', fm.x)
        for (x1, x2) in zip(fm.x, fm.x2):
            self.assertEquals(x1, x2)

        self.assertRaises(ValueError, lambda: fm.add_field('x3', [1]))
        self.assertRaises(ValueError, lambda: fm.add_field('x2', fm.x))

        # Test removing fields:
        fm.rm_field('x2')
        self.assertRaises(ValueError, lambda: fm.rm_field('x2'))

        # Add a new subject
        fm_add = fixmat.TestFixmatFactory(categories=[7],
                                          filenumbers=[10],
                                          subjectindices=[100],
                                          params={
                                              'pixels_per_degree': 10,
                                              'image_size': [100, 500]
                                          })
        fm.join(fm_add)
        fm_add2 = fixmat.TestFixmatFactory(categories=[7],
                                           filenumbers=[10],
                                           subjectindices=[100, 101],
                                           params={
                                               'pixels_per_degree': 10,
                                               'image_size': [100, 500]
                                           })
        self.assertRaises(RuntimeError, lambda: fm.join(fm_add2))
        fm_add = fixmat.TestFixmatFactory(categories=[7],
                                          filenumbers=[10],
                                          subjectindices=[100, 101],
                                          params={
                                              'pixels_per_degree': 10,
                                              'image_size': [101, 500]
                                          })
        self.assertRaises(RuntimeError, lambda: fm.join(fm_add2))

        fm_cmp = fm[(fm.SUBJECTINDEX == 100) & (fm.category == 7)]
        self.compare_fixmats(fm_add, fm_cmp)
Exemplo n.º 16
0
 def setUp(self):
     self.fm = fixmat.TestFixmatFactory()