import os
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

dir_path = os.path.dirname(os.path.realpath(__file__))

# Read in red wine data
red = pd.read_csv(dir_path + "/winequality-red.csv", sep=';')

# Read in white wine data
white = pd.read_csv(dir_path + "/winequality-white.csv", sep=';')

# import pdb; pdb.set_trace()

# Add `type` column to `red` with value 1
red['type'] = 1

# Add `type` column to `white` with value 0
white['type'] = 0

# Append `white` to `red`
wines = red.append(white, ignore_index=True)

# Specify the data
X = wines.ix[:, 0:11]

# Specify the target labels and flatten the array
y = np.ravel(wines.type)

# Initialize the constructor
model = Sequential()
model.add(Dense(12, activation='relu', input_shape=(11,)))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Model output shape
model.output_shape

# Model summary
model.summary()

# Model config
model.get_config()

# List all weight tensors
model.get_weights()

model.compile(loss='binary_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

whole = True
if whole:
    # Define the scaler
    scaler = StandardScaler().fit(X)

    # Scale the train set
    X = scaler.transform(X)

    model.fit(X, y, epochs=20, batch_size=1, verbose=1)
else:
    # Split the data up in train and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,
                                                        random_state=42)

    # Define the scaler
    scaler = StandardScaler().fit(X_train)

    # Scale the train set
    X_train = scaler.transform(X_train)

    # Scale the test set
    X_test = scaler.transform(X_test)

    model.fit(X_train, y_train, epochs=20, batch_size=1, verbose=1)

    # y_pred = model.predict(X_test)
    #
    # score = model.evaluate(X_test, y_test, verbose=1)
    #
    # print(score)
