prepare_data.py
def save_statistic_features(patient_list, sorce_path="ICHI14_dataset\data", save_path="features.csv", 
                            window_len=60, n_sleep_stages=1, scaler=False):
    
    """
    Save .csv file with extracted statistic features for each windows and axis.
    
    List of all features: ["id", "sleep_stage", "gender", "age", "std_x", "std_y", "std_z", "ptp_x", "ptp_y", "ptp_z", "mean_x", "mean_y", "mean_z", "rms_x", "rms_y", "rms_z", "crest_factor_x", "crest_factor_y", "crest_factor_z", "max_val_x", "max_val_y", "max_val_z", "min_val_x", "min_val_y", "min_val_z"]
    """
    
    columns = ["id", "sleep_stage", "gender", "age", "std_x", "std_y", "std_z", "ptp_x", "ptp_y", "ptp_z",
              "mean_x", "mean_y", "mean_z", "rms_x", "rms_y", "rms_z", "crest_factor_x", "crest_factor_y",
              "crest_factor_z", "max_val_x", "max_val_y", "max_val_z", "min_val_x", "min_val_y", "min_val_z"]
    statistics_df = pd.DataFrame(columns=columns)

    patient_data = np.load(sorce_path + '/pat_inf.npy')
    
    for patient in patient_list:
        
        X, y = get_one_patient_data(data_path=sorce_path, patient=patient, 
                                    window_len=window_len, n_sleep_stages=n_sleep_stages, scaler=scaler)
        
        patient_id = np.array([patient] * y.shape[0]).reshape(y.shape[0], 1)
        std = np.std(X, axis=1)
        ptp = np.ptp(X, axis=1)
        mean = np.mean(X, axis=1)
        rms = np.sqrt(np.mean(np.square(X), axis=1))
        crest_factor = np.max(X, axis=1) / rms
        max_val = np.amax(X, axis=1)
        min_val = np.amin(X, axis=1)
        
        gender = 0
        age = 0
        for i, p in enumerate(patient_data[1:, 0]):
            if patient == p.decode('utf-8'):
                age = int(patient_data[i+1, 2].decode('utf-8'))
                
                if "m" == patient_data[i+1, 1].decode('utf-8'):
                    gender = 1
        age = age * np.ones((y.shape[0], 1))
        gender = gender * np.ones((y.shape[0], 1))
     
        y = y.reshape(y.shape[0], 1)
        X_new = np.concatenate((patient_id, y, gender, age, std, ptp, mean, rms, crest_factor, max_val, min_val), axis=1)
        X_new_df = pd.DataFrame(X_new, columns=columns)
        
        statistics_df = statistics_df.append(X_new_df, ignore_index=True)
        
    statistics_df.to_csv(save_path, sep=',', header=True, index=None)

    Save .csv file with extracted statistic features for each windows and axis.
    
    List of all features: ["id", "sleep_stage", "gender", "age", "std_x", "std_y", "std_z", "ptp_x", "ptp_y", "ptp_z", "mean_x", "mean_y", "mean_z", "rms_x", "rms_y", "rms_z", "crest_factor_x", "crest_factor_y", "crest_factor_z", "max_val_x", "max_val_y", "max_val_z", "min_val_x", "min_val_y", "min_val_z"]
    
Save .csv file with extracted statistic features for each windows and axis.
Save . csv file with extracted statistic features for each windows and axis .
def save_statistic_features(patient_list, sorce_path="ICHI14_dataset\data", save_path="features.csv", 
                            window_len=60, n_sleep_stages=1, scaler=False):
    columns = ["id", "sleep_stage", "gender", "age", "std_x", "std_y", "std_z", "ptp_x", "ptp_y", "ptp_z",
              "mean_x", "mean_y", "mean_z", "rms_x", "rms_y", "rms_z", "crest_factor_x", "crest_factor_y",
              "crest_factor_z", "max_val_x", "max_val_y", "max_val_z", "min_val_x", "min_val_y", "min_val_z"]
    statistics_df = pd.DataFrame(columns=columns)
    patient_data = np.load(sorce_path + '/pat_inf.npy')
    for patient in patient_list:
        X, y = get_one_patient_data(data_path=sorce_path, patient=patient, 
                                    window_len=window_len, n_sleep_stages=n_sleep_stages, scaler=scaler)
        patient_id = np.array([patient] * y.shape[0]).reshape(y.shape[0], 1)
        std = np.std(X, axis=1)
        ptp = np.ptp(X, axis=1)
        mean = np.mean(X, axis=1)
        rms = np.sqrt(np.mean(np.square(X), axis=1))
        crest_factor = np.max(X, axis=1) / rms
        max_val = np.amax(X, axis=1)
        min_val = np.amin(X, axis=1)
        gender = 0
        age = 0
        for i, p in enumerate(patient_data[1:, 0]):
            if patient == p.decode('utf-8'):
                age = int(patient_data[i+1, 2].decode('utf-8'))
                if "m" == patient_data[i+1, 1].decode('utf-8'):
                    gender = 1
        age = age * np.ones((y.shape[0], 1))
        gender = gender * np.ones((y.shape[0], 1))
        y = y.reshape(y.shape[0], 1)
        X_new = np.concatenate((patient_id, y, gender, age, std, ptp, mean, rms, crest_factor, max_val, min_val), axis=1)
        X_new_df = pd.DataFrame(X_new, columns=columns)
        statistics_df = statistics_df.append(X_new_df, ignore_index=True)
    statistics_df.to_csv(save_path, sep=',', header=True, index=None)
def save_statistic_features ( patient_list , sorce_path = "ICHI14_dataset\data" , save_path = "features.csv" , window_len = 60 , n_sleep_stages = 1 , scaler = False ) : columns = [ "id" , "sleep_stage" , "gender" , "age" , "std_x" , "std_y" , "std_z" , "ptp_x" , "ptp_y" , "ptp_z" , "mean_x" , "mean_y" , "mean_z" , "rms_x" , "rms_y" , "rms_z" , "crest_factor_x" , "crest_factor_y" , "crest_factor_z" , "max_val_x" , "max_val_y" , "max_val_z" , "min_val_x" , "min_val_y" , "min_val_z" ] statistics_df = pd . DataFrame ( columns = columns ) patient_data = np . load ( sorce_path + '/pat_inf.npy' ) for patient in patient_list : X , y = get_one_patient_data ( data_path = sorce_path , patient = patient , window_len = window_len , n_sleep_stages = n_sleep_stages , scaler = scaler ) patient_id = np . array ( [ patient ] * y . shape [ 0 ] ) . reshape ( y . shape [ 0 ] , 1 ) std = np . std ( X , axis = 1 ) ptp = np . ptp ( X , axis = 1 ) mean = np . mean ( X , axis = 1 ) rms = np . sqrt ( np . mean ( np . square ( X ) , axis = 1 ) ) crest_factor = np . max ( X , axis = 1 ) / rms max_val = np . amax ( X , axis = 1 ) min_val = np . amin ( X , axis = 1 ) gender = 0 age = 0 for i , p in enumerate ( patient_data [ 1 : , 0 ] ) : if patient == p . decode ( 'utf-8' ) : age = int ( patient_data [ i + 1 , 2 ] . decode ( 'utf-8' ) ) if "m" == patient_data [ i + 1 , 1 ] . decode ( 'utf-8' ) : gender = 1 age = age * np . ones ( ( y . shape [ 0 ] , 1 ) ) gender = gender * np . ones ( ( y . shape [ 0 ] , 1 ) ) y = y . reshape ( y . shape [ 0 ] , 1 ) X_new = np . concatenate ( ( patient_id , y , gender , age , std , ptp , mean , rms , crest_factor , max_val , min_val ) , axis = 1 ) X_new_df = pd . DataFrame ( X_new , columns = columns ) statistics_df = statistics_df . append ( X_new_df , ignore_index = True ) statistics_df . to_csv ( save_path , sep = ',' , header = True , index = None )
Save .csv file with extracted statistic features for each windows and axis.
Save . csv file with extracted statistic features for each windows and axis .
"""
    Save .csv file with extracted statistic features for each windows and axis.
    
    List of all features: ["id", "sleep_stage", "gender", "age", "std_x", "std_y", "std_z", "ptp_x", "ptp_y", "ptp_z", "mean_x", "mean_y", "mean_z", "rms_x", "rms_y", "rms_z", "crest_factor_x", "crest_factor_y", "crest_factor_z", "max_val_x", "max_val_y", "max_val_z", "min_val_x", "min_val_y", "min_val_z"]
    """
patient_list
sorce_path
save_path
window_len
n_sleep_stages
scaler