-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbids_validate_file.m
More file actions
66 lines (56 loc) · 2.35 KB
/
bids_validate_file.m
File metadata and controls
66 lines (56 loc) · 2.35 KB
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
function results = bids_validate_file(filepaths)
% this is a Matlab wrapper that calls the BIDS validator python version to validate a file
% make sure this is installed in your python environment: python -m pip install bids_validator
%
% INPUT:
% filepaths can be a string array: ["/sub-01/anat/sub-01_rec-CSD_T1w.nii.gz", "/sub-01/anat/sub-01_acq-23_rec-CSD_T1w.json"]
% filepaths can be a cell array: {"/sub-01/anat/sub-01_rec-CSD_T1w.nii.gz", "/sub-01/anat/sub-01_acq-23_rec-CSD_T1w.json"}
% filepaths can be a single file: "/sub-01/anat/sub-01_rec-CSD_T1w.nii.gz"
%
% OUTPUT:
% returns a logical array indicating whether each file is BIDS-compliant
%
% Cyril Pernet 08-10-2025
% Check if Python is available
try
pyenv;
catch
error('Python environment not available. Please configure Python for MATLAB using pyenv.');
end
% Ensure filepaths is a cell array
if ischar(filepaths)
% Single character string
filepaths = {filepaths};
elseif isstring(filepaths)
% String array - convert to cell array
filepaths = cellstr(filepaths);
elseif iscell(filepaths)
% Already a cell array - ensure all elements are char
filepaths = cellfun(@char, filepaths, 'UniformOutput', false);
else
error('filepaths must be a char, string array, or cell array of filenames');
end
% Initialize results array
results = false(length(filepaths), 1);
try
% Import and initialize the BIDS validator
pyrun("from bids_validator import BIDSValidator");
pyrun("validator = BIDSValidator()");
% Validate each file
for i = 1:length(filepaths)
filepath = filepaths{i};
% Use pyrun to call the validator
result = pyrun("result = validator.is_bids(filepath)", "result", filepath=filepath);
results(i) = logical(result);
% Display result
fprintf('File: %s -> BIDS valid: %s\n', filepath, string(results(i)));
end
catch ME
if contains(ME.message, 'bids_validator')
error(['BIDS validator not found. Please install it using:\n' ...
'python -m pip install bids_validator\n' ...
'Original error: %s'], ME.message);
else
rethrow(ME);
end
end