# Path: snntoolbox/utils/utils.py
# def import_helpers(filepath, config):
#     """Import a module with helper functions from ``filepath``.
# 
#     Parameters
#     ----------
# 
#     filepath: str
#         Filename or relative or absolute path of module to import. If only
#         the filename is given, module is assumed to be in current working
#         directory (``config.get('paths', 'path_wd')``). Non-absolute paths are
#         taken relative to working dir.
#     config: configparser.ConfigParser
#         Settings.
# 
#     Returns
#     -------
# 
#     :
#         Module with helper functions.
# 
#     """
# 
#     path, filename = get_abs_path(filepath, config)
# 
#     return import_script(path, filename)


import json
import os
import numpy as np
from configparser import NoOptionError
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from snntoolbox.utils.utils import import_helpers
            from keras_rewiring.utilities.load_dataset \
                import load_and_preprocess_dataset
    # ________________________________ npz ___________________________________#
    if dataset_format == 'npz':
        print("Loading data set from '.npz' files in {}.\n".format(
            dataset_path))
        if is_testset_needed:
            num_to_test = config.getint('simulation', 'num_to_test')
            x_test = load_npz(dataset_path, 'x_test.npz')[:num_to_test]
            y_test = load_npz(dataset_path, 'y_test.npz')[:num_to_test]
            testset = {'x_test': x_test, 'y_test': y_test}
        if is_normset_needed:
            x_norm = load_npz(dataset_path, 'x_norm.npz')
            normset['x_norm'] = x_norm

    # ________________________________ jpg ___________________________________#
    elif dataset_format in {'jpg', 'png'}:
        print("Loading data set from ImageDataGenerator, using images in "
              "{}.\n".format(dataset_path))
        # Transform str to dict
        datagen_kwargs = eval(config.get('input', 'datagen_kwargs'))
        dataflow_kwargs = eval(config.get('input', 'dataflow_kwargs'))

        # Get class labels
        class_idx_path = config.get('paths', 'class_idx_path')
        if class_idx_path != '':
            class_idx = json.load(open(os.path.abspath(class_idx_path)))
            dataflow_kwargs['classes'] = \
                [class_idx[str(idx)][0] for idx in range(len(class_idx))]

        # Get proprocessing function
        if 'preprocessing_function' in datagen_kwargs:

# Path: snntoolbox/utils/utils.py
# def import_helpers(filepath, config):
#     """Import a module with helper functions from ``filepath``.
# 
#     Parameters
#     ----------
# 
#     filepath: str
#         Filename or relative or absolute path of module to import. If only
#         the filename is given, module is assumed to be in current working
#         directory (``config.get('paths', 'path_wd')``). Non-absolute paths are
#         taken relative to working dir.
#     config: configparser.ConfigParser
#         Settings.
# 
#     Returns
#     -------
# 
#     :
#         Module with helper functions.
# 
#     """
# 
#     path, filename = get_abs_path(filepath, config)
# 
#     return import_script(path, filename)


# Path: snntoolbox/datasets/utils.py
import json
import os
import numpy as np
from configparser import NoOptionError
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from snntoolbox.utils.utils import import_helpers
            from keras_rewiring.utilities.load_dataset \
                import load_and_preprocess_dataset

    # ________________________________ npz ___________________________________#
    if dataset_format == 'npz':
        print("Loading data set from '.npz' files in {}.\n".format(
            dataset_path))
        if is_testset_needed:
            num_to_test = config.getint('simulation', 'num_to_test')
            x_test = load_npz(dataset_path, 'x_test.npz')[:num_to_test]
            y_test = load_npz(dataset_path, 'y_test.npz')[:num_to_test]
            testset = {'x_test': x_test, 'y_test': y_test}
        if is_normset_needed:
            x_norm = load_npz(dataset_path, 'x_norm.npz')
            normset['x_norm'] = x_norm

    # ________________________________ jpg ___________________________________#
    elif dataset_format in {'jpg', 'png'}:
        print("Loading data set from ImageDataGenerator, using images in "
              "{}.\n".format(dataset_path))
        # Transform str to dict
        datagen_kwargs = eval(config.get('input', 'datagen_kwargs'))
        dataflow_kwargs = eval(config.get('input', 'dataflow_kwargs'))

        # Get class labels
        class_idx_path = config.get('paths', 'class_idx_path')
        if class_idx_path != '':
            class_idx = json.load(open(os.path.abspath(class_idx_path)))
            dataflow_kwargs['classes'] = \
                [class_idx[str(idx)][0] for idx in range(len(class_idx))]

        # Get proprocessing function
        if 'preprocessing_function' in datagen_kwargs:

            helpers = import_helpers(datagen_kwargs['preprocessing_function'],