# Path: pysaliency/datasets.py
# def read_hdf5(source):
#     if isinstance(source, str):
#         return _read_hdf5_from_file(source)
# 
#     data_type = decode_string(source.attrs['type'])
# 
#     if data_type == 'Fixations':
#         return Fixations.read_hdf5(source)
#     elif data_type == 'FixationTrains':
#         return FixationTrains.read_hdf5(source)
#     elif data_type == 'Stimuli':
#         return Stimuli.read_hdf5(source)
#     elif data_type == 'FileStimuli':
#         return FileStimuli.read_hdf5(source)
#     else:
#         raise ValueError("Invalid HDF content type:", data_type)
# 
# Path: pysaliency/filter_datasets.py
# def filter_fixations_by_number(fixations, intervals):
#     intervals = _check_intervals(intervals, type=int)
#     inds = np.zeros_like(fixations.x, dtype=bool)
# 
#     for n1, n2 in intervals:
#         _inds = np.logical_and(fixations.lengths >= n1, fixations.lengths < n2)
#         inds = np.logical_or(inds, _inds)
# 
#     return fixations[inds]
# 
# def filter_stimuli_by_number(stimuli, fixations, intervals):
#     intervals = _check_intervals(intervals, type=int)
#     mask = np.zeros(len(stimuli), dtype=bool)
# 
#     for n1, n2 in intervals:
#         mask[n1:n2] = True
# 
#     indices = list(np.nonzero(mask)[0])
# 
#     return create_subset(stimuli, fixations, indices)
# 
# def filter_stimuli_by_size(stimuli, fixations, size=None, sizes=None):
#     if size is not None:
#         sizes = [size]
#     sizes = [tuple(size) for size in sizes]
# 
#     indices = [i for i in range(len(stimuli)) if stimuli.sizes[i] in sizes]
# 
#     return create_subset(stimuli, fixations, indices)
# 
# def train_split(stimuli, fixations, crossval_folds, fold_no, val_folds=1, test_folds=1, random=True, stratified_attributes=None):
#     return crossval_split(stimuli, fixations, crossval_folds, fold_no, val_folds=val_folds, test_folds=test_folds, random=random, split='train', stratified_attributes=stratified_attributes)
# 
# def validation_split(stimuli, fixations, crossval_folds, fold_no, val_folds=1, test_folds=1, random=True, stratified_attributes=None):
#     return crossval_split(stimuli, fixations, crossval_folds, fold_no, val_folds=val_folds, test_folds=test_folds, random=random, split='val', stratified_attributes=stratified_attributes)
# 
# def test_split(stimuli, fixations, crossval_folds, fold_no, val_folds=1, test_folds=1, random=True, stratified_attributes=None):
#     return crossval_split(stimuli, fixations, crossval_folds, fold_no, val_folds=val_folds, test_folds=test_folds, random=random, split='test', stratified_attributes=stratified_attributes)


from .datasets import read_hdf5
from .filter_datasets import (
    filter_fixations_by_number,
    filter_stimuli_by_number,
    filter_stimuli_by_size,
    train_split,
    validation_split,
    test_split
)
from schema import Schema, Optional



dataset_config_schema = Schema({
    'stimuli': str,
    'fixations': str,
    Optional('filters', default=[]): [{
        'type': str,
        Optional('parameters', default={}): dict,
    }],
})


def load_dataset_from_config(config):
    config = dataset_config_schema.validate(config)
    stimuli = read_hdf5(config['stimuli'])
    fixations = read_hdf5(config['fixations'])

    for filter_config in config['filters']:
        stimuli, fixations = apply_dataset_filter_config(stimuli, fixations, filter_config)

    return stimuli, fixations


def apply_dataset_filter_config(stimuli, fixations, filter_config):
    filter_dict = {
        'filter_fixations_by_number': add_stimuli_argument(filter_fixations_by_number),

# Path: pysaliency/datasets.py
# def read_hdf5(source):
#     if isinstance(source, str):
#         return _read_hdf5_from_file(source)
# 
#     data_type = decode_string(source.attrs['type'])
# 
#     if data_type == 'Fixations':
#         return Fixations.read_hdf5(source)
#     elif data_type == 'FixationTrains':
#         return FixationTrains.read_hdf5(source)
#     elif data_type == 'Stimuli':
#         return Stimuli.read_hdf5(source)
#     elif data_type == 'FileStimuli':
#         return FileStimuli.read_hdf5(source)
#     else:
#         raise ValueError("Invalid HDF content type:", data_type)
# 
# Path: pysaliency/filter_datasets.py
# def filter_fixations_by_number(fixations, intervals):
#     intervals = _check_intervals(intervals, type=int)
#     inds = np.zeros_like(fixations.x, dtype=bool)
# 
#     for n1, n2 in intervals:
#         _inds = np.logical_and(fixations.lengths >= n1, fixations.lengths < n2)
#         inds = np.logical_or(inds, _inds)
# 
#     return fixations[inds]
# 
# def filter_stimuli_by_number(stimuli, fixations, intervals):
#     intervals = _check_intervals(intervals, type=int)
#     mask = np.zeros(len(stimuli), dtype=bool)
# 
#     for n1, n2 in intervals:
#         mask[n1:n2] = True
# 
#     indices = list(np.nonzero(mask)[0])
# 
#     return create_subset(stimuli, fixations, indices)
# 
# def filter_stimuli_by_size(stimuli, fixations, size=None, sizes=None):
#     if size is not None:
#         sizes = [size]
#     sizes = [tuple(size) for size in sizes]
# 
#     indices = [i for i in range(len(stimuli)) if stimuli.sizes[i] in sizes]
# 
#     return create_subset(stimuli, fixations, indices)
# 
# def train_split(stimuli, fixations, crossval_folds, fold_no, val_folds=1, test_folds=1, random=True, stratified_attributes=None):
#     return crossval_split(stimuli, fixations, crossval_folds, fold_no, val_folds=val_folds, test_folds=test_folds, random=random, split='train', stratified_attributes=stratified_attributes)
# 
# def validation_split(stimuli, fixations, crossval_folds, fold_no, val_folds=1, test_folds=1, random=True, stratified_attributes=None):
#     return crossval_split(stimuli, fixations, crossval_folds, fold_no, val_folds=val_folds, test_folds=test_folds, random=random, split='val', stratified_attributes=stratified_attributes)
# 
# def test_split(stimuli, fixations, crossval_folds, fold_no, val_folds=1, test_folds=1, random=True, stratified_attributes=None):
#     return crossval_split(stimuli, fixations, crossval_folds, fold_no, val_folds=val_folds, test_folds=test_folds, random=random, split='test', stratified_attributes=stratified_attributes)


# Path: pysaliency/dataset_config.py
from .datasets import read_hdf5
from .filter_datasets import (
    filter_fixations_by_number,
    filter_stimuli_by_number,
    filter_stimuli_by_size,
    train_split,
    validation_split,
    test_split
)
from schema import Schema, Optional




dataset_config_schema = Schema({
    'stimuli': str,
    'fixations': str,
    Optional('filters', default=[]): [{
        'type': str,
        Optional('parameters', default={}): dict,
    }],
})


def load_dataset_from_config(config):
    config = dataset_config_schema.validate(config)
    stimuli = read_hdf5(config['stimuli'])
    fixations = read_hdf5(config['fixations'])

    for filter_config in config['filters']:
        stimuli, fixations = apply_dataset_filter_config(stimuli, fixations, filter_config)

    return stimuli, fixations


def apply_dataset_filter_config(stimuli, fixations, filter_config):
    filter_dict = {
        'filter_fixations_by_number': add_stimuli_argument(filter_fixations_by_number),

        'filter_stimuli_by_number': filter_stimuli_by_number,