Пример #1
0
def calculate_and_save_correlation(subj_1_num, subj_2_num):
    """
    Calculates correlation using smoothed 2-D data with 8 full width half
    maximum mm, and saves values into a designated correlation_path. If a file
    with calculated correlations already exists, uses that cached version
    instead.

    Parameters
    ----------
    subj_1_num : int
    subj_2_num : int

    Returns
    -------
    None
    """
    correlation_path = dp.get_correlation_path(subj_1_num, subj_2_num)
    if not exists(correlation_path) or not USE_CACHED_DATA:
        subj_1_data = np.load(dp.get_smoothed_2d_path(subj_1_num, 8))
        subj_2_data = np.load(dp.get_smoothed_2d_path(subj_2_num, 8))
        correlations = correlation(subj_1_data, subj_2_data)
        np.save(correlation_path, correlations)
        print('Saved {0}'.format(correlation_path))
    else:
        print('Using cached version of {0}'.format(correlation_path))
def main():
    subj_num, fwhm_mm = 1, 4
    voxels_sorted_by_t_statistic = lm.VoxelExtractor(subj_num,
                                                     'int-ext').t_stat()
    design_matrix = dm.DesignMatrix(dp.get_smoothed_2d_path(subj_num, fwhm_mm))
    train_volume_indices = pv.get_train_indices()
    cv_values = []
    for num_features in NUM_FEATURES:
        voxel_feature_indices = voxels_sorted_by_t_statistic[:num_features]

        X_train = design_matrix.get_design_matrix(train_volume_indices,
                                                  voxel_feature_indices)
        y_train = np.array(design_matrix.get_labels(train_volume_indices))

        cv_accuracies = []
        for train, test in KFold(len(X_train), 5):
            X_cv_train = X_train[train, :]
            y_cv_train = y_train[train]
            X_cv_test = X_train[test, :]
            y_cv_test = y_train[test]
            model = rf.Classifier(X_cv_train, y_cv_train)
            model.train()
            y_predicted = model.predict(X_cv_test)
            cv_accuracies.append(accuracy_score(y_predicted, y_cv_test))

        print(np.mean(cv_accuracies))
        cv_values.append(np.mean(cv_accuracies))
    output_path = '{0}/figures/rf_cross_validated_accuracies.txt'.format(
        REPO_HOME_PATH)
    np.savetxt(output_path, cv_values)
    print('Saved {0}'.format(output_path))
Пример #3
0
def main():
    subj_num, fwhm_mm = 1, 4
    voxels_sorted_by_t_statistic = lm.VoxelExtractor(subj_num,
                                                     'int-ext').t_stat()
    design_matrix = dm.DesignMatrix(dp.get_smoothed_2d_path(subj_num, fwhm_mm))
    train_volume_indices = pv.get_train_indices()
    cv_values = []
    for num_features in NUM_FEATURES:
        voxel_feature_indices = voxels_sorted_by_t_statistic[:num_features]

        X_train = design_matrix.get_design_matrix(train_volume_indices,
                                                  voxel_feature_indices)
        y_train = np.array(design_matrix.get_labels(train_volume_indices))

        cv_accuracies = []
        for train, test in KFold(len(X_train), 5):
            X_cv_train = X_train[train, :]
            y_cv_train = y_train[train]
            X_cv_test = X_train[test, :]
            y_cv_test = y_train[test]
            model = rf.Classifier(X_cv_train, y_cv_train)
            model.train()
            y_predicted = model.predict(X_cv_test)
            cv_accuracies.append(accuracy_score(y_predicted, y_cv_test))

        print(np.mean(cv_accuracies))
        cv_values.append(np.mean(cv_accuracies))
    output_path = '{0}/figures/rf_cross_validated_accuracies.txt'.format(
        REPO_HOME_PATH)
    np.savetxt(output_path, cv_values)
    print('Saved {0}'.format(output_path))
Пример #4
0
    def __init__(self, subject, interest_col_str, data=None):
        """
        VoxelExtractor generates the t-statistic for each voxel based on a
        given interest column. For example, to see the voxel that can best
        predict interior or exterior, you would use the following code.

        ve = VoxelExtractor(1, 'int-ext')
        a = ve.t_stat()[0]
        ve.plot_single_voxel(0)

        interest_col_str takes in either 'day-night' or 'int-ext'.
        data can also be overrided if needed, else the smoothed 2d version
        will be used.
        """
        self.subject = subject
        if interest_col_str == "int-ext":
            self.interest_col_ind = 0
        elif interest_col_str == "day-night":
            self.interest_col_ind = 1
        else:
            raise ValueError("Incorrect interest column name: please use either 'int-ext' or 'day-night'")
        self.interest_col_str = interest_col_str
        if data is None:
            data_path = dp.get_smoothed_2d_path(self.subject, 4)
            data = np.load(data_path)
            data = data[:, NUM_OFFSET_VOLUMES:]
        self.data = data
        self.design = None
        self.B = None
        self.t_values = None
Пример #5
0
    def __init__(self, subject, interest_col_str, data=None):
        """
        VoxelExtractor generates the t-statistic for each voxel based on a
        given interest column. For example, to see the voxel that can best
        predict interior or exterior, you would use the following code.

        ve = VoxelExtractor(1, 'int-ext')
        a = ve.t_stat()[0]
        ve.plot_single_voxel(0)

        interest_col_str takes in either 'day-night' or 'int-ext'.
        data can also be overrided if needed, else the smoothed 2d version
        will be used.
        """
        self.subject = subject
        if interest_col_str == "int-ext":
            self.interest_col_ind = 0
        elif interest_col_str == "day-night":
            self.interest_col_ind = 1
        else:
            raise ValueError(
                "Incorrect interest column name: please use either 'int-ext' or 'day-night'"
            )
        self.interest_col_str = interest_col_str
        if data is None:
            data_path = dp.get_smoothed_2d_path(self.subject, 4)
            data = np.load(data_path)
            data = data[:, NUM_OFFSET_VOLUMES:]
        self.data = data
        self.design = None
        self.B = None
        self.t_values = None
Пример #6
0
def test_get_smoothed_2d_path():
	paths = [dp.get_smoothed_2d_path(subject, FWHM_MM) for subject in SUBJECTS]
	assert paths[0] == '{0}/data/processed/sub1_rcds_smoothed_8_mm_2d.npy'.format(
        REPO_HOME_PATH)
	assert paths[1] == '{0}/data/processed/sub2_rcds_smoothed_8_mm_2d.npy'.format(
        REPO_HOME_PATH)
	assert paths[2] == '{0}/data/processed/sub3_rcds_smoothed_8_mm_2d.npy'.format(
        REPO_HOME_PATH)
	assert paths[3] == '{0}/data/processed/sub4_rcds_smoothed_8_mm_2d.npy'.format(
        REPO_HOME_PATH)
	assert paths[4] == '{0}/data/processed/sub5_rcds_smoothed_8_mm_2d.npy'.format(
        REPO_HOME_PATH)
Пример #7
0
def test_get_smoothed_2d_path():
    paths = [dp.get_smoothed_2d_path(subject, FWHM_MM) for subject in SUBJECTS]
    assert paths[
        0] == '{0}/data/processed/sub1_rcds_smoothed_8_mm_2d.npy'.format(
            REPO_HOME_PATH)
    assert paths[
        1] == '{0}/data/processed/sub2_rcds_smoothed_8_mm_2d.npy'.format(
            REPO_HOME_PATH)
    assert paths[
        2] == '{0}/data/processed/sub3_rcds_smoothed_8_mm_2d.npy'.format(
            REPO_HOME_PATH)
    assert paths[
        3] == '{0}/data/processed/sub4_rcds_smoothed_8_mm_2d.npy'.format(
            REPO_HOME_PATH)
    assert paths[
        4] == '{0}/data/processed/sub5_rcds_smoothed_8_mm_2d.npy'.format(
            REPO_HOME_PATH)
Пример #8
0
def main():
    subj_num, fwhm_mm = 1, 4
    voxels_sorted_by_t_statistic = lm.VoxelExtractor(subj_num,
                                                     'int-ext').t_stat()
    design_matrix = dm.DesignMatrix(dp.get_smoothed_2d_path(subj_num, fwhm_mm))
    train_volume_indices = pv.get_train_indices()
    test_volume_indices = pv.get_test_indices()
    voxel_feature_indices = voxels_sorted_by_t_statistic[:NUM_FEATURES]
    X_train = design_matrix.get_design_matrix(train_volume_indices,
                                              voxel_feature_indices)
    y_train = np.array(design_matrix.get_labels(train_volume_indices))
    X_test = design_matrix.get_design_matrix(test_volume_indices,
                                             voxel_feature_indices)
    y_test = np.array(design_matrix.get_labels(test_volume_indices))
    model = rf.Classifier(X_train, y_train)
    model.train()
    y_predicted = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_predicted)
    print('Validation set accuracy: {0}'.format(accuracy))
    output_path = '{0}/figures/validation_accuracy.txt'.format(REPO_HOME_PATH)
    np.savetxt(output_path, np.array([accuracy]))
    print('Saved {0}'.format(output_path))
Пример #9
0
def main():
    subj_num, fwhm_mm = 1, 4
    voxels_sorted_by_t_statistic = lm.VoxelExtractor(subj_num,
                                                     'int-ext').t_stat()
    design_matrix = dm.DesignMatrix(dp.get_smoothed_2d_path(subj_num, fwhm_mm))
    train_volume_indices = pv.get_train_indices()
    test_volume_indices = pv.get_test_indices()
    voxel_feature_indices = voxels_sorted_by_t_statistic[:NUM_FEATURES]
    X_train = design_matrix.get_design_matrix(train_volume_indices,
                                              voxel_feature_indices)
    y_train = np.array(design_matrix.get_labels(train_volume_indices))
    X_test = design_matrix.get_design_matrix(test_volume_indices,
                                              voxel_feature_indices)
    y_test = np.array(design_matrix.get_labels(test_volume_indices))
    model = rf.Classifier(X_train, y_train)
    model.train()
    y_predicted = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_predicted)
    print('Validation set accuracy: {0}'.format(accuracy))
    output_path = '{0}/figures/validation_accuracy.txt'.format(
        REPO_HOME_PATH)
    np.savetxt(output_path, np.array([accuracy]))
    print('Saved {0}'.format(output_path))
    vol_rms_diff = np.sqrt(np.mean(diff_data**2, axis=0))
    return vol_rms_diff[NUM_OFFSET_VOLUMES:]


def save_plot(vol_rms_diff, subj_num):
    """
    Plots the root mean square differences for a particular subject and saves
    that plot into the figures folder

    Parameters
    ----------
    vol_rms_diff : array
    subj_num : int

    Returns
    -------
    None
    """
    plt.plot(vol_rms_diff)
    plot_path = '{0}/figures/subj{1}_vol_rms_diff.png'.format(REPO_HOME_PATH,
                                                              subj_num)
    plt.savefig(plot_path)
    print('Saved {0}'.format(plot_path))


if __name__ == '__main__':
    subj_num, fwhm_mm = 1, 8
    vol_rms_diff = calc_vol_rms_diff(dp.get_smoothed_2d_path(subj_num,
                                                             fwhm_mm))
    save_plot(vol_rms_diff, subj_num)
Пример #11
0
    vol_rms_diff = np.sqrt(np.mean(diff_data**2, axis=0))
    return vol_rms_diff[NUM_OFFSET_VOLUMES:]


def save_plot(vol_rms_diff, subj_num):
    """
    Plots the root mean square differences for a particular subject and saves
    that plot into the figures folder

    Parameters
    ----------
    vol_rms_diff : array
    subj_num : int

    Returns
    -------
    None
    """
    plt.plot(vol_rms_diff)
    plot_path = '{0}/figures/subj{1}_vol_rms_diff.png'.format(
        REPO_HOME_PATH, subj_num)
    plt.savefig(plot_path)
    print('Saved {0}'.format(plot_path))


if __name__ == '__main__':
    subj_num, fwhm_mm = 1, 8
    vol_rms_diff = calc_vol_rms_diff(dp.get_smoothed_2d_path(
        subj_num, fwhm_mm))
    save_plot(vol_rms_diff, subj_num)