Commit 45edab2c authored by cc215's avatar cc215 💬
Browse files

fix bug

parent 4cdef966
......@@ -260,6 +260,10 @@ class runningMySegmentationScore(object):
df.to_csv(save_path, index=False)
print(df.describe())
return df
def get_df(self):
df = pd.DataFrame(self.tables, columns=self.header)
return df
......
......@@ -104,10 +104,10 @@ class CARDIAC_Predict_DATASET(data.Dataset):
new_image = temp_image
## new image shape
self.aft_resample_shape = temp_image_arr.shape
data = sitk.GetArrayFromImage(new_image).astype(float)
patient_id = self.patient_path_list[self.pid].split('/')[-1]
self.aft_resample_shape = data.shape
## save frame data
patient_data[frame] = {'image': data, ##npy data
'origin_itk_image': temp_image, ##original data
......
## old model
python predict.py --sequence LVSA --model_path '/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/checkpoints/Unet_LVSA_best.pkl' --root_dir '/vol/biomedic3/cc215/data/ACDC/bias_corrected_and_normalized/patient_wise/' \
--image_format '{}_img.nrrd' --roi_size 256 --save_folder_path '/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/result/predict/baseline/ACDC_all' --save_name_format 'pred_{}.nrrd' --z_score
## new model
python predict.py --sequence LVSA --model_path '/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/result/composite_train_aug_v2/best/checkpoints/UNet_64$SAX$_Segmentation.pth' --root_dir '/vol/biomedic3/cc215/data/ACDC/bias_corrected_and_normalized/patient_wise/' \
--image_format '{}_img.nrrd' --roi_size 192 --save_folder_path '/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/result/predict/composite_train_aug_v2/ACDC_all' --save_name_format 'pred_{}.nrrd'
\ No newline at end of file
%% Cell type:code id: tags:
```
```
%% Cell type:code id: tags:
```
import os
import numpy as np
import matplotlib.pyplot as plt
import torch
from os.path import join
import SimpleITK as sitk
from common_utils.metrics import runningMySegmentationScore
```
%% Cell type:markdown id: tags:
# standard prediction
%% Cell type:code id: tags:
```
pred_dir = '/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/result/predict/composite_train_aug_v2/ACDC_all/LVSA/'
pred_format='{pid}/pred_{frame}.nrrd'
gt_dir ='/vol/biomedic3/cc215/data/ACDC/bias_corrected_and_normalized/patient_wise'
gt_format='{pid}/{frame}_seg.nrrd'
```
%% Cell type:code id: tags:
```
def load_npy_from_med_file(med_file_path, debug=False):
origin_image = sitk.ReadImage(med_file_path)
original_im_arr=sitk.GetArrayFromImage(origin_image)
original_shape=original_im_arr.shape
pixel_spacing =origin_image.GetSpacing()
if debug:
print ('spacing',pixel_spacing)
print ('shape',original_shape)
return original_im_arr,original_shape,pixel_spacing
```
%% Cell type:markdown id: tags:
Load pred and gt pair
%% Cell type:code id: tags:
```
frames = ['ED','ES']
pid_list =[str.zfill(str(i),3) for i in range(1,101)]
## evaluate metric
n_classes = 4
idx2cls_dict = {0:'BG',1:'LV',2:'MYO',3:'RV'}
metrics_list =['Dice']
foreground_only=False
metric= runningMySegmentationScore(n_classes=n_classes,idx2cls_dict=idx2cls_dict,metrics_list=metrics_list)
for frame in frames:
for pid in pid_list:
pred_path =join(pred_dir,pred_format.format(frame=frame,pid=pid))
gt_path =join(gt_dir,gt_format.format(frame=frame,pid=pid))
pred,shape,spacing = load_npy_from_med_file(pred_path)
gt,shape,spacing = load_npy_from_med_file(gt_path)
metric.update(pid=pid+frame, preds=pred, gts=gt,
voxel_spacing=spacing)
```
%% Cell type:markdown id: tags:
## Visualization
%% Cell type:code id: tags:
```
print(metric.get_scores())
df = metric.get_df()
```
%% Output
({'BG_Dice_mean': 0.0, 'BG_Dice_std': 0.0, 'LV_Dice_mean': 0.8929695697658423, 'LV_Dice_std': 0.09696713161053426, 'MYO_Dice_mean': 0.7603251458184082, 'MYO_Dice_std': 0.0846948994069433, 'RV_Dice_mean': 0.8195179484516761, 'RV_Dice_std': 0.12276178637780168}, [['0.000', '0.893', '0.760', '0.820'], ['0.000', '0.097', '0.085', '0.123']], ['BG_Dice', 'LV_Dice', 'MYO_Dice', 'RV_Dice'])
%% Cell type:code id: tags:
```
df
```
%% Output
patient_id BG_Dice LV_Dice MYO_Dice RV_Dice\n0 001ED 0.0 0.948404 0.785139 0.904986\n1 002ED 0.0 0.951280 0.734814 0.917075\n2 003ED 0.0 0.964692 0.828374 0.890522\n3 004ED 0.0 0.949731 0.786202 0.897323\n4 005ED 0.0 0.963387 0.826521 0.907171\n.. ... ... ... ... ...\n195 096ES 0.0 0.908631 0.776894 0.870971\n196 097ES 0.0 0.888402 0.802307 0.829653\n197 098ES 0.0 0.692916 0.758033 0.822555\n198 099ES 0.0 0.889941 0.787924 0.880020\n199 100ES 0.0 0.873063 0.815719 0.771264\n\n[200 rows x 5 columns]
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment