# Path: snntoolbox/bin/run.py
# def main(filepath=None):
#     """Entry point for running the toolbox.
# 
#     Note
#     ----
# 
#     There is no need to call this function directly, because python sets up an
#     executable during :ref:`installation` that can be called from terminal.
# 
#     """
#     from snntoolbox.bin.utils import update_setup, run_pipeline
# 
#     if filepath is not None:
#         config = update_setup(filepath)
#         run_pipeline(config)
#         return
# 
#     parser = argparse.ArgumentParser(
#         description='Run SNN toolbox to convert an analog neural network into '
#                     'a spiking neural network, and optionally simulate it.')
#     parser.add_argument('config_filepath', nargs='?',
#                         help='Path to configuration file.')
#     parser.add_argument('-t', '--terminal', action='store_true',
#                         help='Set this flag to run the toolbox from terminal. '
#                              'Omit this flag to open GUI.')
#     args = parser.parse_args()
# 
#     _filepath = os.path.abspath(args.config_filepath)
#     if _filepath is not None:
#         config = update_setup(_filepath)
# 
#         if args.terminal:
#             run_pipeline(config)
#         else:
#             from snntoolbox.bin.gui import gui
#             gui.main()
#     else:
#         if args.terminal:
#             parser.error("When using the SNN toolbox from terminal, a "
#                          "config_filepath argument must be provided.")
#             return
#         else:
#             from snntoolbox.bin.gui import gui
#             gui.main()
# 
# Path: snntoolbox/utils/utils.py
# def import_configparser():
#     try:
#         import configparser
#     except ImportError:
#         # noinspection PyPep8Naming
#         import ConfigParser as configparser
# 
#     return configparser


import os
import numpy as np
from tensorflow import keras
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import AveragePooling2D, Flatten, Dropout
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from keras_rewiring import Sparse, SparseConv2D, SparseDepthwiseConv2D
from keras_rewiring.optimizers import NoisySGD
from keras_rewiring.rewiring_callback import RewiringCallback
from snntoolbox.bin.run import main
from snntoolbox.utils.utils import import_configparser
layer = Flatten()(layer)
layer = Dropout(0.01)(layer)
layer = Sparse(units=10,
               activation='softmax',
               use_bias=False,
               connectivity_level=built_in_sparsity.pop(0) or None)(layer)

model = Model(input_layer, layer)

model.summary()

model.compile(
    NoisySGD(
        lr=0.01),
    'categorical_crossentropy',
    ['accuracy'])

# Train model with backprop.
model.fit(x_train, y_train, batch_size=64, epochs=5, verbose=2,
          validation_data=(x_test, y_test),
          callbacks=callback_list)

# Store model so SNN Toolbox can find it.
model_name = 'sparse_mnist_cnn'
keras.models.save_model(model, os.path.join(path_wd, model_name + '.h5'))

# SNN TOOLBOX CONFIGURATION #
#############################

# Create a config file with experimental setup for SNN Toolbox.

# Path: snntoolbox/bin/run.py
# def main(filepath=None):
#     """Entry point for running the toolbox.
# 
#     Note
#     ----
# 
#     There is no need to call this function directly, because python sets up an
#     executable during :ref:`installation` that can be called from terminal.
# 
#     """
#     from snntoolbox.bin.utils import update_setup, run_pipeline
# 
#     if filepath is not None:
#         config = update_setup(filepath)
#         run_pipeline(config)
#         return
# 
#     parser = argparse.ArgumentParser(
#         description='Run SNN toolbox to convert an analog neural network into '
#                     'a spiking neural network, and optionally simulate it.')
#     parser.add_argument('config_filepath', nargs='?',
#                         help='Path to configuration file.')
#     parser.add_argument('-t', '--terminal', action='store_true',
#                         help='Set this flag to run the toolbox from terminal. '
#                              'Omit this flag to open GUI.')
#     args = parser.parse_args()
# 
#     _filepath = os.path.abspath(args.config_filepath)
#     if _filepath is not None:
#         config = update_setup(_filepath)
# 
#         if args.terminal:
#             run_pipeline(config)
#         else:
#             from snntoolbox.bin.gui import gui
#             gui.main()
#     else:
#         if args.terminal:
#             parser.error("When using the SNN toolbox from terminal, a "
#                          "config_filepath argument must be provided.")
#             return
#         else:
#             from snntoolbox.bin.gui import gui
#             gui.main()
# 
# Path: snntoolbox/utils/utils.py
# def import_configparser():
#     try:
#         import configparser
#     except ImportError:
#         # noinspection PyPep8Naming
#         import ConfigParser as configparser
# 
#     return configparser


# Path: examples/mnist_keras_spiNNaker_sparse.py
import os
import numpy as np
from tensorflow import keras
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import AveragePooling2D, Flatten, Dropout
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from keras_rewiring import Sparse, SparseConv2D, SparseDepthwiseConv2D
from keras_rewiring.optimizers import NoisySGD
from keras_rewiring.rewiring_callback import RewiringCallback
from snntoolbox.bin.run import main
from snntoolbox.utils.utils import import_configparser

layer = Flatten()(layer)
layer = Dropout(0.01)(layer)
layer = Sparse(units=10,
               activation='softmax',
               use_bias=False,
               connectivity_level=built_in_sparsity.pop(0) or None)(layer)

model = Model(input_layer, layer)

model.summary()

model.compile(
    NoisySGD(
        lr=0.01),
    'categorical_crossentropy',
    ['accuracy'])

# Train model with backprop.
model.fit(x_train, y_train, batch_size=64, epochs=5, verbose=2,
          validation_data=(x_test, y_test),
          callbacks=callback_list)

# Store model so SNN Toolbox can find it.
model_name = 'sparse_mnist_cnn'
keras.models.save_model(model, os.path.join(path_wd, model_name + '.h5'))

# SNN TOOLBOX CONFIGURATION #
#############################

# Create a config file with experimental setup for SNN Toolbox.

configparser = import_configparser()