-
Notifications
You must be signed in to change notification settings - Fork 5
/
median_levelset.py
81 lines (70 loc) · 2.48 KB
/
median_levelset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import nibabel as nb
import os
import numpy as np
from math import sqrt
import pandas as pd
'''
Finding median levelset surface in a group of subjects
----------------------------------------------------------
* Computes the mean distance of each surface to the average in a narrow band
* Original idea Christine Tardif
'''
# desired path for output csv file
out = '/scr/ilz3/myelinconnect/surfaces/median.csv'
# get list of right and left hemisphere surfaces
rh_surf_dir = '/scr/ilz3/myelinconnect/surfaces/surf_rh/orig/mid_surface/'
rh_surf_files = os.listdir(rh_surf_dir)
lh_surf_dir = '/scr/ilz3/myelinconnect/surfaces/surf_lh/orig/mid_surface/'
lh_surf_files = os.listdir(lh_surf_dir)
# create average right and left hemisphere surfaces
os.chdir(rh_surf_dir)
rh_avg = np.empty_like(nb.load(rh_surf_files[0]).get_data())
for surf in rh_surf_files:
surf_data = nb.load(surf).get_data()
rh_avg += surf_data
rh_avg = rh_avg / len(rh_surf_files)
os.chdir(lh_surf_dir)
lh_avg = np.empty_like(nb.load(lh_surf_files[0]).get_data())
for surf in lh_surf_files:
surf_data = nb.load(surf).get_data()
lh_avg += surf_data
lh_avg = lh_avg / len(lh_surf_files)
# compute distances in narrowband of sqrt(3)
os.chdir(rh_surf_dir)
rh_distances = dict()
for surf in rh_surf_files:
surf_data = nb.load(surf).get_data()
mask = (surf_data <= sqrt(3)) & (surf_data >= -sqrt(3))
narrowband = surf_data[mask]
avg_narrowband = rh_avg[mask]
distance = abs(avg_narrowband - narrowband)
mean_distance = np.mean(distance)
rh_distances[surf[0:4]] = mean_distance
os.chdir(lh_surf_dir)
lh_distances = dict()
for surf in lh_surf_files:
surf_data = nb.load(surf).get_data()
mask = (surf_data <= sqrt(3)) & (surf_data >= -sqrt(3))
narrowband = surf_data[mask]
avg_narrowband = lh_avg[mask]
distance = abs(avg_narrowband - narrowband)
mean_distance = np.mean(distance)
lh_distances[surf[0:4]] = mean_distance
# compute mean distances across left and right hemisphere for each subject
lh_dist = []
rh_dist = []
dist = []
subjects = []
for sub in rh_distances.keys():
rh_val = rh_distances[sub]
rh_dist.append(rh_val)
lh_val = lh_distances[sub]
lh_dist.append(lh_val)
mean_val = (rh_val + lh_val) / 2
dist.append(mean_val)
subjects.append(sub)
# write data to csv file
df = pd.DataFrame(zip(subjects, rh_dist, lh_dist, dist),
columns=["subject", "rh distance",
"lh distance", "mean_distance"])
df.to_csv(out)