Skip to content

Commit 0cda77c

Browse files
authored
Merge pull request #115 from TomasLenc/tomas-dev
enable setting column Levels properties
2 parents 435eb3d + 91b2260 commit 0cda77c

13 files changed

+268
-95
lines changed

tests/test_createDataDictionary.m renamed to manualTests/test_createDataDictionary.m

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ function test_createDataDictionaryBasic()
2525

2626
cfg = createFilename(cfg);
2727

28+
logFile.extraColumns = {'Speed', 'LHL24'};
29+
30+
logFile = saveEventsFile('init', cfg, logFile);
31+
2832
logFile.extraColumns.Speed.length = 1;
2933
logFile.extraColumns.LHL24.length = 3;
30-
logFile = saveEventsFile('init', cfg, logFile);
3134

3235
logFile = saveEventsFile('open', cfg, logFile);
3336

34-
createDataDictionary(cfg, logFile);
35-
3637
%% check that the file has the right path and name
3738

3839
% data to test against
@@ -53,16 +54,17 @@ function test_createDataDictionaryBasic()
5354
% test_createDataDictionary>test_createDataDictionaryBasic:48
5455
% (/github/workspace/tests/test_createDataDictionary.m)
5556

56-
% actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
57-
%
58-
% % data to test against
59-
% expectedStruct = bids.util.jsondecode( ...
60-
% fullfile(pwd, ...
61-
% 'testData', ...
62-
% 'eventsDataDictionary.json'));
63-
%
64-
% % test
65-
% assertTrue(isequal(expectedStruct, actualStruct));
57+
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
58+
59+
% data to test against
60+
expectedStruct = bids.util.jsondecode( ...
61+
fullfile( ...
62+
pwd, ...
63+
'testData', ...
64+
'eventsDataDictionary.json'));
65+
66+
% test
67+
assertTrue(isequal(expectedStruct, actualStruct));
6668

6769
end
6870

@@ -85,18 +87,18 @@ function test_createDataDictionaryStim()
8587

8688
cfg = createFilename(cfg);
8789

90+
stimLogFile.extraColumns = {'Speed', 'LHL24', 'is_Fixation'};
91+
92+
stimLogFile = saveEventsFile('init_stim', cfg, stimLogFile);
93+
8894
stimLogFile.extraColumns.Speed.length = 1;
8995
stimLogFile.extraColumns.LHL24.length = 3;
9096
stimLogFile.extraColumns.is_Fixation.length = 1;
9197

9298
stimLogFile.SamplingFrequency = 100;
9399
stimLogFile.StartTime = 0;
94100

95-
stimLogFile = saveEventsFile('init', cfg, stimLogFile);
96-
97-
stimLogFile = saveEventsFile('open_stim', cfg, stimLogFile);
98-
99-
createDataDictionary(cfg, stimLogFile);
101+
stimLogFile = saveEventsFile('open', cfg, stimLogFile);
100102

101103
%% check that the file has the right path and name
102104

@@ -118,4 +120,16 @@ function test_createDataDictionaryStim()
118120
% test_createDataDictionary>test_createDataDictionaryBasic:48
119121
% (/github/workspace/tests/test_createDataDictionary.m)
120122

123+
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
124+
125+
% data to test against
126+
expectedStruct = bids.util.jsondecode( ...
127+
fullfile( ...
128+
pwd, ...
129+
'testData', ...
130+
'stimDataDictionary.json'));
131+
132+
% test
133+
assertTrue(isequal(expectedStruct, actualStruct));
134+
121135
end

manualTests/test_makeRawDataset.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ function test_makeRawDataset()
2323

2424
cfg.task.name = 'testtask';
2525
cfg.task.instructions = 'do this';
26+
27+
cfg.verbosity = 0;
28+
29+
cfg = createFilename(cfg);
2630

2731
logFile.extraColumns.Speed.length = 1;
2832
logFile.extraColumns.LHL24.length = 3;
2933
logFile.extraColumns.is_Fixation.length = 1;
3034

31-
cfg = createFilename(cfg);
32-
35+
logFile = saveEventsFile('init', cfg, logFile);
36+
3337
extraInfo = struct('extraInfo', struct('nestedExtraInfo', 'something extra'));
3438
createJson(cfg, extraInfo);
3539

@@ -72,20 +76,21 @@ function test_makeRawDataset()
7276

7377
% add dummy stim data
7478
stimLogFile.extraColumns.Speed.length = 1;
75-
stimLogFile.extraColumns.LHL24.length = 3;
79+
stimLogFile.extraColumns.LHL24.length = 1;
7680
stimLogFile.extraColumns.is_Fixation.length = 1;
7781

7882
stimLogFile.SamplingFrequency = cfg.mri.repetitionTime;
7983
stimLogFile.StartTime = 0;
8084

81-
stimLogFile = saveEventsFile('open_stim', cfg, stimLogFile);
85+
stimLogFile = saveEventsFile('init_stim', cfg, stimLogFile);
86+
stimLogFile = saveEventsFile('open', cfg, stimLogFile);
8287
for i = 1:100
8388
stimLogFile(i, 1).onset = cfg.mri.repetitionTime * i;
8489
stimLogFile(i, 1).trial_type = 'test';
8590
stimLogFile(i, 1).duration = 1;
8691
stimLogFile(i, 1).Speed = rand(1);
8792
stimLogFile(i, 1).is_Fixation = rand > 0.5;
88-
stimLogFile(i, 1).LHL24 = randn(1, 3);
93+
stimLogFile(i, 1).LHL24 = randn();
8994
end
9095
saveEventsFile('save', cfg, stimLogFile);
9196
saveEventsFile('close', cfg, stimLogFile);

src/createDataDictionary.m

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,23 @@ function createDataDictionary(cfg, logFile)
1717
fileName = strrep(logFile(1).filename, '.tsv', '.json');
1818
fullFilename = getFullFilename(fileName, cfg);
1919

20-
jsonContent = setJsonContent(fullFilename, logFile);
20+
jsonContent = setJsonContent(logFile);
2121

2222
opts.Indent = ' ';
23+
2324
bids.util.jsonencode(fullFilename, jsonContent, opts);
2425

2526
end
2627

27-
function jsonContent = setJsonContent(fullFilename, logFile)
28+
function jsonContent = setJsonContent(logFile)
2829

29-
% transfer content of extra fields to json content
30-
namesExtraColumns = returnNamesExtraColumns(logFile);
30+
% regular _events file: add default _event file fields to the json content
31+
if ~isfield(logFile, 'isStim') || isempty(logFile.isStim) || ~logFile.isStim
3132

32-
% default content for events file that will be overriddent if we are dealing
33-
% with a stim file
34-
jsonContent = struct( ...
35-
'onset', struct( ...
36-
'Description', 'time elapsed since experiment start', ...
37-
'Units', 's'), ...
38-
'trial_type', struct( ...
39-
'Description', 'types of trial', ...
40-
'Levels', ''), ...
41-
'duration', struct( ...
42-
'Description', 'duration of the event', ...
43-
'Units', 's') ...
44-
);
45-
46-
if ismember('_stim', fullFilename)
33+
jsonContent = logFile.columns;
34+
35+
% _stim file: write stim-specific fields to the json content
36+
elseif logFile.isStim
4737

4838
samplingFrequency = nan;
4939
startTime = nan;
@@ -59,9 +49,11 @@ function createDataDictionary(cfg, logFile)
5949
'SamplingFrequency', samplingFrequency, ...
6050
'StartTime', startTime, ...
6151
'Columns', []);
62-
6352
end
6453

54+
% transfer content of extra fields to json content
55+
namesExtraColumns = returnNamesExtraColumns(logFile);
56+
6557
for iExtraColumn = 1:numel(namesExtraColumns)
6658

6759
nbCol = returnNbColumns(logFile, namesExtraColumns{iExtraColumn});
@@ -70,10 +62,8 @@ function createDataDictionary(cfg, logFile)
7062

7163
headerName = returnHeaderName(namesExtraColumns{iExtraColumn}, nbCol, iCol);
7264

73-
if ismember('_stim', fullFilename)
74-
65+
if isfield(logFile, 'isStim') && ~isempty(logFile.isStim) && logFile.isStim
7566
jsonContent.Columns{end + 1} = headerName;
76-
7767
end
7868

7969
jsonContent.(headerName) = ...

0 commit comments

Comments
 (0)