-
Notifications
You must be signed in to change notification settings - Fork 9
[ENH] refactor removing date entity and allow to use bids fitler to only convert certain files from source to raw #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
function convertSourceToRaw(cfg) | ||
function convertSourceToRaw(varargin) | ||
% | ||
% Function attempts to convert a source dataset created with CPP_BIDS into a valid | ||
% BIDS data set. | ||
% | ||
% | ||
% USAGE:: | ||
% | ||
% convertSourceToRaw(cfg) | ||
% convertSourceToRaw(cfg, 'filter', filter) | ||
% | ||
% :param cfg: cfg structure is needed only for providing the path in ``cfg.dir.output``. | ||
% :type cfg: structure | ||
% | ||
% :param filter: bids.query filter to only convert a subset of files. | ||
% :type filter: structure | ||
% | ||
% :output: | ||
% - :creates: a dummy README and CHANGE file | ||
% - :copies: ``source`` directory to ``raw`` directory | ||
|
@@ -19,39 +22,80 @@ function convertSourceToRaw(cfg) | |
% | ||
% (C) Copyright 2020 CPP_BIDS developers | ||
|
||
args = inputParser; | ||
|
||
default_filter = struct([]); | ||
|
||
args.addRequired('cfg', @isstruct); | ||
args.addParameter('filter', default_filter, @isstruct); | ||
|
||
args.parse(varargin{:}); | ||
|
||
cfg = args.Results.cfg; | ||
filter = args.Results.filter; | ||
|
||
sourceDir = fullfile(cfg.dir.output, 'source'); | ||
rawDir = fullfile(cfg.dir.output, 'raw'); | ||
|
||
% add dummy README and CHANGE file | ||
templateFolder = fullfile(fileparts(mfilename('fullpath')), '..', 'templates'); | ||
% back up description to not overwrite | ||
% TODO bids malab should be smart enought to not do that | ||
isFile = @(x) exist(x, 'file'); | ||
if isFile(fullfile(rawDir, 'dataset_description.json')) | ||
copyfile(fullfile(rawDir, 'dataset_description.json'), ... | ||
fullfile(rawDir, 'dataset_description_bu.json')); | ||
end | ||
|
||
copyfile(fullfile(templateFolder, 'README'), ... | ||
sourceDir); | ||
copyfile(fullfile(templateFolder, 'CHANGES'), ... | ||
sourceDir); | ||
copyfile(fullfile(templateFolder, '.bidsignore'), ... | ||
sourceDir); | ||
% trick use bids matlab copy dataset function | ||
bids.copy_to_derivative(sourceDir, ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reuse the bids matlab "copy dataset" function as it was literally made for this. |
||
'pipeline_name', '.', ... | ||
'out_path', rawDir, ... | ||
'filter', filter, ... | ||
'unzip', false, ... | ||
'force', true, ... | ||
'skip_dep', false, ... | ||
'use_schema', false, ... | ||
'verbose', true); | ||
|
||
copyfile(sourceDir, rawDir); | ||
if isFile(fullfile(rawDir, 'dataset_description_bu.json')) | ||
copyfile(fullfile(rawDir, 'dataset_description_bu.json'), ... | ||
fullfile(rawDir, 'dataset_description.json')); | ||
delete(fullfile(rawDir, 'dataset_description_bu.json')); | ||
else | ||
% clean up description | ||
description = bids.util.jsondecode(fullfile(rawDir, 'dataset_description.json')); | ||
description.BIDSVersion = '1.7.0'; | ||
description.Name = 'FIXME'; | ||
description.DatasetType = 'raw'; | ||
description = rmfield(description, 'GeneratedBy'); | ||
description = rmfield(description, 'SourceDatasets'); | ||
bids.util.jsonencode(fullfile(rawDir, 'dataset_description.json'), description); | ||
end | ||
|
||
BIDS = bids.layout(rawDir, 'use_schema', false); | ||
removeDateEntity(rawDir, 'filter', filter); | ||
|
||
data = bids.query(BIDS, 'data'); | ||
metadata = bids.query(BIDS, 'metadata'); | ||
gunzipTimeSeries(rawDir); | ||
|
||
for i = 1:size(data, 1) | ||
bf = bids.File(data{i}); | ||
if isfield(bf.entities, 'date') | ||
% TODO probably JSON renaming should be passed to bids-matlab | ||
sourceJson = fullfile(fileparts(bf.path), bf.json_filename); | ||
bf.entities.date = ''; | ||
bf.rename('dry_run', false, 'force', true); | ||
bids.util.jsonencode(fullfile(fileparts(bf.path), bf.json_filename), metadata{i}); | ||
delete(sourceJson); | ||
end | ||
% add dummy README and CHANGE file | ||
templateFolder = fullfile(fileparts(mfilename('fullpath')), '..', 'templates'); | ||
|
||
if ~isFile(fullfile(rawDir, 'README')) | ||
copyfile(fullfile(templateFolder, 'README'), ... | ||
rawDir); | ||
end | ||
if ~isFile(fullfile(rawDir, 'CHANGES')) | ||
copyfile(fullfile(templateFolder, 'CHANGES'), ... | ||
rawDir); | ||
end | ||
if ~isFile(fullfile(rawDir, '.bidsignore')) | ||
copyfile(fullfile(templateFolder, '.bidsignore'), ... | ||
rawDir); | ||
end | ||
|
||
end | ||
|
||
BIDS = bids.layout(rawDir, 'use_schema', false); | ||
function gunzipTimeSeries(pathToDataSet) | ||
|
||
BIDS = bids.layout(pathToDataSet, 'use_schema', false); | ||
data = bids.query(BIDS, 'data', 'suffix', {'stim', 'physio' }, 'ext', '.tsv'); | ||
|
||
for i = 1:size(data, 1) | ||
|
@@ -60,5 +104,4 @@ function convertSourceToRaw(cfg) | |
delete(data{i}); | ||
end | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
function removeDateEntity(varargin) | ||
% | ||
% Removes the date entity from all the files in a BIDS data set | ||
% | ||
% | ||
% USAGE:: | ||
% | ||
% removeDateEntity(pathToDataSet, 'filter', filter) | ||
% | ||
% | ||
% (C) Copyright 2022 CPP_BIDS developers | ||
Comment on lines
+1
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is now a stand alone function to remove date entities |
||
|
||
args = inputParser; | ||
|
||
default_filter = struct([]); | ||
|
||
args.addRequired('pathToDataSet', @isdir); | ||
args.addParameter('filter', default_filter, @isstruct); | ||
|
||
args.parse(varargin{:}); | ||
|
||
pathToDataSet = args.Results.pathToDataSet; | ||
filter = args.Results.filter; | ||
|
||
BIDS = bids.layout(pathToDataSet, 'use_schema', false); | ||
|
||
filter.date = '[0-9]+'; | ||
data = bids.query(BIDS, 'data', filter); | ||
metadata = bids.query(BIDS, 'metadata', filter); | ||
|
||
for i = 1:size(data, 1) | ||
bf = bids.File(data{i}); | ||
% TODO probably JSON renaming should be passed to bids-matlab | ||
sourceJson = fullfile(fileparts(bf.path), bf.json_filename); | ||
bf.entities.date = ''; | ||
bf.rename('dry_run', false, 'force', true); | ||
bids.util.jsonencode(fullfile(fileparts(bf.path), bf.json_filename), metadata{i}); | ||
delete(sourceJson); | ||
end | ||
|
||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should now be possible to use a typical bids.query fitler to only convert a subset of files (only those from
beh
or from a given subject)