import dataset as ds
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf


# Download database
ds.download('UCI HAR')

"""
Explanation: TempScrip
Script to developing and test partial functionality.
End of explanation
"""


# Paths and filenames
DATASET_PATH = "../dataset/UCI HAR/UCI HAR Dataset"
TEST_RELPATH = "/test"
TRAIN_RELPATH = "/train"

VARS_FILENAMES = [
    'body_acc_x_',
    'body_acc_y_',
    'body_acc_z_',
    'body_gyro_x_',
    'body_gyro_y_',
    'body_gyro_z_',
    'total_acc_x_',
    'total_acc_y_',
    'total_acc_z_']

LABELS_DEF_FILE = DATASET_PATH + "/activity_labels.txt"

# Make a list of files for training
trainFiles = [DATASET_PATH + TRAIN_RELPATH + '/Inertial Signals/' + var_filename + 'train.txt' for var_filename in VARS_FILENAMES]
# Make an tensor with data for training
dataTrain = ds.get_data(trainFiles, print_on = True)
# Show dataTrain dimensions
print dataTrain.shape

# Make a list of files for testing
testFiles = [DATASET_PATH + TEST_RELPATH + '/Inertial Signals/' + var_filename + 'test.txt' for var_filename in VARS_FILENAMES]
# Make an tensor with data for training
dataTest = ds.get_data(testFiles, print_on = True)
# Show dataTrain dimensions
print dataTest.shape


# Sensor 0 : Sample 1 (128 samples) (Training set)
fig = plt.figure()
plt.figure(figsize=(16,8))
dataTrain[0,1,:]
plt.plot(dataTrain[0,1,:])
plt.show()

# Sensor 1 : Sample 2 (128 samples) (Test set)
fig = plt.figure()
plt.figure(figsize=(16,8))
dataTest[1,2,:]
plt.plot(dataTest[1,2,:])
plt.show()

# Get the labels values for training samples
trainLabelsFile = DATASET_PATH + TRAIN_RELPATH + '/' + 'y_train.txt'
labelsTrain = ds.get_labels(trainLabelsFile, print_on = True)

print labelsTrain.shape #show dimension

# Get the labels values for testing samples
testLabelsFile = DATASET_PATH + TEST_RELPATH + '/' + 'y_test.txt'
labelsTest = ds.get_labels(testLabelsFile, print_on = True)

print labelsTest.shape #show dimension


# convert outputs to one-hot code
labelsTrainEncoded = ds.encode_onehot(labelsTrain)
labelsTestEncoded = ds.encode_onehot(labelsTest)

# Make a dictionary
labelDictionary = ds.make_labels_dictionary(LABELS_DEF_FILE)

print label_dict
print "\n"

sel = 300
print "label {} ({}) -> {}".format(labelsTrain[sel], label_dict[labelsTrain[sel]], labelsTrainEncoded[sel])


"""
Explanation: Reading Dataset
Idea here is to read the files in the dataset to extract data for training, testing and the corresponding (activity) labels for them. The outcome is a set of numpy arrays for each set.
End of explanation
"""


activityToPlot = 2.0

fig = plt.figure()
plt.figure(figsize=(16,8))
plt.title(label_dict[activityToPlot])

for idx, activity in enumerate(labelsTrain):
    if activityToPlot == activity:
        plt.plot(dataTrain[4,idx,:])


plt.show()

"""
Explanation: Filtered plots
End of explanation
"""


numLayers = 50;
lstm_cell = tf.contrib.rnn.BasicRNNCell(numLayers)

lstm_cell

"""
Explanation: RNN
first tries
End of explanation
"""


