|
1 | | -classdef Fast_TFCE_cpp |
2 | | - |
3 | | - properties |
4 | | - level = "edge"; |
5 | | - permutation_based = true; |
6 | | - permutations = 800; % Override permutation number |
7 | | - method_params = Fast_TFCE_cpp.get_fast_tfce_params() |
8 | | - end |
9 | | - |
10 | | - methods (Static, Access = private) |
11 | | - function method_params = get_fast_tfce_params() |
12 | | - method_params = struct(); |
13 | | - method_params.dh = 0.1; |
14 | | - method_params.H = 3.0; |
15 | | - method_params.E = 0.4; |
| 1 | + classdef Fast_TFCE_cpp |
| 2 | + |
| 3 | + properties |
| 4 | + level = "edge"; |
| 5 | + permutation_based = true; |
| 6 | + permutations = 800; % Override permutation number |
| 7 | + method_params = Fast_TFCE_cpp.get_fast_tfce_params() |
16 | 8 | end |
17 | | - end |
18 | 9 |
|
19 | | - methods |
20 | | - |
21 | | - function pval = run_method(obj,varargin) |
22 | | - |
23 | | - % Applies Threshold-Free Cluster Enhancement (TFCE) and computes p-values |
24 | | - % using a permutation-based approach. |
25 | | - % |
26 | | - % Inputs: |
27 | | - % - STATS: Structure containing statistical parameters, including threshold. |
28 | | - % - edge_stats: Raw test statistics for edges. |
29 | | - % - permutation_edge_data: Precomputed permutation edge statistics. |
30 | | - % |
31 | | - % Outputs: |
32 | | - % - pval: TFCE-corrected p-values. |
33 | | - |
34 | | - params = struct(varargin{:}); |
35 | | - |
36 | | - % Extract relevant inputs |
37 | | - STATS = params.statistical_parameters; |
38 | | - edge_stats = params.edge_stats; |
39 | | - permuted_edge_stats = params.permuted_edge_data; % Explicitly using the new argument |
40 | | - |
41 | | - % Convert the edge statistics back into a matrix |
42 | | - test_stat_mat = STATS.unflatten_matrix(edge_stats); |
43 | | - |
44 | | - % Apply TFCE transformation to the observed test statistics |
45 | | - cluster_stats_target = apply_tfce_cpp(test_stat_mat, obj.method_params.dh, ... |
46 | | - obj.method_params.H, obj.method_params.E); |
47 | | - cluster_stats_target = flat_matrix(cluster_stats_target, STATS.mask); |
48 | | - |
49 | | - % Ensure permutation data is provided |
50 | | - if isempty(permuted_edge_stats) |
51 | | - error('Permutation data is missing. Ensure precomputed permutations are provided.'); |
| 10 | + methods (Static, Access = private) |
| 11 | + function method_params = get_fast_tfce_params() |
| 12 | + method_params = struct(); |
| 13 | + method_params.dh = 0.1; |
| 14 | + method_params.H = 3.0; |
| 15 | + method_params.E = 0.4; |
52 | 16 | end |
| 17 | + end |
53 | 18 |
|
54 | | - % Number of permutations |
55 | | - if size(permuted_edge_stats, 2) < obj.permutations |
56 | | - K = size(permuted_edge_stats, 2); |
57 | | - else |
58 | | - K = obj.permutations; |
59 | | - end |
60 | | - null_dist = zeros(K, 1); |
61 | | - |
62 | | - % Apply TFCE transformation to each permutation |
63 | | - for i = 1:K |
64 | | - perm_stat_mat = STATS.unflatten_matrix(permuted_edge_stats(:, i)); |
65 | | - tfce_null = apply_tfce_cpp(perm_stat_mat, obj.method_params.dh, ... |
| 19 | + methods |
| 20 | + |
| 21 | + function pval = run_method(obj,varargin) |
| 22 | + |
| 23 | + % Applies Threshold-Free Cluster Enhancement (TFCE) and computes p-values |
| 24 | + % using a permutation-based approach. |
| 25 | + % |
| 26 | + % Inputs: |
| 27 | + % - STATS: Structure containing statistical parameters, including threshold. |
| 28 | + % - edge_stats: Raw test statistics for edges. |
| 29 | + % - permutation_edge_data: Precomputed permutation edge statistics. |
| 30 | + % |
| 31 | + % Outputs: |
| 32 | + % - pval: TFCE-corrected p-values. |
| 33 | + |
| 34 | + params = struct(varargin{:}); |
| 35 | + |
| 36 | + % Extract relevant inputs |
| 37 | + STATS = params.statistical_parameters; |
| 38 | + edge_stats = params.edge_stats; |
| 39 | + permuted_edge_stats = params.permuted_edge_data; % Explicitly using the new argument |
| 40 | + |
| 41 | + % Convert the edge statistics back into a matrix |
| 42 | + test_stat_mat = STATS.unflatten_matrix(edge_stats); |
| 43 | + |
| 44 | + % Apply TFCE transformation to the observed test statistics |
| 45 | + cluster_stats_target = apply_tfce_cpp(test_stat_mat, obj.method_params.dh, ... |
66 | 46 | obj.method_params.H, obj.method_params.E); |
67 | | - null_dist(i) = max(tfce_null(:)); % Store max TFCE value for permutation |
| 47 | + cluster_stats_target = STATS.flatten_matrix(cluster_stats_target); |
| 48 | + |
| 49 | + % Ensure permutation data is provided |
| 50 | + if isempty(permuted_edge_stats) |
| 51 | + error('Permutation data is missing. Ensure precomputed permutations are provided.'); |
| 52 | + end |
| 53 | + |
| 54 | + % Number of permutations |
| 55 | + if size(permuted_edge_stats, 2) < obj.permutations |
| 56 | + K = size(permuted_edge_stats, 2); |
| 57 | + else |
| 58 | + K = obj.permutations; |
| 59 | + end |
| 60 | + null_dist = zeros(K, 1); |
| 61 | + |
| 62 | + % Apply TFCE transformation to each permutation |
| 63 | + for i = 1:K |
| 64 | + perm_stat_mat = STATS.unflatten_matrix(permuted_edge_stats(:, i)); |
| 65 | + tfce_null = apply_tfce_cpp(perm_stat_mat, obj.method_params.dh, ... |
| 66 | + obj.method_params.H, obj.method_params.E); |
| 67 | + null_dist(i) = max(tfce_null(:)); % Store max TFCE value for permutation |
| 68 | + end |
| 69 | + |
| 70 | + % Compute p-values using permutation-based FWER correction |
| 71 | + pval = arrayfun(@(stat) (sum(stat <= null_dist)) /K, cluster_stats_target(:)); |
| 72 | + |
68 | 73 | end |
69 | | - |
70 | | - % Compute p-values using permutation-based FWER correction |
71 | | - pval = arrayfun(@(stat) (sum(stat <= null_dist)) /K, cluster_stats_target(:)); |
72 | | - |
| 74 | + |
73 | 75 | end |
74 | | - |
| 76 | + |
75 | 77 | end |
76 | | - |
77 | | -end |
78 | | - |
| 78 | + |
79 | 79 |
|
0 commit comments