-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SpaceNet (this PR succeeds PR #219) #657
Changes from all commits
8ca044a
65f5caf
4c2840c
1533924
c006de5
3063b37
f66bc33
c9cf969
dea6589
da010e2
a259fb0
6dcb129
eaf476d
5bdfee4
e6d10ab
4207eb7
fdc2117
fa8975e
275587c
0327958
afbfaa0
9b7d4af
1b306ac
f29ef58
dd4fde0
6c54bed
5a5107b
ad5938a
659c5d9
4a0dec5
a61efdf
1d4ba7b
0bda401
deebcaf
ce08d7a
5f5c968
04fd579
50d8458
3ca7ba2
eb6690e
2363178
a5ebb09
bb7e03e
fbcf0fa
c8eb247
0e9f2dd
05ce211
5981826
bb7e8cd
9bc1837
9c4bccf
fecfbfd
6fcc0b9
0905b98
8b5630b
545ca52
19474a7
d638e8e
fc1d50b
1385ab9
41f6915
bb00562
831040d
5b99a52
0182bbe
a384116
51984e1
17a8466
90313b7
5903f8b
1b8587a
6282dc0
32b4275
4b30122
def76ac
6c3cc84
747540f
9e51f12
ea4045c
6d68dbd
d307121
ad7f7bf
f3acb9a
2680fd0
4ca7aa1
90c34ff
f7c092d
dcdd18e
138411f
44326c2
d89536b
645517f
b7c72fc
47b22c2
37e90f7
e395757
4f7f446
0cc8ba7
a3d4242
519406a
2616629
f283f1d
1f47067
44795e5
22b00ef
25cea37
986aacd
9753492
9df0afd
cd26b23
29077e1
a636fd5
275f81c
589ee74
a005c33
acdc374
2d74e04
5a86e89
ca36a35
55fd082
9cbdb10
8cc69f8
b259caa
d182c29
de2ea32
27bc450
ec60eee
04075a4
adb15b7
a9e3899
fea6b04
172a30e
82772ea
db317a9
49032f7
606b340
f8a7309
b555aa1
eb48480
c943318
a72cc97
afd79da
abd843b
770a427
dc108dc
fd2c674
421adad
90de959
d048f21
05f62f7
6a36e3e
7b0a810
d6cc057
1d8e5f5
a54ff62
819c85c
af044d4
a253f2b
a94c6aa
1c6b70f
092772a
f8d50f4
edf05ab
98c5981
8abf120
8e45d53
a829be4
0b73aec
5f55713
92d44cc
33cb60a
a2c6484
2339440
acf88bb
0107e51
5736628
715e21b
fe3b8b2
61986ab
f37b578
e0ed463
3db00fb
39df752
fefa61d
e35f7e1
0447753
fcf6bed
07e4d48
f89fb15
b2678cc
30c06ef
ba96096
97c5c56
e06d2ee
ac6d4c6
b96fb08
28b7d96
686bf86
68ab565
0416112
313e7c3
f1dcb45
aed4893
2499f81
dfccea4
3b76060
57f18e5
a47d485
8a443cb
1f9a30f
5e7300b
01c4b91
d4dcba1
0e9c5fc
71d8399
8391e2b
b761f88
4cbe10c
c8798dd
1abf7dc
c3a6afc
293aa72
5bc8125
26c40f7
4bf8d75
4300da3
f9bc35d
b8edb47
93d67a1
d0452ba
1ef334c
a61211e
6c8adcc
0ba41b7
e870fb6
7577546
2352791
3cbaee3
6e52349
6f4142c
3821dab
6ec3d1a
14d08fe
99e1bc1
4f5b683
11c7492
37fd0a5
4158665
74923aa
3efa3c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
*.swo | ||
.DS_Store | ||
build | ||
#* | ||
*# | ||
|
||
nilearn.egg-info/ | ||
dist/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
.. for doctests to run, we need to define variables that are define in | ||
the literal includes | ||
>>> import numpy as np | ||
>>> from sklearn import datasets | ||
>>> iris = datasets.load_iris() | ||
>>> fmri_masked = iris.data | ||
>>> target = iris.target | ||
>>> session = np.ones_like(target) | ||
>>> n_samples = len(target) | ||
|
||
.. _space_net: | ||
|
||
===================================== | ||
Multivariate decoding with SpaceNet | ||
===================================== | ||
|
||
The SpaceNet decoder | ||
-------------------- | ||
SpaceNet implements a suite of multi-variate priors which for improved | ||
brain decoding. It uses priors like TV (Total Variation) `[Michel et | ||
al. 2011] <https://hal.inria.fr/inria-00563468/document>`_, TV-L1 | ||
`[Baldassarre et al. 2012] | ||
<http://www0.cs.ucl.ac.uk/staff/M.Pontil/reading/neurosparse_prni.pdf>`_, | ||
`[Gramfort et al. 2013] <https://hal.inria.fr/hal-00839984>`_ | ||
(option: penalty="tvl1"), and Graph-Net `[Hebiri et al. 2011] | ||
<https://hal.archives-ouvertes.fr/hal-00462882/document>`_ (known | ||
as GraphNet in neuroimaging `[Grosenick et al. 2013] | ||
<https://hal.inria.fr/hal-00839984>`_) (option: | ||
penalty="smooth-lasso") to regularize classification and regression | ||
problems in brain imaging. The result are brain maps which are both | ||
sparse (i.e regression coefficients are zero everywhere, except at | ||
predictive voxels) and structured (blobby). The superiority of TV-L1 | ||
over methods without structured priors like the Lasso, SVM, ANOVA, | ||
Ridge, etc. for yielding more interpretable maps and improved | ||
prediction scores is now well established `[Baldassarre et al. 2012] | ||
<http://www0.cs.ucl.ac.uk/staff/M.Pontil/reading/neurosparse_prni.pdf>`_, | ||
`[Gramfort et al. 2013] <https://hal.inria.fr/hal-00839984>`_, | ||
`[Grosenick et al. 2013] <https://hal.inria.fr/hal-00839984>`_. | ||
|
||
|
||
The following table summarizes the parameter(s) used to activate a | ||
given penalty: | ||
|
||
- TV-L1: `penalty="tv-l1"` | ||
- Graph-Net: `penalty="smooth-lasso"` (this is the default prior in | ||
SpaceNet) | ||
|
||
Note that TV-L1 prior leads to a hard optimization problem, and so can | ||
be slow to run. Under the hood, a few heuristics are used to make | ||
things a bit faster. These include: | ||
|
||
- Feature preprocessing, where an F-test is used to eliminate | ||
non-predictive voxels, thus reducting the size of the brain mask in | ||
a principled way. | ||
- Continuation is used along the regularization path, where the | ||
solution of the optimization problem for a given value of the | ||
regularization parameter `alpha` is used as initialization | ||
of for next the regularization (smaller) value on the regularization | ||
grid. | ||
|
||
**Implementation:** See `[Dohmatob et al. 2015 (PRNI)] | ||
<https://hal.inria.fr/hal-01147731>`_ and `[Dohmatob | ||
et al. 2014 (PRNI)] <https://hal.inria.fr/hal-00991743>`_ for | ||
technical details regarding the implementation of SpaceNet. | ||
|
||
Mixed gambles | ||
............. | ||
|
||
.. figure:: ../auto_examples/decoding/images/plot_mixed_gambles_space_net_001.png | ||
:align: right | ||
:scale: 60 | ||
|
||
.. figure:: ../auto_examples/decoding/images/plot_mixed_gambles_space_net_002.png | ||
:scale: 60 | ||
|
||
.. topic:: **Code** | ||
|
||
The complete script can be found | ||
:ref:`here <example_decoding_plot_mixed_gambles_space_net.py>`. | ||
|
||
|
||
Haxby | ||
..... | ||
|
||
.. figure:: ../auto_examples/decoding/images/plot_haxby_space_net_001.png | ||
:align: right | ||
:scale: 60 | ||
|
||
.. figure:: ../auto_examples/decoding/images/plot_haxby_space_net_002.png | ||
:scale: 60 | ||
|
||
.. topic:: **Code** | ||
|
||
The complete script can be found | ||
:ref:`here <example_decoding_plot_haxby_space_net.py>`. | ||
|
||
.. seealso:: | ||
|
||
* :ref:`Age prediction on OASIS dataset with SpaceNet <example_decoding_plot_oasis_vbm_space_net.py>`. | ||
|
||
* The `scikit-learn documentation <http://scikit-learn.org>`_ | ||
has very detailed explanations on a large variety of estimators and | ||
machine learning techniques. To become better at decoding, you need | ||
to study it. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
Decoding with SpaceNet: face vs house object recognition | ||
========================================================= | ||
|
||
Here is a simple example of decoding with a SpaceNet prior (i.e Graph-Net, | ||
TV-l1, etc.), reproducing the Haxby 2001 study on a face vs house | ||
discrimination task. | ||
""" | ||
|
||
### Load Haxby dataset ######################################################## | ||
from nilearn.datasets import fetch_haxby | ||
data_files = fetch_haxby() | ||
|
||
### Load Target labels ######################################################## | ||
import numpy as np | ||
labels = np.recfromcsv(data_files.session_target[0], delimiter=" ") | ||
|
||
|
||
### Split data into train and test samples #################################### | ||
target = labels['labels'] | ||
condition_mask = np.logical_or(target == "face", target == "house") | ||
condition_mask_train = np.logical_and(condition_mask, labels['chunks'] <= 6) | ||
condition_mask_test = np.logical_and(condition_mask, labels['chunks'] > 6) | ||
|
||
### make X (design matrix) and y (response variable) | ||
import nibabel | ||
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)) | ||
print("Number of train samples : %i" % condition_mask_train.sum()) | ||
print("Number of test samples : %i" % condition_mask_test.sum()) | ||
print("Classification accuracy : %g%%" % accuracy) | ||
print("_" * 80) | ||
|
||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
SpaceNet on Jimura et al "mixed gambles" dataset. | ||
================================================== | ||
|
||
The segmenting power of SpaceNet is quite visible here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain here that you are using the regressor object given that the task is to predict a continuous variable, the gain of the gamble. Also, maybe we should leave out some data and show a prediction power (if it somewhat works). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Tue, Jul 28, 2015 at 5:22 PM, Gael Varoquaux notifications@github.com
DED There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise, "it's like simulated annealing, without decreasing the temperature" -source: probably @GaelVaroquaux There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like @eickenberg to my ear ;) |
||
""" | ||
# author: DOHMATOB Elvis Dopgima, | ||
# GRAMFORT Alexandre | ||
|
||
|
||
### Load data ################################################################ | ||
import numpy as np | ||
import nibabel | ||
from scipy import ndimage | ||
from nilearn.datasets import fetch_mixed_gambles | ||
data = fetch_mixed_gambles(n_subjects=16) | ||
zmaps, object_category, mask_img = data.zmaps, data.gain, data.mask_img | ||
|
||
|
||
### Fit TV-L1 ################################################################# | ||
# Here we're using the regressor object given that the task is to predict a | ||
# continuous variable, the gain of the gamble. | ||
from nilearn.decoding import SpaceNetRegressor | ||
decoder = SpaceNetRegressor(mask=mask_img, penalty="tv-l1", | ||
eps=1e-1, # prefer large alphas | ||
memory="cache") | ||
decoder.fit(zmaps, object_category) # fit | ||
|
||
### Visualize TV-L1 weights | ||
import matplotlib.pyplot as plt | ||
from nilearn.plotting import plot_stat_map | ||
plot_stat_map(decoder.coef_img_, title="tv-l1", display_mode="yz", | ||
cut_coords=[20, -2]) | ||
|
||
|
||
### Fit Graph-Net ########################################################## | ||
decoder = SpaceNetRegressor(mask=mask_img, penalty="graph-net", | ||
eps=1e-1, # prefer large alphas | ||
memory="cache") | ||
decoder.fit(zmaps, object_category) # fit | ||
|
||
### Visualize Graph-Net weights | ||
plot_stat_map(decoder.coef_img_, title="graph-net", | ||
display_mode="yz", cut_coords=[20, -2]) | ||
|
||
|
||
plt.show() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reducing