Skip to content

Commit b52ab2a

Browse files
authored
Merge pull request #71 from Remi-Gau/remi-stim_file
improve creation of stim file and their conversion to raw
2 parents 53dab15 + 1bd5a4e commit b52ab2a

File tree

13 files changed

+250
-64
lines changed

13 files changed

+250
-64
lines changed

.gitignore

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
*octave-workspace
77

88
# exclude content of logfiles folders
9-
tests/output/*
10-
manualTests/output/*
11-
output/*
9+
*output*
1210
*.tsv
1311
*.mat
1412

1513
# exclude temp files from tests and coverage
16-
tests/coverage*
17-
tests/test_code_report.txt
18-
test_code_report.txt
14+
*test_code_report.txt
15+
*coverage*
16+
17+
tests/*.nii*
18+
tests/*.json*
19+
tests/*.tsv*
1920

2021
# exclude report from check_my_code
2122
check_my_code_report.txt

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
[![Build Status](https://travis-ci.com/cpp-lln-lab/CPP_BIDS.svg?branch=master)](https://travis-ci.com/cpp-lln-lab/CPP_BIDS)
44

5-
**Unit tests**
5+
**Unit tests and coverage**
66

77
[![](https://img.shields.io/badge/Octave-CI-blue?logo=Octave&logoColor=white)](https://github.com/cpp-lln-lab/CPP_BIDS/actions)
88
![](https://github.com/cpp-lln-lab/CPP_BIDS/workflows/CI/badge.svg)
99

10+
[![codecov](https://codecov.io/gh/cpp-lln-lab/CPP_BIDS/branch/master/graph/badge.svg)](https://codecov.io/gh/cpp-lln-lab/CPP_BIDS)
11+
1012
**Contributors**
1113

1214
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)
File renamed without changes.
File renamed without changes.
File renamed without changes.

manualTests/test_makeRawDataset.m

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,25 @@ function test_makeRawDataset()
6868
% close the file
6969
saveEventsFile('close', cfg, logFile);
7070

71+
% add dummy stim data
72+
stimLogFile = saveEventsFile('open_stim', cfg, logFile);
73+
for i = 1:100
74+
stimLogFile(i, 1).onset = cfg.mri.repetitionTime * i;
75+
stimLogFile(i, 1).trial_type = 'test';
76+
stimLogFile(i, 1).duration = 1;
77+
stimLogFile(i, 1).Speed = rand(1);
78+
stimLogFile(i, 1).is_Fixation = rand > 0.5;
79+
stimLogFile(i, 1).LHL24 = randn(1, 3);
80+
end
81+
saveEventsFile('save', cfg, stimLogFile);
82+
saveEventsFile('close', cfg, stimLogFile);
83+
7184
% add dummy functional data
7285
funcDir = fullfile(cfg.dir.output, 'source', 'sub-001', 'ses-001', 'func');
7386
boldFilename = 'sub-001_ses-001_task-testtask_run-001_bold.nii.gz';
7487

7588
copyfile( ...
76-
fullfile('..', 'dummyData', 'dummyData.nii.gz'), ...
89+
fullfile('dummyData', 'dummyData.nii.gz'), ...
7790
fullfile(funcDir, boldFilename));
7891

7992
%% MRI bold rest data and fancy suffixes
@@ -87,6 +100,7 @@ function test_makeRawDataset()
87100
cfg.subject.sessionNb = 3;
88101
cfg.subject.runNb = 4;
89102

103+
% deal with MRI suffixes
90104
cfg.mri.reconstruction = 'fast recon';
91105
cfg.mri.contrastEnhancement = 'test';
92106
cfg.mri.phaseEncodingDirection = 'y pos';
@@ -100,19 +114,21 @@ function test_makeRawDataset()
100114

101115
createBoldJson(cfg);
102116

103-
% add dummy functional data
117+
%% add dummy functional data
104118
funcDir = fullfile(cfg.dir.output, 'source', 'sub-002', 'ses-003', 'func');
105119
boldFilename = ['sub-002_ses-003_task-rest', ...
106120
'_acq-newTYpe_ce-test_dir-yPos_rec-fastRecon', ...
107121
'_run-004_echo-1_bold.nii.gz'];
108122

109123
copyfile( ...
110-
fullfile('..', 'dummyData', 'dummyData.nii.gz'), ...
124+
fullfile('dummyData', 'dummyData.nii.gz'), ...
111125
fullfile(funcDir, boldFilename));
112126

113-
%%
127+
%% actually do the conversion of the source data thus created
114128
clear;
129+
115130
outputDir = fullfile(fileparts(mfilename('fullpath')), 'output');
116131
cfg.dir.output = outputDir;
117132
convertSourceToRaw(cfg);
133+
118134
end

src/convertSourceToRaw.m

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ function convertSourceToRaw(cfg)
77
% - copy source dir to raw dir
88
% - remove the date suffix (_date-YYYYMMDDHHMM) from the files where it is present
99
%
10+
% Only covers func folder at the moment
1011

1112
sourceDir = fullfile(cfg.dir.output, 'source');
1213
rawDir = fullfile(cfg.dir.output, 'rawdata');
1314

1415
% add dummy README and CHANGE file
1516
copyfile(fullfile( ...
16-
fileparts(mfilename('fullpath')), '..', 'dummyData', 'README'), ...
17+
fileparts(mfilename('fullpath')), '..', 'manualTests', 'dummyData', 'README'), ...
1718
sourceDir);
1819
copyfile(fullfile( ...
19-
fileparts(mfilename('fullpath')), '..', 'dummyData', 'CHANGES'), ...
20+
fileparts(mfilename('fullpath')), '..', 'manualTests', 'dummyData', 'CHANGES'), ...
2021
sourceDir);
2122

2223
copyfile(sourceDir, rawDir);
@@ -40,43 +41,3 @@ function convertSourceToRaw(cfg)
4041
end
4142

4243
end
43-
44-
function parseFunc(rawDir, subjName, sesName)
45-
46-
subjectPath = fullfile(rawDir, subjName, sesName, 'func');
47-
48-
if exist(subjectPath, 'dir')
49-
50-
% do events
51-
filenames = file_utils('List', subjectPath, ...
52-
sprintf('^%s.*_task-.*_events_date-.*$', subjName));
53-
54-
removeDateSuffix(filenames, subjectPath);
55-
56-
% do bold
57-
filenames = file_utils('List', subjectPath, ...
58-
sprintf('^%s.*_task-.*_bold_date-.*$', subjName));
59-
60-
removeDateSuffix(filenames, subjectPath);
61-
62-
end
63-
end
64-
65-
function removeDateSuffix(filenames, subjectPath)
66-
if isempty(filenames)
67-
filenames = {};
68-
else
69-
filenames = cellstr(filenames);
70-
end
71-
72-
for i = 1:numel(filenames)
73-
74-
[~, name, ext] = fileparts(filenames{i});
75-
76-
[parts, ~] = regexp(name, '(?:_date-)+', 'split', 'match');
77-
78-
movefile(fullfile(subjectPath, filenames{i}), ...
79-
fullfile(subjectPath, [parts{1} ext]));
80-
81-
end
82-
end

src/saveEventsFile.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@
6464

6565
case 'open'
6666

67-
logFile.filename = cfg.fileName.events;
67+
logFile(1).filename = cfg.fileName.events;
6868

6969
logFile = initializeFile(cfg, logFile);
7070

7171
case 'open_stim'
7272

73-
logFile.filename = cfg.fileName.stim;
73+
logFile(1).filename = cfg.fileName.stim;
7474

7575
logFile = initializeFile(cfg, logFile);
7676

@@ -143,21 +143,22 @@
143143

144144
% Initialize txt logfiles and empty fields for the standard BIDS
145145
% event file
146-
logFile.fileID = fopen( ...
146+
logFile(1).fileID = fopen( ...
147147
fullfile( ...
148148
cfg.dir.outputSubject, ...
149149
cfg.fileName.modality, ...
150150
logFile.filename), ...
151151
'w');
152152

153153
% print the basic BIDS columns
154-
fprintf(logFile.fileID, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
154+
fprintf(logFile(1).fileID, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
155155
fprintf(1, '%s\t%s\t%s', 'onset', 'duration', 'trial_type');
156156

157157
printHeaderExtraColumns(logFile);
158158

159159
% next line so we start printing at the right place
160-
fprintf(logFile.fileID, '\n');
160+
fprintf(logFile(1).fileID, '\n');
161+
fprintf(1, '\n');
161162

162163
end
163164

@@ -174,7 +175,7 @@ function printHeaderExtraColumns(logFile)
174175

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

177-
fprintf(logFile.fileID, '\t%s', headerName);
178+
fprintf(logFile(1).fileID, '\t%s', headerName);
178179
fprintf(1, '\t%s', headerName);
179180

180181
end

src/subfun/parseFunc.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function parseFunc(rawDir, subjName, sesName)
2+
% parseFunc(rawDir, subjName, sesName)
3+
%
4+
5+
subjectPath = fullfile(rawDir, subjName, sesName, 'func');
6+
7+
if exist(subjectPath, 'dir')
8+
9+
% do events
10+
filenames = file_utils('List', subjectPath, ...
11+
sprintf('^%s.*_task-.*_events_date-.*$', subjName));
12+
13+
removeDateSuffix(filenames, subjectPath);
14+
15+
% do bold
16+
filenames = file_utils('List', subjectPath, ...
17+
sprintf('^%s.*_task-.*_bold_date-.*$', subjName));
18+
19+
removeDateSuffix(filenames, subjectPath);
20+
21+
% do stim
22+
filenames = file_utils('List', subjectPath, ...
23+
sprintf('^%s.*_task-.*_stim_date-.*tsv$', subjName));
24+
compressFiles(filenames, subjectPath);
25+
filenames = file_utils('List', subjectPath, ...
26+
sprintf('^%s.*_task-.*_stim_date-.*tsv.gz$', subjName));
27+
removeDateSuffix(filenames, subjectPath);
28+
29+
end
30+
end
31+
32+
function compressFiles(filenames, subjectPath)
33+
if isempty(filenames)
34+
filenames = {};
35+
else
36+
filenames = cellstr(filenames);
37+
end
38+
39+
for i = 1:numel(filenames)
40+
41+
gzip(fullfile(subjectPath, filenames{i}));
42+
delete(fullfile(subjectPath, filenames{i}));
43+
44+
end
45+
end

src/subfun/removeDateSuffix.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function removeDateSuffix(filenames, subjectPath)
2+
% removeDateSuffix(filenames, subjectPath)
3+
%
4+
%
5+
6+
if isempty(filenames)
7+
filenames = {};
8+
else
9+
filenames = cellstr(filenames);
10+
end
11+
12+
for i = 1:numel(filenames)
13+
14+
[~, name, ext] = fileparts(filenames{i});
15+
16+
if strcmp(ext, '.gz')
17+
[~, name, ext2] = fileparts(name);
18+
ext = [ext2, ext]; %#ok<AGROW>
19+
end
20+
21+
[parts, ~] = regexp(name, '(?:_date-)+', 'split', 'match');
22+
23+
% remove suffix file if there was one
24+
if ~strcmp(filenames{i}, [parts{1} ext])
25+
movefile(fullfile(subjectPath, filenames{i}), ...
26+
fullfile(subjectPath, [parts{1} ext]));
27+
end
28+
29+
end
30+
31+
end

0 commit comments

Comments
 (0)