# 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 sys
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, BatchNormalization, Activation
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
    'simulator': 'loihi',           # Chooses execution backend of SNN toolbox.
    'duration': 100,                # Number of time steps to run each sample.
    'num_to_test': 100,             # How many test samples to run.
    'batch_size': 20
}

config['loihi'] = {
    # Reset mode 'soft'/'hard' for reset-by-subtraction (more accurate)
    # or reset-to-zero (less resources).
    'reset_mode': reset_mode,
    # Scaling factor between max input and threshold.
    'desired_threshold_to_input_ratio': 2 ** 3 if reset_mode == 'hard' else 1,
    # Arguments passed to NxTF layer.
    'compartment_kwargs': {'biasExp': 6, 'vThMant': 512},
    'connection_kwargs': {'numWeightBits': 8, 'weightExponent': 0,
                          'numBiasBits': 12},
    'validate_partitions': False,   # Validates correctness of compiled layers.
    'save_output': False,           # Plots compiler information.
    'do_overflow_estimate': True,   # Estimate overflow of dendritic accumul.
    'normalize_thresholds': True,   # Tune thresholds to optimal dynamic range.
}

# Store config file.
config_filepath = os.path.join(path_wd, 'config')
with open(config_filepath, 'w') as configfile:
    config.write(configfile)

# RUN 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_loihi.py
import os
import sys
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, BatchNormalization, Activation
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

    'simulator': 'loihi',           # Chooses execution backend of SNN toolbox.
    'duration': 100,                # Number of time steps to run each sample.
    'num_to_test': 100,             # How many test samples to run.
    'batch_size': 20
}

config['loihi'] = {
    # Reset mode 'soft'/'hard' for reset-by-subtraction (more accurate)
    # or reset-to-zero (less resources).
    'reset_mode': reset_mode,
    # Scaling factor between max input and threshold.
    'desired_threshold_to_input_ratio': 2 ** 3 if reset_mode == 'hard' else 1,
    # Arguments passed to NxTF layer.
    'compartment_kwargs': {'biasExp': 6, 'vThMant': 512},
    'connection_kwargs': {'numWeightBits': 8, 'weightExponent': 0,
                          'numBiasBits': 12},
    'validate_partitions': False,   # Validates correctness of compiled layers.
    'save_output': False,           # Plots compiler information.
    'do_overflow_estimate': True,   # Estimate overflow of dendritic accumul.
    'normalize_thresholds': True,   # Tune thresholds to optimal dynamic range.
}

# Store config file.
config_filepath = os.path.join(path_wd, 'config')
with open(config_filepath, 'w') as configfile:
    config.write(configfile)

# RUN SNN TOOLBOX #
###################


main(config_filepath)