def longest_sample_RUL_prediction():
    for sub_dataset in ['FD001','FD002','FD003','FD004']:
        
        res_rmse = pd.read_csv(os.path.join(results_dir, sub_dataset, sub_dataset+'.csv'))
        best_model_name = 'model_'+str(res_rmse.iloc[res_rmse[' RMSE'].idxmin(),0])+'_results.npz'
        res_data = np.load(os.path.join(results_dir, sub_dataset, best_model_name),allow_pickle=True)
        
        y_true = res_data['y_true']
        y_pred = res_data['y_pred']
                
        md_data = np.load(os.path.join('processed_data', 'CMAPSS_md_dataset.npz'), allow_pickle=True)[sub_dataset][()]
        x_md = md_data['x_test_md']
        x_ds = md_data['test_deg']
        test_lens = [len(x) for i, x in enumerate(x_md) if x_ds[i] < len(x) -1]

        
        max_len_sample_ind = np.argwhere(test_lens == np.max(test_lens))[0][0]
        sample_true = y_true[max_len_sample_ind]
        sample_pred = y_pred[max_len_sample_ind]
        sample_rmse = rmse_eval(sample_true, sample_pred)
        
        plt.figure()
        plt.plot(np.arange(1, len(sample_true)+1,1), sample_true[::-1], label='True RUL')    
        plt.plot(np.arange(1, len(sample_true)+1,1), sample_pred[::-1], 'r', label='Predicted RUL')
        plt.xlabel('Operational cycles since degradation start detection')
        plt.ylabel('Remaining Useful Life')
        plt.legend()
        plt.text(0.65, 0.7, 'Sample RMSE '+'{:.2f}'.format(sample_rmse), 
                 fontsize=10, bbox=dict(facecolor='green', alpha=0.3), transform=plt.gca().transAxes)
        plt.savefig(os.path.join('plots','RUL_results',sub_dataset+'_longest_sample_RUL.png'),
                    bbox_inches='tight', pad_inches=0)
def FD004_cases_RUL():
        
    res_rmse = pd.read_csv(os.path.join(results_dir, 'FD004', 'FD004.csv'))
    best_model_name = 'model_'+str(res_rmse.iloc[res_rmse[' RMSE'].idxmin(),0])+'_results.npz'
    res_data = np.load(os.path.join(results_dir, 'FD004', best_model_name),allow_pickle=True)
    
    y_true = res_data['y_true']
    y_pred = res_data['y_pred']
            
    md_data = np.load(os.path.join('processed_data', 'CMAPSS_md_dataset.npz'), allow_pickle=True)['FD004'][()]
    x_md = md_data['x_test_md']
    x_ds = md_data['test_deg']
    threshold = md_data['threshold']
    md_ind = [i for i, x in enumerate(x_md) if x_ds[i] < len(x) -1]  


    indexes = []
    # Longest RUL sample
    test_lens =  [len(y) for y in y_pred]
    indexes.append(np.argwhere(test_lens == np.max(test_lens))[0][0])
    
    # Random regeneration
    pred_ruls = [np.min(y[150:]) if len(y)>150 else np.inf for y in y_pred]
    indexes.append(np.argwhere(pred_ruls == np.min(pred_ruls))[0][0])
    
    for ind in indexes:
        sample_true = y_true[ind]
        sample_pred = y_pred[ind]
        sample_rmse = rmse_eval(sample_true, sample_pred)

        md_sample = x_md[md_ind[ind]]
        no_deg_sample = md_sample[:x_ds[md_ind[ind]]]
        deg_sample = md_sample[x_ds[md_ind[ind]]:]
        
        fig, ax = plt.subplots(2,1, figsize=(7,7))
        ax.flat
                
        ax[0].plot(x_ds[md_ind[ind]]+np.arange(1, len(sample_true)+1,1), sample_true[::-1], label='True RUL')    
        ax[0].plot(x_ds[md_ind[ind]]+np.arange(1, len(sample_true)+1,1), sample_pred[::-1], 'r', label='Predicted RUL')
        ax[0].set_xlim(xmin=-5)
        ax[0].set_ylabel('RUL')       
        ax[0].legend()
        
        ax[1].plot(range(len(no_deg_sample)), no_deg_sample, color='g')
        ax[1].plot(range(len(no_deg_sample), len(no_deg_sample)+len(deg_sample)), deg_sample, color='r')  
        ax[1].axhline(threshold, c='k', label='MD threshold')
        ax[1].set_xlim(xmin=-5)
        ax[1].set_xlabel('Operational cycles')
        ax[1].set_ylabel('MD')
        ax[1].legend()

        fig.savefig(os.path.join('plots','FD004_samples_{}.png'.format(md_ind[ind]+1)),
                    bbox_inches='tight', pad_inches=0)
def RUL_RMSE_stats_non_degraded_samples():
    dataset_name = 'CMAPSS_unfiltered'
    results_dir = os.path.join(os.getcwd(), 'training_results', dataset_name, model)

    for sub_dataset in ['FD001','FD002','FD003','FD004']:
        
        rmse = []
        runs = [i for i in os.listdir(os.path.join(results_dir, sub_dataset)) if 'ignored' in i]
        for run in runs:
            data = np.load(os.path.join(results_dir, sub_dataset, run), allow_pickle=True)
            y_true = np.asarray([int(i[0]) for i in data['y_true']])
            y_pred = np.asarray([int(i[0]) for i in data['y_pred']])
            rmse.append(rmse_eval(y_true, y_pred))
        print(sub_dataset+' mean RMSE: {:.2f}'.format(np.mean(rmse)))
        print(sub_dataset+' std. RMSE: {:.2f}'.format(np.std(rmse)))        
def end_RUL_prediction():
    for sub_dataset in ['FD001','FD002','FD003','FD004']:
        res_rmse = pd.read_csv(os.path.join(results_dir, sub_dataset, sub_dataset+'.csv'))
        best_model_name = 'model_'+str(res_rmse.iloc[res_rmse[' RMSE'].idxmin(),0])+'_results.npz'
        res_data = np.load(os.path.join(results_dir, sub_dataset, best_model_name),allow_pickle=True)
        
        y_true = np.asarray([i[0] for i in res_data['y_true']]).flatten()
        y_pred = np.asarray([i[0] for i in res_data['y_pred']]).flatten()
        sample_rmse = rmse_eval(y_true, y_pred)
        
        sorted_idx = np.argsort(y_true)
        y_true = y_true[sorted_idx]
        y_pred = y_pred[sorted_idx]
        
        plt.figure()
        plt.plot(np.arange(1, len(y_true)+1,1), y_true[::-1], label='True RUL')    
        plt.plot(np.arange(1, len(y_true)+1,1), y_pred[::-1], 'r', label='Predicted RUL')
        plt.xlabel('Engine (sorted by RUL)')
        plt.ylabel('Remaining Useful Life')
        plt.legend()
        plt.text(0.75, 0.7, 'RMSE '+'{:.2f}'.format(sample_rmse), 
                 fontsize=10, bbox=dict(facecolor='green', alpha=0.3), transform=plt.gca().transAxes)
        plt.savefig(os.path.join('plots','RUL_results',sub_dataset+'_end_RUL.png'),
                    bbox_inches='tight', pad_inches=0)
Ejemplo n.º 5
0
    x_train = scaler_x.fit_transform(x_train)
    x_valid = scaler_x.transform(x_valid)
    x_test = scaler_x.transform(x_test)

    saver = ResultsSaver(results_dir, sub_dataset, sub_dataset)
    Model = ANNModel(x_train, y_train, x_valid, y_valid, model_type=model_name)

    for i in range(10):
        model = Model.model_train(params)
        y_pred = model.predict(x_test)

        prediction_plots(y_test,
                         y_pred,
                         plot_name=sub_dataset + '_iter_' + str(i + 1),
                         save_dir=os.path.join(results_dir, sub_dataset))
        test_loss = rmse_eval(y_test, y_pred, sub_dataset)
        saver.save_iter(i + 1, test_loss)

        # Test set results saving
        x_test_samples, y_test_samples = dataset_loader.get_samples(
            sub_dataset, 'test')
        y_pred = []
        for x_sample in x_test_samples:
            y_pred.append(model.predict(scaler_x.transform(x_sample)))
        np.savez(os.path.join(results_dir, sub_dataset,
                              'model_' + str(i + 1) + '_results.npz'),
                 y_true=y_test_samples,
                 y_pred=y_pred)

        if dataset_name == 'CMAPSS_unfiltered':
            x_test_samples, y_test_samples = dataset_loader.get_samples(
Ejemplo n.º 6
0
    x_train = scaler_x.fit_transform(x_train)
    x_valid = scaler_x.transform(x_valid)
    x_test = scaler_x.transform(x_test)

    saver = ResultsSaver(results_dir, sample_name, sample_name)
    Model = ANNModel(x_train,
                     y_train,
                     x_valid,
                     y_valid,
                     x_test=x_test,
                     y_test=y_test,
                     model_type=model_name)

    for i in range(10):
        model = Model.model_train(params)
        y_pred = model.predict(x_test)

        prediction_plots(y_test,
                         y_pred,
                         plot_name=sample_name + '_iter_' + str(i + 1),
                         save_dir=os.path.join(results_dir, sample_name))
        test_loss = rmse_eval(y_test, y_pred, sample_name)
        saver.save_iter(i + 1, test_loss)

        # Test set results saving
        np.savez(os.path.join(results_dir, sample_name,
                              'model_' + str(i + 1) + '_results.npz'),
                 y_true=y_test,
                 y_pred=y_pred)

Model.model_plot(model, results_dir, model_name=dataset_name + '_model.png')