def test_factories(self): 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) with NamedTemporaryFile(mode='w', prefix='fix_occ_test', suffix='.mat') as ntf: ntf.write(get_data('ocupy.tests', 'fixmat_demo.mat')) ntf.seek(0) fm2 = fixmat.DirectoryFixmatFactory(os.path.dirname(ntf.name), glob_str='fix_occ_test*.mat') self.compare_fixmats(fm, fm2) self.assertRaises( ValueError, lambda: fixmat.DirectoryFixmatFactory( '.', glob_str='xxx*.mat'))
def test_cmp2fixmat(self): # This test only works with some scipy versions. 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) 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_ref = loadmat(ntf.name, struct_as_record=True)['fixmat'][0][0] for field in fm._fields: l1 = fm_ref[field] l2 = fm.__dict__[field] self.assertEquals(l1.size, l2.size) self.assertTrue((l1 == l2).all())
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_anglendiff(self): fm = fixmat.FixmatFactory('/net/store/users/nwilming/eq/analysis/qoamp/fixmat_photos.mat') gen = simulator.FixGen(fm) for angle in [-180, -90, 0, 45, 90, 135]: print "Running anglendiff test on "+repr(angle)+" degrees." coord = [(0,0)] length = 1 cur_angle = [np.nan] cur_angle.append(angle) # Create artificial fixmat with fixed angle differences for j in range(len(fm.x)-1): coord.append(simulator.calc_xy(coord[-1], cur_angle[-1], length)) cur_angle.append(cur_angle[-1]+angle) fm.x = np.array([x[0] for x in coord]) fm.y = np.array([x[1] for x in coord]) #XXX Parameter should be None, not "None" gen.initialize_data(fit=None) # Use anglendiff to calculate angles and angle_diff a, l, ad, ld = simulator.anglendiff(fm, return_abs=True) a = np.round(a[0]) a[a==-180]=180 cur_angle = simulator.reshift(np.array(cur_angle[0:-1])) # Assign nans to make arrays comparable cur_angle[np.isnan(a)]=np.nan cur_angle[np.round(cur_angle)==-180]=180 ad = np.round(simulator.reshift(ad[0][~np.isnan(ad[0])])) if (angle==180 or angle==-180): self.assertTrue(np.logical_or(ad==angle, ad==-angle).all()) else: self.assertTrue((ad==angle).all()) self.assertTrue((a[~np.isnan(a)]==cur_angle[~np.isnan(cur_angle)]).all())