Skip to content

Commit 958df8e

Browse files
committed
Added dataset assertion to check data integrety before running calcs'
1 parent 94ec7a4 commit 958df8e

13 files changed

Lines changed: 125 additions & 1 deletion

File tree

.DS_Store

4 KB
Binary file not shown.

assertion_scripts/assert_dataset.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function assert_dataset(Dataset)
2+
% assert_dataset
3+
% Validates a PRISME dataset before it enters the pipeline. Fails loud at
4+
% ingestion rather than letting bad values propagate into curve fits.
5+
% 1. brain_data has no NaN (and no Inf)
6+
% 2. brain_data.data is a proper 2-D edge × subject matrix
7+
% 3. sub_ids length matches the number of data columns (subjects)
8+
9+
assert(isfield(Dataset, 'brain_data'), ...
10+
'assert_dataset: missing brain_data field');
11+
12+
conds = fieldnames(Dataset.brain_data);
13+
assert(~isempty(conds), 'assert_dataset: brain_data has no conditions');
14+
15+
for c = 1:numel(conds)
16+
cond = conds{c};
17+
S = Dataset.brain_data.(cond);
18+
19+
assert( ...
20+
isfield(S, 'data') && isfield(S, 'sub_ids'), ...
21+
'assert_dataset: %s missing data or sub_ids field', ...
22+
cond ...
23+
);
24+
25+
d = S.data;
26+
27+
[n_vars, n_subj] = size(d);
28+
n_ids = numel(S.sub_ids);
29+
30+
% Subjects are the COLUMNS, defined by sub_ids. Check that the column
31+
% count matches, and that edges (rows) outnumber subjects as expected.
32+
assert(n_subj == n_ids, ...
33+
['assert_dataset: %s has %d sub_ids but %d data columns — ' ...
34+
'subject labels and brain data are misaligned (or matrix transposed: ' ...
35+
'data is %d x %d)'], cond, n_ids, n_subj, n_vars, n_subj);
36+
37+
% --- Check 1: no NaN, no Inf ---
38+
n_nan = sum(isnan(d(:)));
39+
n_inf = sum(isinf(d(:)));
40+
if n_nan > 0 || n_inf > 0
41+
per_subject = sum(isnan(d), 1);
42+
bad = find(per_subject > 0);
43+
error('assert_dataset:badValues', ...
44+
'%s.data has %d NaN and %d Inf across %d subjects: [%s]', ...
45+
cond, n_nan, n_inf, numel(bad), num2str(bad));
46+
end
47+
48+
end
49+
50+
end
0 Bytes
Binary file not shown.

data/test_act_act.mat

-117 Bytes
Binary file not shown.

data/test_hcp_fc.mat

0 Bytes
Binary file not shown.

data/test_r_fc.mat

0 Bytes
Binary file not shown.

data/test_t2_fc.mat

0 Bytes
Binary file not shown.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
% clean_rosenblatt_nans.m
2+
% Standalone script: loads s_slim_fc_rosenblatt.mat, removes any subject whose
3+
% brain data contains a NaN (across all conditions under brain_data), removes the
4+
% matching entries from sub_ids, sub_ids_motion, and motion, and saves the result
5+
% as s_slim_fc_rosenblatt_fabi.mat in the same directory.
6+
7+
clear; clc;
8+
9+
in_file = '/Users/f.cravogomes/Desktop/Cloned Repos/PRISME-Brain-Power-Calculator/data/s_slim_fc_rosenblatt.mat';
10+
out_file = '/Users/f.cravogomes/Desktop/Cloned Repos/PRISME-Brain-Power-Calculator/data/s_slim_fc_rosenblatt_fabi.mat';
11+
12+
fprintf('Loading %s\n', in_file);
13+
Dataset = load(in_file);
14+
outcome = Dataset.outcome;
15+
study_info = Dataset.study_info;
16+
17+
conds = fieldnames(Dataset.brain_data);
18+
19+
for c = 1:numel(conds)
20+
cond = conds{c};
21+
S = Dataset.brain_data.(cond);
22+
23+
% data is edge × subject: a bad subject is any column containing a NaN
24+
good = ~any(isnan(S.data), 1); % 1 × n_subjects logical
25+
n_bad = sum(~good);
26+
27+
if n_bad == 0
28+
fprintf('%s: no NaN subjects, unchanged (%d subjects)\n', cond, numel(good));
29+
continue;
30+
end
31+
32+
% Subset every subject-aligned field by the SAME column selection
33+
S.data = S.data(:, good);
34+
S.sub_ids = S.sub_ids(good);
35+
S.sub_ids_motion = S.sub_ids_motion(good);
36+
S.motion = S.motion(good);
37+
38+
assert(~any(isnan(S.data(:))), '%s: NaN remaining after removal', cond);
39+
40+
brain_data.(cond) = S;
41+
end
42+
43+
% Save under the ORIGINAL variable name so downstream loaders still work.
44+
fprintf('Saving %s\n', out_file);
45+
save(out_file, 'brain_data', 'outcome', 'study_info', '-v7.3');
46+
47+
fprintf('Done.\n');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function transpose_ukb_brain_data()
2+
origin = '/Users/f.cravogomes/Desktop/Cloned Repos/PRISME-Brain-Power-Calculator/data/s_ukb_fc_jiang.mat';
3+
dest = '/Users/f.cravogomes/Desktop/Cloned Repos/PRISME-Brain-Power-Calculator/data/s_ukb_fc_fabricio.mat';
4+
5+
S = load(origin);
6+
7+
conds = fieldnames(S.brain_data);
8+
for c = 1:numel(conds)
9+
S.brain_data.(conds{c}).data = S.brain_data.(conds{c}).data.';
10+
end
11+
12+
builtin('save', dest, '-struct', 'S', '-v7.3');
13+
14+
end

power_calculator_tools/.DS_Store

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)