Skip to content

Commit

Permalink
updated rf_cross_validate to work with linear_modeling APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonDaks committed Dec 13, 2015
1 parent 3b1885a commit c40405d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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))

27 changes: 16 additions & 11 deletions code/stat159lambda/linear_modeling/linear_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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))
Expand All @@ -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()

Expand Down Expand Up @@ -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):
Expand All @@ -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()

0 comments on commit c40405d

Please sign in to comment.