File size: 7,042 Bytes
286a978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import argparse
import re
import warnings

import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap

segm_cm = cm.get_cmap('Dark2', 256)
segm_cm = segm_cm(np.linspace(0, 1, 28))
segm_cm[0, :] = np.asarray([0, 0, 0, 0])
segm_cm = ListedColormap(segm_cm)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('-d', '--dir', type=str, help='Directories where the models are stored', default=None)
    parser.add_argument('-o', '--output', type=str, help='Output directory', default=os.getcwd())
    parser.add_argument('--overwrite', type=bool, default=True)
    args = parser.parse_args()
    assert args.dir is not None, "No directories provided. Stopping"

    list_fix_img = list()
    list_mov_img = list()
    list_fix_seg = list()
    list_mov_seg = list()
    list_pred_img = list()
    list_pred_seg = list()
    print('Fetching data...')
    for r, d, f in os.walk(args.dir):
        if os.path.split(r)[1] == 'Evaluation_paper':
            for name in f:
                if re.search('^050', name) and name.endswith('nii.gz'):
                    if re.search('fix_img', name) and name.endswith('nii.gz'):
                        list_fix_img.append(os.path.join(r, name))
                    elif re.search('mov_img', name):
                        list_mov_img.append(os.path.join(r, name))
                    elif re.search('fix_seg', name):
                        list_fix_seg.append(os.path.join(r, name))
                    elif re.search('mov_seg', name):
                        list_mov_seg.append(os.path.join(r, name))
                    elif re.search('pred_img', name):
                        list_pred_img.append(os.path.join(r, name))
                    elif re.search('pred_seg', name):
                        list_pred_seg.append(os.path.join(r, name))

    # Figure: all coronal views
    # Fix img | Mov img
    # BASELINE 1 | BASELINE 2 | SEGGUIDED
    # UW 1 | UW 2 | UW 3
    list_fix_img.sort()
    list_fix_seg.sort()
    list_mov_img.sort()
    list_mov_seg.sort()
    list_pred_img.sort()
    list_pred_seg.sort()
    print('Making Test_data.png...')
    selected_slice = 30
    fix_img = np.asarray(nib.load(list_fix_img[0]).dataobj)[..., selected_slice, 0]
    mov_img = np.asarray(nib.load(list_mov_img[0]).dataobj)[..., selected_slice, 0]
    fix_seg = np.asarray(nib.load(list_fix_seg[0]).dataobj)[..., selected_slice, 0]
    mov_seg = np.asarray(nib.load(list_mov_seg[0]).dataobj)[..., selected_slice, 0]

    fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(9, 3), dpi=200)

    for i, (img, title) in enumerate(zip([(fix_img, fix_seg), (mov_img, mov_seg)],
                                         [('Fixed image', 'Fixed Segms.'), ('Moving image', 'Moving Segms.')])):

        ax[i].imshow(img[0], origin='lower', cmap='Greys_r')
        ax[i+2].imshow(img[0], origin='lower', cmap='Greys_r')
        ax[i+2].imshow(img[1], origin='lower', cmap=segm_cm, alpha=0.6)

        ax[i].tick_params(axis='both', which='both', bottom=False, left=False, labelleft=False, labelbottom=False)
        ax[i+2].tick_params(axis='both', which='both', bottom=False, left=False, labelleft=False, labelbottom=False)

        ax[i].set_xlabel(title[0], fontsize=16)
        ax[i+2].set_xlabel(title[1], fontsize=16)

    plt.tight_layout()
    if not args.overwrite and os.path.exists(os.path.join(args.output, 'Test_data.png')):
        warnings.warn('File Test_data.png already exists. Skipping')
    else:
        plt.savefig(os.path.join(args.output, 'Test_data.png'), format='png')
    plt.close()

    print('Making Pred_data.png...')
    fig, ax = plt.subplots(nrows=2, ncols=6, figsize=(9, 3), dpi=200)

    for i, (pred_img_path, pred_seg_path) in enumerate(zip(list_pred_img, list_pred_seg)):
        img = np.asarray(nib.load(pred_img_path).dataobj)[..., selected_slice, 0]
        seg = np.asarray(nib.load(pred_seg_path).dataobj)[..., selected_slice, 0]

        ax[0, i].imshow(img, origin='lower', cmap='Greys_r')
        ax[1, i].imshow(img, origin='lower', cmap='Greys_r')
        ax[1, i].imshow(seg, origin='lower', cmap=segm_cm, alpha=0.6)

        ax[0, i].tick_params(axis='both', which='both', bottom=False, left=False, labelleft=False, labelbottom=False)
        ax[1, i].tick_params(axis='both', which='both', bottom=False, left=False, labelleft=False, labelbottom=False)

        model = re.search('((UW|SEGGUIDED|BASELINE).*)_{2,}MET', pred_img_path).group(1).rstrip('_')
        model = model.replace('_Lsim', ' ')
        model = model.replace('_Lseg', ' ')
        model = model.replace('_L', ' ')
        model = model.replace('_', ' ')
        model = model.upper()
        model = ' '.join(model.split())

        ax[1, i].set_xlabel(model, fontsize=9)
    plt.tight_layout()
    if not args.overwrite and os.path.exists(os.path.join(args.output, 'Pred_data.png')):
        warnings.warn('File Pred_data.png already exists. Skipping')
    else:
        plt.savefig(os.path.join(args.output, 'Pred_data.png'), format='png')
    plt.close()

    print('Making Pred_data_large.png...')
    fig, ax = plt.subplots(nrows=2, ncols=8, figsize=(9, 3), dpi=200)
    list_pred_img = [list_mov_img[0]] + list_pred_img
    list_pred_img = [list_fix_img[0]] + list_pred_img
    list_pred_seg = [list_mov_seg[0]] + list_pred_seg
    list_pred_seg = [list_fix_seg[0]] + list_pred_seg

    for i, (pred_img_path, pred_seg_path) in enumerate(zip(list_pred_img, list_pred_seg)):
        img = np.asarray(nib.load(pred_img_path).dataobj)[..., selected_slice, 0]
        seg = np.asarray(nib.load(pred_seg_path).dataobj)[..., selected_slice, 0]

        ax[0, i].imshow(img, origin='lower', cmap='Greys_r')
        ax[1, i].imshow(img, origin='lower', cmap='Greys_r')
        ax[1, i].imshow(seg, origin='lower', cmap=segm_cm, alpha=0.6)

        ax[0, i].tick_params(axis='both', which='both', bottom=False, left=False, labelleft=False, labelbottom=False)
        ax[1, i].tick_params(axis='both', which='both', bottom=False, left=False, labelleft=False, labelbottom=False)

        if i > 1:
            model = re.search('((UW|SEGGUIDED|BASELINE).*)_{2,}MET', pred_img_path).group(1).rstrip('_')
            model = model.replace('_Lsim', ' ')
            model = model.replace('_Lseg', ' ')
            model = model.replace('_L', ' ')
            model = model.replace('_', ' ')
            model = model.upper()
            model = ' '.join(model.split())
        elif i == 0:
            model = 'Moving image'
        else:
            model = 'Fixed image'

        ax[1, i].set_xlabel(model, fontsize=7)
    plt.tight_layout()
    if not args.overwrite and os.path.exists(os.path.join(args.output, 'Pred_data_large.png')):
        warnings.warn('File Pred_data.png already exists. Skipping')
    else:
        plt.savefig(os.path.join(args.output, 'Pred_data_large.png'), format='png')
    plt.close()

    print('...done!')