def test_load_from_disk(self): # Generate a tmp fake data structure img_per_cat = {2: range(1, 10), 8: range(30, 40), 111: range(6, 15)} path, _ = create_tmp_structure(img_per_cat) l = loader.LoadFromDisk(impath=path) for cat in img_per_cat.keys(): for image in img_per_cat[cat]: l.get_image(cat, image) self.assertTrue(l.test_for_category(cat)) self.assertTrue(l.test_for_image(cat, image)) # Test checks for non existing images no_img_per_cat = {0: range(1, 10), 7: range(30, 40), 110: range(6, 15)} for cat in no_img_per_cat.keys(): for image in no_img_per_cat[cat]: self.assertTrue(not l.test_for_category(cat)) self.assertTrue(not l.test_for_image(cat, image)) no_img_per_cat = { 2: range(11, 20), 8: range(20, 30), 111: range(16, 40) } for cat in no_img_per_cat.keys(): for image in no_img_per_cat[cat]: self.assertTrue(not l.test_for_image(cat, image)) rm_tmp_structure(path)
def test_save_to_disk(self): path = mkdtemp() ftrpath = mkdtemp() im_tmp = np.ones((100, 100)) l = loader.SaveToDisk(impath=path, ftrpath=ftrpath, size=(10, 10)) # Generate a tmp fake data structure img_per_cat = { 2: list(range(1, 10)), 8: list(range(30, 40)), 111: list(range(6, 15)) } for cat in list(img_per_cat.keys()): # Create category dir for image in img_per_cat[cat]: l.save_image(cat, image, im_tmp) for f in ['a', 'b']: l.save_feature(cat, image, f, np.ones((10, 10))) l = loader.LoadFromDisk(impath=path, ftrpath=ftrpath, size=(10, 10)) for cat in list(img_per_cat.keys()): for image in img_per_cat[cat]: im = l.get_image(cat, image) self.assertTrue(im.shape[0] == 10 and im.shape[1] == 10) self.assertTrue(l.test_for_category(cat)) self.assertTrue(l.test_for_image(cat, image)) self.assertTrue(l.test_for_feature(cat, image, 'a'), True) os.system('rm -rf %s' % path) os.system('rm -rf %s' % ftrpath)
def test_load_from_disk_scaling(self): # Generate a tmp fake data structure img_per_cat = {2: range(1, 10), 8: range(30, 40), 111: range(6, 15)} path, _ = create_tmp_structure(img_per_cat) l = loader.LoadFromDisk(impath=path, size=(10, 10)) for cat in img_per_cat.keys(): for image in img_per_cat[cat]: im = l.get_image(cat, image) self.assertTrue(im.shape[0] == 10 and im.shape[1] == 10) self.assertTrue(l.test_for_category(cat)) self.assertTrue(l.test_for_image(cat, image)) rm_tmp_structure(path)
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)
def test_directory_stimuli_factory(self): 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.DirectoryStimuliFactory(l) for cat in img_per_cat.keys(): inp[cat] for img in img_per_cat[cat]: inp[cat][img] inp[cat][img]['a'] inp[cat][img].data test_loader.rm_tmp_structure(path) test_loader.rm_tmp_structure(ftrpath)