import sklearn
sklearn.__version__

import pandas as pd
import numpy as np
import time
import datetime as dt
import matplotlib.pyplot as plt
from sklearn import tree
from sklearn.model_selection import train_test_split

%matplotlib inline

"""
Explanation: 12-752 Final Project
Authors: Ben Neiman (bneiman), Parangat Shukla (parangas)
Step 1: Importing and Cleaning Data
Import libraries:
End of explanation
"""


column_names = pd.read_excel('langevincodebook.xlsx',sheetname = 'Sheet2')
data_names = column_names['Description'].values

data = pd.read_csv('LANGEVIN_DATA.txt',sep=' ',names = data_names,index_col =False)

"""
Explanation: Importing the data and the column names from the codebook:
End of explanation
"""


def toTimestamp(datenum):
    python_datetime = dt.datetime.fromordinal(int(datenum) - 366) + dt.timedelta(days=datenum%1)
    return python_datetime

data['Timestamp'] = [toTimestamp(data['Time'][date]) for date in range(len(data['Time']))]
data['Occupant Number']=data['Occupant Number'].astype(int)
data['Hour'] = data['Timestamp'].dt.hour
data['Month'] = data['Timestamp'].dt.month

"""
Explanation: The time in the data is measured with MATLAB's absolute time, converting to a meaningful timestamp:
End of explanation
"""


data = data[pd.notnull(data['General Thermal Comfort (right now)'])]
uncont_data = data[['Occupant Number','Timestamp','Gender','Age','General Thermal Comfort (right now)','INDOOR Ambient Temp.','INDOOR Relative Humidity','OUTDOOR Ambient Temp.','OUTDOOR Relative Humidity','INDOOR Air Velocity','OUTDOOR Air Velocity','Hour','Month']]


"""
Explanation: Now we need to remove any times where there is no Thermal comfort measurement taken and select our uncontrollable variables:
End of explanation
"""


print(uncont_data.isnull().sum())
o = uncont_data.isnull().values
plt.imshow(o,aspect = 'auto',interpolation = 'nearest')

"""
Explanation: Check if there are still any null values left and check where they are:
End of explanation
"""


uncont_data = uncont_data.dropna()

uncont_data.head(10)

"""
Explanation: Comments:

Occupant 21 has no age data.
Missing 17 indoor temp values for occupant 5.

Now to remove those rows with nulls:
End of explanation
"""


temp_plot = uncont_data.plot('Timestamp',['INDOOR Ambient Temp.','OUTDOOR Ambient Temp.'],figsize = (8,5))
print('The average absolute difference in temperature is: ' + str(np.average(abs(uncont_data['INDOOR Ambient Temp.']-uncont_data['OUTDOOR Ambient Temp.'])))+' Celsius')

hum_plot = uncont_data.plot('Timestamp',['INDOOR Relative Humidity','OUTDOOR Relative Humidity'],figsize = (10,8))
print('The average absolute difference in relative humidity is: ' + str(np.average(abs(uncont_data['INDOOR Relative Humidity']-uncont_data['OUTDOOR Relative Humidity']))))

velo_plot = uncont_data.plot('Timestamp',['INDOOR Air Velocity','OUTDOOR Air Velocity'],figsize = (8,5))
print('The average absolute difference in air velocity is: ' + str(np.average(abs(uncont_data['INDOOR Air Velocity']-uncont_data['OUTDOOR Air Velocity']))))

"""
Explanation: With the data cleaned we move on to the analysis.
Step 2: Exploratory Analysis
We thought it would be interesting to look at how much the indoor/outdoor measurements vary from each other to get an idea for how climate controlled the building is.
End of explanation
"""


y = uncont_data['General Thermal Comfort (right now)']
x1 = uncont_data['INDOOR Ambient Temp.']
x2 = uncont_data['INDOOR Relative Humidity']
x3 = uncont_data['INDOOR Air Velocity']

fig = plt.figure(figsize=(15,10))
ax1 = plt.subplot(311)
plt.scatter(x1,y)
ax1.set_xlabel("Indoor Ambient Temperature")
ax1.set_ylabel('General Thermal Comfort')
ax1.set_ylim(1,6)

ax2 = plt.subplot(312,sharey = ax1)
plt.scatter(x2,y)
ax2.set_xlabel('Indoor Relative Humidity')

ax3 = plt.subplot(313,sharey = ax1)
plt.scatter(x3,y)
ax3.set_xlabel("Indoor Air Velocity")

plt.tight_layout()
plt.savefig('comfort_subplots.png')

# Correlation Values for each plot
z = uncont_data['General Thermal Comfort (right now)']
print('Correlation coefficient for Thermal Comfort and Temp: ',z.corr(uncont_data['INDOOR Ambient Temp.']))
print('Correlation coefficient for Thermal Comfort and Relative Humidity: ',z.corr(uncont_data['INDOOR Relative Humidity']))
print('Correlation coefficient for Thermal Comfort and Air Velocity',z.corr(uncont_data['INDOOR Air Velocity']))

"""
Explanation: Comments:

There is a large difference between indoor and outdoor conditions in all variables suggesting that the building is well-climate controlled. This implies that our outdoor variable probably will have minimal predictive power in our regression tree.
Indoor air velocity is probably negligible for this experiment as well because there is almost no variance in its values.

Next we decided to look at how General comfort level related to some of the uncontrollable variables.
End of explanation
"""


X1 = uncont_data[['INDOOR Ambient Temp.','INDOOR Relative Humidity','INDOOR Air Velocity']]
Y = uncont_data['General Thermal Comfort (right now)']

"""
Explanation: Step 3: Regression Trees
Now we're testing how effectively the uncontrollable variables can predict the general comfort level. We'll run two regression trees, one with our environmental and time variables and just splitting on occupant number. If occupant number plays a significant role in predicting the general thermal comfort level than personal preference will tell us more about general thermal comfort level than the uncontrollable variables.
First let's set up our features and response.
End of explanation
"""


X1_train, X1_test, Y_train, Y_test = train_test_split(X1, Y, test_size=0.3)

reg = tree.DecisionTreeRegressor()
reg = reg.fit(X1_train,Y_train)

r2_score_avg1 = np.average([reg.score(X1_test,Y_test) for i in range(5000)])
print('R^2 value: ', r2_score_avg1)

"""
Explanation: The first regression tree will test our environmental and time variables.
End of explanation
"""


X2 = uncont_data['Occupant Number']
X2 = X2[:,None]


X2_train, X2_test, Y_train, Y_test = train_test_split(X2, Y, test_size=0.3)

reg = tree.DecisionTreeRegressor()
reg = reg.fit(X2_train,Y_train)

r2_score_avg2 = np.average([reg.score(X2_test,Y_test) for i in range(5000)])
print('R^2 Value: ',r2_score_avg2)

"""
Explanation: The second regression tree just checks the predictive power of occupant number alone.
End of explanation
"""


fig1 = temp_plot.get_figure()
fig1.savefig('temp_plot.png')

fig2 = hum_plot.get_figure()
fig2.savefig('humidity_plot.png')

fig3 = velo_plot.get_figure()
fig3.savefig('velocity_plot.png')

uncont_data.describe()

"""
Explanation: Comments
Our first regression tree had absolutely no predictive power, implying that environmental factors and time of day/year had little to no effect on general thermal comfort. This could be due to the relative stability of indoor temp, air velocity, and humidity year round. The fact that the occupant number alone has some predictive power demonstrates that general thermal comfort is more likely due to personal preference. 
Step 4: Exporting Images
End of explanation
"""


