From c40405dce6920323da6436a3928aa83872e0e465 Mon Sep 17 00:00:00 2001 From: Alon Daks Date: Sun, 13 Dec 2015 01:37:38 -0800 Subject: [PATCH] updated rf_cross_validate to work with linear_modeling APIs --- .../random_forest/rf_cross_validate.py | 4 +-- .../linear_modeling/linear_modeling.py | 27 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/code/stat159lambda/classification/random_forest/rf_cross_validate.py b/code/stat159lambda/classification/random_forest/rf_cross_validate.py index 99d1343..b28f793 100644 --- a/code/stat159lambda/classification/random_forest/rf_cross_validate.py +++ b/code/stat159lambda/classification/random_forest/rf_cross_validate.py @@ -10,7 +10,7 @@ from stat159lambda.utils import data_path as dp -voxels_sorted_by_t_statistic = lm.get_voxels_by_t_statistic('int-ext', 1) +voxels_sorted_by_t_statistic = lm.VoxelExtractor(1, 'int-ext').t_stat() num_features_values = range(100, NUM_VOXELS/100, 100) for num_features in num_features_values: voxel_feature_indices = voxels_sorted_by_t_statistic[:num_features_values] @@ -31,5 +31,5 @@ y_predicted = model.predict(X_cv_test) cv_accuracies.append(accuracy_score(y_predicted, y_cv_test)) - print np.mean(cv_accuracies) + print(np.mean(cv_accuracies)) diff --git a/code/stat159lambda/linear_modeling/linear_modeling.py b/code/stat159lambda/linear_modeling/linear_modeling.py index 0f48688..ddd4e2d 100644 --- a/code/stat159lambda/linear_modeling/linear_modeling.py +++ b/code/stat159lambda/linear_modeling/linear_modeling.py @@ -10,7 +10,6 @@ class VoxelExtractor: - def __init__(self, subject, interest_col_str, data=None): """ VoxelExtractor generates the t-statistic for each voxel based on a @@ -28,11 +27,10 @@ def __init__(self, subject, interest_col_str, data=None): self.subject = subject self.interest_col_str = interest_col_str if not data: - data_path = dp.get_smoothed_path_2d(self.subject, 4) - data = nib.load(data_path).get_data() - data = data[:, :, :, NUM_OFFSET_VOLUMES:] - # Data is shaped as number of voxels by time - self.data = np.reshape(data, (-1, data.shape[-1])) + 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 @@ -51,7 +49,8 @@ def get_design_matrix(self): elif self.interest_col_str == "day-night": interest_col_ind = 0 else: - print("Incorrect interest column name: please use either 'int-ext' or 'day-night'") + print( + "Incorrect interest column name: please use either 'int-ext' or 'day-night'") interest_col = ss.get_scene_slices()[interest_col_ind] n_trs = self.data.shape[-1] design = np.ones((n_trs, 3)) @@ -70,9 +69,13 @@ def plot_design_matrix(self): if self.design is None: self.get_design_matrix() design_fig = plt.gcf() - plt.imshow(self.design, aspect=0.1, cmap='gray', interpolation='nearest') + plt.imshow(self.design, + aspect=0.1, + cmap='gray', + interpolation='nearest') plt.xticks([]) - design_fig_path = '{0}/figures/design_fig_{1}.png'.format(REPO_HOME_PATH, self.interest_col_str) + design_fig_path = '{0}/figures/design_fig_{1}.png'.format( + REPO_HOME_PATH, self.interest_col_str) design_fig.savefig(design_fig_path, dpi=100) plt.clf() @@ -112,7 +115,8 @@ def t_stat(self): SE[SE == 0] = np.amin(SE[SE != 0]) t = c.T.dot(beta) / SE self.t_values = abs(t[0]) - self.t_indices = np.array(self.t_values).argsort()[::-1][:self.t_values.size] + self.t_indices = np.array(self.t_values).argsort( + )[::-1][:self.t_values.size] return self.t_indices def plot_single_voxel(self, voxel_index): @@ -125,6 +129,7 @@ def plot_single_voxel(self, voxel_index): """ voxel_img = plt.gcf() plt.plot(self.data[voxel_index, :]) - voxel_img_path = '{0}/figures/voxel_{1}.png'.format(REPO_HOME_PATH, voxel_index) + voxel_img_path = '{0}/figures/voxel_{1}.png'.format(REPO_HOME_PATH, + voxel_index) voxel_img.savefig(voxel_img_path, dpi=100) plt.clf()