Micipsa - Songsheng YING
Modified from Christophe Pallier's git which allows to perform fMRI encoding experiments with regressions.
Requirements:
- Python: pandas, nistats, nibabel, nilearn, statsmodels
- Pythonize the original
makefile
and use class methods to build regressors from onsets, design matrices from regressors. - Use config files to define model parameters and model comparison settings.
- File organization by model and modelComp.
- Use Ridge regression as the default regression model instead of GLM.
- Generates r2, MSE scores for whole models, no longer provides feature-wise contrast maps.
- Large GridSearch with shrinkage: step-wise forward feature selection & alpha.
Three class interfaces are created in lib/utils
to manage file paths (Env
), model configuration and training (Model
) and cross-model comparison (ModelComparison
). In any case of non-clarity of the following text, please refer to the raw code.
-
Clone the original repo
-
Execute the following code from the root directory to create necessary folders
from lib import utils
env = utils.Env('./', <lingua>)
-
Load event onsets (generation of semantic onsets in another repo) into
./data/onsets/<lingua>
,<lingua>
in the Micipsa project should be set tofr
. -
Load normalized fMRI data file in
./data/fmri/<lingua>/<subject>/<run.nii.gz>
-
Create model configurations in
./models/<lingua>/<model_name>
by creatingconfig.json
. Examples are available in the existing repo. -
In the configuration file,
base_regressors
will ask the model to look in onset folders with exact-name-matching onset files. E.g.rms
will point to<run_id>_rms.csv
-
embedding
files will sequentially point to a series of onset files. It contains three subfields, withdim
an integer value,name_base
serving as onset file-name filters, andregressors
which takes either a list of onset feature names, or a stringinfer
, which would look for onset file starting withname_base
and ending with an integer ranging from0
todim
. E.g. inrms-wrate-cwrate-sim100
model, a list of regressor names are passed. Inrms-wrate-cwrate-asn200
,<run_id>_<name_base><dim_range>.csv
are consulted. -
alpha
takes a list, of which members can either be a number, or a dictionary composed of three mandatory keys:start
,end
andstep
, which would be inflated into a log range. -
dimension
is similar toalpha
, but the ranges are linear. -
orthonormalize
indicates if regressor orthonormalization should be performed at the creation of design matrices. If this field is absent, the model would set the value to true by default.
- Create model python object
model = utils.Model('<model_name>', env)
- Configure model with configuration file, or by default with
config.json
in the corresponding folder
model.config_model()
- Convolute event onsets to obtain separate regressors (regressors are shared across models)
model.generate_regressors()
- Merge regressors into design matrices in the model local folder, check
model.orthonormalize
andenv.verbose
. Verbose flag would print and generate design matrix analysis files.
model.generate_design_matrices()
- Finally the regression! The code will perform 9 cross validations (as the recordings are divided into 9 blocks both in English and French experiment), leaving 1 run out as validation data and 8 as training data. Voxel models are considered as independent and a uniform brain masker will be applied to transform the 3d fMRI array into an 1d array. The regression will store run-wise validation scores with each combination of
alpha
anddimension
.
model.generate_individual_results(core_number=-1, verbose=None, output_type=['r2', 'mse', 'r'])
- Group averages can be calculated with the following code.
model.generate_group_results()
The results generated with ModelComparison
class is not used in the master's thesis.
Model comparison configuration files are located at ./model_contrasts/<lingua>/<comparison_name>
.