<commit_before># -*- coding: utf-8 -*-

"""
This script starts or resumes the training of an autoencoder or an encoder that is 
defined in model_definitions.
It also contatins the adress of the training files and the epoch
"""


from keras.models import load_model
from keras import optimizers
import numpy as np
import h5py
import os
import sys
import argparse

from util.run_cnn import *
from model_definitions import *


# start.py "vgg_1_xzt" 1 0 0 0 2 "up_down" True 0 11 18 50 1 
def parse_input():
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('modeltag', type=str, help='e.g vgg_3-sgd; -XXX indicates version number and is ommited when looking up model by modeltag')
    parser.add_argument('runs', type=int)
    parser.add_argument("autoencoder_stage", type=int)
    parser.add_argument("autoencoder_epoch", type=int)
    parser.add_argument("encoder_epoch", type=int)
    parser.add_argument("class_type_bins", type=int)
    parser.add_argument("class_type_name", type=str)
    parser.add_argument("zero_center", type=bool)
    parser.add_argument("verbose", type=int)    
    parser.add_argument("n_bins", nargs=4, type=int)
    parser.add_argument("learning_rate", type=float)
    parser.add_argument("learning_rate_decay", default=0.05, type=float)
    parser.add_argument("epsilon", default=-1, type=int, help="Exponent of the epsilon used for adam.") #exponent of epsilon, adam default: 1e-08
    parser.add_argument("lambda_comp", default=False, type=bool)
    parser.add_argument("optimizer", type=str)
    parser.add_argument("encoder_version", default="", nargs="?", type=str, help="e.g. -LeLu; Str added to the supervised file names to allow multiple runs on the same model.")
    
    args = parser.parse_args()
    params = vars(args)

    return params

params = parse_input()
modeltag = params["modeltag"]
runs=params["runs"]
autoencoder_stage=params["autoencoder_stage"]
autoencoder_epoch=params["autoencoder_epoch"]
encoder_epoch=params["encoder_epoch"]
class_type = (params["class_type_bins"], params["class_type_name"])
zero_center = params["zero_center"]
verbose=params["verbose"]
n_bins = params["n_bins"]
learning_rate = params["learning_rate"]
learning_rate_decay = params["learning_rate_decay"]
epsilon = params["epsilon"]
lambda_comp = params["lambda_comp"]
use_opti = params["optimizer"]
encoder_version=params["encoder_version"]

"""
#Tag for the model used; Identifies both autoencoder and encoder
modeltag="vgg_1_xzt"

#How many additinal epochs the network will be trained for by executing this script:
runs=1

#Type of training/network
# 0: autoencoder
# 1: encoder+ from autoencoder w/ frozen layers
# 2: encoder+ from scratch, completely unfrozen
autoencoder_stage=2

#Define starting epoch of autoencoder model
autoencoder_epoch=0

#If in encoder stage (1 or 2), encoder_epoch is used to identify a possibly
#existing supervised-trained encoder network
encoder_epoch=0
#Define what the supervised encoder network is trained for, and how many neurons are in the output
#This also defines the name of the saved model
class_type = (2, 'up_down')

#Wheter to use a precalculated zero-center image or not
zero_center = True

#Verbose bar during training?
#0: silent, 1:verbose, 2: one log line per epoch
verbose=0

# x  y  z  t
# 11 13 18 50
n_bins = (11,18,50,1)

#Learning rate, usually 0.001
learning_rate = 0.001
"""

#Naming scheme for models
"""
Autoencoder
"trained_" + modeltag + "_supervised_" + class_type[1] + "_epoch" + encoder_epoch + ".h5" 

Encoder+
"trained_" + modeltag + "_autoencoder_" + class_type[1] + "_epoch" + encoder_epoch + ".h5"

Encoder+ new
"trained_" + modeltag + "_autoencoder_" + autoencoder_epoch + "_supervised_" + class_type[1] + "_epoch" + encoder_epoch + ".h5" 
"""


def execute_training(modeltag, runs, autoencoder_stage, epoch, encoder_epoch, class_type, zero_center, verbose, n_bins, learning_rate, learning_rate_decay=0.05, epsilon=0.1, lambda_comp=False, use_opti=use_opti, encoder_version=encoder_version):
    
    #Path to training and testing datafiles on HPC for xyz
    """
    data_path = "/home/woody/capn/mppi033h/Data/ORCA_JTE_NEMOWATER/h5_input_projections_3-100GeV/4dTo3d/h5/xyz/concatenated/"
    train_data = "train_muon-CC_and_elec-CC_each_480_xyz_shuffled.h5"
    test_data = "test_muon-CC_and_elec-CC_each_120_xyz_shuffled.h5"
    zero_center_data = "train_muon-CC_and_elec-CC_each_480_xyz_shuffled.h5_zero_center_mean.npy"
    """
    raise("aaa:", encoder_version)
    #for xzt
    data_path = "/home/woody/capn/mppi033h/Data/ORCA_JTE_NEMOWATER/h5_input_projections_3-100GeV/4dTo3d/h5/xzt/concatenated/"
    train_data = "train_muon-CC_and_elec-CC_each_240_xzt_shuffled.h5"
    test_data = "test_muon-CC_and_elec-CC_each_60_xzt_shuffled.h5"
    zero_center_data = "train_muon-CC_and_elec-CC_each_240_xzt_shuffled.h5_zero_center_mean.npy"
    
    
    train_file=data_path+train_data
    test_file=data_path+test_data
    zero_center_file=data_path+zero_center_data
    
    
    #Path to my Km3_net-Autoencoder folder on HPC:
    home_path="/home/woody/capn/mppi013h/Km3-Autoencoder/"
    
    #For debug testing on my laptop these are overwritten:
    """
    home_path="../"
    train_file="Daten/JTE_KM3Sim_gseagen_muon-CC_3-100GeV-9_1E7-1bin-3_0gspec_ORCA115_9m_2016_588_xyz.h5"
    test_file="Daten/JTE_KM3Sim_gseagen_muon-CC_3-100GeV-9_1E7-1bin-3_0gspec_ORCA115_9m_2016_588_xyz.h5"
    #file=h5py.File(train_file, 'r')
    #xyz_hists = np.array(file["x"]).reshape((3498,11,13,18,1))
    """
    
    #All models are now saved in their own folder   models/"modeltag"/
    model_folder = home_path + "models/" + modeltag + "/"
    if not os.path.exists(model_folder):
        os.makedirs(model_folder)
    
    #Optimizer used in all the networks:
    lr = learning_rate # 0.01 default for SGD, 0.001 for Adam
    lr_decay = learning_rate_decay # % decay for each epoch, e.g. if 0.05 -> lr_new = lr*(1-0.05)=0.95*lr
    
    
    #automatically look for latest epoch if -1 was given:
    if autoencoder_stage==0 and epoch==-1:
        epoch=0
        while True:
            if os.path.isfile(model_folder + "trained_" + modeltag + "_autoencoder_epoch" + str(epoch+1) + '.h5')==True:
                epoch+=1
            else:
                break
    elif autoencoder_stage==1 and encoder_epoch == -1:
        encoder_epoch=0
        while True:
            if os.path.isfile(model_folder + "trained_" + modeltag + "_autoencoder_epoch" + str(epoch) +  "_supervised_" + class_type[1] + '_epoch' + str(encoder_epoch+1) + '.h5')==True:
                encoder_epoch+=1
            else:
                break
    elif autoencoder_stage==2 and encoder_epoch == -1:
        encoder_epoch=0
        while True:
            if os.path.isfile(model_folder + "trained_" + modeltag + "_supervised_" + class_type[1] + '_epoch' + str(encoder_epoch+1) + '.h5')==True:
                encoder_epoch+=1
            else:
                break
    
    #if lr is negative, take its absolute as the starting lr and apply the decays that happend during the
    #previous epochs; The lr gets decayed once when train_and_test_model is called (so epoch-1 here)
    if lr<0:
        if autoencoder_stage==0  and epoch>0:
            lr=abs(lr*(1-float(lr_decay))**(epoch-1))
            
        elif (autoencoder_stage==1 or autoencoder_stage==2)  and encoder_epoch>0:
            lr=abs(lr*(1-float(lr_decay))**(encoder_epoch-1))
        else:
            lr=abs(lr)
    
    #Optimizer to be used. If an epsilon is specified, adam is used with epsilon=10**(given epsilon). If epsilon is given as 0, SGD is used.<commit_msg>Bug fix<commit_after># -*- coding: utf-8 -*-

"""
This script starts or resumes the training of an autoencoder or an encoder that is 
defined in model_definitions.
It also contatins the adress of the training files and the epoch
"""


from keras.models import load_model
from keras import optimizers
import numpy as np
import h5py
import os
import sys
import argparse

from util.run_cnn import *
from model_definitions import *


# start.py "vgg_1_xzt" 1 0 0 0 2 "up_down" True 0 11 18 50 1 
def parse_input():
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('modeltag', type=str, help='e.g vgg_3-sgd; -XXX indicates version number and is ommited when looking up model by modeltag')
    parser.add_argument('runs', type=int)
    parser.add_argument("autoencoder_stage", type=int)
    parser.add_argument("autoencoder_epoch", type=int)
    parser.add_argument("encoder_epoch", type=int)
    parser.add_argument("class_type_bins", type=int)
    parser.add_argument("class_type_name", type=str)
    parser.add_argument("zero_center", type=bool)
    parser.add_argument("verbose", type=int)    
    parser.add_argument("n_bins", nargs=4, type=int)
    parser.add_argument("learning_rate", type=float)
    parser.add_argument("learning_rate_decay", default=0.05, type=float)
    parser.add_argument("epsilon", default=-1, type=int, help="Exponent of the epsilon used for adam.") #exponent of epsilon, adam default: 1e-08
    parser.add_argument("lambda_comp", default=False, type=bool)
    parser.add_argument("optimizer", type=str)
    parser.add_argument("encoder_version", default="", nargs="?", type=str, help="e.g. -LeLu; Str added to the supervised file names to allow multiple runs on the same model.")
    
    args = parser.parse_args()
    params = vars(args)

    return params

params = parse_input()
modeltag = params["modeltag"]
runs=params["runs"]
autoencoder_stage=params["autoencoder_stage"]
autoencoder_epoch=params["autoencoder_epoch"]
encoder_epoch=params["encoder_epoch"]
class_type = (params["class_type_bins"], params["class_type_name"])
zero_center = params["zero_center"]
verbose=params["verbose"]
n_bins = params["n_bins"]
learning_rate = params["learning_rate"]
learning_rate_decay = params["learning_rate_decay"]
epsilon = params["epsilon"]
lambda_comp = params["lambda_comp"]
use_opti = params["optimizer"]
encoder_version=params["encoder_version"]

"""
#Tag for the model used; Identifies both autoencoder and encoder
modeltag="vgg_1_xzt"

#How many additinal epochs the network will be trained for by executing this script:
runs=1

#Type of training/network
# 0: autoencoder
# 1: encoder+ from autoencoder w/ frozen layers
# 2: encoder+ from scratch, completely unfrozen
autoencoder_stage=2

#Define starting epoch of autoencoder model
autoencoder_epoch=0

#If in encoder stage (1 or 2), encoder_epoch is used to identify a possibly
#existing supervised-trained encoder network
encoder_epoch=0
#Define what the supervised encoder network is trained for, and how many neurons are in the output
#This also defines the name of the saved model
class_type = (2, 'up_down')

#Wheter to use a precalculated zero-center image or not
zero_center = True

#Verbose bar during training?
#0: silent, 1:verbose, 2: one log line per epoch
verbose=0

# x  y  z  t
# 11 13 18 50
n_bins = (11,18,50,1)

#Learning rate, usually 0.001
learning_rate = 0.001
"""

#Naming scheme for models
"""
Autoencoder
"trained_" + modeltag + "_supervised_" + class_type[1] + "_epoch" + encoder_epoch + ".h5" 

Encoder+
"trained_" + modeltag + "_autoencoder_" + class_type[1] + "_epoch" + encoder_epoch + ".h5"

Encoder+ new
"trained_" + modeltag + "_autoencoder_" + autoencoder_epoch + "_supervised_" + class_type[1] + "_epoch" + encoder_epoch + ".h5" 
"""


def execute_training(modeltag, runs, autoencoder_stage, epoch, encoder_epoch, class_type, zero_center, verbose, n_bins, learning_rate, learning_rate_decay=0.05, epsilon=0.1, lambda_comp=False, use_opti=use_opti, encoder_version=encoder_version):
    
    #Path to training and testing datafiles on HPC for xyz
    """
    data_path = "/home/woody/capn/mppi033h/Data/ORCA_JTE_NEMOWATER/h5_input_projections_3-100GeV/4dTo3d/h5/xyz/concatenated/"
    train_data = "train_muon-CC_and_elec-CC_each_480_xyz_shuffled.h5"
    test_data = "test_muon-CC_and_elec-CC_each_120_xyz_shuffled.h5"
    zero_center_data = "train_muon-CC_and_elec-CC_each_480_xyz_shuffled.h5_zero_center_mean.npy"
    """
    #for xzt
    data_path = "/home/woody/capn/mppi033h/Data/ORCA_JTE_NEMOWATER/h5_input_projections_3-100GeV/4dTo3d/h5/xzt/concatenated/"
    train_data = "train_muon-CC_and_elec-CC_each_240_xzt_shuffled.h5"
    test_data = "test_muon-CC_and_elec-CC_each_60_xzt_shuffled.h5"
    zero_center_data = "train_muon-CC_and_elec-CC_each_240_xzt_shuffled.h5_zero_center_mean.npy"
    
    
    train_file=data_path+train_data
    test_file=data_path+test_data
    zero_center_file=data_path+zero_center_data
    
    
    #Path to my Km3_net-Autoencoder folder on HPC:
    home_path="/home/woody/capn/mppi013h/Km3-Autoencoder/"
    
    #For debug testing on my laptop these are overwritten:
    """
    home_path="../"
    train_file="Daten/JTE_KM3Sim_gseagen_muon-CC_3-100GeV-9_1E7-1bin-3_0gspec_ORCA115_9m_2016_588_xyz.h5"
    test_file="Daten/JTE_KM3Sim_gseagen_muon-CC_3-100GeV-9_1E7-1bin-3_0gspec_ORCA115_9m_2016_588_xyz.h5"
    #file=h5py.File(train_file, 'r')
    #xyz_hists = np.array(file["x"]).reshape((3498,11,13,18,1))
    """
    
    #All models are now saved in their own folder   models/"modeltag"/
    model_folder = home_path + "models/" + modeltag + "/"
    if not os.path.exists(model_folder):
        os.makedirs(model_folder)
    
    #Optimizer used in all the networks:
    lr = learning_rate # 0.01 default for SGD, 0.001 for Adam
    lr_decay = learning_rate_decay # % decay for each epoch, e.g. if 0.05 -> lr_new = lr*(1-0.05)=0.95*lr
    
    
    #automatically look for latest epoch if -1 was given:
    if autoencoder_stage==0 and epoch==-1:
        epoch=0
        while True:
            if os.path.isfile(model_folder + "trained_" + modeltag + "_autoencoder_epoch" + str(epoch+1) + '.h5')==True:
                epoch+=1
            else:
                break
    elif autoencoder_stage==1 and encoder_epoch == -1:
        encoder_epoch=0
        while True:
            if os.path.isfile(model_folder + "trained_" + modeltag + "_autoencoder_epoch" + str(epoch) +  "_supervised_" + class_type[1] + encoder_version + '_epoch' + str(encoder_epoch+1) + '.h5')==True:
                encoder_epoch+=1
            else:
                break
    elif autoencoder_stage==2 and encoder_epoch == -1:
        encoder_epoch=0
        while True:
            if os.path.isfile(model_folder + "trained_" + modeltag + "_supervised_" + class_type[1] + encoder_version + '_epoch' + str(encoder_epoch+1) + '.h5')==True:
                encoder_epoch+=1
            else:
                break
    
    #if lr is negative, take its absolute as the starting lr and apply the decays that happend during the
    #previous epochs; The lr gets decayed once when train_and_test_model is called (so epoch-1 here)
    if lr<0:
        if autoencoder_stage==0  and epoch>0:
            lr=abs(lr*(1-float(lr_decay))**(epoch-1))
            
        elif (autoencoder_stage==1 or autoencoder_stage==2)  and encoder_epoch>0:
            lr=abs(lr*(1-float(lr_decay))**(encoder_epoch-1))
        else:
            lr=abs(lr)
    
    #Optimizer to be used. If an epsilon is specified, adam is used with epsilon=10**(given epsilon). If epsilon is given as 0, SGD is used.
{'fraction_non_alphanumeric': 0.10963910461397898, 'fraction_numerical': 0.03054232199960843, 'mean_word_length': 4.0209698558322415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3485649', 'n_tokens_mistral': 5642, 'n_tokens_neox': 5298, 'n_words': 1250}