|
| 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