-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
163 lines (129 loc) · 5.24 KB
/
helpers.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
import logging
from argparse import ArgumentParser
import nibabel as nib
import numpy as np
import glob
import os
"""
Logging helpers
"""
def logging_init(log_fname, log_lvl=logging.INFO):
logging.basicConfig(format='%(asctime)s [%(levelname)-5.5s] %(message)s',
level=log_lvl,
handlers=[
logging.FileHandler(log_fname, mode='w'),
logging.StreamHandler()
])
def logging_args_config(logger):
pass
"""
training helpers
"""
def add_training_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
parser.add_argument('--data_folder', nargs='+', type=str, default='/cluster/liu/data/')
parser.add_argument('--datasetmode',type=int, required=True,help='4 mode',default=1)
parser.add_argument('--opt',type=str, required=True,help='2 optimizers',default='Adam')
parser.add_argument('--resume',type=bool, required=False,help='if continue train',default=False)
parser.add_argument('--lastcheckpoint',type=str, required=False,help='path to lastcheckpoint',default='')
parser.add_argument('--hpar',type=str, required=False,help='path to lastcheckpoint',default='')
parser.add_argument('--ckpt',type=str,default='local')
parser.add_argument('--cluster', type=bool, default=False)
parser.add_argument("--worker", type=int, default=8)
parser.add_argument("--batch_size", type=int, default=4)
return parser
def add_inference_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
parser.add_argument('--ckpt_dir', type=str, default=None, help='Path to the model checkpoint, regularly saved in ./lightning_logs/..')
parser.add_argument('--raw_dir', type=str, default=None, help='Path to the directory of the nifti image files.')
parser.add_argument('--cuda', type=bool, default=True, help='True, if GPU accelerator is available.')
return parser
"""
file manipulation helper
"""
def get_ckpts_paths(version):
ckpts = ['.\\lightning_logs\\version_{}\\final.ckpt'.format(version)]
ckpts.extend(glob.glob('.\\lightning_logs\\version_{}\\checkpoints\\*.ckpt'.format(version)))
return ckpts
"""
Evaluation helper
"""
def get_fname_with_suffix(fname, suffix):
# example 10000081_ct.nii.gz
basename = fname.split('.')[0]
old_suffix = fname[len(basename):]
return basename + '_' + suffix + old_suffix
def get_model_id(model):
base = os.path.basename(model)
return base[:-(len(base.split('.')[-1]) + 1)]
def get_version(model):
# get verision from ckpt dir
# print(model)
if type(model) is not str:
ValueError('Model should only be path')
dir = model.split(os.sep)
# print(dir)
if dir[-1] == 'final.ckpt':
version_str = dir[-2]
# print(version_str)
else:
version_str = dir[-3]
version = version_str.split('_')[-1]
return int(version)
def scale_intensity_range(img, amin, amax, bmin, bmax):
# scale the img intensity from (amin, amax) to (bmin, bmax)
# resemble monai.transforms.ScaleIntensityRange(...)
img = (img - amin) / (amax - amin)
img = img * (bmax - bmin) + bmin
return img
def create_confusion_matrix(gt, eval_vol, num_cls):
# flatten the inputs
gt = np.array(gt).flatten()
eval_vol = np.array(eval_vol).flatten()
confusion = np.zeros([num_cls, num_cls])
# confusion[i, j]: num of pixels of class i, predicted as class j
for i in range(num_cls):
for j in range(num_cls):
confusion[i, j] = np.sum(np.logical_and(gt == i, eval_vol == j))
return confusion.astype(np.int32)
def MOS_eval(pred_path, gt_path, label_list):
gt = nib.load(gt_path).get_fdata()
eval_vol = nib.load(pred_path).get_fdata()
# print(gt.shape)
# print(eval_vol.shape)
# Caution, class 13 is class 1
gt[gt == 13] = 1
# Since the ground truth is always smaller, crop the eval_vol according to ground truth
num_slices = gt.shape[-1]
eval_vol = eval_vol[:, :, :num_slices]
print('')
print('Creation of confusion matrix in progress: ')
confusion = create_confusion_matrix(gt, eval_vol, len(label_list))
# print(confusion)
# classwise recall and precision
eps = 1e-4
recall = np.diagonal(confusion) / (np.sum(confusion, axis=1) + eps)
precision = np.diagonal(confusion) / (np.sum(confusion, axis=0) + eps)
dice = 2 * (recall * precision) / (recall + precision + eps)
print('')
name_msg = ''
for l in label_list:
name_msg += l.ljust(10)
print(name_msg)
print()
recall_msg = ''
for r in recall:
recall_msg += '{0:.2g}% |'.format(r * 100).ljust(10)
print('|**Recall**|', recall_msg)
precision_msg = ''
for p in precision:
precision_msg += '{0:.2g}% |'.format(p * 100).ljust(10)
print('|**Precision**|', precision_msg)
dice_msg = ''
for d in dice:
dice_msg += '{0:.2g}% |'.format(d * 100).ljust(10)
print('|**dice**|', dice_msg)
if __name__ == '__main__':
atlas_seg_path = 'D:\\Chang\\MultiOrganSeg\\mostoolkit\\OrganPositionEmbedding\\Model_output\\atlas_seg.nii.gz'
gt_seg_path = 'D:\\Data\\ct_data\\visceral_manual_seg\\10000081_seg.nii.gz'
MOS_eval(atlas_seg_path, gt_seg_path)