forked from bids-standard/bids-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
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 bids-standard#95 from Remi-Gau/remi-apply_linter
apply MISS_HIT linter across the board
- Loading branch information
Showing
17 changed files
with
1,835 additions
and
1,744 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,86 +1,90 @@ | ||
function meta = get_metadata(filename, pattern) | ||
% Read a BIDS's file metadata according to the inheritance principle | ||
% FORMAT meta = bids.internal.get_metadata(filename, pattern) | ||
% filename - name of file following BIDS standard | ||
% pattern - regular expression matching metadata file | ||
% meta - metadata structure | ||
%__________________________________________________________________________ | ||
% Read a BIDS's file metadata according to the inheritance principle | ||
% FORMAT meta = bids.internal.get_metadata(filename, pattern) | ||
% filename - name of file following BIDS standard | ||
% pattern - regular expression matching metadata file | ||
% meta - metadata structure | ||
% __________________________________________________________________________ | ||
|
||
% Copyright (C) 2016-2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging | ||
% Copyright (C) 2018--, BIDS-MATLAB developers | ||
% Copyright (C) 2016-2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging | ||
% Copyright (C) 2018--, BIDS-MATLAB developers | ||
|
||
if nargin == 1 | ||
pattern = '^.*%s\\.json$'; | ||
end | ||
|
||
if nargin == 1, pattern = '^.*%s\\.json$'; end | ||
pth = fileparts(filename); | ||
p = bids.internal.parse_filename(filename); | ||
|
||
pth = fileparts(filename); | ||
p = bids.internal.parse_filename(filename); | ||
meta = struct(); | ||
|
||
meta = struct(); | ||
N = 3; | ||
|
||
N = 3; | ||
% -There is a session level in the hierarchy | ||
if isfield(p, 'ses') && ~isempty(p.ses) | ||
N = N + 1; | ||
end | ||
|
||
%-There is a session level in the hierarchy | ||
if isfield(p,'ses') && ~isempty(p.ses) | ||
N = N + 1; | ||
end | ||
|
||
%-Loop from the directory where the file of interest is back to the | ||
% top level of the BIDS hierarchy | ||
for n=1:N | ||
|
||
%-List the potential metadata files associated with this file suffix type | ||
% -Loop from the directory where the file of interest is back to the | ||
% top level of the BIDS hierarchy | ||
for n = 1:N | ||
|
||
% -List the potential metadata files associated with this file suffix type | ||
% Default is to assume it is a JSON file | ||
metafile = bids.internal.file_utils('FPList', pth, sprintf(pattern, p.type)); | ||
|
||
if isempty(metafile), metafile = {}; else metafile = cellstr(metafile); end | ||
|
||
%-For all those files we find which one is potentially associated with | ||
|
||
if isempty(metafile) | ||
metafile = {}; | ||
else | ||
metafile = cellstr(metafile); | ||
end | ||
|
||
% -For all those files we find which one is potentially associated with | ||
% the file of interest | ||
for i=1:numel(metafile) | ||
|
||
p2 = bids.internal.parse_filename(metafile{i}); | ||
fn = setdiff(fieldnames(p2),{'filename','ext','type'}); | ||
|
||
%-Check if this metadata file contains the same entity-label pairs as its | ||
% data file counterpart | ||
ismeta = true; | ||
for j=1:numel(fn) | ||
if ~isfield(p,fn{j}) || ~strcmp(p.(fn{j}),p2.(fn{j})) | ||
ismeta = false; | ||
break; | ||
end | ||
for i = 1:numel(metafile) | ||
|
||
p2 = bids.internal.parse_filename(metafile{i}); | ||
fn = setdiff(fieldnames(p2), {'filename', 'ext', 'type'}); | ||
|
||
% -Check if this metadata file contains the same entity-label pairs as its | ||
% data file counterpart | ||
ismeta = true; | ||
for j = 1:numel(fn) | ||
if ~isfield(p, fn{j}) || ~strcmp(p.(fn{j}), p2.(fn{j})) | ||
ismeta = false; | ||
break | ||
end | ||
%-Read the content of the metadata file if it is a JSON file and update | ||
% the metadata concerning the file of interest otherwise store the filename | ||
if ismeta | ||
if strcmp(p2.ext,'.json') | ||
meta = update_metadata(meta,bids.util.jsondecode(metafile{i}),metafile{i}); | ||
else | ||
meta.filename = metafile{i}; | ||
end | ||
end | ||
|
||
% -Read the content of the metadata file if it is a JSON file and update | ||
% the metadata concerning the file of interest otherwise store the filename | ||
if ismeta | ||
if strcmp(p2.ext, '.json') | ||
meta = update_metadata(meta, bids.util.jsondecode(metafile{i}), metafile{i}); | ||
else | ||
meta.filename = metafile{i}; | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
%-Go up to the parent folder | ||
pth = fullfile(pth,'..'); | ||
|
||
end | ||
|
||
|
||
%========================================================================== | ||
%-Inheritance principle | ||
%========================================================================== | ||
function s1 = update_metadata(s1,s2,file) | ||
if isempty(s2) | ||
return; | ||
elseif ~isstruct(s2) | ||
error('Metadata file contents were neither struct nor empty. File: %s',file); | ||
end | ||
fn = fieldnames(s2); | ||
for i=1:numel(fn) | ||
if ~isfield(s1,fn{i}) | ||
s1.(fn{i}) = s2.(fn{i}); | ||
|
||
% -Go up to the parent folder | ||
pth = fullfile(pth, '..'); | ||
|
||
end | ||
|
||
% ========================================================================== | ||
% -Inheritance principle | ||
% ========================================================================== | ||
function s1 = update_metadata(s1, s2, file) | ||
if isempty(s2) | ||
return | ||
elseif ~isstruct(s2) | ||
error('Metadata file contents were neither struct nor empty. File: %s', file); | ||
end | ||
fn = fieldnames(s2); | ||
for i = 1:numel(fn) | ||
if ~isfield(s1, fn{i}) | ||
s1.(fn{i}) = s2.(fn{i}); | ||
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 |
---|---|---|
@@ -1,56 +1,56 @@ | ||
function p = parse_filename(filename,fields) | ||
% Split a filename into its building constituents | ||
% FORMAT p = bids.internal.parse_filename(filename,fields) | ||
% | ||
% Example: | ||
% | ||
% >> filename = '../sub-16/anat/sub-16_ses-mri_run-1_echo-2_FLASH.nii.gz'; | ||
% >> bids.internal.parse_filename(filename) | ||
% | ||
% ans = | ||
% | ||
% struct with fields: | ||
% | ||
% filename: 'sub-16_ses-mri_run-1_echo-2_FLASH.nii.gz' | ||
% type: 'FLASH' | ||
% ext: '.nii.gz' | ||
% sub: '16' | ||
% ses: 'mri' | ||
% run: '1' | ||
% echo: '2' | ||
%__________________________________________________________________________ | ||
function p = parse_filename(filename, fields) | ||
% Split a filename into its building constituents | ||
% FORMAT p = bids.internal.parse_filename(filename, fields) | ||
% | ||
% Example: | ||
% | ||
% >> filename = '../sub-16/anat/sub-16_ses-mri_run-1_echo-2_FLASH.nii.gz'; | ||
% >> bids.internal.parse_filename(filename) | ||
% | ||
% ans = | ||
% | ||
% struct with fields: | ||
% | ||
% filename: 'sub-16_ses-mri_run-1_echo-2_FLASH.nii.gz' | ||
% type: 'FLASH' | ||
% ext: '.nii.gz' | ||
% sub: '16' | ||
% ses: 'mri' | ||
% run: '1' | ||
% echo: '2' | ||
% __________________________________________________________________________ | ||
|
||
% Copyright (C) 2016-2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging | ||
% Copyright (C) 2018--, BIDS-MATLAB developers | ||
% Copyright (C) 2016-2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging | ||
% Copyright (C) 2018--, BIDS-MATLAB developers | ||
|
||
filename = bids.internal.file_utils(filename,'filename'); | ||
filename = bids.internal.file_utils(filename, 'filename'); | ||
|
||
%-Identify all the BIDS entity-label pairs present in the filename (delimited by "_") | ||
% https://bids-specification.readthedocs.io/en/stable/99-appendices/04-entity-table.html | ||
[parts, dummy] = regexp(filename,'(?:_)+','split','match'); %#ok<ASGLU> | ||
p.filename = filename; | ||
% -Identify all the BIDS entity-label pairs present in the filename (delimited by "_") | ||
% https://bids-specification.readthedocs.io/en/stable/99-appendices/04-entity-table.html | ||
[parts, dummy] = regexp(filename, '(?:_)+', 'split', 'match'); %#ok<ASGLU> | ||
p.filename = filename; | ||
|
||
%-Identify the suffix and extension of this file | ||
% https://bids-specification.readthedocs.io/en/stable/02-common-principles.html#file-name-structure | ||
[p.type, p.ext] = strtok(parts{end},'.'); | ||
% -Identify the suffix and extension of this file | ||
% https://bids-specification.readthedocs.io/en/stable/02-common-principles.html#file-name-structure | ||
[p.type, p.ext] = strtok(parts{end}, '.'); | ||
|
||
%-Separate the entity from the label for each pair identified above | ||
for i=1:numel(parts)-1 | ||
[d, dummy] = regexp(parts{i},'(?:\-)+','split','match'); %#ok<ASGLU> | ||
% -Separate the entity from the label for each pair identified above | ||
for i = 1:numel(parts) - 1 | ||
[d, dummy] = regexp(parts{i}, '(?:\-)+', 'split', 'match'); %#ok<ASGLU> | ||
p.(d{1}) = d{2}; | ||
end | ||
end | ||
|
||
%-Extra fields can be added to the structure and ordered specifically. | ||
if nargin == 2 | ||
for i=1:numel(fields) | ||
if ~isfield(p,fields{i}) | ||
p.(fields{i}) = ''; | ||
end | ||
% -Extra fields can be added to the structure and ordered specifically. | ||
if nargin == 2 | ||
for i = 1:numel(fields) | ||
if ~isfield(p, fields{i}) | ||
p.(fields{i}) = ''; | ||
end | ||
end | ||
try | ||
p = orderfields(p,['filename','ext','type',fields]); | ||
p = orderfields(p, ['filename', 'ext', 'type', fields]); | ||
catch | ||
warning('Ignoring file ''%s'' not matching template.',filename); | ||
p = struct([]); | ||
warning('Ignoring file ''%s'' not matching template.', filename); | ||
p = struct([]); | ||
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 |
---|---|---|
@@ -1,30 +1,31 @@ | ||
function value = jsondecode(file, varargin) | ||
% Decode JSON-formatted file | ||
% FORMAT value = bids.util.jsondecode(file, opts) | ||
% file - name of a JSON file or JSON string | ||
% opts - structure of optional parameters (only with JSONio): | ||
% replacementStyle: string to control how non-alphanumeric | ||
% characters are replaced {'underscore','hex','delete','nop'} | ||
% [Default: 'underscore'] | ||
% | ||
% json - JSON structure | ||
% Decode JSON-formatted file | ||
% FORMAT value = bids.util.jsondecode(file, opts) | ||
% file - name of a JSON file or JSON string | ||
% opts - structure of optional parameters (only with JSONio): | ||
% replacementStyle: string to control how non-alphanumeric | ||
% characters are replaced {'underscore','hex','delete','nop'} | ||
% [Default: 'underscore'] | ||
% | ||
% json - JSON structure | ||
|
||
% Copyright (C) 2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging | ||
% Copyright (C) 2018--, BIDS-MATLAB developers | ||
% Copyright (C) 2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging | ||
% Copyright (C) 2018--, BIDS-MATLAB developers | ||
|
||
persistent has_jsondecode | ||
if isempty(has_jsondecode) | ||
persistent has_jsondecode | ||
if isempty(has_jsondecode) | ||
has_jsondecode = ... | ||
exist('jsondecode','builtin') == 5 || ... % MATLAB >= R2016b | ||
ismember(exist('jsondecode','file'), [2 3]); % jsonstuff or other Matlab-compatible implementation | ||
end | ||
exist('jsondecode', 'builtin') == 5 || ... % MATLAB >= R2016b | ||
ismember(exist('jsondecode', 'file'), [2 3]); % jsonstuff / Matlab-compatible implementation | ||
end | ||
|
||
if has_jsondecode | ||
if has_jsondecode | ||
value = jsondecode(fileread(file)); | ||
elseif exist('spm_jsonread','file') == 3 % SPM12 | ||
elseif exist('spm_jsonread', 'file') == 3 % SPM12 | ||
value = spm_jsonread(file, varargin{:}); | ||
elseif exist('jsonread','file') == 3 % JSONio | ||
elseif exist('jsonread', 'file') == 3 % JSONio | ||
value = jsonread(file, varargin{:}); | ||
else | ||
error('JSON library required: install JSONio from https://github.com/gllmflndn/JSONio'); | ||
end | ||
else | ||
url = 'https://github.com/gllmflndn/JSONio'; | ||
error('JSON library required: install JSONio from: %s', url); | ||
end |
Oops, something went wrong.