-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") |