-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from Remi-Gau/remi-implement_schema
[ENH] implement bids-schema - part 1
- Loading branch information
Showing
44 changed files
with
1,742 additions
and
1,257 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
function structure = add_missing_field(structure, field) | ||
if ~isfield(structure, field) | ||
structure(1).(field) = ''; | ||
end | ||
end |
This file contains 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,91 @@ | ||
function subject = append_to_layout(file, subject, modality, schema) | ||
% | ||
% appends a file to the BIDS layout by parsing it according to the provided schema | ||
% | ||
% USAGE:: | ||
% | ||
% subject = append_to_layout(file, subject, modality, schema == []) | ||
% | ||
% :param file: | ||
% :type file: string | ||
% :param subject: subject sub-structure from the BIDS layout | ||
% :type subject: strcture | ||
% :param modality: | ||
% :type modality: string | ||
% :param schema: | ||
% :type schema: strcture | ||
% | ||
% | ||
% Copyright (C) 2021--, BIDS-MATLAB developers | ||
|
||
if ~exist('schema', 'var') | ||
schema = []; | ||
end | ||
|
||
% Parse file fist to identify the suffix group in the template. | ||
% Then reparse the file using the entity-label pairs defined in the schema. | ||
p = bids.internal.parse_filename(file); | ||
|
||
idx = find_suffix_group(modality, p.suffix, schema); | ||
|
||
if ~isempty(schema) | ||
|
||
if isempty(idx) | ||
warning('append_to_structure:noMatchingSuffix', ... | ||
'Skipping file with no valid suffix in schema: %s', file); | ||
return | ||
end | ||
|
||
entities = bids.schema.return_modality_entities(schema.datatypes.(modality)(idx), schema); | ||
p = bids.internal.parse_filename(file, entities); | ||
|
||
end | ||
|
||
% Check any new entity field that needs to be added into the layout or the output | ||
% of the parsing to make sure the 2 structures can be concatenated | ||
if ~isempty(subject.(modality)) | ||
|
||
[subject.(modality), p] = bids.internal.match_structure_fields(subject.(modality), p); | ||
|
||
end | ||
|
||
if isempty(subject.(modality)) | ||
subject.(modality) = p; | ||
else | ||
subject.(modality)(end + 1, 1) = p; | ||
end | ||
|
||
end | ||
|
||
function idx = find_suffix_group(modality, suffix, schema) | ||
|
||
idx = []; | ||
|
||
if isempty(schema) | ||
return | ||
end | ||
|
||
% the following loop could probably be improved with some cellfun magic | ||
% cellfun(@(x, y) any(strcmp(x,y)), {p.type}, suffix_groups) | ||
for i = 1:size(schema.datatypes.(modality), 1) | ||
|
||
this_suffix_group = schema.datatypes.(modality)(i); | ||
|
||
% for CI | ||
if iscell(this_suffix_group) | ||
this_suffix_group = this_suffix_group{1}; | ||
end | ||
|
||
if any(strcmp(suffix, this_suffix_group.suffixes)) | ||
idx = i; | ||
break | ||
end | ||
|
||
end | ||
|
||
if isempty(idx) | ||
warning('findSuffix:noMatchingSuffix', ... | ||
'No corresponding suffix in schema for %s for datatype %s', suffix, modality); | ||
end | ||
|
||
end |
This file contains 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 |
---|---|---|
|
@@ -282,7 +282,6 @@ | |
files = dirs; | ||
|
||
else | ||
|
||
t = regexp(files, expr); | ||
|
||
if numel(files) == 1 && ~iscell(t) | ||
|
This file contains 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 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,32 @@ | ||
function status = keep_file(file_struct, options) | ||
|
||
status = true; | ||
|
||
% suffix is treated separately as it is not one of the entities | ||
for l = 1:size(options, 1) | ||
if strcmp(options{l, 1}, 'suffix') && ~ismember(file_struct.suffix, options{l, 2}) | ||
status = false; | ||
return | ||
end | ||
end | ||
|
||
for l = 1:size(options, 1) | ||
|
||
if ~strcmp(options{l, 1}, 'suffix') | ||
|
||
if ~ismember(options{l, 1}, fieldnames(file_struct.entities)) | ||
status = false; | ||
break | ||
end | ||
|
||
if isfield(file_struct.entities, options{l, 1}) && ... | ||
~ismember(file_struct.entities.(options{l, 1}), options{l, 2}) | ||
status = false; | ||
break | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains 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,14 @@ | ||
function [s1, s2] = match_structure_fields(s1, s2) | ||
|
||
missing_fields = setxor(fieldnames(s1), fieldnames(s2)); | ||
|
||
if ~isempty(missing_fields) | ||
for iField = 1:numel(missing_fields) | ||
|
||
s1 = bids.internal.add_missing_field(s1, missing_fields{iField}); | ||
s2 = bids.internal.add_missing_field(s2, missing_fields{iField}); | ||
|
||
end | ||
end | ||
|
||
end |
This file contains 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 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,19 @@ | ||
function extensions = return_modality_extensions(modality) | ||
|
||
extensions = '('; | ||
|
||
% for CI | ||
if iscell(modality) | ||
modality = modality{1}; | ||
end | ||
|
||
for iExt = 1:numel(modality.extensions) | ||
if ~strcmp(modality.extensions{iExt}, '.json') | ||
extensions = [extensions, modality.extensions{iExt}, '|']; %#ok<AGROW> | ||
end | ||
end | ||
|
||
% Replace final "|" by a "){1}" | ||
extensions(end:end + 3) = '){1}'; | ||
|
||
end |
This file contains 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,8 @@ | ||
function regular_expression = return_modality_regular_expression(modality) | ||
|
||
suffixes = bids.internal.return_modality_suffixes(modality); | ||
extensions = bids.internal.return_modality_extensions(modality); | ||
|
||
regular_expression = ['^%s.*' suffixes extensions '$']; | ||
|
||
end |
This file contains 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,17 @@ | ||
function suffixes = return_modality_suffixes(modality) | ||
|
||
suffixes = '_('; | ||
|
||
% For CI | ||
if iscell(modality) | ||
modality = modality{1}; | ||
end | ||
|
||
for iExt = 1:numel(modality(:).suffixes) | ||
suffixes = [suffixes, modality.suffixes{iExt}, '|']; %#ok<AGROW> | ||
end | ||
|
||
% Replace final "|" by a "){1}" | ||
suffixes(end:end + 3) = '){1}'; | ||
|
||
end |
Oops, something went wrong.