-
Notifications
You must be signed in to change notification settings - Fork 0
/
bidsdigest.m
96 lines (86 loc) · 3.23 KB
/
bidsdigest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function data = bidsdigest(jbidsfolder, outputfile, rootname, varargin)
%
% bidsdigest(jbidsfolder, outputfile, rootname)
% or
% data = bidsdigest(jbidsfolder, outputfile, rootname, 'param1', value1, 'param2', value2, ...)
%
% Merging all JSON-wrapped searchable BIDS data files into a single
% JSON-formatted BIDS digest file for automation and integration
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% jbidsfolder: the folder storing bidssub2json or bids2json outputs
% outputfile: the output BIDS digest JSON file name; if missing,
% returns data in memory; one can also set the file extension
% to .jbd/.bjd to save the output to a binary JSON file
% (https://neurojson.org/bjdata/draft2), .h5 to an HDF5
% (https://hdfgroup.org) file, .msgpack to a messagepack file
% or a .ubj to a UBJSON (https://ubjson.org) file
% rootname: the root object name; if not given, use ''
% optional param/value pairs: these additional param/value pairs
% will be passed to loadjd and savejd functions provided by
% JSONLab (https://neurojson.org/jsonlab) to customize
% loading/saving options
%
% output:
% data: a struct containing the parsed JSON-encoded data/metadata of
% the specified BIDS folder
%
% examples:
% bidsdigest('/path/to/ds1/sub-01', '/tmp/bids/sub-01.json')
% bidsdigest('/path/to/ds1/sub-02', '/tmp/bids/sub-02.json', 'sub-02', 'compact', 1)
% data = bidsdigest('/path/to/ds1')
% disp(data.README)
%
% license:
% BSD license, see LICENSE_BSD.txt files for details
%
% -- this function is part of JBIDS toolbox (https://neurojson.org/#software)
%
if (nargin < 3)
rootname = '';
end
opt = varargin2struct(varargin{:});
if (~isfield(opt, 'usemap'))
opt.usemap = 1;
end
if (~isfield(opt, 'savebinary'))
opt.savebinary = 0;
end
json = parsefolder(jbidsfolder, opt);
if (nargin > 1)
savejd(rootname, json, 'filename', outputfile, opt);
end
if (nargout >= 1 || nargin < 2)
data = json;
end
%%
function json = parsefolder(foldername, varargin)
jbids = dir(foldername);
if (~isfield(jbids, 'folder')) % for old matlab
jbids = arrayfun(@(x) setfield(x, 'folder', foldername), jbids);
end
json = containers.Map;
for i = 1:length(jbids)
fname = jbids(i).name;
if (~isempty(regexp(jbids(i).folder, ['\' filesep '\.att$'], 'once')))
continue
end
if (jbids(i).isdir)
% if foldername.json exist, it is a previously generated digest and
% the folder will not be scanned again
if (~isempty(dir(fullfile(jbids(i).folder, jbids(i).name, '.jbids'))))
continue
end
if (isempty(regexp(fname, '^\.', 'once')))
json(fname) = parsefolder(fullfile(jbids(i).folder, fname), varargin{:});
end
continue
end
fullname = fullfile(jbids(i).folder, fname);
fprintf(1, 'merging %d/%d [%s]\n', i, length(jbids), fullname);
origfile = regexprep(fname, '\.jbids$|\.jnii$', '');
origfile = regexprep(origfile, '(\.tsv|\.jpg|\.png|\.tif|\.bmp|\.snirf).json$', '$1');
json(origfile) = loadjd(fullname, 'jdatadecode', 0, varargin{:});
end