Skip to content

Commit be1ad2e

Browse files
committed
Added gitignore
1 parent bdb320c commit be1ad2e

18 files changed

+200
-76
lines changed

.DS_Store

14 KB
Binary file not shown.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data/*
2+
power_calculator_results/*
3+
*.asv
4+
/test_power_calculator/*

NBS_addon/NBSedge_level_parametric_corr.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,15 @@
8080
return;
8181
end
8282

83-
83+
%% CHECK IF NUMBER OF UNIQUE SUBJECTS
8484
if strcmp(GLM.test,'onesample')
8585
% Calculate degrees of freedom for one-sample t-test
8686
df = GLM.n_observations - 1;
8787
% Calculate uncorrected p-values from the t distribution
8888
p_uncorr = tcdf(-edge_stats__target, df);
8989
elseif strcmp(GLM.test,'ttest')
90-
%if strcmp(ttest_type,'paired') % TODO: create
91-
% warning('Assuming paired sample and right-tailed t-test.');
92-
df = GLM.n_observations/2-1; % observations are 2 per subject
93-
%elseif strcmp(ttest_type,'unpaired')
94-
% df=GLM.n_observations-2; % assuming equal variances
95-
%end
90+
df = GLM.n_observations - 2;
91+
9692
p_uncorr = tcdf(-edge_stats__target, df);
9793
elseif strcmp(GLM.test,'ftest')
9894
error('Under development.');

NBS_addon/NBSglm_smn.m

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,26 @@
4747

4848
test_stat=zeros(1, GLM.n_GLMs);
4949

50-
beta = zeros(GLM.n_predictors,GLM.n_GLMs);
5150
beta = GLM.X\GLM.y;
5251

52+
% Round beta to the nearest multiple of 1e-14 to remove numerical issues
53+
beta = round(beta, 14);
54+
5355
%Compute statistic of interest
5456
% TODO: consider moving to switch/case in glm_setup
55-
if strcmp(GLM.test,'onesample')
57+
if strcmp(GLM.test,'onesample') || strcmp(GLM.test,'ttest')
5658

5759
% Compute the residuals
5860
resid = GLM.y - GLM.X * beta;
5961
% Mean squared error
6062
mse = sum(resid.^2) / (GLM.n_observations - GLM.n_predictors);
6163
% Compute the standard error using contrast
6264
se = sqrt(mse .* (GLM.contrast / (GLM.X' * GLM.X) * GLM.contrast'));
65+
% Prevent division by zero - numerical fix
66+
se(se < 1e-15) = 1e-15;
6367
% Compute the t-statistic
6468
test_stat(:) = (GLM.contrast * beta) ./ se;
6569

66-
elseif strcmp(GLM.test,'ttest')
67-
68-
resid=zeros(GLM.n_observations,GLM.n_GLMs);
69-
mse=zeros(GLM.n_observations,GLM.n_GLMs);
70-
resid=GLM.y-GLM.X*beta;
71-
mse=sum(resid.^2)/(GLM.n_observations-GLM.n_predictors);
72-
se=sqrt(mse*(GLM.contrast*inv(GLM.X'*GLM.X)*GLM.contrast'));
73-
test_stat(:)=(GLM.contrast*beta)./se;
74-
7570
elseif strcmp(GLM.test,'ftest')
7671

7772
sse=zeros(1,GLM.n_GLMs);

NBS_benchmarking/setup_benchmarking.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@
5050
create_test_contrast(RepParams.test_type, RepParams.n_subs_subset);
5151

5252

53+
%% Number of observations
54+
switch RepParams.test_type
55+
56+
case 't'
57+
RepParams.observations = RepParams.n_subs_subset;
58+
59+
case 't2'
60+
RepParams.observations = RepParams.n_subs_subset_c1 + RepParams.n_subs_subset_c2;
61+
62+
case 'r'
63+
RepParams.observations = RepParams.n_subs_subset;
64+
65+
otherwise
66+
error('Test type not supported')
67+
68+
end
69+
70+
71+
5372
%% Assign params to structures
5473
% Goal: should be able to run config file, load rep_params and UI from reference, and replicate reference results
5574

@@ -90,7 +109,7 @@
90109

91110
%% Set up DIMS
92111
UI.DIMS.nodes = RepParams.n_nodes;
93-
UI.DIMS.observations = RepParams.n_subs_subset;
112+
UI.DIMS.observations = RepParams.observations;
94113
UI.DIMS.predictors = size(RepParams.X_rep, 2);
95114

96115

0 Bytes
Binary file not shown.

config_files/setparams.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
% NBS toolbox
1212
Params.save_directory = './power_calculator_results/';
13-
%Params.data_dir = './data/s_abcd_fc_rosenblatt.mat';
14-
Params.data_dir = './data/s_hcp_fc_noble_tasks.mat';
13+
Params.data_dir = './data/s_abcd_fc_rosenblatt.mat';
14+
%Params.data_dir = './data/s_hcp_fc_noble_tasks.mat';
1515
Params.gt_data_dir = './power_calculator_results/ground_truth/';
1616

1717
Params.gt_origin = 'power_calculator';
@@ -42,8 +42,8 @@
4242
Params.n_frames.REST2 = 1200;
4343

4444
%%% Resampling parameters %%%
45-
Params.n_workers = 5; % num parallel workers for parfor, best if # workers = # cores
4645
Params.parallel = false; % run stuff sequentially or in parallel
46+
Params.n_workers = 8; % num parallel workers for parfor, best if # workers = # cores
4747
Params.n_repetitions = 500; % 500 recommended
4848

4949
%% List of subjects per subset

plot_scripts/.DS_Store

6 KB
Binary file not shown.

plot_scripts/mean_power_plot.m

Whitespace-only changes.

plot_scripts/plot_power_results.m

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
function plot_power_results(dataset_or_directory)
2+
% If input is a dataset name, construct the expected directory path
3+
base_dir = './power_calculator_results/power_calculation/';
4+
5+
if isfolder(fullfile(base_dir, dataset_or_directory))
6+
data_dir = fullfile(base_dir, dataset_or_directory);
7+
elseif isfolder(dataset_or_directory)
8+
data_dir = dataset_or_directory; % Assume full directory path is given
9+
else
10+
error('Invalid dataset name or directory: %s', dataset_or_directory);
11+
end
12+
13+
% Get list of all result files in directory
14+
files = dir(fullfile(data_dir, '*.mat'));
15+
if isempty(files)
16+
error('No result files found in the specified directory: %s', data_dir);
17+
end
18+
19+
% Initialize structure to store power results
20+
power_results = struct();
21+
unique_subject_numbers = [];
22+
23+
% Process each file
24+
for i = 1:numel(files)
25+
file_path = fullfile(files(i).folder, files(i).name);
26+
data = load(file_path);
27+
28+
% Ensure meta-data and TPR exist
29+
if ~isfield(data, 'power_data') || ~isfield(data.meta_data, 'subject_number') || ...
30+
~isfield(data, 'meta_data') || ~isfield(data.power_data, 'tpr')
31+
warning('Skipping file (missing meta_data or brain_data.tpr): %s', files(i).name);
32+
continue;
33+
end
34+
35+
% Extract subject number from meta_data
36+
n_subjects = data.meta_data.subject_number;
37+
38+
% Keep track of unique subject numbers
39+
if ~ismember(n_subjects, unique_subject_numbers)
40+
unique_subject_numbers = [unique_subject_numbers, n_subjects]; %#ok<AGROW>
41+
end
42+
43+
% Extract power values (vector across tasks)
44+
tpr_values = data.power_data.tpr(:);
45+
46+
% Extract method name from meta_data
47+
method_name = data.meta_data.test_type;
48+
test_components = get_test_components_from_meta_data(data.meta_data.test_components);
49+
50+
% Define field path
51+
field_path = {method_name, sprintf('n_%d', n_subjects), test_components};
52+
53+
% Assign value safely using `setfield`
54+
power_results = setfield(power_results, field_path{:}, mean(tpr_values));
55+
56+
end
57+
58+
% Check if we have results
59+
if isempty(fieldnames(power_results))
60+
error('No valid power results found.');
61+
end
62+
63+
% Sort subject numbers and methods
64+
unique_subject_numbers = sort(unique_subject_numbers);
65+
subject_labels = arrayfun(@(x) sprintf('nsub %d', x), unique_subject_numbers, 'UniformOutput', false);
66+
num_subjects = numel(unique_subject_numbers);
67+
method_names = fieldnames(power_results);
68+
plot_method_names = strrep(method_names, '_', ' ');
69+
num_methods = numel(method_names);
70+
71+
% Generate a figure with subplots (one per subject number)
72+
num_subplots = numel(unique_subject_numbers);
73+
figure;
74+
set(gcf, 'Position', [100, 100, 200 * num_subplots, 500]); % Adjust figure size
75+
76+
77+
% Collect power data
78+
for i = 1:num_subjects
79+
n_subjects = unique_subject_numbers(i);
80+
for j = 1:num_methods
81+
method_name = method_names{j};
82+
83+
if isfield(power_results.(method_name), sprintf('n_%d', n_subjects))
84+
% Extract all task-specific power values
85+
task_values = struct2cell(power_results.(method_name).(sprintf('n_%d', n_subjects)));
86+
87+
% Convert to array
88+
all_task_values = cell2mat(task_values);
89+
90+
% Compute mean and standard error
91+
mean_power(i, j) = mean(all_task_values);
92+
error_power(i, j) = std(all_task_values) / sqrt(length(all_task_values));
93+
end
94+
end
95+
end
96+
97+
% Create figure
98+
figure;
99+
hold on;
100+
101+
% Define colors for each method
102+
colors = lines(num_methods);
103+
104+
% Create grouped bar plot
105+
bar_handle = bar(subject_labels, mean_power, 'grouped');
106+
107+
% Apply colors and add error bars
108+
for j = 1:num_methods
109+
bar_handle(j).FaceColor = colors(j, :); % Assign color per method
110+
x_positions = bar_handle(j).XEndPoints;
111+
errorbar(x_positions, mean_power(:, j), error_power(:, j), 'k.', 'LineWidth', 1.5);
112+
end
113+
114+
% Formatting
115+
xlabel('Number of Subjects');
116+
ylabel('Average Power (%)');
117+
ylim([0, 100]);
118+
legend(plot_method_names, 'Location', 'northwest', 'Interpreter', 'none', 'FontSize', 12);
119+
grid on;
120+
hold off;
121+
122+
dataset_or_directory = strrep(dataset_or_directory, '_', ' ');
123+
% Main title
124+
sgtitle(sprintf('Power Calculation Results for %s', dataset_or_directory), ...
125+
'FontSize', 16, 'FontWeight', 'bold');
126+
127+
end
128+

0 commit comments

Comments
 (0)