-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_tissue_models.py
97 lines (77 loc) · 2.77 KB
/
get_tissue_models.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import nibabel as nib
from pathlib import Path
import copy
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import pandas as pd
import statsmodels.api as sm
import SimpleITK as sitk
output_folder = Path("training_ours_reg/raw")
image_files = [i for i in (output_folder / Path("training-images")).rglob("*.nii.gz")]
label_files = [
Path(str(i.parent).replace("images", "labels"))
/ Path(Path(i.stem).stem + "_3C.nii.gz")
for i in image_files
]
CSFLabel = 1
GMLabel = 2
WMLabel = 3
BGLabel = 0
def calc_prob(image, label, plot=False):
image = image.astype(int)
csf = image * (label == CSFLabel)
gm = image * (label == GMLabel)
wm = image * (label == WMLabel)
bg = image * (label == BGLabel)
image = image.flatten()
label = label.flatten()
onlybrain = image[label != 0]
mask = label[label != 0]
csf = onlybrain[mask == CSFLabel]
gm = onlybrain[mask == GMLabel]
wm = onlybrain[mask == WMLabel]
bins = tuple(range(0, np.amax(onlybrain) + 1))
CSF_hist, _ = np.histogram(csf, bins, density=True)
GM_hist, _ = np.histogram(gm, bins, density=True)
WM_hist, _ = np.histogram(wm, bins, density=True)
bins = bins[:-1]
# plot histograms
if plot:
plt.plot(bins, CSF_hist, bins, GM_hist, bins, WM_hist)
# plt.title("CSF Histogram")
plt.xlabel("Intensity")
plt.ylabel("Probability")
plt.legend(["CSF", "GM", "WM"])
plt.savefig(output_folder / Path("intensity_hist.png"))
plt.show()
return WM_hist, GM_hist, CSF_hist
def normalize(image, max_val):
image = ((image - np.amin(image)) / (np.amax(image) - np.amin(image))) * float(
max_val
)
image = image.astype(int)
image = image.astype(np.float64)
return image
def main(image_files, label_files):
images = [sitk.GetArrayFromImage(sitk.ReadImage(str(i))) for i in image_files]
labels = [sitk.GetArrayFromImage(sitk.ReadImage(str(i))) for i in label_files]
skull_stripped_normalized = [normalize(i, 255) for i in images]
p_intensity_wm, p_intensity_gm, p_intensity_csf = calc_prob(
np.stack(skull_stripped_normalized), np.stack(labels)
)
prob_df = pd.DataFrame(
{
"intensity": range(len(p_intensity_csf)),
"p_intensity_wm": p_intensity_wm,
"p_intensity_gm": p_intensity_gm,
"p_intensity_csf": p_intensity_csf,
}
)
prob_df.to_csv("tissue_map.csv")
prob_df["p_intensity_gm"] = prob_df["p_intensity_gm"].bfill().ffill()
prob_df["p_intensity_wm"] = prob_df["p_intensity_wm"].bfill().ffill()
prob_df["p_intensity_csf"] = prob_df["p_intensity_csf"].bfill().ffill()
prob_df.to_csv("tissue_map_imputed.csv")
if __name__ == "__main__":
main(image_files, label_files)