Skip to content

Commit e4374b2

Browse files
committed
Changed output directory structure to be more organized
1 parent f6f135b commit e4374b2

File tree

12 files changed

+193
-462
lines changed

12 files changed

+193
-462
lines changed

NBS_benchmarking/summary_tools.m

Lines changed: 24 additions & 414 deletions
Large diffs are not rendered by default.

TFCE_Benchmarkin/compare_full_tfce.m

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,41 @@
66
n = 268; % Full connectivity matrix for 268 ROIs (35,778 edges)
77
img = zeros(n, n);
88

9-
% Create random functional connectivity values between -1 and 1
10-
% Fill the upper triangular part of the matrix with random FC values
9+
% Parameters for peak generation
10+
num_peaks = 8; % Number of functional network "hubs"
11+
peak_width = 15; % Width of Gaussian peaks (in ROI units)
12+
peak_strength = 0.99; % Maximum correlation strength
13+
background_noise = 0.15; % Background connectivity noise level
14+
15+
% Generate random peak centers (representing functional network hubs)
16+
rng(20); % Set seed for reproducibility
17+
peak_centers = randi([1, n], num_peaks, 2);
18+
19+
% Create the connectivity matrix
1120
for i = 1:n
1221
for j = (i+1):n
13-
img(i,j) = (rand() * 2) - 1; % Random value between -1 and 1
22+
% Start with Gaussian background noise
23+
value = randn() * background_noise;
24+
25+
% Add Gaussian peaks centered at network hubs
26+
for p = 1:num_peaks
27+
% Distance from this edge to the peak center
28+
dist_i = min(abs(i - peak_centers(p, 1)), abs(i - peak_centers(p, 2)));
29+
dist_j = min(abs(j - peak_centers(p, 1)), abs(j - peak_centers(p, 2)));
30+
dist = sqrt(dist_i^2 + dist_j^2);
31+
32+
% Add Gaussian contribution from this peak
33+
value = value + peak_strength * exp(-dist^2 / (2 * peak_width^2));
34+
end
35+
36+
% Add some distance-dependent decay (closer ROIs more correlated)
37+
distance_factor = exp(-abs(i-j) / 100);
38+
value = value + 0.2 * distance_factor * randn();
39+
40+
img(i, j) = value;
1441
end
1542
end
1643

17-
1844
% Make it symmetric
1945
img = img + img';
2046

@@ -87,6 +113,9 @@
87113
title('Absolute Difference', 'FontSize', 18, 'FontWeight', 'bold');
88114
colormap(gca, 'redblue');
89115
colorbar('FontSize', 12);
116+
tfce_max = max([max(tfced1(:)), max(tfced2(:))]);
117+
caxis([0, 1]);
90118
set(gca, 'FontSize', 14);
91119
xlabel('ROI', 'FontSize', 14);
92-
ylabel('ROI', 'FontSize', 14);
120+
ylabel('ROI', 'FontSize', 14);
121+

calculate_power.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ function calculate_power(varargin)
6666
[Params.output, ~, ~] = get_data_set_name(Study_Info, Params);
6767

6868
%% Process each repetition file one by one to reduce memory usage
69-
rep_files = dir(fullfile(Params.save_directory, Params.output, '*.mat'));
69+
rep_files = dir(fullfile(Params.save_directory, Params.output, "repetitions",'*.mat'));
7070

7171
% If no files were found output an error
7272
if isempty(rep_files)
7373
error('No files found.')
7474
end
7575

7676
%% Create output directory (only if it doesn't exist)
77-
Params = create_power_output_directory(Params);
77+
[Params.gt_data_dir, Params.gt_output] = setup_gt_directory(Params);
78+
Params.save_directory = create_power_output_directory(Params);
7879

7980
for i = 1:length(rep_files)
8081
% Load a single repetition data file
@@ -86,7 +87,6 @@ function calculate_power(varargin)
8687
method_list = rep_data.meta_data.method_list;
8788

8889

89-
9090
[test_components, test_type, sub_number, testing_code] = get_data_for_file_naming(rep_data.meta_data);
9191
[~, file_name] = create_and_check_rep_file(Params.save_directory, Params.output, test_components, ...
9292
test_type, ...
@@ -116,9 +116,9 @@ function calculate_power(varargin)
116116
method = method_list{j};
117117
method_data = rep_data.(method);
118118

119-
gt_filename = construct_gt_filename(rep_data.meta_data, Params.output);
120-
gt_fullpath = fullfile(Params.gt_data_dir, Params.output, gt_filename);
121-
119+
gt_filename = construct_gt_filename(rep_data.meta_data, Params.gt_output);
120+
gt_fullpath = fullfile(Params.gt_data_dir, gt_filename);
121+
122122
if exist(gt_filename, 'file')
123123
gt_data = load(gt_fullpath);
124124
else

config_files/setparams.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
% Params.data_dir = './data/s_abcd_fc_rosenblatt.mat';
3232
Params.data_dir = './data/s_hcp_fc_noble_tasks.mat';
3333
% Params.data_dir = './data/s_hcp_act_noble_1.mat';
34-
Params.output = 'tfce_power_comp';
34+
Params.output = 'test_new_output_struct';
3535

3636
% Save specifications - if NaN output becomes dataset file name
3737
Params.save_directory = './power_calculator_results/';
38-
Params.gt_data_dir = './power_calculator_results/ground_truth/';
38+
% Params.gt_data_dir = './power_calculator_results/';
3939

4040

4141

@@ -56,7 +56,7 @@
5656
Params.other_scripts_dir='./NBS_benchmarking/support_scripts/';
5757

5858
%%% Resampling parameters %%%
59-
Params.parallel = true; % run stuff sequentially or in parallel
59+
Params.parallel = false; % run stuff sequentially or in parallel
6060
Params.n_workers = 5; % num parallel workers for parfor, best if # workers = # cores
6161
Params.n_repetitions = 500; % 500 recommended
6262
Params.batch_size = 5;
@@ -92,8 +92,8 @@
9292
%%%%% DEVELOPERS ONLY %%%%%
9393
% Use a small subset of permutations for faster development -- inappropriate for inference
9494

95-
Params.testing = false;
96-
Params.test_n_perms = 50;
95+
Params.testing = true;
96+
Params.test_n_perms = 10;
9797
Params.test_n_repetitions = 5;
9898
Params.test_n_workers = 1;
9999
Params.test_disable_save = false;

data_fit_scripts/get_output_file_statistics.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
function results = get_output_file_statistics(file_name)
1+
function results = get_output_file_statistics(file_name, varargin)
2+
3+
% Input Parser
4+
p = inputParser;
5+
6+
addOptional(p, 'attribute', 'tpr');
7+
parse(p, varargin{:});
8+
attribute = p.Results.attribute;
9+
210

311
results = struct();
412
results.mean = struct();
@@ -11,8 +19,8 @@
1119
for m = 1:length(method_list)
1220
method_name = method_list{m};
1321

14-
results.mean.(method_name) = mean(data.(method_name).tpr, 'omitnan');
15-
results.deviation.(method_name) = std(data.(method_name).tpr, 'omitnan');
22+
results.mean.(method_name) = mean(data.(method_name).(attribute), 'omitnan');
23+
results.deviation.(method_name) = std(data.(method_name).(attribute), 'omitnan');
1624

1725
end
1826

data_fit_scripts/multi_experiment_average.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
function results_cell = multi_experiment_average(input_path)
1+
function results_cell = multi_experiment_average(input_path, varargin)
2+
3+
% Input Parser
4+
p = inputParser;
5+
6+
addOptional(p, 'attribute_name_calculation', 'tpr');
7+
8+
parse(p, varargin{:});
9+
attribute = p.Results.attribute_name_calculation;
210

311
%% Check if input is multiple files or single
412
multiple_files = false;
@@ -36,7 +44,7 @@
3644
for fi = 1:num_files
3745
full_file = files{fi};
3846

39-
file_results = get_output_file_statistics(full_file);
47+
file_results = get_output_file_statistics(full_file, 'attribute', attribute);
4048

4149
results_cell{fi} = file_results;
4250
end

plot_scripts/plot_aggregated_power_curve.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ function plot_aggregated_power_curve(varargin)
1111
default_dir = '/Users/f.cravogomes/Desktop/Cloned Repos/Power_Calculator/power_calculator_results/power_calculation/tfce_power_comp';
1212
default_undesired_sub_numbers = {};
1313
default_map = map_tfce_comp;
14+
default_attribute_name = 'tpr';
1415

1516
% Add optional parameter
1617
addParameter(p, 'dir', default_dir);
1718
addParameter(p, 'undesired_subject_numbers', default_undesired_sub_numbers);
1819
addParameter(p, 'map_function', default_map);
20+
addParameter(p, 'attribute_name_calculation', default_attribute_name)
1921

2022
% Parse input
2123
parse(p, varargin{:});
2224

2325
directory = p.Results.dir;
2426
undesired_subject_numbers = p.Results.undesired_subject_numbers;
2527
map_function = p.Results.map_function;
28+
attribute = p.Results.attribute_name_calculation;
2629

2730
%% Check if input is a directory
2831
if ~isfolder(directory)
@@ -31,7 +34,7 @@ function plot_aggregated_power_curve(varargin)
3134
%%%%% Config end %%%%%%%%%%%%%%%%%%%%%%%%%%%
3235

3336
%% Get averages
34-
multi_variable_average = multi_experiment_average(directory);
37+
multi_variable_average = multi_experiment_average(directory, 'attribute_name_calculation', attribute);
3538

3639
data_agregator = struct();
3740

power_calculator_tools/create_output_directory.m

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Params = create_output_directory(Params)
1+
function save_directory = create_output_directory(Params)
22
%% create_output_directory
33
% **Description**
44
% Ensures the existence of the output directory for saving results. If the
@@ -22,30 +22,26 @@
2222
% **Author**: Fabricio Cravo
2323
% **Date**: March 2025
2424

25+
save_directory = [Params.save_directory, Params.output];
26+
27+
if ~exist(save_directory, 'dir') % Check if the directory does not exist
28+
mkdir(save_directory); % Create the directory
29+
end
30+
2531
if ~Params.ground_truth
2632

27-
if ~exist(Params.save_directory, 'dir') % Check if the directory does not exist
28-
mkdir(Params.save_directory); % Create the directory
29-
end
30-
31-
Params.save_directory = [Params.save_directory, Params.output, '/'];
33+
save_directory = [save_directory, '/repetitions/'];
3234

3335
if ~exist(Params.save_directory, 'dir') % Check if the directory does not exist
3436
mkdir(Params.save_directory); % Create the directory
3537
end
3638

3739
else
38-
39-
Params.save_directory = [Params.save_directory, 'ground_truth/'];
40-
41-
if ~exist(Params.save_directory, 'dir') % Check if the directory does not exist
42-
mkdir(Params.save_directory); % Create the directory
43-
end
4440

45-
Params.save_directory = [Params.save_directory, Params.output, '/'];
41+
save_directory = [save_directory, '/ground_truth/'];
4642

47-
if ~exist(Params.save_directory, 'dir') % Check if the directory does not exist
48-
mkdir(Params.save_directory); % Create the directory
43+
if ~exist(save_directory, 'dir') % Check if the directory does not exist
44+
mkdir(save_directory); % Create the directory
4945
end
5046

5147
end
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
function Params = create_power_output_directory(Params)
2-
3-
Params.save_directory = [Params.save_directory, 'power_calculation/'];
1+
function save_directory = create_power_output_directory(Params)
42

5-
if ~exist(Params.save_directory, 'dir') % Check if the directory does not exist
6-
mkdir(Params.save_directory); % Create the directory
7-
end
8-
9-
Params.save_directory = [Params.save_directory, Params.output, '/'];
3+
save_directory = [Params.save_directory, Params.output, '/power_calculation/'];
104

11-
if ~exist(Params.save_directory, 'dir') % Check if the directory does not exist
12-
mkdir(Params.save_directory); % Create the directory
5+
if ~exist(save_directory, 'dir') % Check if the directory does not exist
6+
mkdir(save_directory); % Create the directory
137
end
148

159
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function positives = get_significance_vector(is_fp, ids_pos_vec, ids_neg_vec, PowerRes, ...
2+
gt_size, stat_gt_level_str, pos_effect, neg_effect)
3+
%%
4+
% Calculates the absolute number of positives for both the tpr
5+
% calculation and fpr
6+
%
7+
% is_fpr - bool - if we are calculating the fpr we require negation
8+
% ids_pos_vec - bool array - varaibles with positive effect according to gt
9+
% ids_neg_vec - bool array - variables with negative effect acordin to gt
10+
% PowerRes - struct - current struct that stores the values (for gt accumulation)
11+
% gt_size - array - size of gt variable
12+
% stat_gt_level_str - level of statistics used
13+
% pos_effect - for whole-brain only, if there is a pos_effect
14+
% neg_effect - whole-brain only, if there is also a negative effect
15+
16+
if is_fp
17+
ids_pos = ~ids_pos_vec;
18+
ids_neg = ~ids_neg_vec;
19+
else
20+
ids_pos = ids_pos_vec;
21+
ids_neg = ids_neg_vec;
22+
end
23+
24+
% calculate TPR
25+
positives = nan(gt_size);
26+
if contains(stat_gt_level_str, 'variable')
27+
28+
positives(ids_pos)=PowerRes.positives_total(ids_pos);
29+
positives(ids_neg)=PowerRes.positives_total_neg(ids_neg);
30+
31+
elseif contains(stat_gt_level_str,'network')
32+
33+
positives(ids_pos)=PowerRes.positives_total(ids_pos);
34+
positives(ids_neg)=PowerRes.positives_total_neg(ids_neg);
35+
36+
elseif contains(stat_gt_level_str,'whole_brain')
37+
38+
positives = 0;
39+
if xor(pos_effect, is_fp)
40+
positives = positives + PowerRes.positives_total;
41+
end
42+
if xor(neg_effect, is_fp)
43+
positives = positives + PowerRes.positives_total_neg;
44+
end
45+
46+
%% Divide by two only if both positive and negative effects are present
47+
if xor(pos_effect, is_fp) && xor(neg_effect, is_fp)
48+
positives = floor(positives/2);
49+
end
50+
51+
end
52+
53+
end

0 commit comments

Comments
 (0)