From 00cfbd8d725fa742727b117105340b65375a4def Mon Sep 17 00:00:00 2001 From: Harneet Singh Khanuja Date: Mon, 22 Jan 2024 17:12:02 +0100 Subject: [PATCH 1/5] Merge branch 'dev' of github.com:naman0210/pysaliency into dev --- pysaliency/precomputed_models.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/pysaliency/precomputed_models.py b/pysaliency/precomputed_models.py index 8dc3bed..8fbb0d4 100644 --- a/pysaliency/precomputed_models.py +++ b/pysaliency/precomputed_models.py @@ -183,6 +183,29 @@ def _log_density(self, stimulus): return smap +def get_keys_recursive(group, prefix=''): + import h5py + + keys = [] + + for subgroup_name, subgroup in group.items(): + if isinstance(subgroup, h5py.Group): + subprefix = f"{prefix}{subgroup_name}/" + keys.extend(get_keys_recursive(subgroup, prefix=subprefix)) + else: + keys.append(f"{prefix}{subgroup_name}") + + return keys + +def get_stimulus_key(stimulus_name, all_keys): + matching_keys = [key for key in all_keys if key.endswith(stimulus_name)] + if len(matching_keys) == 0: + raise ValueError(f"Stimulus {stimulus_name} not found in hdf5 file!") + elif len(matching_keys) > 1: + raise ValueError(f"Stimulus {stimulus_name} not unique in hdf5 file!") + return matching_keys[0] + + class HDF5SaliencyMapModel(SaliencyMapModel): """ exposes a HDF5 file with saliency maps as pysaliency model @@ -203,15 +226,17 @@ def __init__(self, stimuli, filename, check_shape=True, **kwargs): import h5py self.hdf5_file = h5py.File(self.filename, 'r') + self.all_keys = get_keys_recursive(self.hdf5_file) def _saliency_map(self, stimulus): stimulus_id = get_image_hash(stimulus) stimulus_index = self.stimuli.stimulus_ids.index(stimulus_id) stimulus_filename = self.names[stimulus_index] - smap = self.hdf5_file[stimulus_filename][:] + stimulus_key = get_stimulus_key(stimulus_filename, self.all_keys) + smap = self.hdf5_file[stimulus_key][:] if not smap.shape == (stimulus.shape[0], stimulus.shape[1]): if self.check_shape: - warnings.warn('Wrong shape for stimulus {}'.format(stimulus_filename)) + warnings.warn('Wrong shape for stimulus {}'.format(stimulus_key)) return smap From bd442299ec0dbff82c1615e9df619069c40cd150 Mon Sep 17 00:00:00 2001 From: Harneet Singh Khanuja Date: Mon, 22 Jan 2024 18:57:18 +0100 Subject: [PATCH 2/5] Update test_precomputed_models.py --- tests/test_precomputed_models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_precomputed_models.py b/tests/test_precomputed_models.py index 66ab172..1e507c3 100644 --- a/tests/test_precomputed_models.py +++ b/tests/test_precomputed_models.py @@ -17,7 +17,7 @@ def file_stimuli(tmpdir): filenames = [] for i in range(3): - filename = tmpdir.join('stimulus_{:04d}.png'.format(i)) + filename = tmpdir.join('_stimulus_{:04d}.png'.format(i)) imsave(str(filename), np.random.randint(low=0, high=255, size=(100, 100, 3), dtype=np.uint8)) filenames.append(str(filename)) @@ -36,7 +36,7 @@ def stimuli_with_filenames(tmpdir): filenames = [] stimuli = [] for i in range(3): - filename = tmpdir.join('stimulus_{:04d}.png'.format(i)) + 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)) From 41fac5348b77769e2336d58ae95121b80a4f03ac Mon Sep 17 00:00:00 2001 From: matthias-k Date: Mon, 22 Jan 2024 21:26:17 +0100 Subject: [PATCH 3/5] Update tests/test_precomputed_models.py added comment --- tests/test_precomputed_models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_precomputed_models.py b/tests/test_precomputed_models.py index 1e507c3..49254ca 100644 --- a/tests/test_precomputed_models.py +++ b/tests/test_precomputed_models.py @@ -17,6 +17,7 @@ def file_stimuli(tmpdir): filenames = [] for i in range(3): + # TODO: change back to stimulus_... once this is supported again filename = tmpdir.join('_stimulus_{:04d}.png'.format(i)) imsave(str(filename), np.random.randint(low=0, high=255, size=(100, 100, 3), dtype=np.uint8)) filenames.append(str(filename)) From 6ed3c33f74eac451136736f963a99da6e2dc9ec7 Mon Sep 17 00:00:00 2001 From: matthias-k Date: Mon, 22 Jan 2024 21:26:45 +0100 Subject: [PATCH 4/5] Update tests/test_precomputed_models.py --- tests/test_precomputed_models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_precomputed_models.py b/tests/test_precomputed_models.py index 49254ca..1b25a57 100644 --- a/tests/test_precomputed_models.py +++ b/tests/test_precomputed_models.py @@ -37,6 +37,7 @@ def stimuli_with_filenames(tmpdir): filenames = [] stimuli = [] for i in range(3): + # TODO: change back to stimulus_... once this is supported again 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)) From aa9e2fcedcc2be0226d45dc2ee58eb186556e726 Mon Sep 17 00:00:00 2001 From: Harneet Singh Khanuja Date: Wed, 31 Jan 2024 18:30:02 +0100 Subject: [PATCH 5/5] Update dataset_config_schema to include lmdb path --- pysaliency/dataset_config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pysaliency/dataset_config.py b/pysaliency/dataset_config.py index 0dd84e2..df42ef4 100644 --- a/pysaliency/dataset_config.py +++ b/pysaliency/dataset_config.py @@ -13,7 +13,7 @@ remove_stimuli_without_fixations ) -from schema import Schema, Optional +from schema import Schema, Optional, Or dataset_config_schema = Schema({ @@ -23,6 +23,7 @@ 'type': str, Optional('parameters', default={}): dict, }], + Optional('lmdb_path', default=None): Or(str, None), })