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
makefileand 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_regressorswill ask the model to look in onset folders with exact-name-matching onset files. E.g.rmswill point to<run_id>_rms.csv -
embeddingfiles will sequentially point to a series of onset files. It contains three subfields, withdiman integer value,name_baseserving as onset file-name filters, andregressorswhich takes either a list of onset feature names, or a stringinfer, which would look for onset file starting withname_baseand ending with an integer ranging from0todim. E.g. inrms-wrate-cwrate-sim100model, a list of regressor names are passed. Inrms-wrate-cwrate-asn200,<run_id>_<name_base><dim_range>.csvare consulted. -
alphatakes a list, of which members can either be a number, or a dictionary composed of three mandatory keys:start,endandstep, which would be inflated into a log range. -
dimensionis similar toalpha, but the ranges are linear. -
orthonormalizeindicates 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.jsonin 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.orthonormalizeandenv.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
alphaanddimension.
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>.