@@ -35,37 +35,33 @@ def calculate_threshold_statistics_map(
35
35
# Initialize an empty map to store threshold statistics
36
36
threshold_statistics_map = np .zeros_like (parcellated_masks [0 ], dtype = float )
37
37
38
- # Iterate over each parcellated mask
39
38
for mask_idx , mask in enumerate (parcellated_masks ):
39
+ import time
40
+ t = time .time ()
41
+
40
42
# Get voxel coordinates where mask is True using np.nonzero
41
43
voxel_indices = np .nonzero (mask )
42
44
print (f"Mask { mask_idx } with { len (voxel_indices [0 ])} voxels." )
43
45
44
- # Get memory usage in bytes
45
- import psutil
46
- memory_usage_bytes = psutil .Process ().memory_info ().rss
47
- print (f"Memory usage: { memory_usage_bytes / 1024 ** 2 } MB" )
48
-
49
- # Iterate over voxel coordinates
50
- for voxel_idx in zip (* voxel_indices ):
51
- voxel_values = []
46
+ voxel_values = np .zeros ((len (accuracy_maps ), len (voxel_indices [0 ])))
52
47
53
- # Load accuracy maps from image directory
54
- for accuracy_map_path in accuracy_maps :
55
- accuracy_map_img = nib .load (accuracy_map_path )
56
- accuracy_map_data = accuracy_map_img . get_fdata ()
48
+ # Load accuracy maps and extract voxel values
49
+ for map_idx , accuracy_map in enumerate ( accuracy_maps ) :
50
+ accuracy_map_data = nib .load (accuracy_map ). get_fdata ( )
51
+ voxel_values [ map_idx , :] = accuracy_map_data [ voxel_indices ]
57
52
58
- # Get voxel value from accuracy map at current voxel index
59
- voxel_value = accuracy_map_data [ voxel_idx ]
60
- voxel_values . append ( voxel_value )
53
+ # Calculate the threshold statistic for each voxel using np.percentile
54
+ percentile_value = 100 * ( 1 - p_value_threshold )
55
+ threshold_statistic = np . percentile ( voxel_values , percentile_value , axis = 0 )
61
56
62
- # Calculate the threshold statistic using np.percentile
63
- threshold_statistic = np .percentile (
64
- voxel_values , 100 * (1 - p_value_threshold )
65
- )
57
+ # Assign the computed threshold statistic to the map at the voxel indices
58
+ threshold_statistics_map [voxel_indices ] = threshold_statistic
66
59
67
- # Store the threshold statistic in the map at the current voxel index
68
- threshold_statistics_map [voxel_idx ] = threshold_statistic
60
+ import psutil
61
+ # Get memory usage in bytes after processing each mask
62
+ memory_usage_bytes = psutil .Process ().memory_info ().rss
63
+ print (f"Memory usage: { memory_usage_bytes / 1024 ** 2 } MB" )
64
+ print (f"Time for parcellation: { time .time ()- t } s" )
69
65
70
66
return threshold_statistics_map
71
67
@@ -361,6 +357,7 @@ def keep_significant_clusters(accuracy_map, accuracy_cluster_map, cluster_size):
361
357
permutation_dir = Path ("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/permutations/random_accuracy_maps" )
362
358
brain_mask = Path ("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/permutations/brain_mask.nii.gz" )
363
359
# This represents the true original accuracy map. Here we use a map generated by generate_fake_clusters
360
+ # This map represents the group accuracy map, that is the mean of the single-subject accuracy maps
364
361
accuracy_map = Path ("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/permutations/clustered_accuracy_map.nii.gz" )
365
362
base_output_path = Path ("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/stat_results" )
366
363
perm_output_path = Path (base_output_path , "permuted" )
0 commit comments