def stimuli_with_attributes(): stimuli_data = [np.random.randint(0, 255, size=(25, 30, 3)) for i in range(10)] attributes = { 'dva': list(range(10)), 'other_stuff': np.random.randn(10), } return pysaliency.Stimuli(stimuli_data, attributes=attributes)
def test_constant(self): stimuli = pysaliency.Stimuli([np.random.randn(600, 1000, 3), np.random.randn(600, 1000, 3)]) csmm = ConstantSaliencyModel() log_likelihoods = csmm.log_likelihoods(stimuli, self.f) np.testing.assert_allclose(log_likelihoods, -np.log(600*1000))
def stimuli_with_filenames(tmpdir): filenames = [] stimuli = [] for i in range(3): filename = tmpdir.join('stimulus_{:04d}.png'.format(i)) stimuli.append( np.random.randint(low=0, high=255, size=(100, 100, 3), dtype=np.uint8)) filenames.append(str(filename)) for sub_directory_index in range(3): sub_directory = tmpdir.join( 'sub_directory_{:04d}'.format(sub_directory_index)) for i in range(5): filename = sub_directory.join('stimulus_{:04d}.png'.format(i)) stimuli.append( np.random.randint(low=0, high=255, size=(100, 100, 3), dtype=np.uint8)) filenames.append(str(filename)) return pysaliency.Stimuli(stimuli=stimuli, attributes={'filenames': filenames})
def test_slicing(self): count = 10 widths = np.random.randint(20, 200, size=count) heights = np.random.randint(20, 200, size=count) images = [np.random.randn(h, w, 3) for h, w in zip(heights, widths)] stimuli = pysaliency.Stimuli(images) for i in range(count): s = stimuli[i] np.testing.assert_allclose(s.stimulus_data, stimuli.stimuli[i]) self.assertEqual(s.stimulus_id, stimuli.stimulus_ids[i]) self.assertEqual(s.shape, stimuli.shapes[i]) self.assertEqual(s.size, stimuli.sizes[i]) indices = [2, 4, 7] ss = stimuli[indices] for k, i in enumerate(indices): np.testing.assert_allclose(ss.stimuli[k], stimuli.stimuli[i]) self.assertEqual(ss.stimulus_ids[k], stimuli.stimulus_ids[i]) self.assertEqual(ss.shapes[k], stimuli.shapes[i]) self.assertEqual(ss.sizes[k], stimuli.sizes[i]) slc = slice(2, 8, 3) ss = stimuli[slc] indices = range(len(stimuli))[slc] for k, i in enumerate(indices): np.testing.assert_allclose(ss.stimuli[k], stimuli.stimuli[i]) self.assertEqual(ss.stimulus_ids[k], stimuli.stimulus_ids[i]) self.assertEqual(ss.shapes[k], stimuli.shapes[i]) self.assertEqual(ss.sizes[k], stimuli.sizes[i])
def test_stimuli(self): img1 = np.random.randn(100, 200, 3) img2 = np.random.randn(50, 150) stimuli = pysaliency.Stimuli([img1, img2]) self.assertEqual(stimuli.stimuli, [img1, img2]) self.assertEqual(stimuli.shapes, [(100, 200, 3), (50, 150)]) self.assertEqual(list(stimuli.sizes), [(100, 200), (50, 150)]) self.assertEqual(stimuli.stimulus_ids[1], pysaliency.datasets.get_image_hash(img2)) np.testing.assert_allclose(stimuli.stimulus_objects[1].stimulus_data, img2) self.assertEqual(stimuli.stimulus_objects[1].stimulus_id, stimuli.stimulus_ids[1]) new_stimuli = self.pickle_and_reload(stimuli, pickler=dill) print(new_stimuli.stimuli) self.assertEqual(len(new_stimuli.stimuli), 2) for s1, s2 in zip(new_stimuli.stimuli, [img1, img2]): np.testing.assert_allclose(s1, s2) self.assertEqual(new_stimuli.shapes, [(100, 200, 3), (50, 150)]) self.assertEqual(list(new_stimuli.sizes), [(100, 200), (50, 150)]) self.assertEqual(new_stimuli.stimulus_ids[1], pysaliency.datasets.get_image_hash(img2)) self.assertEqual(new_stimuli.stimulus_objects[1].stimulus_id, stimuli.stimulus_ids[1])
def test_gauss(self): stimuli = pysaliency.Stimuli([np.random.randn(40, 40, 3), np.random.randn(40, 40, 3)]) gsmm = GaussianSaliencyModel() log_likelihoods = gsmm.log_likelihoods(stimuli, self.f) np.testing.assert_allclose(log_likelihoods, np.array([-10.276835, -9.764182, -9.286885, -9.286885, -9.286885, -9.057075, -8.067126, -9.905604]))
def many_stimuli(): stimuli = [np.random.randn(40, 40, 3) for i in range(1003)] category = np.array([i % 10 for i in range(len(stimuli))]) category2 = np.array([i // 500 for i in range(len(stimuli))]) return pysaliency.Stimuli(stimuli, attributes={ 'category': category, 'category2': category2 })
def test_constant(self): stimuli = pysaliency.Stimuli([np.random.randn(600, 1000, 3), np.random.randn(600, 1000, 3)]) csmm = ConstantSaliencyModel() fb_kl = csmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations='uniform') np.testing.assert_allclose(fb_kl, 0.0) fb_kl = csmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations='shuffled') np.testing.assert_allclose(fb_kl, 0.0)
def test_gauss(self): stimuli = pysaliency.Stimuli([np.random.randn(40, 40, 3), np.random.randn(40, 40, 3)]) gsmm = GaussianSaliencyModel() fb_kl = gsmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations='uniform') np.testing.assert_allclose(fb_kl, 0.8763042309253437) fb_kl = gsmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations='shuffled') np.testing.assert_allclose(fb_kl, 0.0)
def test_constant(self): stimuli = pysaliency.Stimuli([np.random.randn(600, 1000, 3), np.random.randn(600, 1000, 3)]) csmm = ConstantSaliencyModel() aucs = csmm.AUCs(stimuli, self.f, nonfixations='uniform') np.testing.assert_allclose(aucs, np.ones(len(self.f.x))*0.5) aucs = csmm.AUCs(stimuli, self.f, nonfixations='shuffled') np.testing.assert_allclose(aucs, np.ones(len(self.f.x))*0.5)
def stimuli_with_different_sizes(): return pysaliency.Stimuli([ np.random.randn(40, 40, 3), np.random.randn(40, 40, 3), np.random.randn(20, 40, 3), np.random.randn(20, 40, 3), np.random.randn(40, 20, 3), np.random.randn(40, 20, 3), np.random.randn(40, 20), np.random.randn(20, 20, 3), np.random.randn(20, 20, 3), ])
def test_mixed(self): stimuli = pysaliency.Stimuli([np.random.randn(40, 40, 3), np.random.randn(40, 40, 3)]) gsmm = MixedSaliencyMapModel() fb_kl = gsmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations='uniform') np.testing.assert_allclose(fb_kl, 0.19700844437943388) fb_kl = gsmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations='shuffled') np.testing.assert_allclose(fb_kl, 5.874655219107867) fb_kl = gsmm.fixation_based_KL_divergence(stimuli, self.f, nonfixations=self.f) np.testing.assert_allclose(fb_kl, 0.0)
def test_model(self): stimuli = pysaliency.Stimuli([np.random.randn(40, 40, 3), np.random.randn(40, 50, 3)]) model = pysaliency.UniformModel() np.testing.assert_allclose(model.log_likelihoods(stimuli, self.f), [-np.log(40*40), -np.log(40*40), -np.log(40*40), -np.log(40*40), -np.log(40*40), -np.log(40*50), -np.log(40*50), -np.log(40*50), ])
def test_gauss(self): stimuli = pysaliency.Stimuli([np.random.randn(40, 40, 3), np.random.randn(40, 40, 3)]) gsmm = GaussianSaliencyModel() aucs = gsmm.AUCs(stimuli, self.f, nonfixations='uniform') np.testing.assert_allclose(aucs, [0.099375, 0.158125, 0.241875, 0.241875, 0.241875, 0.291875, 0.509375, 0.138125], rtol=1e-6) aucs = gsmm.AUCs(stimuli, self.f, nonfixations='shuffled') np.testing.assert_allclose(aucs, [0.0, 0.33333333, 0.33333333, 0.33333333, 0.33333333, 1., 1., 0.2], rtol=1e-6)
def test_saliency_map_model_operators(): stimuli = pysaliency.Stimuli([ np.random.randn(50, 50, 3), np.random.randn(50, 50, 3), np.random.randn(100, 200, 3) ]) m1 = ConstantSaliencyMapModel() m2 = GaussianSaliencyMapModel() for s in stimuli: smap1 = m1.saliency_map(s) smap2 = m2.saliency_map(s) np.testing.assert_allclose((m1 + m2).saliency_map(s), smap1 + smap2) np.testing.assert_allclose((m1 - m2).saliency_map(s), smap1 - smap2) np.testing.assert_allclose((m1 * m2).saliency_map(s), smap1 * smap2) np.testing.assert_allclose((m1 / m2).saliency_map(s), smap1 / smap2)
def test_gauss(self): stimuli = pysaliency.Stimuli([np.random.randn(40, 40, 3), np.random.randn(40, 40, 3)]) gsmm = GaussianSaliencyMapModel() constant_gold = ConstantSaliencyMapModel() gold = pysaliency.FixationMap(stimuli, self.f, kernel_size = 10) ib_kl = gsmm.image_based_kl_divergence(stimuli, gsmm) np.testing.assert_allclose(ib_kl, 0.0) ib_kl = gsmm.image_based_kl_divergence(stimuli, constant_gold) np.testing.assert_allclose(ib_kl, 0.8152272380729648) ib_kl = gsmm.image_based_kl_divergence(stimuli, gold) np.testing.assert_allclose(ib_kl, 1.961124862592289) ib_kl = gold.image_based_kl_divergence(stimuli, gold) np.testing.assert_allclose(ib_kl, 0.0)
def setUp(self): xs_trains = [ [0, 1, 2], [2, 2], [1, 10, 3], [4, 5, 33, 7]] ys_trains = [ [10, 11, 12], [12, 12], [21, 25, 33], [41, 42, 43, 44]] ts_trains = [ [0, 200, 600], [100, 400], [50, 500, 900], [0, 1, 2, 3]] ns = [0, 0, 1, 2] subjects = [0, 1, 1, 0] self.f = pysaliency.FixationTrains.from_fixation_trains(xs_trains, ys_trains, ts_trains, ns, subjects) self.stimuli = pysaliency.Stimuli([np.random.randn(50, 50, 3), np.random.randn(50, 50, 3), np.random.randn(100, 200, 3)])
def test_lambda_saliency_map_model(): stimuli = pysaliency.Stimuli([ np.random.randn(50, 50, 3), np.random.randn(50, 50, 3), np.random.randn(100, 200, 3) ]) m1 = ConstantSaliencyMapModel() m2 = GaussianSaliencyMapModel() fn1 = lambda smaps: np.exp(smaps[0]) fn2 = lambda smaps: np.sum(smaps, axis=0) lambda_model_1 = pysaliency.saliency_map_models.LambdaSaliencyMapModel( [m1, m2], fn1) lambda_model_2 = pysaliency.saliency_map_models.LambdaSaliencyMapModel( [m1, m2], fn2) for s in stimuli: smap1 = m1.saliency_map(s) smap2 = m2.saliency_map(s) np.testing.assert_allclose(lambda_model_1.saliency_map(s), fn1([smap1, smap2])) np.testing.assert_allclose(lambda_model_2.saliency_map(s), fn2([smap1, smap2]))
def stimuli(): return pysaliency.Stimuli( [np.random.randn(40, 40, 3), np.random.randn(40, 40, 3)])
def many_stimuli(): return pysaliency.Stimuli([np.random.randn(40, 40, 3) for i in range(1003)])
def stimuli(): return pysaliency.Stimuli( [np.random.randint(0, 255, size=(25, 30, 3)) for i in range(50)])
def more_stimuli(): return pysaliency.Stimuli([ np.random.randn(50, 50, 3), np.random.randn(50, 50, 3), np.random.randn(100, 200, 3) ])