-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_of_cells_after_segmentation.py
executable file
·69 lines (62 loc) · 2.95 KB
/
table_of_cells_after_segmentation.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
import json
import pandas as pd
import os
import argparse
parser = argparse.ArgumentParser('table_of_cells')
parser.add_argument('--inputdir', '-i', required=True, help="Folder where json files generated by infer.py are stored")
parser.add_argument('--patient_id', '-s', required=True, help="Patient ID for which the segmentation table will be created")
parser.add_argument('--segmentation_dir', default= '/home/mathiane/LNENWork/CFlow_mathiane/CFlow/TumorSegmentation_Ki67_Baseline_2809_1004/segmentation_maps_3_thresholds' , required=False)
# Get parameters
args = parser.parse_args()
inputdir = args.inputdir
segmentation_dir = args.segmentation_dir
patient_id = args.patient_id
# Name of the output table
df_name = patient_id + '_cells_detected_segmented.csv'
print('patient_id ', patient_id)
print("Get tiles ID within the tumoral area")
# Get segmentation table
segmentation_correct = False
for tneid in os.listdir(segmentation_dir):
if tneid.find(patient_id) != -1:
for ele in os.listdir(os.path.join(segmentation_dir, tneid)):
if ele.find("csv") != -1:
df_seg = pd.read_csv(os.path.join(segmentation_dir, tneid, ele))
segmentation_correct = True
# Take into account only the tumoral tiles
df_seg_tumor = df_seg[df_seg['PredTumorNomal'] == 'Tumor']
print(df_seg_tumor.head())
if segmentation_correct:
print("Get the infrence json filename of the tumoral tiles")
tiles_to_include = []
for i in range(df_seg_tumor.shape[0]):
# Get name of json files generated by infer.py
tile_id = df_seg_tumor.iloc[i,1].split('/')[-1][:-3] + 'json'
tiles_to_include.append(tile_id)
print("Create the coordianate table")
x_list = []
y_list = []
label_list = []
print(os.path.join(inputdir,args.patient_id, 'accept'))
for file in os.listdir(os.path.join(inputdir,args.patient_id, 'accept')):
# Read inference files if the tiles belongs to the tumor area
if file.find('json') != -1 and file in tiles_to_include:
# Get tile coords
xul = int(file.split('_')[1])
yul = int(file.split('_')[2].split('.')[0])
with open(os.path.join(inputdir, args.patient_id ,'accept', file)) as json_file:
cells = json.load(json_file)
# Get all cells predicted in the tiles
for cell in cells:
x_list.append(cell['x']+ xul )
y_list.append(cell['y'] + yul)
label_list.append(cell['label_id'])
# Table summarizing the coordinated and the class of the cells detected by Pathonet in the tumor area
df = pd.DataFrame()
df['x'] = x_list
df['y'] = y_list
df['label'] = label_list
print("Write the table")
df.to_csv(os.path.join(inputdir,args.patient_id, df_name))
else:
print("Segmentation table not found")