Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:bids-standard/bids-matlab into nb_js…
Browse files Browse the repository at this point in the history
…onfile
  • Loading branch information
Beliy Nikita committed Aug 8, 2023
2 parents d3f09c8 + 10baa5d commit 9ac9e74
Show file tree
Hide file tree
Showing 28 changed files with 621 additions and 156 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
2 changes: 1 addition & 1 deletion +bids/+internal/return_file_index.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'\n\t- modality: %s', ...
'\n\t- file: %s', ...
'\nThis may happen if your dataset is not valid.'], ...
BIDS.pth, ...
bids.internal.format_path(BIDS.pth), ...
BIDS.subjects(sub_idx).name, ...
modality, ...
filename);
Expand Down
8 changes: 6 additions & 2 deletions +bids/+util/create_sessions_tsv.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
sessions_list = bids.query(layout, 'sessions');
if isempty(sessions_list) && use_schema
msg = sprintf(['There are no session in this dataset:\n', ...
'\t%s'], layout.pth);
'\t%s'], bids.internal.format_path(layout.pth));
bids.internal.error_handling(mfilename(), 'noSessionInDataset', msg, tolerant, verbose);
return
end
Expand All @@ -74,7 +74,9 @@
if exist(sessions_file, 'file')
msg = sprintf(['"sessions.tsv" %s already exist for the following dataset.', ...
'Will not overwrite.\n', ...
'\t%s'], sessions_file, layout.pth);
'\t%s'], ...
bids.internal.format_path(sessions_file), ...
bids.internal.format_path(layout.pth));
bids.internal.error_handling(mfilename(), 'sessionsFileExist', msg, true, verbose);
continue
end
Expand Down Expand Up @@ -105,6 +107,8 @@
end

if verbose && ~isempty(output_filenames)
output_filenames = bids.internal.format_path(output_filenames);

fprintf(1, ['\nCreated the following "sessions.tsv" in the dataset.', ...
'\n\t%s\n', ...
'Please add any extra information manually.\n', ...
Expand Down
6 changes: 4 additions & 2 deletions +bids/+util/jsondecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
end

if ~exist(file, 'file')
msg = sprintf('Unable to read file ''%s'': file not found', file);
msg = sprintf('Unable to read file ''%s'': file not found', ...
bids.internal.format_path(file));
bids.internal.error_handling(mfilename(), 'FileNotFound', ...
msg, false);
end
Expand Down Expand Up @@ -74,7 +75,8 @@
end

function warning_cannot_read_json(file)
msg = sprintf('Could not read file:\n%s', file);
msg = sprintf('Could not read file:\n%s', ...
bids.internal.format_path(file));
bids.internal.error_handling(mfilename(), 'CannotReadJson', ...
msg, true, true);
end
22 changes: 16 additions & 6 deletions +bids/+util/tsvwrite.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ function tsvwrite(filename, var)
%
% Based on spm_save.m from SPM12.
%
%

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

% (C) Copyright 2018 BIDS-MATLAB developers

delim = sprintf('\t');
Expand Down Expand Up @@ -57,6 +54,7 @@ function tsvwrite(filename, var)
var = num2cell(var);
end

var = convert_datetimes(var);
var = cellfun(@(x) num2str(x, 16), var, 'UniformOutput', false);
var = strtrim(var);
var(cellfun(@(x) strcmp(x, 'NaN'), var)) = {'n/a'};
Expand All @@ -66,9 +64,10 @@ function tsvwrite(filename, var)
write_to_file(filename, var, delim);

elseif isa(var, 'table')
writetable(var, filename, ...
'FileType', 'text', ...
'Delimiter', delim);
header = var.Properties.VariableNames;
var = table2cell(var);
var = cat(1, header, var);
bids.util.tsvwrite(filename, var);

else
error('Unknown data type.');
Expand All @@ -77,6 +76,17 @@ function tsvwrite(filename, var)

end

function var = convert_datetimes(var)
if bids.internal.is_octave
return
end
is_datetime = find(cellfun(@(x) isdatetime(x), var(:)));
for i = 1:numel(is_datetime)
idx = is_datetime(i);
var{idx} = char(var{idx}, 'yyyy-MM-dd''T''HH:mm:ss.SSS'); % bids-compliant datetime
end
end

function write_to_file(filename, var, delim)

fid = fopen(filename, 'Wt');
Expand Down
Loading

0 comments on commit 9ac9e74

Please sign in to comment.