forked from sccn/EEG-BIDS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bids_importjson.m
97 lines (93 loc) · 3.28 KB
/
bids_importjson.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
97
% bids_importjson - Import json file following BIDS key-based inheritance rule
% (https://bids-specification.readthedocs.io/en/stable/common-principles.html#the-inheritance-principle)
%
% Usage:
% curjsondata = bids_importjson(curFile, ext)
%
% Inputs:
% curFile - [string] full path to the current json file
% ext - [string] BIDS post fix extension of the file.
% e.g. _events.json
%
% Outputs:
% curjsondata - [struct] json data imported as matlab structure
%
% Author: Dung Truong, 2024
function curjsondata = bids_importjson(curFile, ext, oriFile)
if nargin < 1
help bids_importjson
return
end
if nargin < 2
% no extension is provided. Default to using the filename
splitted = strsplit(curFile, '_');
ext = ['_' splitted{end}];
end
if nargin < 3
% no extension is provided. Default to using the filename
oriFile = curFile;
end
% resolve wildcard if applicable
curFileDir = dir(curFile);
if ~isempty(curFileDir)
curFile = fullfile(curFileDir(1).folder, curFileDir(1).name);
end
if ~exist(curFile, 'file') || isempty(curFile)
curjsondata = struct([]);
else
curjsondata = readjson(curFile);
end
if ~isTopLevel(curFile)
upperFile = fullfile(fileparts(fileparts(curFile)), ['*' ext]);
% resolve wildcard if applicable
upperFileDir = dir(upperFile);
if ~isempty(upperFileDir)
for u=1:numel(upperFileDir)
% check using rule 2.b and 2.c
% of https://bids-specification.readthedocs.io/en/stable/common-principles.html#the-inheritance-principle
upperFileName = upperFileDir(u).name;
upperFileName_parts = strsplit(upperFileName, '_');
if contains(oriFile, upperFileName_parts)
upperFile = fullfile(upperFileDir(u).folder, upperFileDir(u).name);
break
end
end
end
upperjsondata = bids_importjson(upperFile, ext, oriFile);
% mergeStructures credit: https://www.mathworks.com/matlabcentral/fileexchange/131718-mergestructures-merge-or-concatenate-nested-structures?s_tid=mwa_osa_a
curjsondata = mergeStructures(curjsondata, upperjsondata);
end
function res = readjson(file)
if exist('jsondecode.m','file')
res = jsondecode( importalltxt( file ));
else
res = jsonread(file);
end
end
% Import full text file
% ---------------------
function str = importalltxt(fileName)
str = [];
fid =fopen(fileName, 'r');
while ~feof(fid)
str = [str 10 fgetl(fid) ];
end
str(1) = [];
end
function res = isTopLevel(curfile)
res = true;
if ~isempty(curfile)
curpath = fileparts(curfile);
files = dir(curpath);
if ~isempty(files)
for f=1:numel(files)
if ~files(f).isdir && (strcmp(files(f).name, 'README') || strcmp(files(f).name, 'dataset_description.json'))
res = true;
return
end
end
end
res = false;
end
end
end