Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small bugs and enhancements #60

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pysaliency/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import models
from . import external_models
from . import external_datasets
from . import utils

from .datasets import (
Fixations,
Expand Down
7 changes: 6 additions & 1 deletion pysaliency/external_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
CovSal,
)

from .utils import ExternalModelMixin
from .deepgaze import (
DeepGazeI,
DeepGazeIIE,
)

from .utils import ExternalModelMixin
4 changes: 3 additions & 1 deletion pysaliency/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ def log_likelihoods(self, stimuli, fixations, verbose=False):
for stimulus_index in stimulus_indices:
stimulus_shapes[stimulus_index] = stimuli.stimulus_objects[stimulus_index].size

stimulus_log_likelihoods = -np.log(stimulus_shapes).sum(axis=1)
with np.errstate(divide='ignore'): # ignore log(0) warnings, we won't use them anyway
stimulus_log_likelihoods = -np.log(stimulus_shapes).sum(axis=1)

return stimulus_log_likelihoods[fixations.n]


Expand Down
15 changes: 14 additions & 1 deletion pysaliency/saliency_map_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,14 @@ def AUC_per_image(self, stimuli, fixations, nonfixations='uniform', thresholds='
nonfixations = FullShuffledNonfixationProvider(stimuli, fixations)

for n in tqdm(range(len(stimuli)), disable=not verbose):
inds = fixations.n == n
if not inds.sum():
rocs_per_image.append(np.nan)
continue

out = self.saliency_map(stimuli.stimulus_objects[n])
check_prediction_shape(out, stimuli[n])
inds = fixations.n == n

positives = np.asarray(out[fixations.y_int[inds], fixations.x_int[inds]])
if nonfixations == 'uniform':
negatives = out.flatten()
Expand Down Expand Up @@ -482,6 +487,14 @@ def AUC(self, stimuli, fixations, nonfixations='uniform', average='fixation', th

return np.average(aucs, weights=weights)
elif average == 'image':
stimulus_indices = set(fixations.n)
nan_value_indices = np.nonzero(np.isnan(aucs))[0]

if stimulus_indices.intersection(nan_value_indices):
raise ValueError("Some images with fixations returned AUC of nan, which should not happen")

aucs = aucs[~np.isnan(aucs)]

return np.mean(aucs)
else:
raise ValueError(average)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_saliency_map_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ def test_auc_gauss(stimuli, fixation_trains):
np.testing.assert_allclose(aucs_single, aucs_combined)


def test_auc_per_image(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

aucs = gsmm.AUC_per_image(stimuli, fixation_trains)
np.testing.assert_allclose(aucs,
[0.196625, 0.313125],
rtol=1e-6)


def test_auc_per_image_images_without_fixations(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

aucs = gsmm.AUC_per_image(stimuli, fixation_trains[:5],)
np.testing.assert_allclose(aucs,
[0.196625, np.nan],
rtol=1e-6)


def test_auc_image_average_with_images_without_fixations(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

auc = gsmm.AUC(stimuli, fixation_trains[:5], average='image')
np.testing.assert_allclose(auc, 0.196625, rtol=1e-6)


def test_nss_gauss(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

Expand Down
Loading