import pandas as pd
from tensorflow import keras 
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as rmse
from sacred import Experiment
from datetime import datetime
from sacred.observers import MongoObserver
ex = Experiment("file_observer", interactive=False, save_git_info=False)
ex.observers.append(MongoObserver(url='mongodb://mongo_user:mongo_password_IUM_2021@172.17.0.1:27017', db_name='sacred'))


@ex.config
def my_config():
    test_size = 0.2
    epochs = 100
    batch_size = 32


@ex.capture
def create_model(test_size, epochs, batch_size, _run):
    _run.info["prepare_model_ts"] = str(datetime.now())
    df = pd.read_csv('country_vaccinations.csv').dropna()
    dataset = df.iloc[:, 3:-3]
    dataset = df.groupby(by=["country"], dropna=True).sum()
    X = dataset.loc[:,dataset.columns != "daily_vaccinations"]
    y = dataset.loc[:,dataset.columns == "daily_vaccinations"]

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = test_size, random_state = 6)

    model = keras.Sequential([
        keras.layers.Dense(512,input_dim = X_train.shape[1],kernel_initializer='normal', activation='relu'),
        keras.layers.Dense(512,kernel_initializer='normal', activation='relu'),
        keras.layers.Dense(256,kernel_initializer='normal', activation='relu'),
        keras.layers.Dense(256,kernel_initializer='normal', activation='relu'),
        keras.layers.Dense(128,kernel_initializer='normal', activation='relu'),
        keras.layers.Dense(1,kernel_initializer='normal', activation='linear'),
    ])

    model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])

    model.fit(X_train, y_train, epochs = epochs, validation_split = 0.3, batch_size = batch_size)

    prediction = model.predict(X_test)
    rmse_result = rmse(y_test, prediction, squared = False)
    print(prediction)
    _run.info["Results: "] = rmse_result
    model.save('vaccines_model')
    return rmse_result


@ex.automain
def my_main(test_size, epochs, batch_size):
    print(create_model())

r = ex.run()
ex.add_artifact("vaccines_model/saved_model.pb")