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())
def setUp(self): self.features = range(1, 11) self.inputs = { 'a': { 1: self.features, 2: self.features, 3: self.features }, 'b': { 10: self.features } } self.test_loader = loader.TestLoader(self.inputs) self.inp = stimuli.Categories(self.test_loader, self.inputs)
def test_testloader(self): img_per_cat = {1: range(1, 10), 2: range(1, 10)} l = loader.TestLoader(img_per_cat=img_per_cat, features=['a', 'b']) for cat in range(1, 10): for img in range(1, 100): if cat in img_per_cat.keys() and img in img_per_cat[cat]: self.assertEquals(l.test_for_category(cat), True) self.assertEquals(l.test_for_image(cat, img), True) self.assertEquals(l.test_for_feature(cat, img, 'a'), True) l.get_image(cat, img) l.get_feature(cat, img, 'a') l.get_feature(cat, img, 'b') self.assertTrue(l.test_for_feature(cat, img, 'a')) else: self.assertEquals(l.test_for_image(cat, img), False) self.assertRaises(IndexError, lambda: l.get_image(cat, img))
def test_fixmat_input_factory(self): # Another way to create an input object is with the # FixmatInputFactory features = ['a', 'b', 'c', 'd'] img_per_cat = {7: range(1, 65), 8: range(1, 65)} l = loader.TestLoader(img_per_cat, features, size=(10, 10)) with NamedTemporaryFile(mode='w', prefix='fix_occ_test', suffix='.mat') as ntf: ntf.write(get_data('ocupy.tests', 'fixmat_demo.mat')) ntf.seek(0) fm = fixmat.FixmatFactory(ntf.name) fm = fm[fm.category > 0] inp = stimuli.FixmatStimuliFactory(fm, l) # Now we can iterate over the input object and get fixations on each image for cat in inp: for img in cat: self.assertEquals(img.fixations.filenumber[0], img.image) self.assertEquals(img.fixations.category[0], img.category)
def test_fixations(self): img_per_cat = {7: range(1, 65), 8: range(1, 65)} l = loader.TestLoader(img_per_cat, size=(10, 10)) with NamedTemporaryFile(mode='w', prefix='fix_occ_test', suffix='.mat') as ntf: ntf.write(get_data('ocupy.tests', 'fixmat_demo.mat')) ntf.seek(0) fm = fixmat.FixmatFactory(ntf.name) fm = fm[fm.category > 0] inp = stimuli.FixmatStimuliFactory(fm, l) # Now we can iterate over the input object and get fixations on each image for cat in inp: for img in cat: self.assertEqual(img.fixations.filenumber[0], img.image) self.assertEqual(img.fixations.category[0], img.category) inp = stimuli.Categories(l, img_per_cat) self.assertRaises(RuntimeError, lambda: inp.fixations) self.assertRaises(RuntimeError, lambda: inp[7].fixations) self.assertRaises(RuntimeError, lambda: inp[7][1].fixations)
def test_general(self): #Create an input object: There are two ways, one is the features = ['a', 'b', 'c', 'd'] img_per_cat = {7: range(1, 65), 8: range(1, 65)} l = loader.TestLoader(img_per_cat, features, size=(10, 10)) inp = stimuli.Categories(l, img_per_cat, features) # In this case it should have two categories (2,9) with 10 and 50 images # Let's check this self.assert_(7 in inp.categories()) self.assert_(8 in inp.categories()) self.assertEquals(len(inp[7].images()), 64) self.assertEquals(len(inp[8].images()), 64) # Good, now we can access some data img = inp[7][16].data self.assertTrue(img.shape[0] == 10 and img.shape[1] == 10) # In general we can use [] to access elements in the object # But we can also iterate trough these objects # This should take a long time because it loads all images # from disk for cat in inp: for img in cat: img.image img.category img.data