Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ function test_createDataDictionaryBasic()

cfg = createFilename(cfg);

logFile.extraColumns = {'Speed', 'LHL24'};

logFile = saveEventsFile('init', cfg, logFile);

logFile.extraColumns.Speed.length = 1;
logFile.extraColumns.LHL24.length = 3;
logFile = saveEventsFile('init', cfg, logFile);

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

createDataDictionary(cfg, logFile);

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

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

% actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));
%
% % data to test against
% expectedStruct = bids.util.jsondecode( ...
% fullfile(pwd, ...
% 'testData', ...
% 'eventsDataDictionary.json'));
%
% % test
% assertTrue(isequal(expectedStruct, actualStruct));
actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));

% data to test against
expectedStruct = bids.util.jsondecode( ...
fullfile( ...
pwd, ...
'testData', ...
'eventsDataDictionary.json'));

% test
assertTrue(isequal(expectedStruct, actualStruct));

end

Expand All @@ -85,18 +87,18 @@ function test_createDataDictionaryStim()

cfg = createFilename(cfg);

stimLogFile.extraColumns = {'Speed', 'LHL24', 'is_Fixation'};

stimLogFile = saveEventsFile('init_stim', cfg, stimLogFile);

stimLogFile.extraColumns.Speed.length = 1;
stimLogFile.extraColumns.LHL24.length = 3;
stimLogFile.extraColumns.is_Fixation.length = 1;

stimLogFile.SamplingFrequency = 100;
stimLogFile.StartTime = 0;

stimLogFile = saveEventsFile('init', cfg, stimLogFile);

stimLogFile = saveEventsFile('open_stim', cfg, stimLogFile);

createDataDictionary(cfg, stimLogFile);
stimLogFile = saveEventsFile('open', cfg, stimLogFile);

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

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

actualStruct = bids.util.jsondecode(fullfile(funcDir, jsonFilename));

% data to test against
expectedStruct = bids.util.jsondecode( ...
fullfile( ...
pwd, ...
'testData', ...
'stimDataDictionary.json'));

% test
assertTrue(isequal(expectedStruct, actualStruct));

end
15 changes: 10 additions & 5 deletions manualTests/test_makeRawDataset.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ function test_makeRawDataset()

cfg.task.name = 'testtask';
cfg.task.instructions = 'do this';

cfg.verbosity = 0;

cfg = createFilename(cfg);

logFile.extraColumns.Speed.length = 1;
logFile.extraColumns.LHL24.length = 3;
logFile.extraColumns.is_Fixation.length = 1;

cfg = createFilename(cfg);

logFile = saveEventsFile('init', cfg, logFile);
extraInfo = struct('extraInfo', struct('nestedExtraInfo', 'something extra'));
createJson(cfg, extraInfo);

Expand Down Expand Up @@ -72,20 +76,21 @@ function test_makeRawDataset()

% add dummy stim data
stimLogFile.extraColumns.Speed.length = 1;
stimLogFile.extraColumns.LHL24.length = 3;
stimLogFile.extraColumns.LHL24.length = 1;
stimLogFile.extraColumns.is_Fixation.length = 1;

stimLogFile.SamplingFrequency = cfg.mri.repetitionTime;
stimLogFile.StartTime = 0;

stimLogFile = saveEventsFile('open_stim', cfg, stimLogFile);
stimLogFile = saveEventsFile('init_stim', cfg, stimLogFile);
stimLogFile = saveEventsFile('open', cfg, stimLogFile);
for i = 1:100
stimLogFile(i, 1).onset = cfg.mri.repetitionTime * i;
stimLogFile(i, 1).trial_type = 'test';
stimLogFile(i, 1).duration = 1;
stimLogFile(i, 1).Speed = rand(1);
stimLogFile(i, 1).is_Fixation = rand > 0.5;
stimLogFile(i, 1).LHL24 = randn(1, 3);
stimLogFile(i, 1).LHL24 = randn();
end
saveEventsFile('save', cfg, stimLogFile);
saveEventsFile('close', cfg, stimLogFile);
Expand Down
36 changes: 13 additions & 23 deletions src/createDataDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,23 @@ function createDataDictionary(cfg, logFile)
fileName = strrep(logFile(1).filename, '.tsv', '.json');
fullFilename = getFullFilename(fileName, cfg);

jsonContent = setJsonContent(fullFilename, logFile);
jsonContent = setJsonContent(logFile);

opts.Indent = ' ';

bids.util.jsonencode(fullFilename, jsonContent, opts);

end

function jsonContent = setJsonContent(fullFilename, logFile)
function jsonContent = setJsonContent(logFile)

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

% default content for events file that will be overriddent if we are dealing
% with a stim file
jsonContent = struct( ...
'onset', struct( ...
'Description', 'time elapsed since experiment start', ...
'Units', 's'), ...
'trial_type', struct( ...
'Description', 'types of trial', ...
'Levels', ''), ...
'duration', struct( ...
'Description', 'duration of the event', ...
'Units', 's') ...
);

if ismember('_stim', fullFilename)
jsonContent = logFile.columns;

% _stim file: write stim-specific fields to the json content
elseif logFile.isStim

samplingFrequency = nan;
startTime = nan;
Expand All @@ -59,9 +49,11 @@ function createDataDictionary(cfg, logFile)
'SamplingFrequency', samplingFrequency, ...
'StartTime', startTime, ...
'Columns', []);

end

% transfer content of extra fields to json content
namesExtraColumns = returnNamesExtraColumns(logFile);

for iExtraColumn = 1:numel(namesExtraColumns)

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

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

if ismember('_stim', fullFilename)

if isfield(logFile, 'isStim') && ~isempty(logFile.isStim) && logFile.isStim
jsonContent.Columns{end + 1} = headerName;

end

jsonContent.(headerName) = ...
Expand Down
Loading