<commit_before># -*- coding: utf-8 -*-
"""
Building and simulating spiking neural networks using
`SpiNNaker <http://apt.cs.manchester.ac.uk/projects/SpiNNaker/>`_.

Dependency: `SpyNNaker software
<http://spinnakermanchester.github.io/development/devenv.html>`_

@author: UMan, rbodo, piewchee
"""
import warnings

import numpy as np
import os
import sys
from tensorflow import keras

from snntoolbox.parsing.utils import get_type
from snntoolbox.simulation.target_simulators.pyNN_target_sim import \
    SNN as PYSNN
from snntoolbox.simulation.target_simulators.pyNN_target_sim import \
    get_shape_from_label
from snntoolbox.simulation.utils import build_convolution, \
    build_depthwise_convolution, build_1d_convolution, build_pooling
from snntoolbox.utils.utils import confirm_overwrite


class SNN(PYSNN):

    def scale_weights(self, weights):

        from math import exp
        # This ignores the leak term
        tau_syn_E = self.config.getfloat('cell', 'tau_syn_E')
        tau_syn_I = self.config.getfloat('cell', 'tau_syn_I')
        # just to give a sensible answer if tau_syn_E and I are different
        t = self._dt
        tau = (tau_syn_E + tau_syn_I) / 2
        scale = 10 * t / (tau * (exp(-(t / tau)) + 1))
        print('Weights scaled by a factor of {0}'.format(scale,))
        if isinstance(weights, list):
            weights = [(i, j, weight * scale, delay)
                       for (i, j, weight, delay) in weights]
        elif isinstance(weights, np.ndarray):
            weights = weights * scale
        else:
            raise Exception("Not a valid weight type")
        return weights

    def setup_layers(self, batch_shape):
        '''Iterates over all layers to instantiate them in the simulator.'''

        self.add_input_layer(batch_shape)

        for layer in self.parsed_model.layers[1:]:
            print("Instantiating layer: {}".format(layer.name))
            self.add_layer(layer)

            layer_type = get_type(layer)
            print("Building layer: {}".format(layer.name))
            if layer_type == 'Flatten':
                self.flatten_shapes.append(
                    (layer.name, get_shape_from_label(self.layers[-1].label)))
                self.build_flatten(layer)
                continue
            if layer_type in {'Dense', 'Sparse'}:
                self.build_dense(layer)
            elif layer_type in {'Conv1D', 'Conv2D', 'DepthwiseConv2D',
                                'SparseConv2D', 'SparseDepthwiseConv2D'}:
                self.build_convolution(layer)
                self.data_format = layer.data_format
            elif layer_type in {'MaxPooling2D', 'AveragePooling2D'}:
                self.build_pooling(layer)

            elif layer_type == 'ZeroPadding':
                padding = layer.padding
                if set(padding).issubset((1, (1, 1))):
                    self.change_padding = True
                    return
                else:
                    raise NotImplementedError(
                        "Border_mode {} not supported.".format(padding))

    def add_layer(self, layer):

        # This implementation of ZeroPadding layers assumes symmetric single
        # padding ((1, 1), (1, 1)).
        # Todo: Generalize for asymmetric padding or arbitrary size.
        if 'ZeroPadding' in layer.__class__.__name__:
            return
        if 'Flatten' in layer.__class__.__name__:
            return
        if 'Reshape' in layer.__class__.__name__:
            return
        self.layers.append(self.sim.Population(
            np.asscalar(np.prod(layer.output_shape[1:], dtype=np.int)),
            self.sim.IF_curr_exp, self.cellparams, label=layer.name))

        self.layers[-1].initialize(v=self.layers[-1].get('v_rest'))

    def build_dense(self, layer):
        """

        Parameters
        ----------
        layer : keras.layers.Dense

        Returns
        -------

        """<commit_msg>Remove call of np.asscalar() (#124)<commit_after># -*- coding: utf-8 -*-
"""
Building and simulating spiking neural networks using
`SpiNNaker <http://apt.cs.manchester.ac.uk/projects/SpiNNaker/>`_.

Dependency: `SpyNNaker software
<http://spinnakermanchester.github.io/development/devenv.html>`_

@author: UMan, rbodo, piewchee
"""
import warnings

import numpy as np
import os
import sys
from tensorflow import keras

from snntoolbox.parsing.utils import get_type
from snntoolbox.simulation.target_simulators.pyNN_target_sim import \
    SNN as PYSNN
from snntoolbox.simulation.target_simulators.pyNN_target_sim import \
    get_shape_from_label
from snntoolbox.simulation.utils import build_convolution, \
    build_depthwise_convolution, build_1d_convolution, build_pooling
from snntoolbox.utils.utils import confirm_overwrite


class SNN(PYSNN):

    def scale_weights(self, weights):

        from math import exp
        # This ignores the leak term
        tau_syn_E = self.config.getfloat('cell', 'tau_syn_E')
        tau_syn_I = self.config.getfloat('cell', 'tau_syn_I')
        # just to give a sensible answer if tau_syn_E and I are different
        t = self._dt
        tau = (tau_syn_E + tau_syn_I) / 2
        scale = 10 * t / (tau * (exp(-(t / tau)) + 1))
        print('Weights scaled by a factor of {0}'.format(scale,))
        if isinstance(weights, list):
            weights = [(i, j, weight * scale, delay)
                       for (i, j, weight, delay) in weights]
        elif isinstance(weights, np.ndarray):
            weights = weights * scale
        else:
            raise Exception("Not a valid weight type")
        return weights

    def setup_layers(self, batch_shape):
        '''Iterates over all layers to instantiate them in the simulator.'''

        self.add_input_layer(batch_shape)

        for layer in self.parsed_model.layers[1:]:
            print("Instantiating layer: {}".format(layer.name))
            self.add_layer(layer)

            layer_type = get_type(layer)
            print("Building layer: {}".format(layer.name))
            if layer_type == 'Flatten':
                self.flatten_shapes.append(
                    (layer.name, get_shape_from_label(self.layers[-1].label)))
                self.build_flatten(layer)
                continue
            if layer_type in {'Dense', 'Sparse'}:
                self.build_dense(layer)
            elif layer_type in {'Conv1D', 'Conv2D', 'DepthwiseConv2D',
                                'SparseConv2D', 'SparseDepthwiseConv2D'}:
                self.build_convolution(layer)
                self.data_format = layer.data_format
            elif layer_type in {'MaxPooling2D', 'AveragePooling2D'}:
                self.build_pooling(layer)

            elif layer_type == 'ZeroPadding':
                padding = layer.padding
                if set(padding).issubset((1, (1, 1))):
                    self.change_padding = True
                    return
                else:
                    raise NotImplementedError(
                        "Border_mode {} not supported.".format(padding))

    def add_layer(self, layer):

        # This implementation of ZeroPadding layers assumes symmetric single
        # padding ((1, 1), (1, 1)).
        # Todo: Generalize for asymmetric padding or arbitrary size.
        if 'ZeroPadding' in layer.__class__.__name__:
            return
        if 'Flatten' in layer.__class__.__name__:
            return
        if 'Reshape' in layer.__class__.__name__:
            return
        self.layers.append(self.sim.Population(
            np.prod(layer.output_shape[1:], dtype=np.int).item(),
            self.sim.IF_curr_exp, self.cellparams, label=layer.name))

        self.layers[-1].initialize(v=self.layers[-1].get('v_rest'))

    def build_dense(self, layer):
        """

        Parameters
        ----------
        layer : keras.layers.Dense

        Returns
        -------

        """
{'fraction_non_alphanumeric': 0.10632911392405063, 'fraction_numerical': 0.006962025316455696, 'mean_word_length': 2.157873701039169, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7486820', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2258, 'n_words': 546}