Beispiel #1
0
    plt.show()


def plot_predicted_trajectory(preds_per_trial, targets_per_trial):
    plt.figure(figsize=(32, 12))
    t = np.arange(preds_per_trial.shape[0]) / srate
    plt.plot(t, preds_per_trial)
    plt.plot(t, targets_per_trial)
    plt.legend(('Predicted', 'Actual'), fontsize=14)
    plt.title('Fold = {:d}, CC = {:f}'.format(0, cc_folds[0]))
    plt.xlabel('time [s]')


if __name__ == '__main__':
    model_file = '/models/saved_models/best_model_1'
    model = load_model(model_file)

    data_file = 'ALL_11_FR1_day1_absVel'
    data = Data(home + f'/previous_work/{data_file}.mat', -1)
    n_preds_per_input = get_output_shape(model, data.in_channels, 1200)[1]
    data.cut_input(input_time_length, n_preds_per_input, False)
    train_set, test_set = data.train_set, data.test_set

    select_modules = [
        'conv_spat', 'conv_2', 'conv_3', 'conv_4', 'conv_classifier'
    ]  # Specify intermediate outputs
    modules = list(model.named_children())  # Extract modules from model
    model_pert = SelectiveSequential(select_modules, modules)  # Wrap modules
    model_pert.eval()
    model_pert.double()
    model.eval()
Beispiel #2
0
     model_string = f'sbp0_sm_vel_shuffled'
     variable = 'vel'
 else:
     model_string = 'sbp0_sm_absVel_shuffled'
     variable = 'absVel'
 saved_model_dir = f'lr_{lr}'
 all_xs = []
 all_ys = []
 all_zs = []
 corr_coefs_full = []
 corr_coefs_hp = []
 for patient_index in range(1, 13):
     input_channels = get_num_of_channels(
         home + f'/previous_work/P{patient_index}_data.mat')
     changed_model_full = load_model(
         f'/models/saved_models/{saved_model_dir}/sbp0_shuffled_sm_{variable}_k3_d3/sbp0_shuffled_sm_{variable}_k3_d3_p_{patient_index}//best_model_split_0'
     )
     changed_model_hp = load_model(
         f'/models/saved_models/{saved_model_dir}/sbp0_shuffled_hp_sm2_{variable}_k3_d3/sbp0_shuffled_hp_sm2_{variable}_k3_d3_p_{patient_index}//best_model_split_0'
     )
     model_name = 'k3_d3'
     n_preds_per_input = get_output_shape(changed_model_full,
                                          input_channels,
                                          input_time_length)[1]
     small_window = input_time_length - n_preds_per_input + 1
     if shift_by is None:
         shift_index = int(small_window / 2)
     else:
         shift_index = int((small_window / 2) - shift_by)
     train_file = open(f'{home}/models/indices/train.dict', 'rb')
     valid_file = open(f'{home}/models/indices/valid.dict', 'rb')
Beispiel #3
0
def prepare_for_gradients(patient_index,
                          model_name,
                          trained_mode,
                          eval_mode,
                          saved_model_dir,
                          model_file=None,
                          shift=False,
                          high_pass=False,
                          trajectory_index=0,
                          multi_layer=False,
                          motor_channels=None,
                          low_pass=False,
                          shift_by=None,
                          whiten=False):
    """
    Puts together the variables necessary for gradient visualization.
    First the model is loaded based on its name. Then the data is prepared to be given to the loaded model on input.
    The model is set into eval mode (its weights are frozen) and it is ready for gradient calculation

    :param patient_index: specifies on which patient data the model was build
    :param model_name: name of the model to be loaded
    :param trained_mode: specifies if the model should be trained or untrained
    :param eval_mode: specifies if the set should be the train set or the validation set
    :param saved_model_dir: the directory where the model is saved
    :param model_file:
    :param shift: specifies if the data in the dataset should be shifted
    :param high_pass: specifies if the data in the datasets should be high-pass filtered
    :param trajectory_index: 0 for velocity, 1 for absolute velocity
    :param multi_layer: specifies if only the last layer should be returned or all layers will be inspected
    :param motor_channels: specifies if only gradients for motor channels should be returned
    :param low_pass: specifies if the data in the datasets should be low-pass filtered
    :param shift_by: specifies by how much the predicted time-point should be shifted with respect to he receptive field.
    :param whiten: specifies if data should be whitened
    """
    if shift_by is not None:
        shift_str = f'shift_{shift_by}'
        model_name_list = model_name.split('/')
        model_name_list = [model_name_list[0], shift_str, model_name_list[1]]
        model_name = '/'.join(model_name_list)
        index = 2
        random_valid = False
    else:
        index = 1
        shift_str = f''
        random_valid = True
    if model_file is None:
        if '/' in model_name:
            other_model_name = model_name.split(
                '/')[index] + f'_p_{patient_index}'
        else:
            other_model_name = f'{model_name}_p_{patient_index}'
        if trained_mode == 'untrained':

            model_file = f'/models/saved_models/{model_name}/{other_model_name}/initial_{other_model_name}'
        else:
            # model_file = f'/models/saved_models/{model_name}/{other_model_name}/last_model'
            if shift_str != '':
                model_file = f'/models/saved_models/{model_name}/{other_model_name}/best_model_split_0'
            else:
                model_file = f'/models/saved_models/{model_name}/{other_model_name}/last_model'
    output = f'{output_dir}/hp_graphs/{model_name}/{eval_mode}/{trained_mode}/'
    # Path(output).mkdir(parents=True, exist_ok=True)
    model = load_model(model_file)
    print(model_file)
    print('motor channels:', motor_channels)

    in_channels = get_num_of_channels(
        home + f'/previous_work/P{patient_index}_data.mat')
    n_preds_per_input = get_output_shape(model, in_channels, 1200)[1]
    shift_window = input_time_length - n_preds_per_input + 1
    small_window = min((input_time_length - n_preds_per_input) * 2, 1200)
    print('small window:', small_window, model_name)
    print('shift window:', shift_window, shift)
    if shift_by is None:
        shift_index = int(shift_window / 2)
    else:
        shift_index = int((shift_window / 2) - shift_by)

    data = Data(home + f'/previous_work/P{patient_index}_data.mat',
                -1,
                low_pass=low_pass,
                trajectory_index=trajectory_index,
                shift_data=shift,
                high_pass=high_pass,
                shift_by=shift_index,
                pre_whiten=whiten,
                random_valid=random_valid)

    data.cut_input(input_time_length, n_preds_per_input, False)
    train_set, test_set = data.train_set, data.test_set
    corrcoef = get_corr_coef(train_set, model)
    num_channels = None

    if eval_mode == 'validation':
        train_set = test_set

    X_reshaped = np.asarray(train_set.X)
    print(X_reshaped.shape)
    X_reshaped = reshape_Xs(input_time_length, X_reshaped)
    # summary(model.float(), input_size=(data.in_channels, 683, 1))
    if not multi_layer:
        new_model = create_new_model(model,
                                     'conv_classifier',
                                     input_channels=num_channels)
    else:
        new_model = model
    # with torch.no_grad():
    #     test_out = new_model(np_to_var(X_reshaped[:2]).double())
    new_model.eval()
    # n_filters = test_out.shape[1]

    return corrcoef, new_model, X_reshaped, small_window, output, data.motor_channels, data.non_motor_channels
def model_gradients_heatmap(files,
                            layers,
                            variable,
                            prefix,
                            gradient_dir,
                            saved_models_dir='lr_0.001'):
    for layer in layers:
        output_dir = f'{home}/outputs/{gradient_dir}/{layer}/{prefix}/'
        Path(output_dir).mkdir(parents=True, exist_ok=True)
        fig, ax = plt.subplots(1, 3, sharey='row', figsize=(15, 6))
        gradient_dfs = []
        titles = []

        for i, gradient_kind in enumerate(['ALLCH', 'MCH', 'NCH']):
            gradient_dict = {}
            index_dict = {}
            for file in files:
                gradient_df = pandas.DataFrame()
                gradient = get_gradient_for_file(file, layer, gradient_kind,
                                                 variable, prefix,
                                                 gradient_dir)
                gradient_dict[file] = gradient
                gradient_df[file] = gradient
                if 'sbp1' in file:
                    shape = min((input_time_length - 519) * 2, 1200)
                else:
                    model = load_model(
                        f'/models/saved_models/{saved_models_dir}/{prefix}_{file}/{prefix}_{file}_p_1/last_model'
                    )
                    with torch.no_grad():
                        in_channels = list(model.parameters())[2].shape[3]
                        test_out = model.double()(np_to_var(
                            np.zeros([1, in_channels, 1200])).cuda())
                    shape = min((input_time_length - test_out.shape[1]) * 2,
                                1200)
                y = np.around(np.fft.rfftfreq(shape, 1 / 250.0), 0)
                gradient_df = gradient_df.set_index(pandas.Index(y), drop=True)
                index_dict[file] = list(gradient_df.index.values)
            sorted_gradient_dict = {
                k: v
                for k, v in sorted(gradient_dict.items(),
                                   key=lambda item: len(item[1]),
                                   reverse=True)
            }
            longest_k = list(sorted_gradient_dict.keys())[0]
            gradient_df = pandas.DataFrame()
            gradient_df[longest_k] = sorted_gradient_dict[longest_k]
            gradient_df.set_index(pandas.Index(index_dict[longest_k]),
                                  drop=True)
            for k, v in sorted_gradient_dict.items():
                if k != longest_k:
                    gradient, new_index = extend_second_list(
                        long_list=sorted_gradient_dict[longest_k],
                        long_index_list=index_dict[longest_k],
                        shorter_list=v,
                        shorter_index_list=index_dict[k])
                    gradient_df[k] = gradient
            # print(gradient_df[f'{variable}_k1_d3'].tolist())
            gradient_df = gradient_df.reindex([
                f'{variable}_k1_d3', f'{variable}_k2_d3', f'{variable}_k3_d3',
                f'{variable}_k2_d1', f'{variable}_k3_d1', f'{variable}_k2_d2',
                f'{variable}_k3_d2'
            ],
                                              axis=1)
            gradient_df = gradient_df.rename(
                {
                    f'{variable}_k3_d3': f'{variable}_k3_d3_sbp0',
                    f'{variable}_k1_d3': f'{variable}_k1'
                },
                axis=1)
            # print(gradient_df[f'{variable}_k1'].tolist())

            title = get_gradient_title(layer, gradient_kind)
            gradient_dfs.append(gradient_df)
            titles.append(title)
        output = f'{output_dir}/{variable}_model_gradients_all_kinds.png'
        plot_gradient_heatmap(gradient_dfs,
                              titles,
                              output,
                              xlabel='Models',
                              ax=ax)
        plt.tight_layout()
        plt.savefig(output)
        model_string = 'sbp0_dm_absVel'
        variable = 'absVel'
    all_xs = []
    all_ys = []
    all_zs = []
    corr_coefs_full = []
    corr_coefs_hp = []
    for patient_index in range(1, 13):
        input_channels = get_num_of_channels(
            home + f'/previous_work/P{patient_index}_data.mat')
        # model, changed_model_full, model_name = get_model(input_channels, input_time_length,
        #                                                   dilations=dilation[0],
        #                                                   kernel_sizes=kernel_size, padding=False)
        # changed_model_full = load_model(f'/models/saved_models/lr_0.001/sbp1_sm_{variable}_k_3333/sbp1_sm_{variable}_k_3333_p_{patient_index}/best_model_split_0')
        changed_model_full = load_model(
            f'/models/saved_models/lr_0.001/sbp0_m_{variable}_k3_d3/sbp0_m_{variable}_k3_d3_p_{patient_index}/best_model_split_0'
        )
        # _, changed_model_hp, _ = get_model(input_channels, input_time_length,
        #                                    dilations=dilation[0],
        #                                    kernel_sizes=kernel_size, padding=False)
        # changed_model_hp = load_model(f'/models/saved_models/lr_0.001/sbp1_hps_{variable}_k_3333/sbp1_hps_{variable}_k_3333_p_{patient_index}/best_model_split_0')
        changed_model_hp = load_model(
            f'/models/saved_models/lr_0.001/sbp0_hp_m_{variable}_k3_d3/sbp0_hp_m_{variable}_k3_d3_p_{patient_index}/best_model_split_0'
        )

        model_name = 'k3_d3'
        n_preds_per_input = get_output_shape(changed_model_full,
                                             input_channels,
                                             input_time_length)[1]
        small_window = input_time_length - n_preds_per_input + 1
        if shift_by is None: