Skip to content

Commit c7dafee

Browse files
authored
Merge pull request #4 from InstitutoDOr/stelzer_stats
speed up calculation
2 parents d08010b + fabe88c commit c7dafee

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

Code/permutation_stats.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,33 @@ def calculate_threshold_statistics_map(
3535
# Initialize an empty map to store threshold statistics
3636
threshold_statistics_map = np.zeros_like(parcellated_masks[0], dtype=float)
3737

38-
# Iterate over each parcellated mask
3938
for mask_idx, mask in enumerate(parcellated_masks):
39+
import time
40+
t = time.time()
41+
4042
# Get voxel coordinates where mask is True using np.nonzero
4143
voxel_indices = np.nonzero(mask)
4244
print(f"Mask {mask_idx} with {len(voxel_indices[0])} voxels.")
4345

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])))
5247

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

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

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
6659

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")
6965

7066
return threshold_statistics_map
7167

@@ -361,6 +357,7 @@ def keep_significant_clusters(accuracy_map, accuracy_cluster_map, cluster_size):
361357
permutation_dir = Path("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/permutations/random_accuracy_maps")
362358
brain_mask = Path("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/permutations/brain_mask.nii.gz")
363359
# 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
364361
accuracy_map = Path("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/permutations/clustered_accuracy_map.nii.gz")
365362
base_output_path = Path("/Users/sebastian.hoefle/projects/idor/brain-analysis/fixtures/stat_results")
366363
perm_output_path = Path(base_output_path, "permuted")

0 commit comments

Comments
 (0)