Skip to content

Commit

Permalink
pass keyword arguments to read_hdf5
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Kümmerer <[email protected]>
  • Loading branch information
matthias-k committed Sep 25, 2024
1 parent 17a2757 commit e50ab8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
20 changes: 10 additions & 10 deletions pysaliency/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@


@cached(WeakValueDictionary())
def _read_hdf5_from_file(source):
def _read_hdf5_from_file(source, **kwargs):
import h5py
with h5py.File(source, 'r') as hdf5_file:
return read_hdf5(hdf5_file)
return read_hdf5(hdf5_file, **kwargs)


def read_hdf5(source):
def read_hdf5(source, **kwargs):
if isinstance(source, (str, pathlib.Path)):
return _read_hdf5_from_file(source)
return _read_hdf5_from_file(source, **kwargs)

data_type = decode_string(source.attrs['type'])

if data_type == 'Fixations':
return Fixations.read_hdf5(source)
return Fixations.read_hdf5(source, **kwargs)
elif data_type == 'ScanpathFixations':
return ScanpathFixations.read_hdf5(source)
return ScanpathFixations.read_hdf5(source, **kwargs)
elif data_type == 'FixationTrains':
return FixationTrains.read_hdf5(source)
return FixationTrains.read_hdf5(source, **kwargs)
elif data_type == 'Scanpaths':
return Scanpaths.read_hdf5(source)
return Scanpaths.read_hdf5(source, **kwargs)
elif data_type == 'Stimuli':
return Stimuli.read_hdf5(source)
return Stimuli.read_hdf5(source, **kwargs)
elif data_type == 'FileStimuli':
return FileStimuli.read_hdf5(source)
return FileStimuli.read_hdf5(source, **kwargs)
else:
raise ValueError("Invalid HDF content type:", data_type)

Expand Down
13 changes: 13 additions & 0 deletions tests/datasets/test_stimuli.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ def test_file_stimuli_attributes(file_stimuli_with_attributes, tmp_path):
assert list(np.array(file_stimuli_with_attributes.attributes['some_strings'])[mask]) == partial_stimuli.attributes['some_strings']


def test_file_stimuli_readhdf5_cached(file_stimuli_with_attributes, tmp_path):
filename = tmp_path / 'stimuli.hdf5'
file_stimuli_with_attributes.to_hdf5(str(filename))

new_stimuli = pysaliency.read_hdf5(str(filename))

assert new_stimuli.cached

new_stimuli2 = pysaliency.read_hdf5(str(filename), cached=False)

assert not new_stimuli2.cached


def test_concatenate_stimuli_with_attributes(stimuli_with_attributes, file_stimuli_with_attributes):
concatenated_stimuli = pysaliency.datasets.concatenate_stimuli([stimuli_with_attributes, file_stimuli_with_attributes])

Expand Down

0 comments on commit e50ab8e

Please sign in to comment.