-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_slide_data.py
executable file
·289 lines (254 loc) · 10.5 KB
/
gen_slide_data.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import _init_all as init
import os
import math
import numpy as np
import draw
import gen_name as gname
import matplotlib.image as mpimg
import test_task
import openslide
def gen_sction_info(patient_id,node_id,section_x,section_y,step,score):
x_pixel =section_x*step
y_pixel =section_y*step
str_info ="patient_"+str(patient_id)+"_node_"+str(node_id)+"_L00_X"+str(x_pixel)+"_Y"+str(y_pixel)+".jpg :"+str(score)+"\n"
return str_info
def gen_section_img_name(patient_id,node_id,section_x,section_y):
cfg = init.config()
step = cfg.step
x_pixel = section_x * step
y_pixel = section_y * step
file_name = "patient_"+str(patient_id).zfill(3)+"_node_"+str(node_id)+"_L00_X"+str(x_pixel).zfill(6)+"_Y"+str(y_pixel).zfill(6)+".jpg"
return file_name
def get_section_score(patient_id,node_id,section_x,section_y):
path_info = init.path_info()
file_name = gen_section_img_name(patient_id,node_id,section_x,section_y)
file_path = os.path.join(path_info.input,file_name)
if os.path.exists(file_path):
return test_task.get_section_score(file_path)
else:
return test_task.gen_maan_score(patient_id,node_id,section_x,section_y)
'''generate the score'''
def gen_slide_score_random(patient_id,node_id):
#slide_info = init.slide()
cfg = init.config()
path_info = init.path_info()
slide_size = init.get_patient_img_param(patient_id,node_id)
x_cnt = slide_size[2]/cfg.step
y_cnt = slide_size[3]/cfg.step
'''generate score from the test_task'''
file_name = gname.gen_section_score_txt_name(patient_id,node_id,cfg.step)
txt_path = os.path.join(path_info.raw_score,file_name)
fd_txt =open(txt_path,'w+')
print("File name is :"+txt_path)
file_name = gname.gen_section_score_dat_name(patient_id,node_id,cfg.step)
dat_path = os.path.join(path_info.raw_score, file_name)
fd_dat = open(dat_path, 'w+')
print("File name is :" + dat_path)
arr_score= np.zeros((y_cnt,x_cnt))
for section_y in range(y_cnt):
for section_x in range(x_cnt):
#section_score = test_task.gen_section_score(patient_id,node_id,section_x,section_y)
section_score = get_section_score(patient_id,node_id,section_x,section_y)
str_txt = gen_sction_info(patient_id,node_id,section_x,section_y,cfg.step,section_score)
fd_txt.write(str_txt)
arr_score[section_y][section_x] = section_score
fd_dat.write(str(section_score))
if section_x != (x_cnt -1):
fd_dat.write(",")
fd_dat.write("\n")
fd_txt.close()
fd_dat.close()
return arr_score
def gen_slide_score_from_img(patient_id,node_id):
#cfg = init.config()
path_info = init.path_info()
img_name = "test_%02d_%d.jpg"%(patient_id,node_id)
file_path = os.path.join(path_info.config,img_name)
imgdat = mpimg.imread(file_path)
newdat = imgdat[:,:,1]
print np.array(newdat).shape
slide_score = newdat/256.0
return slide_score
def matrix_deflate(inarr_mask,level=1):
mask_arr =np.array(inarr_mask)
#if 255 in arr:
# mask_arr =(arr >1)
#if 1 in arr:
# mask_arr = (arr==1)
step = pow(2,level)
sum_gate = 0.4*pow(step,2)
out_arr = np.zeros((int(mask_arr.shape[0]/step),int(mask_arr.shape[1]/step)))
for y_cnt in range(int(mask_arr.shape[0]/step)):
for x_cnt in range(int(mask_arr.shape[1]/step)):
mask_sum = sum(sum(mask_arr[y_cnt*step:(y_cnt+1)*step,x_cnt*step:(x_cnt+1)*step]))
if mask_sum > sum_gate:
out_arr[y_cnt][x_cnt] = 1
else:
out_arr[y_cnt][x_cnt] = 0
return out_arr
def gen_slide_score_from_mask_img(patient_id,node_id):
cfg = init.config()
path_info = init.path_info()
if cfg.get_csv_mode ==0:
mask_name = "patient_%03d_node_%d_mask.tif" % (patient_id, node_id)
elif cfg.get_csv_mode == 1:
test_cnt = patient_id*5+node_id
mask_name = "test_%03d_mask.tif"%test_cnt
else:
print "get raw score mode fail!"
mask_name = "null"
file_path = os.path.join(path_info.mask_dir,mask_name)
if os.path.exists(file_path):
print "open mask image :%s" %(file_path)
slide = openslide.open_slide(file_path)
level_real = int(math.log(cfg.step, 2))
if slide.level_count > level_real:
level = level_real
else:
level = slide.level_count -1
dims = slide.level_dimensions[level]
pixelarray = np.zeros(dims[0]*dims[1], dtype=np.int8)
pixelarray = np.array(slide.read_region((0,0), level, dims)) # read all image to pixelarry
diff_level = level_real - level
#np.save("patient_%d_node_%d_array_npsave.npy"%(patient_id,node_id),pixelarray[:,:,0])
'''
if 1 in pixelarray[:,:0]:
pixel_mask = (pixelarray[:,:,0] == 1)
print "1 in mask image"
else:
pixel_mask = (pixelarray[:,:,0] > 10)
print "1 not in mask image"
'''
pixel_mask = pixelarray[:,:,0] > 100
if 1 in pixel_mask:
print "the mask image : 0-255"
else:
pixel_mask = (pixelarray[:, :, 0] == 1)
print "positive region is label 1"
if diff_level != 0:
outs_arr = matrix_deflate(pixel_mask,diff_level)
else:
#outs_arr = pixelarray[:,:,1]/256.0
outs_arr = pixel_mask
#print "this mask image shape:%s"%(str(outs_arr.shape))
#slide_score = pixelarray[:, :, 1] / 256.0
slide_score = outs_arr*1.0
else:
step = cfg.step
pat_param = init.get_patient_img_param(patient_id,node_id)
x_cnt = int(pat_param[cfg.pst_xsize] / step)
y_cnt = int(pat_param[cfg.pst_ysize] / step)
slide_score = np.zeros((y_cnt,x_cnt))
return slide_score
def gen_slide_score_form_csv(patient_id,node_id):
path_info = init.path_info()
cfg = init.config()
step =cfg.step
max_x = 0
max_y = 0
if cfg.get_csv_mode == 0:
file_name = gname.gen_slide_score_file_name(patient_id,node_id,step)
if file_name != "null":
max_x = int(file_name[29:32])
max_y = int(file_name[34:37])
elif cfg.get_csv_mode == 1:
file_name = gname.get_slide_score_test_csv_name(patient_id,node_id,step)
if file_name != "null":
max_x = int(file_name[19:22])
max_y = int(file_name[24:27])
#max_x = int(file_name[19:23])
#max_y = int(file_name[25:29])
else:
print ("get the csv file fail!")
file_name = "xxx"
dat_path = os.path.join(path_info.input,file_name)
if os.path.exists(dat_path):
print ("Read csv [patient:%d node:%d] %s "%(patient_id,node_id,dat_path))
score_data = np.loadtxt(dat_path,delimiter=',',usecols=(1,2,3))
# get the max size from the input csv
if (max_x + max_y) == 0:
max_x = 1 + int(max(score_data.T[0])/cfg.step)
max_y = 1 + int(max(score_data.T[1])/cfg.step)
#print "max is :%d %d \n" %(max_x,max_y)
else:
max_x = 100
max_y = 100
score_data = np.zeros((max_y,max_x))
'''
# get the image size from the config
slide_cfg = init.get_patient_img_param(patient_id,node_id)
slide_cfg_arr = np.array(slide_cfg)
#print slide_cfg_arr
if slide_cfg_arr[0] != patient_id:
print "cannot find patient: %03d node:%02d config."%(patient_id,node_id)
# get the max size from the config file
x_cnt = int(slide_cfg_arr[cfg.pst_xsize]/step)
y_cnt = int(slide_cfg_arr[cfg.pst_ysize]/step)
'''
x_cnt = max_x
y_cnt = max_y
slide_score = np.zeros((y_cnt,x_cnt))
#print "this image shape is :[%d * %d]"%(y_cnt,x_cnt)
for num_id in range(score_data.shape[0]):
x_pst = int(score_data[num_id][0]/step)
y_pst = int(score_data[num_id][1]/step)
if y_pst >= y_cnt:
print ("y:max:%d real:%d "%(y_cnt,y_pst))
if x_pst >= x_cnt:
print ("x:max:%d real:%d "%(x_cnt,x_pst))
slide_score[y_pst][x_pst] =score_data[num_id][2]
if cfg.save_np_score_mode == 1:
file_name = gname.gen_section_score_npy_name(patient_id,node_id,step)
file_path = os.path.join(path_info.raw_score,file_name)
np.save(file_path,slide_score)
return slide_score
def gen_slide_score_from_np_matrix(patient_id,node_id):
cfg = init.config()
path_info = init.path_info()
step =cfg.step
file_name = gname.gen_section_score_npy_name(patient_id,node_id,step)
file_path = os.path.join(path_info.raw_score,file_name)
slide_score = np.load(file_path)
return slide_score
def gen_slide_score(patient_id,node_id):
cfg = init.config()
if cfg.get_score_mode == 0:
slide_score = gen_slide_score_form_csv(patient_id,node_id)
elif cfg.get_score_mode == 1:
slide_score = gen_slide_score_from_np_matrix(patient_id,node_id)
elif cfg.get_score_mode == 2:
slide_score = gen_slide_score_from_img(patient_id,node_id)
elif cfg.get_score_mode == 3:
slide_score = gen_slide_score_random(patient_id, node_id)
else:
slide_score = gen_slide_score_from_mask_img(patient_id, node_id)
return slide_score
def test_gen_img_score():
gen_slide_score_from_img(1,1)
def test():
print (gname.gen_section_score_txt_name(10,15,256))
print (gname.gen_section_score_dat_name(10,15,128))
if 1:
patient_id =1
node_id = 2
path_info = init.path_info()
arr_score = gen_slide_score(patient_id,node_id)
#arr_score = test_gen_img_score()
#draw.draw_slide_heatmap(arr_score)
file_name = gname.gen_heatmap_img_name(patient_id,node_id, 256, 0.2)
dat_path = os.path.join(path_info.heatmap,file_name)
draw.draw_heatmap(arr_score,0.2,dat_path)
file_name = gname.gen_heatmap_img_name(patient_id,node_id,256,0.7)
dat_path = os.path.join(path_info.heatmap,file_name)
draw.draw_heatmap(arr_score,0.7,dat_path)
else:
#gen_section_img_name(0,0,0,0)
#gen_section_img_name(1,2,10,32)
gen_slide_score(1,2)
def read_save_image(patient_id,node_id):
score = gen_slide_score_from_img(patient_id,node_id)
if __name__ == "__main__":
#test()
#slide_core = test_gen_img_score()
init.all_dir_check()
test()