Skip to content

Commit

Permalink
[ENH] use inheritance in get_meta_list (#609)
Browse files Browse the repository at this point in the history
* use inheritance in get_meta_list

* refactor
  • Loading branch information
Remi-Gau authored Aug 5, 2023
1 parent d2b1c9c commit fd3e0e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
42 changes: 26 additions & 16 deletions +bids/+internal/get_meta_list.m
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
function metalist = get_meta_list(filename, pattern)
function metalist = get_meta_list(varargin)
%
% Read a BIDS's file metadata according to the inheritance principle
%
% USAGE::
%
% meta = bids.internal.get_metadata(filename, pattern = '^.*%s\\.json$')
% metalist = bids.internal.get_metadata(filename, ...
% pattern, ...
% use_inheritance)
%
% :param filename: fullpath name of file following BIDS standard
% :type filename: char
% :param pattern: Regular expression matching the metadata file (default is ``'^.*%s\\.json$'``)
%
% :param pattern: Regular expression matching the metadata file
% default = ``'^.*%s\\.json$'``
% If provided, it must at least be ``'%s'``.
% :type pattern: char
%
% metalist - list of paths to metafiles
% :return: metalist - list of paths to metafiles
%
%

% (C) Copyright 2011-2018 Guillaume Flandin, Wellcome Centre for Human Neuroimaging
%

% (C) Copyright 2018 BIDS-MATLAB developers

if nargin == 1
pattern = '^.*%s\\.json$';
end
args = inputParser;

addRequired(args, 'filename', @ischar);
% Default is to assume it is a JSON file
addOptional(args, 'pattern', '^.*%s\\.json$', @ischar);
addParameter(args, 'use_inheritance', true, @islogical);

parse(args, varargin{:});

filename = args.Results.filename;
pattern = args.Results.pattern;
use_inheritance = args.Results.use_inheritance;

pth = fileparts(filename);
metalist = {};
Expand All @@ -33,12 +44,12 @@
end

% Default assumes we are dealing with a file in the root directory
% like "participants.tsv"
% like "participants.tsv".
% If the file has underscore separated entities ("sub-01_T1w.nii")
% then we look through the hierarchy for potential metadata file associated
% with queried file.
% then we look through the hierarchy for potential metadata file
% associated with queried file.
N = 1;
if isfield(p, 'entities')
if use_inheritance && isfield(p, 'entities')
N = 3;
% -There is a session level in the hierarchy
if isfield(p.entities, 'ses') && ~isempty(p.entities.ses)
Expand All @@ -49,7 +60,6 @@
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.suffix));

if isempty(metafile)
Expand All @@ -58,8 +68,8 @@
metafile = cellstr(metafile);
end

% For all those files we find which one is potentially associated with
% the file of interest
% For all those files we find which one is potentially associated
% with the file of interest
% TODO: not more than one file per level is allowed
for i = 1:numel(metafile)

Expand Down
22 changes: 16 additions & 6 deletions +bids/+internal/get_metadata.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function meta = get_metadata(metafile)
%
% Read a BIDS's file metadata according to the inheritance principle
% Read a BIDS's file metadata according to the inheritance principle.
%
% USAGE::
%
Expand All @@ -11,21 +11,20 @@
%
% :returns: - :meta: metadata structure
%

% .. todo
%
% add explanation on how the inheritance principle is implemented.
%
%

% (C) Copyright 2011-2018 Guillaume Flandin, Wellcome Centre for Human Neuroimaging
%

% (C) Copyright 2018 BIDS-MATLAB developers

meta = struct();
metafile = cellstr(metafile);

for i = 1:numel(metafile)
% assumes files are read from root level files to deeper files
if bids.internal.ends_with(metafile{i}, '.json')
meta = update_metadata(meta, bids.util.jsondecode(metafile{i}), metafile{i});
else
Expand All @@ -35,7 +34,12 @@
end

if isempty(meta)
warning('No metadata for %s', metafile);
bids.internal.error_handling( ...
mfilename(), ...
'NoMetadata', ...
sprintf('No metadata for:%s', ...
bids.internal.create_unordered_list(metafile)), ...
true, true);
end

end
Expand All @@ -47,8 +51,14 @@
if isempty(struct_two)
return
elseif ~isstruct(struct_two)
error('Metadata file contents were neither struct nor empty. File: %s', file);
bids.internal.error_handling( ...
mfilename(), ...
'WrongInputType', ...
sprintf('Input was neither struct nor empty for file:\n%s', ...
file), ...
false);
end

fn = fieldnames(struct_two);
for i = 1:numel(fn)
if ~isfield(struct_one, fn{i})
Expand Down

0 comments on commit fd3e0e6

Please sign in to comment.