Skip to content

Commit

Permalink
adopt code to support SIESTA usecase 2.3 (fieldtrip#2416)
Browse files Browse the repository at this point in the history
* do not use compression when saving

* also detect senstype from data.label, not only from grad/elec/opto

* allow for BIDS inheritance when searching for JSON sidecars

- the JSON sidecar might have fewer entities (e.g., no run-xxx)
- the JSON sidecar might be at a higher directory
  • Loading branch information
robertoostenveld authored Jun 5, 2024
1 parent 5ac30a5 commit 657aa16
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
78 changes: 74 additions & 4 deletions fileio/private/bids_datafile.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%
% See also BIDS_SIDECAR, BIDS_TSV, EVENTS_TSV

% Copyright (C) 2020, Robert Oostenveld
% Copyright (C) 2020-2024, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
Expand Down Expand Up @@ -52,11 +52,81 @@
% this is the reverse procedure than the one implemented in BIDS_SIDECAR
for i=1:numel(modality)
f = [sprintf('%s_', entities{:}), modality{i}];

% we don't know the file extension, hence we have to search with a wildcard
d = dir(fullfile(p, [f '.*']));
if ~isempty(d)
datafile = fullfile(p, d.name);
jsonfile = fullfile(p, [sprintf('%s_', entities{:}), modality{i} '.json']);
break
end

% the json file can have fewer entities and/or can be at a higher level due to the inheritance principle
% we search for the one that is the "closest by"
foundjson = false;

if ~foundjson
% find the corresponding json file in the same directory as the data
alloptions = binoall(numel(entities));
for j=1:size(alloptions,1)
jsonfile = fullfile(p, [sprintf('%s_', entities{alloptions(j,:)}), modality{i} '.json']);
if exist(jsonfile, 'file')
foundjson = true;
break
end
end
end % if ~foundjson
if ~foundjson
% find the corresponding json file one directory up
alloptions = binoall(numel(entities));
p = fileparts(p); % remove the last directory
for j=1:size(alloptions,1)
jsonfile = fullfile(p, [sprintf('%s_', entities{alloptions(j,:)}), modality{i} '.json']);
if exist(jsonfile, 'file')
foundjson = true;
break
end
end
end % if ~foundjson
if ~foundjson
% find the corresponding json file two directories up
alloptions = binoall(numel(entities));
p = fileparts(p); % remove the last directory
for j=1:size(alloptions,1)
jsonfile = fullfile(p, [sprintf('%s_', entities{alloptions(j,:)}), modality{i} '.json']);
if exist(jsonfile, 'file')
foundjson = true;
break
end
end
end % if ~foundjson
if ~foundjson
% find the corresponding json file three directories up
alloptions = binoall(numel(entities));
p = fileparts(p); % remove the last directory
for j=1:size(alloptions,1)
jsonfile = fullfile(p, [sprintf('%s_', entities{alloptions(j,:)}), modality{i} '.json']);
if exist(jsonfile, 'file')
foundjson = true;
break
end
end
end % if ~foundjson

if ~foundjson
error('cannot find JSON sidecar for %s', datafile);
end
end % ~isempty(d)
end % for each modality

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function r = binoall(n)
% this returns an 2^n by n matrix with all possible binomial options
% See also BINORND

r = zeros(2^n, n, 'logical');
for i=0:(2^n-1)
r(i+1,:) = bitget(uint32(i), 1:n);
end
r = fliplr(r);
r = flipud(r);

2 changes: 1 addition & 1 deletion private/savevar.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function savevar(filename, varname, value, hashfile)

% if variable < ~500 MB, store it in old (uncompressed) format, which is faster
if (s.bytes < 500000000)
save(filename, varname, '-v6');
save(filename, varname, '-v7', '-nocompression');
else
save(filename, varname, '-v7.3');
end
Expand Down
15 changes: 5 additions & 10 deletions utilities/ft_checkdata.m
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,12 @@
stype = {stype};
end

if isfield(data, 'grad') || isfield(data, 'elec') || isfield(data, 'opto')
if any(strcmp(ft_senstype(data), stype))
okflag = 1;
elseif any(cellfun(@ft_senstype, repmat({data}, size(stype)), stype))
% this is required to detect more general types, such as "meg" or "ctf" rather than "ctf275"
okflag = 1;
else
okflag = 0;
end
if any(strcmp(ft_senstype(data), stype))
okflag = 1;
elseif any(cellfun(@ft_senstype, repmat({data}, size(stype)), stype))
% this is required to detect more general types, such as "meg" or "ctf" rather than "ctf275"
okflag = 1;
else
% the data does not contain a sensor array
okflag = 0;
end

Expand Down

0 comments on commit 657aa16

Please sign in to comment.