Skip to content

Commit 14959d2

Browse files
committed
transitioning away from custom coded rules to full BIDS schema
1 parent 02ecbfa commit 14959d2

File tree

6 files changed

+19825
-11400
lines changed

6 files changed

+19825
-11400
lines changed

matlab/get_bids_schema.m

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
function schema = get_bids_schema(schema_version)
2+
%GET_BIDS_SCHEMA Get BIDS schema with online fallback
3+
% SCHEMA = GET_BIDS_SCHEMA() gets the latest BIDS schema
4+
% SCHEMA = GET_BIDS_SCHEMA(VERSION) gets a specific version
5+
%
6+
% This function tries to use the packaged schema first, then falls
7+
% back to downloading from online if the local version is not available.
8+
9+
if nargin < 1
10+
schema_version = 'latest';
11+
end
12+
13+
% Try to get from Python package first
14+
try
15+
% Get the path to the Python package
16+
python_package_path = get_python_package_path();
17+
schema_path = fullfile(python_package_path, 'bids_schema.json');
18+
19+
if exist(schema_path, 'file')
20+
schema = read_json_file(schema_path);
21+
return;
22+
end
23+
catch
24+
% Continue to online fallback
25+
end
26+
27+
% Try online download
28+
try
29+
url = sprintf('https://bids-specification.readthedocs.io/en/%s/schema.json', schema_version);
30+
schema = webread(url);
31+
return;
32+
catch
33+
error('BIDS schema not available online or offline');
34+
end
35+
end
36+
37+
function package_path = get_python_package_path()
38+
%GET_PYTHON_PACKAGE_PATH Get path to the pypet2bids package
39+
40+
% Try to find the package in common locations
41+
possible_paths = {
42+
fullfile(pwd, 'pypet2bids', 'pypet2bids'), % Local development
43+
fullfile(fileparts(mfilename('fullpath')), '..', 'pypet2bids', 'pypet2bids'), % Relative to this file
44+
fullfile(userpath, 'pypet2bids'), % User path
45+
};
46+
47+
for i = 1:length(possible_paths)
48+
if exist(possible_paths{i}, 'dir')
49+
package_path = possible_paths{i};
50+
return;
51+
end
52+
end
53+
54+
error('Could not find pypet2bids package');
55+
end
56+
57+
function data = read_json_file(filepath)
58+
%READ_JSON_FILE Read and parse JSON file
59+
60+
fid = fopen(filepath, 'r');
61+
if fid == -1
62+
error('Could not open file: %s', filepath);
63+
end
64+
65+
try
66+
content = fread(fid, inf, 'char=>char')';
67+
data = jsondecode(content);
68+
catch ME
69+
fclose(fid);
70+
rethrow(ME);
71+
end
72+
73+
fclose(fid);
74+
end

0 commit comments

Comments
 (0)