def initialize_simulator(config):
    """Import a module that contains utility functions of spiking simulator."""

    simulator = config.get('simulation', 'simulator')
    print("Initializing {} simulator...\n".format(simulator))
    if simulator in config_string_to_set_of_strings(
            config.get('restrictions', 'simulators_pyNN')):
        if simulator == 'spiNNaker':
            try:
                sim = import_module('pyNN.' + simulator)
            except ImportError:
                sim = import_module('spynnaker8')
        else:
            sim = import_module('pyNN.' + simulator)

        # From the pyNN documentation:
        # "Before using any other functions or classes from PyNN, the user
        # must call the setup() function. Calling setup() a second time
        # resets the simulator entirely, destroying any network that may
        # have been created in the meantime."
        sim.setup(timestep=config.getfloat('simulation', 'dt'))
        return sim
    if simulator == 'brian2':
        return import_module('brian2')
    if simulator == 'loihi':
        import nxsdk.api.n2a as sim
        return sim
    sim_module_str = None
    if simulator == 'INI':
        spike_code = config.get('conversion', 'spike_code')
        sim_module_str = 'inisim.' + spike_code
        if spike_code == 'temporal_mean_rate':
            sim_module_str += '_' + config.get('simulation', 'keras_backend')
    elif simulator == 'MegaSim':
        sim_module_str = 'megasim.megasim'
    if sim_module_str is None:
        sim_module_str = 'inisim.temporal_mean_rate_theano'
    sim = import_module('snntoolbox.simulation.backends.' + sim_module_str)
    assert sim, "Simulator {} could not be initialized.".format(simulator)
    return sim def run_pipeline(config, queue=None):
    """Convert an analog network to a spiking network and simulate it.

    Complete pipeline of
        1. loading and testing a pretrained ANN,
        2. normalizing parameters
        3. converting it to SNN,
        4. running it on a simulator,
        5. given a specified hyperparameter range ``params``,
           repeat simulations with modified parameters.

    Parameters
    ----------

    config: configparser.ConfigParser
        ConfigParser containing the user settings.

    queue: Optional[Queue.Queue]
        Results are added to the queue to be displayed in the GUI.

    Returns
    -------

    results: list
        List of the accuracies obtained after simulating with each parameter
        value in config.get('parameter_sweep', 'param_values').
    """

    from snntoolbox.datasets.utils import get_dataset
    from snntoolbox.conversion.utils import normalize_parameters

    num_to_test = config.getint('simulation', 'num_to_test')

    # Instantiate an empty spiking network
    target_sim = import_target_sim(config)
    spiking_model = target_sim.SNN(config, queue)

    # ___________________________ LOAD DATASET ______________________________ #

    normset, testset = get_dataset(config)

    results = None
    parsed_model = None
    if config.getboolean('tools', 'parse') and not is_stop(queue):

        # __________________________ LOAD MODEL _____________________________ #

        model_lib = import_module('snntoolbox.parsing.model_libs.' +
                                  config.get('input', 'model_lib') +
                                  '_input_lib')
        input_model = model_lib.load(config.get('paths', 'path_wd'),
                                     config.get('paths', 'filename_ann'))

        # Evaluate input model.
        if config.getboolean('tools', 'evaluate_ann') and not is_stop(queue):
            print("Evaluating input model on {} samples...".format(
                num_to_test))
            acc = model_lib.evaluate(input_model['val_fn'],
                                     config.getint('simulation', 'batch_size'),
                                     num_to_test, **testset)
            results = [acc]

        # ____________________________ PARSE ________________________________ #

        print("Parsing input model...")
        model_parser = model_lib.ModelParser(input_model['model'], config)
        model_parser.parse()
        parsed_model = model_parser.build_parsed_model()

        # ___________________________ NORMALIZE _____________________________ #

        if config.getboolean('tools', 'normalize') and not is_stop(queue):
            normalize_parameters(parsed_model, config, **normset)

        # Evaluate parsed model.
        if config.getboolean('tools', 'evaluate_ann') and not is_stop(queue):
            print("Evaluating parsed model on {} samples...".format(
                num_to_test))
            score = model_parser.evaluate(
                config.getint('simulation', 'batch_size'),
                num_to_test, **testset)
            results = [score[1]]

        # Write parsed model to disk
        parsed_model.save(str(
            os.path.join(config.get('paths', 'path_wd'),
                         config.get('paths', 'filename_parsed_model') +
                         '.h5')))

    # _____________________________ CONVERT _________________________________ #

    if config.getboolean('tools', 'convert') and not is_stop(queue):
        if parsed_model is None:
            try:
                parsed_model = load(
                    config.get('paths', 'path_wd'),
                    config.get('paths', 'filename_parsed_model'),
                    filepath_custom_objects=config.get(
                        'paths', 'filepath_custom_objects'))['model']
            except FileNotFoundError:
                print("Could not find parsed model {} in path {}. Consider "
                      "setting `parse = True` in your config file.".format(
                        config.get('paths', 'path_wd'),
                        config.get('paths', 'filename_parsed_model')))

        spiking_model.build(parsed_model, **testset)

        # Export network in a format specific to the simulator with which it
        # will be tested later.
        spiking_model.save(config.get('paths', 'path_wd'),
                           config.get('paths', 'filename_snn'))

    # ______________________________ SIMULATE _______________________________ #

    if config.getboolean('tools', 'simulate') and not is_stop(queue):

        # Decorate the 'run' function of the spiking model with a parameter
        # sweep function.
        @run_parameter_sweep(config, queue)
        def run(snn, **test_set):
            return snn.run(**test_set)

        # Simulate network
        results = run(spiking_model, **testset)

        # Clean up
        spiking_model.end_sim()

        # Add results to queue to be displayed in GUI.
        if queue:
            queue.put(results)

    return results def normalize_parameters(model, config, **kwargs):
    """Normalize the parameters of a network.

    The parameters of each layer are normalized with respect to the maximum
    activation, or the ``n``-th percentile of activations.

    Generates plots of the activity- and weight-distribution before and after
    normalization. Note that plotting the activity-distribution can be very
    time- and memory-consuming for larger networks.
    """

    from snntoolbox.parsing.utils import get_inbound_layers_with_params

    print("Normalizing parameters...")

    norm_dir = kwargs[str('path')] if 'path' in kwargs else \
        os.path.join(config.get('paths', 'log_dir_of_current_run'),
                     'normalization')

    activ_dir = os.path.join(norm_dir, 'activations')
    if not os.path.exists(activ_dir):
        os.makedirs(activ_dir)
    # Store original weights for later plotting
    if not os.path.isfile(os.path.join(activ_dir, 'weights.npz')):
        weights = {}
        for layer in model.layers:
            w = layer.get_weights()
            if len(w) > 0:
                weights[layer.name] = w[0]
        np.savez_compressed(os.path.join(activ_dir, 'weights.npz'), **weights)

    batch_size = config.getint('simulation', 'batch_size')

    # Either load scale factors from disk, or get normalization data set to
    # calculate them.
    x_norm = None
    if 'scale_facs' in kwargs:
        scale_facs = kwargs[str('scale_facs')]
    elif 'x_norm' in kwargs or 'dataflow' in kwargs:
        if 'x_norm' in kwargs:
            x_norm = kwargs[str('x_norm')]
        elif 'dataflow' in kwargs:
            x_norm = []
            dataflow = kwargs[str('dataflow')]
            num_samples_norm = config.getint('normalization', 'num_samples',
                                             fallback='')
            if num_samples_norm == '':
                num_samples_norm = len(dataflow) * dataflow.batch_size
            while len(x_norm) * batch_size < num_samples_norm:
                x = dataflow.next()
                if isinstance(x, tuple):  # Remove class label if present.
                    x = x[0]
                x_norm.append(x)
            x_norm = np.concatenate(x_norm)
        print("Using {} samples for normalization.".format(len(x_norm)))
        sizes = [
            len(x_norm) * np.array(layer.output_shape[1:]).prod() * 32 /
            (8 * 1e9) for layer in model.layers if len(layer.weights) > 0]
        size_str = ['{:.2f}'.format(s) for s in sizes]
        print("INFO: Need {} GB for layer activations.\n".format(size_str) +
              "May have to reduce size of data set used for normalization.")
        scale_facs = OrderedDict({model.layers[0].name: 1})
    else:
        import warnings
        warnings.warn("Scale factors or normalization data set could not be "
                      "loaded. Proceeding without normalization.",
                      RuntimeWarning)
        return

    # If scale factors have not been computed in a previous run, do so now.
    if len(scale_facs) == 1:
        i = 0
        sparsity = []
        for layer in model.layers:
            # Skip if layer has no parameters
            if len(layer.weights) == 0:
                continue

            activations = try_reload_activations(layer, model, x_norm,
                                                 batch_size, activ_dir)
            nonzero_activations = activations[np.nonzero(activations)]
            sparsity.append(1 - nonzero_activations.size / activations.size)
            del activations
            perc = get_percentile(config, i)
            scale_facs[layer.name] = get_scale_fac(nonzero_activations, perc)
            print("Scale factor: {:.2f}.".format(scale_facs[layer.name]))
            # Since we have calculated output activations here, check at this
            # point if the output is mostly negative, in which case we should
            # stick to softmax. Otherwise ReLU is preferred.
            # Todo: Determine the input to the activation by replacing the
            # combined output layer by two distinct layers ``Dense`` and
            # ``Activation``!
            # if layer.activation == 'softmax' and settings['softmax_to_relu']:
            #     softmax_inputs = ...
            #     if np.median(softmax_inputs) < 0:
            #         print("WARNING: You allowed the toolbox to replace "
            #               "softmax by ReLU activations. However, more than "
            #               "half of the activations are negative, which "
            #               "could reduce accuracy. Consider setting "
            #               "settings['softmax_to_relu'] = False.")
            #         settings['softmax_to_relu'] = False
            i += 1
        # Write scale factors to disk
        filepath = os.path.join(norm_dir, config.get('normalization',
                                                     'percentile') + '.json')
        from snntoolbox.utils.utils import confirm_overwrite
        if config.get('output', 'overwrite') or confirm_overwrite(filepath):
            with open(filepath, str('w')) as f:
                json.dump(scale_facs, f)
        np.savez_compressed(os.path.join(norm_dir, 'activations', 'sparsity'),
                            sparsity=sparsity)

    # Apply scale factors to normalize the parameters.
    for layer in model.layers:
        # Skip if layer has no parameters
        if len(layer.weights) == 0:
            continue

        # Scale parameters
        parameters = layer.get_weights()
        if layer.activation.__name__ == 'softmax':
            # When using a certain percentile or even the max, the scaling
            # factor can be extremely low in case of many output classes
            # (e.g. 0.01 for ImageNet). This amplifies weights and biases
            # greatly. But large biases cause large offsets in the beginning
            # of the simulation (spike input absent).
            scale_fac = 1.0
            print("Using scale factor {:.2f} for softmax layer.".format(
                scale_fac))
        else:
            scale_fac = scale_facs[layer.name]
        inbound = get_inbound_layers_with_params(layer)
        if len(inbound) == 0:  # Input layer
            parameters_norm = [
                parameters[0] * scale_facs[model.layers[0].name] / scale_fac,
                parameters[1] / scale_fac]
        elif len(inbound) == 1:
            parameters_norm = [
                parameters[0] * scale_facs[inbound[0].name] / scale_fac,
                parameters[1] / scale_fac]
        else:
            # In case of this layer receiving input from several layers, we can
            # apply scale factor to bias as usual, but need to rescale weights
            # according to their respective input.
            parameters_norm = [parameters[0], parameters[1] / scale_fac]
            if parameters[0].ndim == 4:
                # In conv layers, just need to split up along channel dim.
                offset = 0  # Index offset at input filter dimension
                for inb in inbound:
                    f_out = inb.filters  # Num output features of inbound layer
                    f_in = range(offset, offset + f_out)
                    parameters_norm[0][:, :, f_in, :] *= \
                        scale_facs[inb.name] / scale_fac
                    offset += f_out
            else:
                # Fully-connected layers need more consideration, because they
                # could receive input from several conv layers that are
                # concatenated and then flattened. The neuron position in the
                # flattened layer depend on the image_data_format.
                raise NotImplementedError

        # Check if the layer happens to be Sparse
        # if the layer is sparse, add the mask to the list of parameters
        if len(parameters) == 3:
            parameters_norm.append(parameters[-1])
        # Update model with modified parameters
        layer.set_weights(parameters_norm)

    # Plot distributions of weights and activations before and after norm.
    if 'normalization_activations' in eval(config.get('output', 'plot_vars')):
        from snntoolbox.simulation.plotting import plot_hist
        from snntoolbox.simulation.plotting import plot_max_activ_hist

        # All layers in one plot. Assumes model.get_weights() returns
        # [w, b, w, b, ...].
        # from snntoolbox.simulation.plotting import plot_weight_distribution
        # plot_weight_distribution(norm_dir, model)

        print("Plotting distributions of weights and activations before and "
              "after normalizing...")

        # Load original parsed model to get parameters before normalization
        weights = np.load(os.path.join(activ_dir, 'weights.npz'))
        for idx, layer in enumerate(model.layers):
            # Skip if layer has no parameters
            if len(layer.weights) == 0:
                continue

            label = str(idx) + layer.__class__.__name__ \
                if config.getboolean('output', 'use_simple_labels') \
                else layer.name
            parameters = weights[layer.name]
            parameters_norm = layer.get_weights()[0]
            weight_dict = {'weights': parameters.flatten(),
                           'weights_norm': parameters_norm.flatten()}
            plot_hist(weight_dict, 'Weight', label, norm_dir)

            # Load activations of model before normalization
            activations = try_reload_activations(layer, model, x_norm,
                                                 batch_size, activ_dir)

            if activations is None or x_norm is None:
                continue

            # Compute activations with modified parameters
            nonzero_activations = activations[np.nonzero(activations)]
            activations_norm = get_activations_layer(model.input, layer.output,
                                                     x_norm, batch_size)
            activation_dict = {'Activations': nonzero_activations,
                               'Activations_norm':
                               activations_norm[np.nonzero(activations_norm)]}
            scale_fac = scale_facs[layer.name]
            plot_hist(activation_dict, 'Activation', label, norm_dir,
                      scale_fac)
            ax = tuple(np.arange(len(layer.output_shape))[1:])
            plot_max_activ_hist(
                {'Activations_max': np.max(activations, axis=ax)},
                'Maximum Activation', label, norm_dir, scale_fac)
    print('') def get_dataset(config):
    """Get dataset, either from ``.npz`` files or ``keras.ImageDataGenerator``.

    Returns Dictionaries with keys ``x_test`` and ``y_test`` if data set was
    loaded in ``.npz`` format, or with ``dataflow`` key if data will be loaded
    from ``.jpg``, ``.png``, or ``.bmp`` files by a
    ``keras.ImageDataGenerator``.

    Parameters
    ----------

    config: configparser.ConfigParser
        Settings.

    Returns
    -------

    normset: dict
        Used to normalized the network parameters.

    testset: dict
        Used to test the networks.

    """

    testset = {}
    normset = try_get_normset_from_scalefacs(config)
    dataset_path = config.get('paths', 'dataset_path')
    dataset_format = config.get('input', 'dataset_format')
    normalize_thresholds = config.getboolean('loihi', 'normalize_thresholds',
                                             fallback=False)
    is_testset_needed = config.getboolean('tools', 'evaluate_ann') or \
        config.getboolean('tools', 'simulate') or normalize_thresholds
    is_normset_needed = normalize_thresholds or (
            config.getboolean('tools', 'normalize') and normset == {})
    batch_size = config.getint('simulation', 'batch_size')

    # _______________________________ Keras __________________________________#
    try:
        keras_dataset = config.get('input', 'keras_dataset')
        if keras_dataset:
            from keras_rewiring.utilities.load_dataset \
                import load_and_preprocess_dataset
            num_to_test = config.getint('simulation', 'num_to_test')
            data = load_and_preprocess_dataset(keras_dataset)
            x_test, y_test = data['test']
            testset = {
                'x_test': x_test[:num_to_test],
                'y_test': y_test[:num_to_test]}
            if is_normset_needed:
                normset['x_norm'] = x_test
            return normset, testset
    except (NoOptionError, ImportError) as e:
        print("Warning:", e)

    # ________________________________ 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'],
                                     config)
            datagen_kwargs['preprocessing_function'] = \
                helpers.preprocessing_function

        dataflow_kwargs['directory'] = dataset_path
        if 'batch_size' not in dataflow_kwargs:
            dataflow_kwargs['batch_size'] = batch_size
        datagen = ImageDataGenerator(**datagen_kwargs)
        if (datagen.featurewise_center or datagen.featurewise_std_normalization
                or datagen.zca_whitening):
            # Compute quantities required for featurewise normalization
            # (std, mean, and principal components if ZCA whitening is applied)
            rs = datagen_kwargs.get('rescale', None)
            x_orig = ImageDataGenerator(rescale=rs).flow_from_directory(
                **dataflow_kwargs).next()[0]
            datagen.fit(x_orig)
        if is_normset_needed:
            shuffle = dataflow_kwargs.get('shuffle')
            dataflow_kwargs['shuffle'] = True
            normset['dataflow'] = \
                datagen.flow_from_directory(**dataflow_kwargs)
            dataflow_kwargs['shuffle'] = shuffle
        if is_testset_needed:
            testset = {
                'dataflow': datagen.flow_from_directory(**dataflow_kwargs)}

    # _______________________________ aedat __________________________________#
    elif dataset_format == 'aedat':
        if is_normset_needed:
            print("Loading normalization dataset from '.npz' file in {}.\n"
                  "".format(dataset_path))
            x_norm = load_npz(dataset_path, 'x_norm.npz')
            normset['x_norm'] = x_norm
            # For Loihi threshold normalization we need to pass the
            # normalization data in the testset dict.
            testset = {'x_norm': x_norm}

    return normset, testset def spiketrains_to_rates(spiketrains_n_b_l_t, duration, spike_code):
    """Convert spiketrains to spikerates.

    The output will have the same shape as the input except for the last
    dimension, which is removed by replacing a sequence of spiketimes by a
    single rate value.

    Parameters
    ----------

    spiketrains_n_b_l_t: list[tuple[np.array, str]]

    duration: int
        Duration of simulation.

    spike_code: str
        String specifying the spike encoding mechanism. For instance, with
        'ttfs', the spike rates are computed using the time to first spike.

    Returns
    -------

    spikerates_n_b_l: list[tuple[np.array, str]]
    """

    assert spike_code in {'ttfs', 'ttfs_dyn_thresh', 'ttfs_corrective',
                          'temporal_mean_rate'}

    def t2r_ttfs(t):
        isi = t[np.nonzero(t)]
        return 1. / isi[0] if len(isi) else 0.

    def t2r_ttfs_corrective(t):
        isi = t[np.nonzero(t)]
        return 1. / isi[-1] if len(isi) % 2 else 0.

    def t2r_mean_rate(t):
        # Multiplication with sign is for possible negative spikes
        # (e.g. BinaryNet)
        return np.count_nonzero(t) / duration * np.sign(np.sum(t))

    if spike_code == 'ttfs' or spike_code == 'ttfs_dyn_thresh':
        f = t2r_ttfs
    elif spike_code == 'ttfs_corrective':
        f = t2r_ttfs_corrective
    else:
        f = t2r_mean_rate

    # For output layer, we always have multiple spikes (even with ttfs), so use
    # ``t2r_mean_rate``.
    return [(np.apply_along_axis(f, -1, spiketrains_b_l_t), label)
            for spiketrains_b_l_t, label in spiketrains_n_b_l_t[:-1]] + \
           [(np.apply_along_axis(
               t2r_mean_rate, -1, spiketrains_n_b_l_t[-1][0]),
             spiketrains_n_b_l_t[-1][1])] def import_configparser():
    try:
        import configparser
    except ImportError:
        # noinspection PyPep8Naming
        import ConfigParser as configparser

    return configparser def get_pearson_coefficients(spikerates_batch, activations_batch, max_rate):
    """
    Compute Pearson coefficients.

    Parameters
    ----------

    spikerates_batch : 
    activations_batch :
    max_rate: float
        Highest spike rate.

    Returns
    -------
    
    co: list

    """

    co = []
    for layer_num in range(len(spikerates_batch)):
        c = []
        for sample in range(len(spikerates_batch[0][0])):
            s = spikerates_batch[layer_num][0][sample].flatten()
            a = activations_batch[layer_num][0][sample].flatten()
            if layer_num < len(spikerates_batch) - 1:
                # Remove points at origin and saturated units, except for
                # output layer (has too few units and gets activation of 1
                # because of softmax).
                ss = []
                aa = []
                for sss, aaa in zip(s, a):
                    if (sss > 0 or aaa > 0) and aaa < max_rate:
                        ss.append(sss)
                        aa.append(aaa)
                s = ss
                a = aa
            c.append(np.corrcoef(s, a)[0, 1])
        co.append(c)

    return co def _config(_path_wd, _datapath):
def _config_first(_path_wd, _datapath_first):
def _config_last(_path_wd, _datapath_last):
def get_config(path_wd, datapath):
def _path_wd(tmpdir_factory):
def get_dataset(data_format=None):
def save_dataset(datapath, x, y):
def get_datapath(tmpdir_factory, data_format):
def _datapath_first(tmpdir_factory):
def _datapath_last(tmpdir_factory):
def _datapath(_datapath_first, _datapath_last):
def _dataset(_dataset_first, _dataset_last):
def _dataset_first():
def _dataset_last():
def _normset(_dataset):
def _testset(_dataset):
def _installed_input_libs(_config):
def _model_1(_dataset):
def get_model_1(dataset):
def _model_2(_dataset):
def _model_3(_dataset):
def _model_4(_dataset):
def _model_4_first(_dataset_first):
def get_model_4(dataset):
        def __init__(self, data, target, transform=None):
        def __getitem__(self, index):
        def __len__(self):
def get_examples():
def _example_filepath(request):
    class PytorchDataset(torch.utils.data.Dataset): def _config(_path_wd, _datapath):
def _config_first(_path_wd, _datapath_first):
def _config_last(_path_wd, _datapath_last):
def get_config(path_wd, datapath):
def _path_wd(tmpdir_factory):
def get_dataset(data_format=None):
def save_dataset(datapath, x, y):
def get_datapath(tmpdir_factory, data_format):
def _datapath_first(tmpdir_factory):
def _datapath_last(tmpdir_factory):
def _datapath(_datapath_first, _datapath_last):
def _dataset(_dataset_first, _dataset_last):
def _dataset_first():
def _dataset_last():
def _normset(_dataset):
def _testset(_dataset):
def _installed_input_libs(_config):
def _model_1(_dataset):
def get_model_1(dataset):
def _model_2(_dataset):
def _model_3(_dataset):
def _model_4(_dataset):
def _model_4_first(_dataset_first):
def get_model_4(dataset):
        def __init__(self, data, target, transform=None):
        def __getitem__(self, index):
        def __len__(self):
def get_examples():
def _example_filepath(request):
    class PytorchDataset(torch.utils.data.Dataset): def _config(_path_wd, _datapath):
def _config_first(_path_wd, _datapath_first):
def _config_last(_path_wd, _datapath_last):
def get_config(path_wd, datapath):
def _path_wd(tmpdir_factory):
def get_dataset(data_format=None):
def save_dataset(datapath, x, y):
def get_datapath(tmpdir_factory, data_format):
def _datapath_first(tmpdir_factory):
def _datapath_last(tmpdir_factory):
def _datapath(_datapath_first, _datapath_last):
def _dataset(_dataset_first, _dataset_last):
def _dataset_first():
def _dataset_last():
def _normset(_dataset):
def _testset(_dataset):
def _installed_input_libs(_config):
def _model_1(_dataset):
def get_model_1(dataset):
def _model_2(_dataset):
def _model_3(_dataset):
def _model_4(_dataset):
def _model_4_first(_dataset_first):
def get_model_4(dataset):
        def __init__(self, data, target, transform=None):
        def __getitem__(self, index):
        def __len__(self):
def get_examples():
def _example_filepath(request):
    class PytorchDataset(torch.utils.data.Dataset):
import os
import subprocess
import sys
import numpy as np
from importlib import import_module
from tensorflow.keras import models
from snntoolbox.bin.utils import initialize_simulator
from snntoolbox.bin.utils import run_pipeline
from snntoolbox.conversion.utils import normalize_parameters
from snntoolbox.datasets.utils import get_dataset
from snntoolbox.simulation.utils import spiketrains_to_rates
from snntoolbox.utils.utils import import_configparser, \
    get_pearson_coefficients
from tests.conftest import spinnaker_skip_if_dependency_missing, \
    loihi_skip_if_dependency_missing
from tests.conftest import nest_skip_if_dependency_missing
from tests.conftest import brian2_skip_if_dependency_missing

        updates = {
            'tools': {'evaluate_ann': False},
            'simulation': {
                'simulator': 'nest',
                'duration': 50,
                'num_to_test': 10,
                'batch_size': 1,
                'dt': 0.1},
            'cell': {
                'tau_refrac': 0.1,
                'delay': 0.1,
                'v_thresh': 0.01},
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}}}

        _config.read_dict(updates)

        initialize_simulator(_config)

        acc = run_pipeline(_config)

        assert acc[0] >= 0.95

        corr = get_correlations(_config)
        assert np.all(corr[:-1] > 0.97)
        assert corr[-1] > 0.5

    @spinnaker_skip_if_dependency_missing
    def test_spinnaker(self, _model_1, _config):

        path_wd = _config.get('paths', 'path_wd')
        model_name = _config.get('paths', 'filename_ann')
        models.save_model(_model_1, os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {'evaluate_ann': False},
            'input': {'poisson_input': True},
            'simulation': {
                'simulator': 'spiNNaker',
                'duration': 100,
                'num_to_test': 1,  # smaller to make more feasible
                'batch_size': 1},
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}}}

        _config.read_dict(updates)

        initialize_simulator(_config)

        acc = run_pipeline(_config)

        assert acc[0] >= 0.95

        corr = get_correlations(_config)
        assert np.all(corr[:-1] > 0.97)
        assert corr[-1] > 0.5

    @spinnaker_skip_if_dependency_missing
    def test_spinnaker_sparse(self, _model_3, _config):

        path_wd = _config.get('paths', 'path_wd')
        model_name = _config.get('paths', 'filename_ann')
        models.save_model(_model_3, os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {'evaluate_ann': False},
            'input': {'poisson_input': True},
            'simulation': {
                'simulator': 'spiNNaker',
                'duration': 100,
                'num_to_test': 1,  # smaller to make more feasible
                'batch_size': 1},
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}}}

        _config.read_dict(updates)

        initialize_simulator(_config)

        acc = run_pipeline(_config)

        assert acc[0] >= 0.95

        corr = get_correlations(_config)
        assert np.all(corr[:-1] > 0.97)
        assert corr[-1] > 0.5

    @loihi_skip_if_dependency_missing
    def test_loihi(self, _model_1, _config):

        path_wd = _config.get('paths', 'path_wd')
        model_name = _config.get('paths', 'filename_ann')
        models.save_model(_model_1, os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {'evaluate_ann': True,
                      'normalize': False},
            'loihi': {'reset_mode': 'soft',
                      'desired_threshold_to_input_ratio': 1,
                      'compartment_kwargs': {'biasExp': 6, 'vThMant': 512},
                      'connection_kwargs': {'numWeightBits': 8,
                                            'weightExponent': 0,
                                            'numBiasBits': 12},
                      'validate_partitions': False,
                      'save_output': False,
                      'do_overflow_estimate': False,
                      'normalize_thresholds': True},
            'simulation': {
                'simulator': 'loihi',
                'duration': 512,
                'num_to_test': 100,
                'batch_size': 20},
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}}}

        _config.read_dict(updates)

        initialize_simulator(_config)


        acc = run_pipeline(_config)