# 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 time
import numpy as np
from tensorflow import keras
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Flatten, Dense, \
    Dropout
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from snntoolbox.bin.run import main
from snntoolbox.utils.utils import import_configparser
layer = AveragePooling2D()(layer)
layer = Conv2D(filters=8,
               kernel_size=(3, 3),
               padding='same',
               activation='relu')(layer)
layer = Flatten()(layer)
layer = Dropout(0.01)(layer)
layer = Dense(units=10,
              activation='softmax')(layer)

model = Model(input_layer, layer)

model.summary()

model.compile('adam', 'categorical_crossentropy', ['accuracy'])

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

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

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

dt = 0.1  # Time resolution of simulator.

# 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_nest.py
import os
import time
import numpy as np
from tensorflow import keras
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Flatten, Dense, \
    Dropout
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from snntoolbox.bin.run import main
from snntoolbox.utils.utils import import_configparser

layer = AveragePooling2D()(layer)
layer = Conv2D(filters=8,
               kernel_size=(3, 3),
               padding='same',
               activation='relu')(layer)
layer = Flatten()(layer)
layer = Dropout(0.01)(layer)
layer = Dense(units=10,
              activation='softmax')(layer)

model = Model(input_layer, layer)

model.summary()

model.compile('adam', 'categorical_crossentropy', ['accuracy'])

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

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

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

dt = 0.1  # Time resolution of simulator.

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

configparser = import_configparser()