import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import math

# List all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
"""
### INPUT Number of rows for aggregated dataset
"""
grouped = 100
"""
# Read datasets
## Healthy gearbox
"""
# Healthy gearbox
# ---------------
h30hz0  = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz0.csv")
h30hz10 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz10.csv")
h30hz20 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz20.csv")
h30hz30 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz30.csv")
h30hz40 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz40.csv")
h30hz50 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz50.csv")
h30hz60 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz60.csv")
h30hz70 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz70.csv")
h30hz80 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz80.csv")
h30hz90 = pd.read_csv("../input/gearbox-fault-diagnosis/Healthy/h30hz90.csv")

"""
# Broken gearbox
"""
# Broken gearbox
# --------------
b30hz0  = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz0.csv")
b30hz10 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz10.csv")
b30hz20 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz20.csv")
b30hz30 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz30.csv")
b30hz40 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz40.csv")
b30hz50 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz50.csv")
b30hz60 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz60.csv")
b30hz70 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz70.csv")
b30hz80 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz80.csv")
b30hz90 = pd.read_csv("../input/gearbox-fault-diagnosis/BrokenTooth/b30hz90.csv")
b30hz50
"""
# Compute aggregated datasets
## Function to compute aggregated features: standard deviation
"""
# This function computes the standard deviation for a sample, where:
##  - df:        the dataset, one variable per column
##  - grouped:   the number of consecutive points (rows) in the time series to compute the standard deviation
##  - outcome:   column added to the dataset representing healthy(=1) or failure (=0)
##  RETURNS a dataset (df_result) where each column is the time serie of every variable

def stdev_features(df, grouped, load, outcome):
    #### Create empty dataframe with columns a1,a2,a3,a4
    df_result = pd.DataFrame( [ np.zeros(len(df.columns)-2) ],columns= df.columns[:-2])

    #### Aggregate in groups of nrows computing the standard deviation
    # Remove load & failure columns, keeping only a1,a2,a3,a4
    stdev_lenght=len(df.columns)-2
    
    #### Compute number of rows of the aggregated dataframe
    nrows_raw = len(df.index)
    nrows = math.floor(nrows_raw / grouped)
    nrows_dropped = nrows_raw - nrows*grouped
    print("nrows_raw=", nrows_raw, "   nrows=", nrows, "   Number of dropped rows of grouped= ", nrows_dropped/grouped*100,"%\n")
    
    # Iterate every 'grouped' rows and compute stdev per column
    for i in range(nrows):
      df_group = df.iloc[i*grouped:i*grouped+grouped,:]
      df_stdev = pd.DataFrame(df_group.std()).transpose()
      # Remove load & failure columns
      df_stdev=df_stdev.iloc[:,:stdev_lenght]
      # Add row of calculated stdev
      df_result = df_result.append(df_stdev, ignore_index=True)

      #print ("i*grouped TO i*grouped+grouped", i*grouped, i*grouped+grouped)
      #print ("row, df_stdev=\n", row, df_stdev.iloc[:,:])
      #print ("df_result=\n", df_result)
      #print("row", i, "\n", df_group)


    # Remove the first row (it was the seed of zeros for initializing df_result)
    #print(df_result)
    df_result = df_result.iloc[1:,:]
    # Add the column for 'load'
    df_result['load'] = load*np.ones((nrows,1))

    # Add the column for 'failure'
    failure = np.ones((nrows,1))
    df_result['failure'] = outcome
    print(df_result)
    
    return df_result
    
"""
## Load= 0%
"""
b30hz0.describe()
# [H.0] DATASET BROKEN, 0% LOAD
load = 0

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz0['load'] = load*np.ones((len(b30hz0.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz0.index),1))
b30hz0['failure'] = failureArray
#b30hz0.describe()

b30hz0_stdev= stdev_features (df = b30hz0, grouped= grouped, load= load, outcome= failure)
#b30hz0_stdev.describe()
# [H.0] DATASET HEALTHY, 0% LOAD
load = 0

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz0['load'] = load*np.ones((len(h30hz0.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz0.index),1))
h30hz0['failure'] = failureArray

h30hz0_stdev= stdev_features (df = h30hz0, grouped= grouped, load= load, outcome= failure)
#b30hz0_stdev.describe()
"""
## Load= 10%
"""
b30hz10.describe()
# [H.1] DATASET BROKEN, 10% LOAD
load = 10

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz10['load'] = load*np.ones((len(b30hz10.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz10.index),1))
b30hz10['failure'] = failureArray
#b30hz10.describe()

b30hz10_stdev= stdev_features (df = b30hz10, grouped= grouped, load= load, outcome= failure)
#b30hz10_stdev.describe()
h30hz10.describe()
# [H.1] DATASET HEALTHY, 10% LOAD
load = 10

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz10['load'] = load*np.ones((len(h30hz10.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz10.index),1))
h30hz10['failure'] = failureArray

h30hz10_stdev= stdev_features (df = h30hz10, grouped= grouped, load= load, outcome= failure)
#b30hz10_stdev.describe()
"""
## Load= 20%
"""
# [H.2] DATASET BROKEN, 20% LOAD
load = 20

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz20['load'] = load*np.ones((len(b30hz20.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz20.index),1))
b30hz20['failure'] = failureArray
#b30hz20.describe()

b30hz20_stdev= stdev_features (df = b30hz20, grouped= grouped, load= load, outcome= failure)
#b30hz20_stdev.describe()
# [H.2] DATASET HEALTHY, 20% LOAD
load = 20

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz20['load'] = load*np.ones((len(h30hz20.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz20.index),1))
h30hz20['failure'] = failureArray

h30hz20_stdev= stdev_features (df = h30hz20, grouped= grouped, load= load, outcome= failure)
#b30hz20_stdev.describe()
"""
## Load= 30%
"""
# [H.3] DATASET BROKEN, 30% LOAD
load = 30

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz30['load'] = load*np.ones((len(b30hz30.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz30.index),1))
b30hz30['failure'] = failureArray
#b30hz30.describe()

b30hz30_stdev= stdev_features (df = b30hz30, grouped= grouped, load= load, outcome= failure)
#b30hz30_stdev.describe()
# [H.3] DATASET HEALTHY, 30% LOAD
load = 30

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz30['load'] = load*np.ones((len(h30hz30.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz30.index),1))
h30hz30['failure'] = failureArray

h30hz30_stdev= stdev_features (df = h30hz30, grouped= grouped, load= load, outcome= failure)
#b30hz30_stdev.describe()
"""
## Load= 40%
"""
# [H.4] DATASET BROKEN, 40% LOAD
load = 40

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz40['load'] = load*np.ones((len(b30hz40.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz40.index),1))
b30hz40['failure'] = failureArray
#b30hz40.describe()

b30hz40_stdev= stdev_features (df = b30hz40, grouped= grouped, load= load, outcome= failure)
#b30hz40_stdev.describe()
# [H.4] DATASET HEALTHY, 40% LOAD
load = 40

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz40['load'] = load*np.ones((len(h30hz40.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz40.index),1))
h30hz40['failure'] = failureArray

h30hz40_stdev= stdev_features (df = h30hz40, grouped= grouped, load= load, outcome= failure)
#b30hz40_stdev.describe()
"""
## Load= 50%
"""
# [H.5] DATASET BROKEN, 50% LOAD
load = 50

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz50['load'] = load*np.ones((len(b30hz50.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz50.index),1))
b30hz50['failure'] = failureArray
#b30hz50.describe()

b30hz50_stdev= stdev_features (df = b30hz50, grouped= grouped, load= load, outcome= failure)
#b30hz50_stdev.describe()
# [H.5] DATASET HEALTHY, 50% LOAD
load = 50

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz50['load'] = load*np.ones((len(h30hz50.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz50.index),1))
h30hz50['failure'] = failureArray

h30hz50_stdev= stdev_features (df = h30hz50, grouped= grouped, load= load, outcome= failure)
#b30hz50_stdev.describe()
"""
## Load= 60%
"""
# [H.6] DATASET BROKEN, 60% LOAD
load = 60

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz60['load'] = load*np.ones((len(b30hz60.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz60.index),1))
b30hz60['failure'] = failureArray
#b30hz60.describe()

b30hz60_stdev= stdev_features (df = b30hz60, grouped= grouped, load= load, outcome= failure)
#b30hz60_stdev.describe()
# [H.6] DATASET HEALTHY, 60% LOAD

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz60['load'] = load*np.ones((len(h30hz60.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz60.index),1))
h30hz60['failure'] = failureArray

h30hz60_stdev= stdev_features (df = h30hz60, grouped= grouped, load= load, outcome= failure)
#b30hz60_stdev.describe()
"""
## Load= 70%
"""
# [H.7] DATASET BROKEN, 70% LOAD
load = 70

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz70['load'] = load*np.ones((len(b30hz70.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz70.index),1))
b30hz70['failure'] = failureArray
#b30hz70.describe()

b30hz70_stdev= stdev_features (df = b30hz70, grouped= grouped, load= load, outcome= failure)
#b30hz70_stdev.describe()
# [H.7] DATASET HEALTHY, 70% LOAD
load = 70

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz70['load'] = load*np.ones((len(h30hz70.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz70.index),1))
h30hz70['failure'] = failureArray

h30hz70_stdev= stdev_features (df = h30hz70, grouped= grouped, load= load, outcome= failure)
#b30hz70_stdev.describe()
"""
## Load= 80%
"""
# [H.8] DATASET BROKEN, 80% LOAD
load = 80

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz80['load'] = load*np.ones((len(b30hz80.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz80.index),1))
b30hz80['failure'] = failureArray
#b30hz80.describe()

b30hz80_stdev= stdev_features (df = b30hz80, grouped= grouped, load= load, outcome= failure)
#b30hz80_stdev.describe()
# [H.8] DATASET HEALTHY, 80% LOAD
load = 80

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz80['load'] = load*np.ones((len(h30hz80.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz80.index),1))
h30hz80['failure'] = failureArray

h30hz80_stdev= stdev_features (df = h30hz80, grouped= grouped, load= load, outcome= failure)
#b30hz80_stdev.describe()
"""
## Load= 90%
"""
# [H.9] DATASET BROKEN, 90% LOAD
load = 90

# CREATE THE COLUMN FOR 'load'
## Expressed as %
b30hz90['load'] = load*np.ones((len(b30hz90.index),1))

# CREATE THE COLUMN FOR 'failure'
##   0 is healthy
##   1 is broken
failure = 1
failureArray = np.ones((len(b30hz90.index),1))
b30hz90['failure'] = failureArray
#b30hz90.describe()

b30hz90_stdev= stdev_features (df = b30hz90, grouped= grouped, load= load, outcome= failure)
#b30hz90_stdev.describe()
# [H.9] DATASET HEALTHY, 90% LOAD
load = 90

# CREATE THE COLUMN FOR 'load'
## Expressed as %
h30hz90['load'] = load*np.ones((len(h30hz90.index),1))

# CREATE THE COLUMN FOR 'failure'
failure = 0
failureArray = np.ones((len(h30hz90.index),1))
h30hz90['failure'] = failureArray

h30hz90_stdev= stdev_features (df = h30hz90, grouped= grouped, load= load, outcome= failure)
#b30hz90_stdev.describe()
"""
# Compare stdev of acceleration traversing % load
"""
print("Load 0%")
print("-------")
stdev_bhz0 = pd.DataFrame(b30hz0_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz0 = pd.DataFrame(h30hz0_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz0)
print("Healthy:\n", stdev_hhz0)
print("Load 10%")
print("--------")
stdev_bhz10 = pd.DataFrame(b30hz10_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz10 = pd.DataFrame(h30hz10_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz10)
print("Healthy:\n", stdev_hhz10)
print("Load 20%")
print("--------")
stdev_bhz20 = pd.DataFrame(b30hz20_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz20 = pd.DataFrame(h30hz20_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz20)
print("Healthy:\n", stdev_hhz20)
print("Load 30%")
print("--------")
stdev_bhz30 = pd.DataFrame(b30hz30_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz30 = pd.DataFrame(h30hz30_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz30)
print("Healthy:\n", stdev_hhz30)
print("Load 40%")
print("--------")
stdev_bhz40 = pd.DataFrame(b30hz40_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz40 = pd.DataFrame(h30hz40_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz40)
print("Healthy:\n", stdev_hhz40)
print("Load 50%")
print("--------")
stdev_bhz50 = pd.DataFrame(b30hz50_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz50 = pd.DataFrame(h30hz50_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz50)
print("Healthy:\n", stdev_hhz50)
print("Load 60%")
print("--------")
stdev_bhz60 = pd.DataFrame(b30hz60_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz60 = pd.DataFrame(h30hz60_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz60)
print("Healthy:\n", stdev_hhz60)
print("Load 70%")
print("--------")
stdev_bhz70 = pd.DataFrame(b30hz70_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz70 = pd.DataFrame(h30hz70_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz70)
print("Healthy:\n", stdev_hhz70)
print("Load 80%")
print("--------")
stdev_bhz80 = pd.DataFrame(b30hz80_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz80 = pd.DataFrame(h30hz80_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz80)
print("Healthy:\n", stdev_hhz80)
print("Load 90%")
print("--------")
stdev_bhz90 = pd.DataFrame(b30hz90_stdev.iloc[:,:-2].mean()).transpose()
stdev_hhz90 = pd.DataFrame(h30hz90_stdev.iloc[:,:-2].mean()).transpose()
print("Broken:\n", stdev_bhz90)
print("Healthy:\n", stdev_hhz90)
"""
# Stacked BROKEN dataframe
"""
b30hz_stdev = pd.concat([b30hz0_stdev, b30hz10_stdev, b30hz20_stdev, b30hz30_stdev,b30hz40_stdev,b30hz50_stdev,b30hz60_stdev,b30hz70_stdev,b30hz80_stdev,b30hz90_stdev], axis=0)
b30hz_stdev.describe()
"""
## Export BROKEN dataframe as CSV
"""
b30hz_stdev.to_csv('/kaggle/working/b30hz_stdev.csv', index = False)
"""
# Stacked HEALTHY dataframe
"""
h30hz_stdev = pd.concat([h30hz0_stdev, h30hz10_stdev, h30hz20_stdev, h30hz30_stdev,h30hz40_stdev,h30hz50_stdev,h30hz60_stdev,h30hz70_stdev,h30hz80_stdev,h30hz90_stdev], axis=0)
h30hz_stdev.describe()
"""
## Export HEALTHY dataframe as CSV
"""
h30hz_stdev.to_csv('/kaggle/working/h30hz_stdev.csv', index = False)