@sjcrh/proteinpaint-server 2.85.1 → 2.86.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -7
- package/routes/brainImaging.js +47 -29
- package/routes/termdb.boxplot.js +82 -27
- package/src/app.js +315 -218
- package/utils/plotBrainImaging.py +92 -53
|
@@ -1,69 +1,108 @@
|
|
|
1
1
|
import nibabel as nib # Library for loading data from neuroimaging file formats such as NIfTI
|
|
2
2
|
import matplotlib.pyplot as plt
|
|
3
|
+
import matplotlib.colors as mcolors
|
|
4
|
+
import matplotlib.cm as cm
|
|
3
5
|
import numpy as np
|
|
4
6
|
import sys
|
|
5
7
|
import io
|
|
8
|
+
import json
|
|
6
9
|
|
|
7
10
|
if len(sys.argv) <= 1:
|
|
8
|
-
print('python3 '+sys.argv[0]+' <path/to/template/file>
|
|
11
|
+
print('python3 '+sys.argv[0]+' <path/to/template/file> plane index filesJson.\n filesJson: dictionary containg sample files and color per category).\nplane: L (left, sagittal), F (front, coronal), T (top, axial)')
|
|
12
|
+
sys.exit(1)
|
|
13
|
+
|
|
14
|
+
plane = sys.argv[2]
|
|
15
|
+
if(plane != 'L' and plane != 'F' and plane != 'T'):
|
|
16
|
+
print('Invalid plane')
|
|
17
|
+
sys.exit(1)
|
|
18
|
+
index = sys.argv[3]
|
|
19
|
+
|
|
20
|
+
if(len(index) == 0):
|
|
21
|
+
print('Need to provide index')
|
|
9
22
|
sys.exit(1)
|
|
10
23
|
|
|
11
24
|
templateFile = sys.argv[1]
|
|
12
|
-
labelFile = sys.argv[2]
|
|
13
25
|
|
|
14
26
|
# load data from nifti files
|
|
15
27
|
template = nib.load(templateFile).get_fdata()
|
|
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
|
-
|
|
28
|
+
|
|
29
|
+
vmaxSamples = sys.argv[4]
|
|
30
|
+
|
|
31
|
+
if(len(vmaxSamples) == 0):
|
|
32
|
+
print('Need to provide max samples for normalization')
|
|
33
|
+
sys.exit(1)
|
|
34
|
+
vmaxSamples = int(vmaxSamples)
|
|
35
|
+
|
|
36
|
+
sampleFiles = json.loads(sys.argv[5])
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
index = int(index)
|
|
40
|
+
# (left, sagittal), f (front, coronal), t (top, axial)
|
|
41
|
+
if plane == 'L':
|
|
42
|
+
slice = template[index,:,:]
|
|
43
|
+
slice = np.rot90(slice)
|
|
44
|
+
elif plane == 'F':
|
|
45
|
+
slice = template[:,index,:]
|
|
46
|
+
slice = np.flip(np.rot90(slice),axis=1)
|
|
47
|
+
|
|
48
|
+
else:# plane == 'T'
|
|
49
|
+
slice = template[:,:,index]
|
|
50
|
+
slice = np.flip(np.rot90(slice),axis=1)
|
|
51
|
+
|
|
52
|
+
fig, ax = plt.subplots(1, 1)
|
|
53
|
+
ax.imshow(slice, 'gray', filternorm=False, vmin=0, vmax=100)
|
|
54
|
+
|
|
55
|
+
for key, value in sampleFiles.items():
|
|
56
|
+
if(len(value["samples"]) == 0) :
|
|
57
|
+
continue
|
|
58
|
+
# Load all sample files
|
|
59
|
+
sample_data = [nib.load(file_path).get_fdata() for file_path in value["samples"]]
|
|
60
|
+
|
|
61
|
+
# Initialize the result array with zeros
|
|
62
|
+
labels = np.zeros_like(sample_data[0])
|
|
63
|
+
|
|
64
|
+
# Sum all sample data
|
|
65
|
+
for data in sample_data:
|
|
66
|
+
labels += data
|
|
67
|
+
|
|
68
|
+
labels = np.ma.masked_where(labels == 0, labels) # Mask labels where they are 0
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
index = int(index)
|
|
72
|
+
# (left, sagittal), f (front, coronal), t (top, axial)
|
|
73
|
+
if plane == 'L':
|
|
74
|
+
label = labels[index,:,:]
|
|
75
|
+
label = np.rot90(label)
|
|
76
|
+
elif plane == 'F':
|
|
77
|
+
label = labels[:,index,:]
|
|
78
|
+
label = np.flip(np.rot90(label),axis=1)
|
|
79
|
+
else:# plane == 'T'
|
|
80
|
+
label = labels[:,:,index]
|
|
81
|
+
label = np.flip(np.rot90(label),axis=1)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# create three subplots for sagittal, coronal and axial plane
|
|
86
|
+
vmin = 0
|
|
87
|
+
vmax = 100
|
|
88
|
+
alpha = 0.6
|
|
89
|
+
|
|
90
|
+
color = value['color']
|
|
91
|
+
print(color)
|
|
92
|
+
cmap = mcolors.LinearSegmentedColormap.from_list('my_cmap', ['white', color])
|
|
93
|
+
ax.imshow(label, cmap, alpha=alpha, filternorm=False,vmin=0,vmax=vmaxSamples)
|
|
94
|
+
ax.axis('off')
|
|
95
|
+
|
|
96
|
+
# Create the color bar
|
|
97
|
+
# if showLegend == 1:
|
|
98
|
+
# # Create a color bar without changing figure size
|
|
99
|
+
# norm = mcolors.Normalize(vmin=0, vmax=vmaxSamples)
|
|
100
|
+
# sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
|
|
101
|
+
|
|
102
|
+
# cbar = plt.colorbar(sm, ax=ax, orientation='vertical', fraction=0.01, pad=0.05, alpha=alpha)
|
|
103
|
+
# cbar.set_label('Combined Intensity', color='white', fontsize=6, labelpad=-10)
|
|
104
|
+
# cbar.ax.text(0.5, 1.0001, vmaxSamples, ha='center', va='bottom', transform=cbar.ax.transAxes, color='white', fontsize=6)
|
|
105
|
+
# cbar.ax.text(0.5, -0.0001, 0, ha='center', va='top', transform=cbar.ax.transAxes, color='white', fontsize=6)
|
|
67
106
|
|
|
68
107
|
# Output the image data to stdout
|
|
69
108
|
buf = io.BytesIO()
|