Example #1
0
 def new_SpaceNet(self):
     if self.task == "classification":
         decoder = SpaceNetClassifier(memory="nilearn_cache",
                                      penalty=self.penalty,
                                      screening_percentile=15.,
                                      memory_level=1)
     else:
         decoder = SpaceNetRegressor(memory="nilearn_cache",
                                     penalty=self.penalty,
                                     screening_percentile=15.,
                                     memory_level=1)
     return decoder
Example #2
0
from nilearn.image import index_img
niimgs = nibabel.load(data_files.func[0])
X_train = index_img(niimgs, condition_mask_train)
X_test = index_img(niimgs, condition_mask_test)
y_train = target[condition_mask_train]
y_test = target[condition_mask_test]

### Loop over Graph-Net and TV-L1 penalties ##################################
from nilearn.decoding import SpaceNetClassifier
import matplotlib.pyplot as plt
from nilearn.image import mean_img
from nilearn.plotting import plot_stat_map
background_img = mean_img(data_files.func[0])
for penalty in ['graph-net', 'tv-l1']:
    ### Fit model on train data and predict on test data ######################
    decoder = SpaceNetClassifier(memory="cache", penalty=penalty)
    decoder.fit(X_train, y_train)
    y_pred = decoder.predict(X_test)
    accuracy = (y_pred == y_test).mean() * 100.

    ### Visualization #########################################################
    print("Results")
    print("=" * 80)
    coef_img = decoder.coef_img_
    plot_stat_map(coef_img,
                  background_img,
                  title="%s: accuracy %g%%" % (penalty, accuracy),
                  cut_coords=(-34, -16),
                  display_mode="yz")
    coef_img.to_filename('haxby_%s_weights.nii' % penalty)
    print("- %s %s" % (penalty, '-' * 60))
Example #3
0
y_test = conditions[condition_mask_test]

X_test.shape

# Compute the mean epi to be used for the background of the plotting
from nilearn.image import mean_img
background_img = mean_img(dataset)

## SET & RUN CLASSIFIER - SPACENET WITH GRAPHNET

from nilearn.decoding import SpaceNetClassifier
# Fit model on train data and predict on test data
decoder = SpaceNetClassifier(
    memory_level=1,
    n_jobs=4,
    memory=
    '/projects/niblab/bids_projects/Experiments/ChocoData/derivatives/code/decoding',
    mask=imag_mask,
    standardize=True,
    penalty='graph-net')
decoder.fit(X_train, y_train)

y_pred = decoder.predict(X_test)
accuracy = (y_pred == y_test).mean() * 100.
print("Graph-net classification accuracy : %g%%" % accuracy)

## VISUALIZE AND SAVE RESULTS

from nilearn.plotting import plot_stat_map, show
coef_img = decoder.coef_img_
display = plot_stat_map(coef_img,
                        background_img,
Example #4
0
# Split data into train and test samples, using the chunks
condition_mask_train = (condition_mask) & (behavioral['sess'] == 0)
condition_mask_test = (condition_mask) & (behavioral['sess'] == 1)
# Apply this sample mask to X (fMRI data) and y (behavioral labels)
# Because the data is in one single large 4D image, we need to use
# index_img to do the split easily
X_train = index_img(dataset, condition_mask_train)
X_test = index_img(dataset, condition_mask_test)
y_train = y[condition_mask_train]
y_test = y[condition_mask_test]

X_test.shape

# Compute the mean epi to be used for the background of the plotting
from nilearn.image import mean_img

background_img = mean_img(dataset)

## SET & RUN CLASSIFIER - SPACENET WITH GRAPHNET

from nilearn.decoding import SpaceNetClassifier
# Fit model on train data and predict on test data
decoder = SpaceNetClassifier(
    memory_level=2,
    n_jobs=16,
    memory=
    '/projects/niblab/bids_projects/Experiments/ChocoData/derivatives\/code/decoding',
    mask=imag_mask,
    standardize=True,
    penalty='graph-net')
decoder.fit(X_train, y_train)
X_train = index_img(func_filenames, condition_mask_train)
X_test = index_img(func_filenames, condition_mask_test)
y_train = target[condition_mask_train]
y_test = target[condition_mask_test]

# Compute the mean epi to be used for the background of the plotting
from nilearn.image import mean_img

background_img = mean_img(func_filenames)

##############################################################################
# Fit SpaceNet with a Graph-Net penalty
from nilearn.decoding import SpaceNetClassifier

# Fit model on train data and predict on test data
decoder = SpaceNetClassifier(memory="nilearn_cache", penalty="graph-net")
decoder.fit(X_train, y_train)
y_pred = decoder.predict(X_test)
accuracy = (y_pred == y_test).mean() * 100.0
print("Graph-net classification accuracy : %g%%" % accuracy)

# Visualization
from nilearn.plotting import plot_stat_map, show

coef_img = decoder.coef_img_
plot_stat_map(
    coef_img, background_img, title="graph-net: accuracy %g%%" % accuracy, cut_coords=(-34, -16), display_mode="yz"
)

# Save the coefficients to a nifti file
coef_img.to_filename("haxby_graph-net_weights.nii")
Example #6
0
X_train = index_img(func_filenames, condition_mask_train)
X_test = index_img(func_filenames, condition_mask_test)
y_train = target[condition_mask_train]
y_test = target[condition_mask_test]

# Compute the mean epi to be used for the background of the plotting
from nilearn.image import mean_img

background_img = mean_img(func_filenames)

##############################################################################
# Fit SpaceNet with a Graph-Net penalty
from nilearn.decoding import SpaceNetClassifier

# Fit model on train data and predict on test data
decoder = SpaceNetClassifier(memory="nilearn_cache", penalty='graph-net')
decoder.fit(X_train, y_train)
y_pred = decoder.predict(X_test)
accuracy = (y_pred == y_test).mean() * 100.
print("Graph-net classification accuracy : %g%%" % accuracy)

# Visualization
from nilearn.plotting import plot_stat_map, show

coef_img = decoder.coef_img_
plot_stat_map(coef_img,
              background_img,
              title="graph-net: accuracy %g%%" % accuracy,
              cut_coords=(-34, -16),
              display_mode="yz")
X_test = index_img(func_filenames, condition_mask_test)
y_train = target[condition_mask_train]
y_test = target[condition_mask_test]

# Compute the mean epi to be used for the background of the plotting
from nilearn.image import mean_img
background_img = mean_img(func_filenames)
background_img.to_filename("bg.nii.gz")

##############################################################################
# Fit SpaceNet with a Graph-Net penalty
from nilearn.decoding import SpaceNetClassifier

# Fit model on train data and predict on test data
decoder = SpaceNetClassifier(memory="nilearn_cache",
                             penalty='graph-net',
                             memory_level=2,
                             screening_percentile=20.)
decoder.fit(X_train, y_train)
y_pred = decoder.predict(X_test)
accuracy = (y_pred == y_test).mean() * 100.
print("Graph-net classification accuracy : %g%%" % accuracy)

# Visualization
from nilearn.plotting import plot_stat_map, show
coef_img = decoder.coef_img_
plot_stat_map(coef_img,
              background_img,
              title="graph-net: accuracy %g%%" % accuracy,
              cut_coords=(-34, -16),
              display_mode="yz")