|
1 | | -% Example of how to use transformers to: |
| 1 | +% In this example we use a matlab script to generate a series of transformations |
| 2 | +% and apply them to the content of a TSV file. |
| 3 | +% |
| 4 | +% This also shows how to edit the bids stats model directly, |
| 5 | +% so you can directly "paste" the series of transformations into the JSON |
| 6 | +% of your BIDS stats model. |
| 7 | +% |
| 8 | +% --- |
| 9 | +% |
| 10 | +% The transformations include: |
2 | 11 | % |
3 | 12 | % - "merge" certain trial type by renaming them using the Replace transformer |
4 | 13 | % - "split" the trials of certain conditions |
5 | 14 | % |
6 | | -% For MVPA analyses, this can be used to have 1 beta per trial (and not 1 per run per condition). |
| 15 | +% For MVPA analyses, the latter can be used to have 1 beta per trial |
| 16 | +% (and not 1 per run per condition). |
| 17 | +% |
| 18 | +% |
7 | 19 | % |
8 | 20 | % (C) Copyright 2021 Remi Gau |
9 | 21 |
|
| 22 | +clear; |
| 23 | + |
| 24 | +% conditions_to_split = {'CONG_LEFT' |
| 25 | +% 'CONG_RIGHT' |
| 26 | +% 'INCONG_VL_PR' |
| 27 | +% 'INCONG_VR_PL' |
| 28 | +% 'P_LEFT' |
| 29 | +% 'P_RIGHT' |
| 30 | +% 'V_LEFT' |
| 31 | +% 'V_RIGHT'}; |
| 32 | + |
| 33 | +% same but using as regular expressions |
| 34 | +conditions_to_split = {'^.*LEFT$' |
| 35 | + '^.*RIGHT$' |
| 36 | + '^INCONG.*$'}; |
| 37 | + |
| 38 | +% columns headers where to store the new conditions |
| 39 | +headers = {'LEFT' |
| 40 | + 'RIGHT' |
| 41 | + 'INCONG'}; |
| 42 | + |
| 43 | +tsv_file = fullfile(pwd, 'data', 'sub-03_task-VisuoTact_run-02_events.tsv'); |
| 44 | + |
| 45 | +data = bids.util.tsvread(tsv_file); |
| 46 | + |
| 47 | +%% merge responses |
| 48 | + |
| 49 | +transformers{1}.Name = 'Replace'; |
| 50 | +transformers{1}.Input = 'trial_type'; |
| 51 | +transformers{1}.Replace = struct('key', '^RESPONSE.*', 'value', 'RESPONSE'); |
| 52 | +transformers{1}.Attribute = 'value'; |
| 53 | + |
| 54 | +%% split by trial |
| 55 | + |
| 56 | +for i = 1:numel(conditions_to_split) |
| 57 | + |
| 58 | + % create a new column where each event of a condition is labelled |
| 59 | + % creates a "tmp" and "label" columns that are deleted after. |
| 60 | + transformers{end + 1}.Name = 'Filter'; %#ok<*SAGROW> |
| 61 | + transformers{end}.Input = 'trial_type'; |
| 62 | + transformers{end}.Query = ['trial_type==' conditions_to_split{i}]; |
| 63 | + transformers{end}.Output = 'tmp'; |
| 64 | + |
| 65 | + transformers{end + 1}.Name = 'LabelIdenticalRows'; |
| 66 | + transformers{end}.Cumulative = true; |
| 67 | + transformers{end}.Input = {'tmp'}; |
| 68 | + transformers{end}.Output = {'label'}; |
| 69 | + |
| 70 | + transformers{end + 1}.Name = 'Concatenate'; |
| 71 | + transformers{end}.Input = {'tmp', 'label'}; |
| 72 | + transformers{end}.Output = headers(i); |
| 73 | + |
| 74 | + % clean up |
| 75 | + % insert actual NaN |
| 76 | + transformers{end + 1}.Name = 'Replace'; |
| 77 | + transformers{end}.Input = headers(i); |
| 78 | + transformers{end}.Replace = struct('key', '^NaN.*', 'value', 'n/a'); |
| 79 | + transformers{end}.Attribute = 'value'; |
| 80 | + |
| 81 | + % remove temporary columns |
| 82 | + transformers{end + 1}.Name = 'Delete'; |
| 83 | + transformers{end}.Input = {'tmp', 'label'}; |
| 84 | + |
| 85 | +end |
| 86 | + |
| 87 | +[new_content, json] = bids.transformers(transformers, data); |
| 88 | + |
| 89 | +% save the new TSV for inspection to make sure it looks like what we expect |
| 90 | +bids.util.tsvwrite(fullfile(pwd, 'new_events.tsv'), new_content); |
| 91 | + |
| 92 | +% generate the transformation section that can be added to the bids stats model |
| 93 | +bids.util.jsonencode(fullfile(pwd, 'transformers.json'), json); |
| 94 | + |
| 95 | +%% update the BIDS stats model |
| 96 | + |
| 97 | +model_file = fullfile(pwd, 'models', 'model-VisuoTact_smdl.json'); |
| 98 | + |
| 99 | +bm = BidsModel('file', model_file); |
| 100 | +bm.Nodes{1}.Transformations.Instructions = transformers; |
| 101 | +bm.write(model_file); |
| 102 | + |
| 103 | +%% generate the mat file that SPM will use to specify the model sor the run 2 of subject 3 |
| 104 | + |
10 | 105 | tsvFile = fullfile(pwd, 'data', 'sub-03_task-VisuoTact_run-02_events.tsv'); |
11 | 106 |
|
12 | | -opt.model.file = fullfile(pwd, 'models', 'model-VisuoTact_smdl.json'); |
| 107 | +opt.model.file = model_file; |
13 | 108 | opt.verbosity = 2; |
14 | 109 | opt.glm.useDummyRegressor = false; |
15 | 110 |
|
|
0 commit comments