1
- function convertSourceToRaw(cfg )
1
+ function convertSourceToRaw(varargin )
2
2
%
3
3
% Function attempts to convert a source dataset created with CPP_BIDS into a valid
4
4
% BIDS data set.
5
5
%
6
6
%
7
7
% USAGE::
8
8
%
9
- % convertSourceToRaw(cfg)
9
+ % convertSourceToRaw(cfg, 'filter', filter )
10
10
%
11
11
% :param cfg: cfg structure is needed only for providing the path in ``cfg.dir.output``.
12
12
% :type cfg: structure
13
13
%
14
+ % :param filter: bids.query filter to only convert a subset of files.
15
+ % :type filter: structure
16
+ %
14
17
% :output:
15
18
% - :creates: a dummy README and CHANGE file
16
19
% - :copies: ``source`` directory to ``raw`` directory
@@ -19,39 +22,80 @@ function convertSourceToRaw(cfg)
19
22
%
20
23
% (C) Copyright 2020 CPP_BIDS developers
21
24
25
+ args = inputParser ;
26
+
27
+ default_filter = struct([]);
28
+
29
+ args .addRequired(' cfg' , @isstruct );
30
+ args .addParameter(' filter' , default_filter , @isstruct );
31
+
32
+ args .parse(varargin{: });
33
+
34
+ cfg = args .Results .cfg ;
35
+ filter = args .Results .filter ;
36
+
22
37
sourceDir = fullfile(cfg .dir .output , ' source' );
23
38
rawDir = fullfile(cfg .dir .output , ' raw' );
24
39
25
- % add dummy README and CHANGE file
26
- templateFolder = fullfile(fileparts(mfilename(' fullpath' )), ' ..' , ' templates' );
40
+ % back up description to not overwrite
41
+ % TODO bids malab should be smart enought to not do that
42
+ isFile = @(x ) exist(x , ' file' );
43
+ if isFile(fullfile(rawDir , ' dataset_description.json' ))
44
+ copyfile(fullfile(rawDir , ' dataset_description.json' ), ...
45
+ fullfile(rawDir , ' dataset_description_bu.json' ));
46
+ end
27
47
28
- copyfile(fullfile(templateFolder , ' README' ), ...
29
- sourceDir );
30
- copyfile(fullfile(templateFolder , ' CHANGES' ), ...
31
- sourceDir );
32
- copyfile(fullfile(templateFolder , ' .bidsignore' ), ...
33
- sourceDir );
48
+ % trick use bids matlab copy dataset function
49
+ bids .copy_to_derivative(sourceDir , ...
50
+ ' pipeline_name' , ' .' , ...
51
+ ' out_path' , rawDir , ...
52
+ ' filter' , filter , ...
53
+ ' unzip' , false , ...
54
+ ' force' , true , ...
55
+ ' skip_dep' , false , ...
56
+ ' use_schema' , false , ...
57
+ ' verbose' , true );
34
58
35
- copyfile(sourceDir , rawDir );
59
+ if isFile(fullfile(rawDir , ' dataset_description_bu.json' ))
60
+ copyfile(fullfile(rawDir , ' dataset_description_bu.json' ), ...
61
+ fullfile(rawDir , ' dataset_description.json' ));
62
+ delete(fullfile(rawDir , ' dataset_description_bu.json' ));
63
+ else
64
+ % clean up description
65
+ description = bids .util .jsondecode(fullfile(rawDir , ' dataset_description.json' ));
66
+ description.BIDSVersion = ' 1.7.0' ;
67
+ description.Name = ' FIXME' ;
68
+ description.DatasetType = ' raw' ;
69
+ description = rmfield(description , ' GeneratedBy' );
70
+ description = rmfield(description , ' SourceDatasets' );
71
+ bids .util .jsonencode(fullfile(rawDir , ' dataset_description.json' ), description );
72
+ end
36
73
37
- BIDS = bids .layout (rawDir , ' use_schema ' , false );
74
+ removeDateEntity (rawDir , ' filter ' , filter );
38
75
39
- data = bids .query(BIDS , ' data' );
40
- metadata = bids .query(BIDS , ' metadata' );
76
+ gunzipTimeSeries(rawDir );
41
77
42
- for i = 1 : size( data , 1 )
43
- bf = bids .File(data{ i } );
44
- if isfield( bf . entities , ' date ' )
45
- % TODO probably JSON renaming should be passed to bids-matlab
46
- sourceJson = fullfile(fileparts( bf . path ), bf . json_filename );
47
- bf.entities.date = ' ' ;
48
- bf .rename( ' dry_run ' , false , ' force ' , true );
49
- bids . util .jsonencode (fullfile(fileparts( bf . path ), bf . json_filename ), metadata{ i });
50
- delete( sourceJson );
51
- end
78
+ % add dummy README and CHANGE file
79
+ templateFolder = fullfile(fileparts(mfilename( ' fullpath ' )), ' .. ' , ' templates ' );
80
+
81
+ if ~isFile(fullfile( rawDir , ' README ' ))
82
+ copyfile( fullfile(templateFolder , ' README ' ), ...
83
+ rawDir ) ;
84
+ end
85
+ if ~isFile (fullfile(rawDir , ' CHANGES ' ))
86
+ copyfile(fullfile( templateFolder , ' CHANGES ' ), ...
87
+ rawDir );
52
88
end
89
+ if ~isFile(fullfile(rawDir , ' .bidsignore' ))
90
+ copyfile(fullfile(templateFolder , ' .bidsignore' ), ...
91
+ rawDir );
92
+ end
93
+
94
+ end
53
95
54
- BIDS = bids .layout(rawDir , ' use_schema' , false );
96
+ function gunzipTimeSeries(pathToDataSet )
97
+
98
+ BIDS = bids .layout(pathToDataSet , ' use_schema' , false );
55
99
data = bids .query(BIDS , ' data' , ' suffix' , {' stim' , ' physio' }, ' ext' , ' .tsv' );
56
100
57
101
for i = 1 : size(data , 1 )
@@ -60,5 +104,4 @@ function convertSourceToRaw(cfg)
60
104
delete(data{i });
61
105
end
62
106
end
63
-
64
107
end
0 commit comments