# Path: scripts/progress_bar.py
# def printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
#     """
#     Call in a loop to create terminal progress bar
#     @params:
#         iteration   - Required  : current iteration (Int)
#         total       - Required  : total iterations (Int)
#         prefix      - Optional  : prefix string (Str)
#         suffix      - Optional  : suffix string (Str)
#         decimals    - Optional  : positive number of decimals in percent complete (Int)
#         barLength   - Optional  : character length of bar (Int)
#     """
#     formatStr = "{0:." + str(decimals) + "f}"
#     percent = formatStr.format(100 * (iteration / float(total)))
#     filledLength = int(round(barLength * iteration / float(total)))
#     bar = '#' * filledLength + '-' * (barLength - filledLength)
#     sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
#     if iteration == total:
#         sys.stdout.write('\n')
#     sys.stdout.flush()


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from keras.optimizers import SGD
from models.DataLoader import DataLoader
from keras.models import load_model
from scripts.progress_bar import printProgress
from time import time, localtime
        X_batch, Y_batch = loader.next_train_batch()
        loss_list.append(model.train_on_batch(X_batch, Y_batch))

        # calculate some time information
        time_list.append(time() - time_start_batch)
        eta = (loader.n_iter_train - i) * np.array(time_list).mean()
        printProgress(i, loader.n_iter_train-1, prefix='Progress:', suffix='batch error: %0.5f, ETA: %0.2f sec.'%(np.array(loss_list).mean(), eta), barLength=50)
    printProgress(i, loader.n_iter_train - 1, prefix='Progress:', suffix='batch error: %0.5f' % (np.array(loss_list).mean()), barLength=50)
    train_loss.append(np.asarray(loss_list).mean())
    print('training loss is %f, one epoch uses: %0.2f sec' % (train_loss[-1], time() - time_start))
    valid_loss.append(model.evaluate(x_valid, y_valid))
    print('valid loss is %f' % valid_loss[-1])

    if best_valid > valid_loss[-1]:
        early_stopping_count = 0
        print('saving best valid result...')
        best_valid = valid_loss[-1]
        model.save('./trained_models/multicnn_maximum_best_valid_'+model.name+'.h5')
    else:
        # we wait for early stopping loop until a certain time
        early_stopping_count += 1
        if early_stopping_count > early_stopping_wait:
            early_stopping_count = 0
            if len(learning_rate) > 1:
                learning_rate.pop()
                print('decreasing the learning rate to: %f'%learning_rate[-1])
                model.optimizer.lr.set_value(learning_rate[-1])
            else:
                break


# Path: scripts/progress_bar.py
# def printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
#     """
#     Call in a loop to create terminal progress bar
#     @params:
#         iteration   - Required  : current iteration (Int)
#         total       - Required  : total iterations (Int)
#         prefix      - Optional  : prefix string (Str)
#         suffix      - Optional  : suffix string (Str)
#         decimals    - Optional  : positive number of decimals in percent complete (Int)
#         barLength   - Optional  : character length of bar (Int)
#     """
#     formatStr = "{0:." + str(decimals) + "f}"
#     percent = formatStr.format(100 * (iteration / float(total)))
#     filledLength = int(round(barLength * iteration / float(total)))
#     bar = '#' * filledLength + '-' * (barLength - filledLength)
#     sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
#     if iteration == total:
#         sys.stdout.write('\n')
#     sys.stdout.flush()


# Path: CNN_training_maximum.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from keras.optimizers import SGD
from models.DataLoader import DataLoader
from keras.models import load_model
from scripts.progress_bar import printProgress
from time import time, localtime

        X_batch, Y_batch = loader.next_train_batch()
        loss_list.append(model.train_on_batch(X_batch, Y_batch))

        # calculate some time information
        time_list.append(time() - time_start_batch)
        eta = (loader.n_iter_train - i) * np.array(time_list).mean()
        printProgress(i, loader.n_iter_train-1, prefix='Progress:', suffix='batch error: %0.5f, ETA: %0.2f sec.'%(np.array(loss_list).mean(), eta), barLength=50)
    printProgress(i, loader.n_iter_train - 1, prefix='Progress:', suffix='batch error: %0.5f' % (np.array(loss_list).mean()), barLength=50)
    train_loss.append(np.asarray(loss_list).mean())
    print('training loss is %f, one epoch uses: %0.2f sec' % (train_loss[-1], time() - time_start))
    valid_loss.append(model.evaluate(x_valid, y_valid))
    print('valid loss is %f' % valid_loss[-1])

    if best_valid > valid_loss[-1]:
        early_stopping_count = 0
        print('saving best valid result...')
        best_valid = valid_loss[-1]
        model.save('./trained_models/multicnn_maximum_best_valid_'+model.name+'.h5')
    else:
        # we wait for early stopping loop until a certain time
        early_stopping_count += 1
        if early_stopping_count > early_stopping_wait:
            early_stopping_count = 0
            if len(learning_rate) > 1:
                learning_rate.pop()
                print('decreasing the learning rate to: %f'%learning_rate[-1])
                model.optimizer.lr.set_value(learning_rate[-1])
            else:
                break


lt = localtime()