data = tools.load_file(filename, sep=',', header=1)

data = tools.downsampling(data, 50)

t = tools.selectCol(data, columns_in, "TIME")
acc = tools.selectCol(data, columns_in, col_acc)
gyr = tools.selectCol(data, columns_in, col_gyr)
mag = tools.selectCol(data, columns_in, col_mag)
lab = tools.selectCol(data, columns_in, "LAB")

acc = inertial.convert_units(acc, coeff=sensAccCoeff)
gyr = inertial.convert_units(gyr, coeff=sensGyrCoeff)
mag = inertial.convert_units(mag, coeff=sensMagCoeff)

# tools.array_labels_to_csv(np.column_stack([t, acc]), np.array(columns_in), "./output/preproc_"+filename[7:-4]+".csv")

#-----EXTRACT FEATURES-----

windows, winlab = win.get_windows_no_mix(t, lab, 1, 0.5)
feats_acc, fcol_acc = inertial.extract_features_acc(acc, t, col_acc, windows)
feats_gyr, fcol_gyr = inertial.extract_features_gyr(gyr, t, col_gyr, windows)
feats_mag, fcol_mag = inertial.extract_features_mag(mag, t, col_mag, windows)
feats = np.column_stack([feats_acc, feats_gyr, feats_mag, winlab])
columns_out = np.r_[fcol_acc, fcol_gyr, fcol_mag, np.array(["LAB"])]
# print feats.shape
# print columns.shape, columns
tools.array_labels_to_csv(feats, columns_out,
                          "./output2/feat_" + filename[7:-4] + ".csv")
# # feats.to_csv("./output/feat_6.csv")
plt.figure(1)
plt.plot(data.timestamp, data[lables_acc])
plt.legend(lables_acc)
plt.xlabel("Time (ms)")
plt.ylabel("Acceleration (m/s^2)")
plt.title("Accelerometer")
plt.figure(2)
plt.plot(data.timestamp, data[lables_gyr])
plt.legend(lables_gyr)
plt.xlabel("Time (ms)")
plt.ylabel("Angular Speed (degree/s)")
plt.title("Gyroscope")
plt.figure(3)
plt.plot(data.timestamp, data[lables_mag])
plt.legend(lables_mag)
plt.title("Magnetometer")
plt.ylabel("uT")
plt.xlabel("Time (ms)")
plt.show()

# data=inertial.convert_units(data, lables[1:], coeff=empaticaAccCoeff)
# print data
windows=win.generate_dummy_windows(len(data), 100, 50)
feats_acc=inertial.extract_features_acc(data, windows, fsamp=sensfsamp, col_acc=lables_acc)
feats_gyr=inertial.extract_features_gyr(data, windows, fsamp=sensfsamp, col_gyr=lables_gyr)
feats_mag=inertial.extract_features_mag(data, windows, fsamp=sensfsamp, col_mag=lables_mag)
feats=pd.concat([feats_acc, feats_gyr, feats_mag], axis=1)
print feats.shape
feats.to_csv("./output/feat_"+filename[7:-4]+".csv")

data = tools.load_file(filename, sep=',', header=1)

data=tools.downsampling(data, 50)

t=tools.selectCol(data, columns_in, "TIME")
acc=tools.selectCol(data, columns_in, col_acc)
gyr=tools.selectCol(data, columns_in, col_gyr)
mag=tools.selectCol(data, columns_in, col_mag)
lab=tools.selectCol(data, columns_in, "LAB")

acc= inertial.convert_units(acc, coeff=sensAccCoeff)
gyr= inertial.convert_units(gyr, coeff=sensGyrCoeff)
mag= inertial.convert_units(mag, coeff=sensMagCoeff)

# tools.array_labels_to_csv(np.column_stack([t, acc]), np.array(columns_in), "./output/preproc_"+filename[7:-4]+".csv")

#-----EXTRACT FEATURES-----

windows, winlab=win.get_windows_no_mix(t,lab , 1, 0.5)
feats_acc, fcol_acc= inertial.extract_features_acc(acc, t, col_acc, windows)
feats_gyr, fcol_gyr= inertial.extract_features_gyr(gyr, t, col_gyr, windows)
feats_mag, fcol_mag= inertial.extract_features_mag(mag, t, col_mag, windows)
feats=np.column_stack([feats_acc, feats_gyr, feats_mag, winlab])
columns_out=np.r_[fcol_acc, fcol_gyr, fcol_mag, np.array(["LAB"])]
# print feats.shape
# print columns.shape, columns
tools.array_labels_to_csv(feats, columns_out, "./output2/feat_"+filename[7:-4]+".csv")
# # feats.to_csv("./output/feat_6.csv")