File size: 6,973 Bytes
654f72f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ca2f67
654f72f
 
 
 
 
 
0ca2f67
654f72f
 
 
 
 
 
 
0ca2f67
654f72f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c71fa8b
654f72f
 
 
 
 
 
 
 
 
 
 
 
 
0ca2f67
654f72f
c71fa8b
654f72f
 
 
c71fa8b
654f72f
 
 
 
 
 
 
c71fa8b
654f72f
 
 
 
 
 
 
c71fa8b
654f72f
 
 
 
 
 
 
 
c71fa8b
654f72f
 
 
 
 
 
 
c71fa8b
654f72f
 
 
 
c71fa8b
654f72f
 
 
 
e7e835a
c71fa8b
 
654f72f
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#____________________________________________________________
#            August 2, 2023
#____________________________________________________________
import os
import numpy as np
import gradio as gr
import SimpleITK as sitk
import seaborn as sns
import matplotlib.pyplot as plt

def LoadImage(path_img):
    try:
        img_itk, voxel_size= Load_itk_image(path_img)
    except FileNotFoundError:
        print('ERROR: File not found', path_img)

    img_numpy = sitk.GetArrayFromImage(img_itk[0])
    img_load=img_numpy.transpose()

    #remove background = 0
    img_load[img_load==0]=np.nan

    return img_load


def Load_itk_image(path_img):
    #Read header
    file_reader = sitk.ImageFileReader()
    file_reader.SetFileName(path_img)
    file_reader.ReadImageInformation()
    dim_total = file_reader.GetSize()
    img_vol_ITK = []

    if len(dim_total)==4:
        dim_vol = (dim_total[0:3])
        # make volume for all images
        img_load_all = sitk.ReadImage(path_img, sitk.sitkFloat32)

        img_vol_ITK = []
        for vol_n in range(dim_total[3]):
            #Extract vol out of object
            size = list(dim_total)
            size[3] = 0
            index = [0,0,0,vol_n]
            extractor = sitk.ExtractImageFilter()
            extractor.SetSize(size)
            extractor.SetIndex(index)

            img_vol_ITK.append(extractor.Execute(img_load_all))

    else:
        img_ITK = sitk.ReadImage(path_img, sitk.sitkFloat32)
        img_vol_ITK.append(img_ITK)
        dim_vol = dim_total

    #Voxelsize in mm. forth dimension has no meaning in spacing.
    voxel_size = img_vol_ITK[0].GetSpacing()
    return  img_vol_ITK, voxel_size



def NormalizeMinMax(img):
    #Normalization for Volumes
    minVal = np.nanmin(img)
    maxVal = np.nanmax(img)
 
    img_normalized = (img - minVal)*(1/(maxVal- minVal))
    return img_normalized

def NormalizePercentile(img, minP, maxP):
    #Normalization for Volumes
    minVal = np.nanpercentile(img, minP)
    maxVal = np.nanpercentile(img, maxP)
    img_normalized = (img - minVal)*(1/(maxVal- minVal))
    return img_normalized

def NormalizeZScore(img):
    #Normalization for Volumes
    meanVal = np.nanmean(img)
    stdVal = np.nanstd(img)
     
    img_normalized = (img-meanVal)*(1/stdVal)
    
    return img_normalized
 

def PrepImage(pathName):
      
     
    img_load = LoadImage(pathName)
    img_normalized = NormalizeMinMax(img_load)
    img_normalized_per =NormalizePercentile(img_load, 10, 90)
    img_normalized_per98 =NormalizePercentile(img_load, 2, 98)
    img_normalized_zscore= (NormalizeZScore(img_load) ) 

    return img_load.flatten(),img_normalized.flatten(),img_normalized_per.flatten(),img_normalized_zscore.flatten(), img_normalized_per98.flatten()




def Norm_image(vol_path,normalization_technique):

    imglist = []
    imgClist=[]
    imgNlist=[]
    imgNPlist=[]
    imgNZlist=[]
    imgNPer98list=[]
    print(len(vol_path))
    for file in vol_path:
        pathName = file.name

        img_load = LoadImage(pathName)
        imglist.append(img_load.flatten())
          

        if 'MinMax' in normalization_technique:
            imgNlist.append(NormalizeMinMax(img_load).flatten())
        
        if 'Z-Score' in normalization_technique:
            imgNZlist.append(NormalizeZScore(img_load).flatten())
        
        if 'Percentile (2th - 98th)'in normalization_technique:
            imgNPer98list.append(NormalizePercentile(img_load, 2, 98).flatten())
            
        if 'Percentile (10th - 90th)' in normalization_technique:
            imgNPlist.append(NormalizePercentile(img_load, 10, 90).flatten())

    gr.Info('The volumes are loaded succesfully and different normalization techniques are calculated.')   

    log_scale = False
    plt.figure(11)
    fig, ax = plt.subplots(1,1)
    for i, file in enumerate(vol_path):
        sns.histplot(data=imglist[i].flatten(), kde=False, label=os.path.basename(file.name)[0:25], log_scale=log_scale,element="step", fill=False,bins=500,legend=True).set(title='Original')
    ax.legend()
    plt.savefig("Original.png")
    plots=["Original.png"]
    
    if 'MinMax' in normalization_technique:
        fig, ax = plt.subplots(1,1)
        for i, file in enumerate(vol_path):
            sns.histplot(data=imgNlist[i].flatten(), kde=False, label=os.path.basename(file.name)[0:25], log_scale=log_scale,element="step", fill=False,bins=500,legend=True).set(title='Min-Max')
        ax.legend()
        plt.savefig("MinMax.png")
        plots.append("MinMax.png")
            
    if 'Z-Score' in normalization_technique:
        fig, ax = plt.subplots(1,1)
        for i, file in enumerate(vol_path):
            sns.histplot(data=imgNZlist[i].flatten(), kde=False, label=os.path.basename(file.name)[0:25], log_scale=log_scale,element="step", fill=False,bins=500,legend=True).set(title='Z-Score')
        ax.legend()
        plt.savefig("Zscore.png")
        plots.append("Zscore.png")
        

    if 'Percentile (2th - 98th)'in normalization_technique:
        fig, ax = plt.subplots(1,1)
        for i, file in enumerate(vol_path):
            sns.histplot(data=imgNPer98list[i].flatten(), kde=False, label=os.path.basename(file.name)[0:25], log_scale=log_scale,element="step", fill=False,bins=500,legend=True).set(title='Percentile 2-98')
        ax.legend()
        plt.savefig("Per98.png")
        plots.append("Per98.png")

    if 'Percentile (10th - 90th)' in normalization_technique:
        fig, ax = plt.subplots(1,1)
        for i, file in enumerate(vol_path):
            sns.histplot(data=imgNPlist[i].flatten(), kde=False, label=os.path.basename(file.name)[0:25], log_scale=log_scale,element="step", fill=False,bins=500,legend=True).set(title='Percentile 10 - 90')
        ax.legend()
        plt.savefig("Per90.png")
        plots.append("Per90.png")
        


    return plots


description = 'You can upload mutiple image volumes (recommonded 3-5). The files get read by ITK so mulitple different file formats are possible (e.g. *.nii , *.nii.gz, jpeg,..). You need to wait until the data is uploaded. \

    Once pressing submit the histograms for multiple normalization techniques are calculated. Depending on file size and selected techniques it might take a while to do the calculations. \

          \n The uploaded data is not stored and is deleted once the window is closed. '

inputs = [gr.File(file_count="multiple", label=None),gr.CheckboxGroup(["MinMax", "Z-Score", "Percentile (2th - 98th)", "Percentile (10th - 90th)"])]
demo = gr.Interface(fn=Norm_image,
             inputs=inputs,
             outputs=[gr.Gallery(label="Profiling Dashboard")], #.style(grid=(2,3))],
             description=description,
             )
             
demo.launch().queue()