# # Setup

# In[1]:


"""Tumor detection based on bisected MRI slices.
This module splits the images in the specified folder vertically (in the
middle) into two images. Next, the split images are saved in the
predefined folder. Subsequently, a convolutional neural network (CNN)
is trained to recognize whether there is a vestibular schwannoma in the
split transversal MRI slices. Finally, the CNN is internally and
externally validated and the results are displayed.
"""

__author__ = 'Carole Koechli'

import os
import numpy as np
import image_slicer
from shutil import copyfile
import matplotlib.pyplot as plt
import seaborn as sn
import pandas as pd                                

from fastai.vision.all import*
from fastai.callback.fp16 import*

import torch
import torchvision


# In[2]:


torch.manual_seed(17)


# # Split and save images

# In[3]:


# The folder name is specified where the images are stored that
# need to be split.
foldernames_stored = [
    'VS_AX_T1c_internal',
    'VS_AX_T1c_external',
    'VS_AX_T1_external'
    ]


# In[4]:


# The folder name is specified where the split images are saved.
foldernames_saved = [
    'splitImagesT1cInternal',
    'splitImagesT1cExternal',
    'splitImagesT1External'
    ]


# In[5]:


# The folder name is specified where the rearranged images are saved.
foldernames_sorted = [
    'splitImagesT1cInternalSorted',
    'splitImagesT1cExternalSorted',
    'splitImagesT1ExternalSorted'
    ]


# In[6]:


# All images in the specified folder are split.
defaultpath = '/notebooks/data/'


# In[7]:


# The format of the images is specified.
format_ = [
    '.jpg',
    '.jpeg',
    '.JPG'
    ]


# In[8]:


# The names needed to rearrange the split images are specified.
foldernames_new = ['VS', 'no_VS']
foldernames_old = ['left', 'right']
suffixes = ['_01', '_02']


# In[9]:


# The names of the training and validation subfolders are specified.
foldernames_partition = ['train', 'valid']


# In[10]:


def splitimage(path, directory, prefix, formattype):
    """This function splits the image vertically into two images and
       saves them.
    
    Args:
      path: path where image to be split is stored
      directory: directory where split images are saved
      prefix: prefix of the saved images
      formattype: format of saved images
    
    Returns:
      Nothing
    
    Raises:
      Saves the split images
    """
    slicedimage = image_slicer.slice(path, 2, save=False)
    image_slicer.save_tiles(
        slicedimage,
        directory=directory,
        prefix=prefix,
        format=formattype
        )
    
    return


# In[11]:


def tumorinimage(tumorside, imageside):
    """This function specifies if there is a tumor in the image based on
       the labelling of the image and tumor side given as an input.
    
    Args:
      tumorside: defines whether the tumor is on the left or right
      imageside: defines which image side it is
    
    Returns:
      tumor: specifies whether there is a tumor in the image or not
    """
    
    if tumorside == foldernames_old[0] and imageside == suffixes[1]:
        tumor = foldernames_new[0]
    if tumorside == foldernames_old[1] and imageside == suffixes[0]:
        tumor = foldernames_new[0]
    if tumorside == foldernames_old[0] and imageside == suffixes[0]:
        tumor = foldernames_new[1]
    if tumorside == foldernames_old[1] and imageside == suffixes[1]:
        tumor = foldernames_new[1]
    
    return tumor


# In[12]:


# First, the path substructure of the images to be saved is generated
# according to the substructure of the stored images. Next, the images
# are split and saved in the corresponding path.

# These variables are later used to rearrange and store the split
# images.
pathnamesimages_saved_all = []
pathnamespartial_sorted_all = []
pathnamesimages_sorted_all = []

if len(foldernames_stored) == len(foldernames_saved) and len(
    foldernames_stored) == len(foldernames_sorted):
    for h in range(len(foldernames_stored)):
        pathimages_stored = ''.join([defaultpath, foldernames_stored[h]])
        pathimages_saved = ''.join([defaultpath, foldernames_saved[h]])
        pathimages_sorted = ''.join([defaultpath, foldernames_sorted[h]])
        
        if not os.path.isdir(pathimages_saved):
            os.mkdir(pathimages_saved)
        
        if not os.path.isdir(pathimages_sorted):
            os.mkdir(pathimages_sorted)
        
        # It is checked whether the specified directories exist. In case
        # the directories do not exist, an error is returned.
        if os.path.isdir(pathimages_stored):
            if os.path.isdir(pathimages_saved):
                # In the next steps, the paths containing the images to
                # be split are searched. Simultaneously, the
                # corresponding paths are generated in the folder where
                # the split images will be saved.
                pathstructure = [x[0] for x in os.walk(pathimages_stored)]
                
                # In case the code is ran on a Windows file system, the
                # backslashes need to be replaced by slashes.
                for i in range(len(pathstructure)):
                    pathstructure[i] = pathstructure[i].replace('\\', '/')
                
                # The subfolders for the saved and sorted images are
                # created according to the folder structure of the
                # stored images.
                position = [None for i in range(len(pathstructure))]
                pathnamesimages_stored = []
                pathnamesimages_sorted = []
                pathnamespartial_sorted = []
                
                for i in range(len(pathstructure)):
                    position[i] = pathstructure[i].count('/')
                    directory_saved = ''.join([
                        pathimages_saved,
                        pathstructure[i][len(pathimages_stored):]
                        ])
                    if not os.path.isdir(directory_saved):
                        os.mkdir(directory_saved)
                    
                    directory_sorted = ''.join([
                        pathimages_sorted,
                        pathstructure[i][len(pathimages_stored):]
                        ])
                    for k in range(len(foldernames_old)):
                        if pathstructure[i][len(pathimages_stored):].find(
                                foldernames_old[k]) != -1:
                            directory_sorted = ''.join([
                                directory_sorted[: -len(foldernames_old[k])],
                                foldernames_new[k]
                                ])
                            pathnamesimages_sorted.append(directory_sorted)
                            pathnamespartial_sorted.append(directory_sorted[
                                : -len(foldernames_new[k])])
                    
                    if not os.path.isdir(directory_sorted):
                        os.mkdir(directory_sorted)
                
                # A list is created containing the paths of the stored
                # images.
                for i in range(len(position)):
                    if position[i] == max(position):
                        pathnamesimages_stored.append(pathstructure[i])
                
                # It is specified where the split images will be saved.
                # The same folder structure is selected as for the
                # stored images.
                pathnamesimages_saved = [
                    None for i in range(len(pathnamesimages_stored))
                    ]
                for i in range(len(pathnamesimages_stored)):
                    pathnamesimages_saved[i] = ''.join([
                        pathimages_saved,
                        pathnamesimages_stored[i][len(pathimages_stored) :]
                        ])
                
                # The path names are stored to rearrange the split
                # images at a later stage.
                pathnamesimages_saved_all.append(pathnamesimages_saved)
                pathnamespartial_sorted_all.append(pathnamespartial_sorted)
                pathnamesimages_sorted_all.append(pathnamesimages_sorted)
                
                # It is verified if the folders where the split images
                # will be saved are empty.
                foldersempty = 0
                for i in range(len(pathnamesimages_saved)):
                    if len(os.listdir(pathnamesimages_saved[i])) == 0:
                        foldersempty += 1
                
                # The images are only split and saved if the folders
                # where the split images will be saved are empty.
                if foldersempty == len(pathnamesimages_stored):
                    counter_stored = [0] * len(pathnamesimages_stored)
                    counter_saved = [0] * len(pathnamesimages_saved)
                    
                    # It is looped through the content of the folder and
                    # the split images are saved.
                    for i in range(len(pathnamesimages_stored)):
                        path_stored = pathnamesimages_stored[i]
                        path_saved = pathnamesimages_saved[i]
                        for k in range (len(os.listdir(path_stored))):
                            for m in range (len(format_)):
                                if os.listdir(path_stored)[k].endswith(
                                        format_[m]):
                                    pathimage_stored = ''.join([
                                        path_stored,
                                        '/',
                                        os.listdir(path_stored)[k]
                                        ])
                                    splitimage(pathimage_stored, path_saved,
                                               os.listdir(path_stored)[k][
                                               0 : -len(format_[m])],
                                               format_[1][1:])
                                    
                                    # The number of images to be split
                                    # is counted.
                                    counter_stored[i] += 1
                    
                    for i in range(len(pathnamesimages_saved)):
                        counter_saved[i] += len(os.listdir(
                            pathnamesimages_saved[i]))
                        foldertodelete = '.ipynb_checkpoints'
                        for k in range(len(os.listdir(
                               pathnamesimages_saved[i]))):
                            if os.listdir(pathnamesimages_saved[i])[k] ==                                     foldertodelete:
                                counter_saved[i] -= 1
                                os.rmdir(
                                    ''.join([
                                        pathnamesimages_saved[i],
                                        '/',
                                        foldertodelete
                                        ])
                                    )
                    
                    # The number of stored and split images is printed.
                    print(foldernames_stored[h], ':')
                    print('Number of images stored in specified folder:')
                    for i in range(len(pathnamesimages_stored)):
                        print(pathnamesimages_stored[i], ':',
                              counter_stored[i])
                    
                    print('Number of split images saved in specified folder:')
                    for i in range(len(pathnamesimages_saved)):
                        print(pathnamesimages_saved[i], ':', counter_saved[i])
                    
                    print('\n')
                    
                else:
                    print(foldersempty, 'out of', len(pathnamesimages_stored),
                          'folders are empty where the split images are '
                          'supposed to be saved.\n No images were split and '
                          'saved in this round.')
            
            else:
                print('The path name', pathimages_saved, 'does not exist.\n'
                      'Please check, if the default path and folder names '
                      'were entered correctly:\nfoldernames_saved =',
                      foldernames_saved)
        
        else:
            print('The specified path',  pathimages_stored,
                  'does not exist.\nEither the default path or folder name was '
                  'specified incorrectly:\n foldernames_stored =', foldernames_stored)

else:
     print('An inconsistent amount of folder names was defined regarding the '
           'stored,\nsaved, and sorted folders for the internal, external T1c, '
           'and external\nT1 cohorts. The same number of the respective folder '
           'names is needed as an\ninput for the internal, external T1c, and '
           'external T1 cohort.')


# # Rearrange the split images

# In[13]:


# The split images are rearranged based on whether the split images
# contain a tumor or not according to the labelling.

# The rearrangement of the split images is performed separately for
# every data set.
for h in range(len(pathnamesimages_saved_all)):
    for i in range(len(pathnamesimages_saved_all[h])):
        foldercontent = os.listdir(pathnamesimages_saved_all[h][i])
        
        # It is checked if one of the folder names is equal to one of 
        # the prespecified foldernames_old.
        for k in range(len(foldernames_old)):
            if pathnamesimages_saved_all[h][i].find(foldernames_old[k]) != -1:
                tumorside = foldernames_old[k]
                
                # It is checked if the image is a JPG image
                for m in range(len(foldercontent)):
                    checkformat = False
                    
                    if foldercontent[m].endswith(format_[0]):
                        checkformat = True
                        
                        # It is checked if the suffixes are equal to one
                        # of the prespecified suffixes.
                        checksuffixes = False
                        for n in range(len(suffixes)):
                            suffixlen = len(suffixes[n])
                            formatlen = len(format_[0])
                            suffix = foldercontent[m][
                                -(suffixlen + formatlen) : -formatlen]
                            
                            if suffix == suffixes[n]:
                                checksuffixes = True
                                
                                tumorpresent = tumorinimage(
                                    tumorside,
                                    suffixes[n]
                                    )
                                source = ''.join([
                                    pathnamesimages_saved_all[h][i],
                                    '/',
                                    foldercontent[m]
                                    ])
                                destination = ''.join([
                                    pathnamespartial_sorted_all[h][i],
                                    '/',
                                    tumorpresent,
                                    '/',
                                    foldercontent[m]
                                    ])
                                copyfile(source, destination)
                                
                        if not checksuffixes:
                            print('An unknown suffix was found:\nFolder '
                                  'content =', foldercontent[m],
                                  '\nin path: =',
                                  pathnamesimages_saved_all[h][i])
                
                if not checkformat:
                    print('A deviation in the image format was found:\nformat ',
                          '=', formatFile, '\nin path: =', 
                          pathnamesimages_saved_all[h][i])
    
    # The number of files is counted and printed which are stored in the
    # original and rearranged folders containing the split images.
    counter_saved = [0] * len(pathnamesimages_saved_all[h])
    counter_sorted = [0] * len(pathnamespartial_sorted_all[h])
    
    print(foldernames_stored[h], ':')
    print('Number of split images in original folder structure:')
    for i in range(len(pathnamesimages_saved_all[h])):
        counter_saved[i] = len(os.listdir(pathnamesimages_saved_all[h][i]))
        print(pathnamesimages_saved_all[h][i], ':', counter_saved[i])
    
    print('Number of split images in rearranged folder structure:')
    for i in range(len(pathnamesimages_sorted_all[h])):
        counter_sorted[i] = len(os.listdir(pathnamesimages_sorted_all[h][i]))
        print(pathnamesimages_sorted_all[h][i], ':', counter_sorted[i])
    
    print('\n')


# # Read data

# In[14]:


# The data is loaded and extracted
path = untar_data(foldernames_sorted[0])
path.ls()


# # Label and augment data

# In[15]:


# Data augmentation: According to precursor publication by
# Sager et al.
t1cslices = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=GrandparentSplitter(
        train_name=foldernames_partition[0],
        valid_name=foldernames_partition[1]
        ),
    get_y=parent_label,
    item_tfms=RandomResizedCrop(224, min_scale=0.9),
    batch_tfms=aug_transforms(
        max_lighting=0.1,
        max_rotate=15.0,
        do_flip=False
        )
    )
dls_t1cslices = t1cslices.dataloaders(path)


# # Check datablock

# In[16]:


# Check datablock
dls_t1cslices.show_batch(nrows=2, ncols=4)


# In[17]:


# ResNet-34 without weights is fitted: 15 unfrozen epochs
learn_t1cslices34 = cnn_learner(
    dls_t1cslices,
    resnet34,
    metrics=accuracy
    ).to_fp16()
learn_t1cslices34.fine_tune(15, freeze_epochs=5)


# In[18]:


# The loss function is checked
learn_t1cslices34.loss_func


# # Export models

# In[19]:


# The ResNet-34 is exported
learn_t1cslices34.export('VSBisectedSlices34.pkl')


# # Flattened cross-entropy loss

# In[20]:


def plotcrossentropyloss(learner):
    """This function plots the cross-entropy loss against the number of
       unfrozen epochs. 
    
    Args:
      numberepochs:
      learner: learner specifies the used CNN learner
    
    Returns:
      Nothing
    
    Raises:
      The cross-entropy loss against the number of unfrozen epochs is
      plotted and saved
    """
    numberepochs = len(learner.recorder.values)
    epochs = list(range(1, numberepochs + 1))
    
    trainloss = [None] * numberepochs
    validloss = [None] * numberepochs
    
    for i in range(0, len(learner.recorder.values)):
        trainloss[i] = learner.recorder.values[i][0]
        validloss[i] = learner.recorder.values[i][1]
    
    plt.plot(epochs, trainloss, epochs, validloss)
    plt.legend(labeling, fontsize=12)
    plt.xlabel('Number of epochs', fontsize=15)
    plt.ylabel('Flattened cross-entropy loss', fontsize=15)
    plt.xlim([1, numberepochs])
    plt.xticks(np.arange(1, numberepochs + 1, step=1))
    plt.ylim([0, 1])
    
    plt.savefig('flattenedcrossentropyloss_v2.svg')
    
    return


# In[21]:


# Plot of the flattened cross-entropy loss against the number of
# unfrozen epochs (plot version 1)
plt.figure()
learn_t1cslices34.recorder.plot_loss(skip_start=0)


# In[22]:


# Values of the flattened cross-entropy loss for the unfrozen epochs
learn_t1cslices34.recorder.values


# In[23]:


# Plot of the flattened cross-entropy loss against the number of
# unfrozen epochs (plot version 2)
labeling = ['training', 'internal validation']
plotcrossentropyloss(learn_t1cslices34)


# # Internal validation: Interpret models

# In[24]:


# Built-in interpretation methods are used
interp = ClassificationInterpretation.from_learner(learn_t1cslices34)


# In[25]:


# The top losses are plotted
# Caption: prediction, actual, loss, and probability of actual class
interp.plot_top_losses(k=10, nrows=5, largest=True)


# In[26]:


# The confusion matrix is plotted
interp.plot_confusion_matrix(figsize=(4, 4), dpi=60)


# In[27]:


# The confusion matrix is plotted and saved
plt.figure()

array = interp.confusion_matrix()
df_cm = pd.DataFrame(array, index=foldernames_new, columns=foldernames_new)

sn.set(font_scale=1.3)
hm = sn.heatmap(
    df_cm,
    annot=True,
    annot_kws={'size': 14},
    cbar=False,
    square=True,
    cmap='Blues',
    fmt='d'
    )
sn.set(font_scale=1)
figure = hm.get_figure()

plt.xlabel('Predicted', fontsize=15, labelpad=10)
plt.ylabel('Actual', fontsize=15, labelpad=15)
plt.title('Internal validation', fontsize=24, pad=20)
plt.show()

figure.savefig(
    'confusionmatrixinternalvalidation.svg',
    bbox_inches='tight'
    )


# # External validation T1c

# In[28]:


# The data is loaded and extracted
validt1cpath = untar_data(foldernames_sorted[1])
validt1cfiles = get_image_files(validt1cpath)


# In[29]:


validt1cfiles


# In[30]:


# A validation data loader is created
testt1cdl = dls_t1cslices.test_dl(validt1cfiles, with_labels=True)


# In[31]:


# ResNet-34: External validation T1c (the loss and metric are returned)
learn_t1cslices34.validate(dl=testt1cdl)


# # External validation T1c: Assess model

# In[32]:


# The used metric is checked
learn_t1cslices34.metrics


# In[33]:


# Built-in interpretation methods are used
classinterp_vs34t1c_exvalid = ClassificationInterpretation.from_learner(
    learn_t1cslices34,
    dl=testt1cdl
    )


# In[34]:


# The top losses are plotted
classinterp_vs34t1c_exvalid.plot_top_losses(k=10, nrows=2, largest=True)


# In[35]:


# The confusion matrix is plotted
classinterp_vs34t1c_exvalid.plot_confusion_matrix(figsize=(4, 4), dpi=120)


# In[36]:


# The accuracy is retrieved
classinterp_vs34t1c_exvalid.print_classification_report()


# # External validation T1c: Display MRI slices based on prediction vs. ground truth

# In[37]:


def saveimgdim(paths):
    """This function saves the image dimensions and corresponding ratio
    of the given image paths.
    
    Args:
      paths: path where image to be split is stored
    
    Returns:
      Nothing
    
    Raises:
      Saves the dimensions and the ratio of the dimensions
    """
    for i in range(0,len(paths)):
        img = PILImage.create(paths[i])
        dimx, dimy = img.size
        dim_x.append(dimx)
        dim_y.append(dimy)
        ratio.append(dimx / dimy)
    
    return


# In[38]:


def calcnrlines(nrtumor, nrnotumor):
    """This function calculates the number of lines needed for the plot
    regarding each category.
    
    Args:
      nrtumor: Number of images with a tumor in the respective category
      nrnotumor: Number of images without tumor in the respective
                 category
    
    Returns:
      The number of lines needed for the respective category
    """
    nrimages = [nrtumor, nrnotumor]
    largestcategory = max(nrimages)
    nrlines = math.ceil(largestcategory / nrcol)
    
    return nrlines


# In[39]:


def plotimages(paths, nrsubplots_cat, linestart, colstart, modstart):
    """This function plots the images consecutively.
    
    Args:
      paths: path where images to plot are saved
      nrsubplots_cat: total number of subplots of the respective
                      category
      linestart: line to plot the first image
      colstart: column to plot the first image
      modstart: first entry of dimensions and ratios corresponding to
                the given images
    
    Returns:
      Nothing
    
    Raises:
      Plots the given images accordingly
    """
    line = linestart
    col = colstart
    
    for i in range(0, nrsubplots_cat):
        if i < len(paths):
            img = PILImage.create(paths[i])
            pos = modstart + i
            img_m = img.crop((
                0,
                dim_y[pos] - dim_x[pos]/max(ratio),
                dim_x[pos],
                dim_y[pos]
                ))
            
            axs[line, col].axis('off')
            axs[line, col].imshow(img_m, interpolation='none')
        else:
            axs[line, col].axis('off')
            axs[line, col].imshow(emptyimg, interpolation='none')
        
        if col == colstart + nrcol - 1:
            if colstart < nrcol:
                axs[line, col + 1].set_visible(False)
            
            col = colstart
            line += 1
        else:
            col += 1
    
    if linestart == 0:
        if colstart < nrcol:
            for i in range(0, 2*nrcol + 1):
                axs[line, col].axis('off')
                axs[line, col].imshow(emptyimg, interpolation='none')
                col += 1
    
    return


# In[40]:


# Built-in interpretation methods are used
interp_vs34t1c_exvalid = Interpretation.from_learner(
    learn_t1cslices34,
    dl=testt1cdl
    )


# In[41]:


# The predictions compared to the ground truth are received
print(interp_vs34t1c_exvalid.targs)
print(interp_vs34t1c_exvalid.decoded)


# In[42]:


# The images are assigned into correctly and incorrectly predicted MRI
# slices with and without a VS
corr_tumor = []
incorr_tumor = []

corr_notumor = []
incorr_notumor = []

nrimages = len(interp_vs34t1c_exvalid.targs)

if str(validt1cfiles[0]).count('no') > str(validt1cfiles[-1]).count('no'):
    novs = interp_vs34t1c_exvalid.targs[0]

else:
    novs = interp_vs34t1c_exvalid.targs[-1]

for i in range(nrimages):
    if interp_vs34t1c_exvalid.targs[i] == novs:
        if interp_vs34t1c_exvalid.targs[i] == interp_vs34t1c_exvalid.decoded[
                i]:
            corr_notumor.append(validt1cfiles[i])

        else:
            incorr_notumor.append(validt1cfiles[i])
    else:
        if interp_vs34t1c_exvalid.targs[i] == interp_vs34t1c_exvalid.decoded[
                i]:
            corr_tumor.append(validt1cfiles[i])

        else:
            incorr_tumor.append(validt1cfiles[i])


# In[43]:


# The dimensions and the corresponding ratio of each image is saved
dim_x = []
dim_y = []
ratio = []

start_corr_tumor = 0
saveimgdim(corr_tumor)

start_corr_notumor = len(dim_x)
saveimgdim(corr_notumor)

start_incorr_tumor = len(dim_x)
saveimgdim(incorr_tumor)

start_incorr_notumor = len(dim_x)
saveimgdim(incorr_notumor)


# In[44]:


# The number of lines is calculated for the plot regarding the correctly
# and incorrectly classified images
nrcol = 8

nrlines_corr = calcnrlines(len(corr_tumor), len(corr_notumor))
nrlines_incorr = calcnrlines(len(incorr_tumor), len(incorr_notumor))
nrlines = nrlines_corr + nrlines_incorr


# In[45]:


# The images are displayed based on the prediction and ground truth
plt.figure()
fig, axs = plt.subplots(
    nrows=nrlines + 1,
    ncols=2*nrcol + 1,
    squeeze=True,
    gridspec_kw={'left':0,'right':0.46, 'hspace':0.05}
    )

x = dim_x[0]
y = math.floor(dim_x[0] / max(ratio))
emptyimg = np.zeros([x, y, 3], dtype=np.uint8)
emptyimg.fill(255)

nrsubplots_corr = nrlines_corr * nrcol
plotimages(corr_tumor, nrsubplots_corr, 0, 0, start_corr_tumor)
plotimages(corr_notumor, nrsubplots_corr, 0, nrcol + 1, start_corr_notumor)

nrsubplots_incorr = nrlines_incorr * nrcol
plotimages(
    incorr_tumor,
    nrsubplots_incorr,
    nrlines_corr + 1,
    0,
    start_incorr_tumor
    )
plotimages(
    incorr_notumor,
    nrsubplots_incorr,
    nrlines_corr + 1,
    nrcol + 1,
    start_incorr_notumor
    )

plt.subplots_adjust(
    left=0.125,
    bottom=0.1,
    right=0.9,
    top=0.9,
    wspace=0.05,
    hspace=0.05
    )

plt.show()


# In[46]:


# The figure is saved
fig.savefig('externalvalidation_t1c.svg')


# # External validation T1c: Grad-CAM

# In[47]:


# The image paths are specified. These images are later displayed using
# Grad-CAM.
images_corr = [
    'VS/AX_T1KM_VS_PL_115_1_01_02.jpg',
    'VS/AX_T1KM_VS_PR_154_1_01_01.jpg',
    'VS/AX_T1KM_VS_PR_006_1_01_01.jpg',
    'no_VS/AX_T1KM_VS_SL_066_1_01_01.jpg',
    'no_VS/AX_T1KM_VS_PR_144_1_01_02.jpg'
    ]
images_incorr = [
    'VS/AX_T1KM_VS_PR_148_1_01_01.jpg',
    'VS/AX_T1KM_VS_PL_042_1_01_02.jpg',
    'VS/AX_T1KM_VS_PL_004_1_01_02.jpg',
    'no_VS/AX_T1KM_VS_PR_113_1_01_02.jpg',
    'no_VS/AX_T1KM_VS_PR_109_1_01_02.jpg'
    ]


# In[48]:


# The order of the prediction names is checked. It is also checked how 0
# and 1 correspond to no_VS and VS. This information is necessary for
# the next steps.
learn_t1cslices34.dls.vocab


# In[49]:


# It is specified for each image whether the MRI slice contains a VS or
# not (0 = VS, 1 = no VS in my case).
cls_corr = [
    0,
    0,
    0,
    1,
    1
    ]
cls_incorr = [
    0,
    0,
    0,
    1,
    1
    ]


# In[50]:


class Hook():
    """This class intercepts and stores the activation of a
    convolutional layer.
    
    Attributes:
      m: Represents the given layer of the CNN body
    
    Methods:
      __init__(self, m): Attaches the hook to the given layer of the CNN
                         body
      hook_func(self, m, i, o): Stores the output (i.e. the activations)
      def __enter__(self, *args): This function is called when an object
                                  is created in a with clause
      __exit__(self, *args): Removes Hook once done to prevent memory
                             leakage
    """
    def __init__(self, m): self.hook = m.register_forward_hook(self.hook_func)
    def hook_func(self, m, i, o): self.stored = o.detach().clone()
    def __enter__(self, *args): return self
    def __exit__(self, *args): self.hook.remove()


# In[51]:


class HookBwd():
    """This class intercepts and stores the gradient of a convolutional
    layer.
    
    Attributes:
      m: Represents the given layer of the CNN body
    
    Methods:
      __init__(self, m): Attaches the hook to the given layer of the CNN
                         body
      hook_func(self, m, i, o): Stores the output (i.e. the gradients)
      def __enter__(self, *args): This function is called when an object
                                  is created in a with clause
      __exit__(self, *args): Removes HookBwd once done to prevent memory
                             leakage
    """
    def __init__(self, m): self.hook = m.register_backward_hook(self.hook_func)
    def hook_func(self, m, gi, go): self.stored = go[0].detach().clone()
    def __enter__(self, *args): return self
    def __exit__(self, *args): self.hook.remove()


# In[52]:


def plotgradCAM(col, img, cam_map):
    """This function plots the Grad-CAM images.
    
    Args:
      col: Defines the column of the subplot
      img: Defines the image to plot
      cam_map: Defines the CAM map
    
    Returns:
      Nothing
    
    Raises:
      Plots Grad-CAM images accordingly
    """
    cam_map = torch.unsqueeze(cam_map, 0)
    cam_map = torchvision.transforms.functional.resize(
        cam_map,
        (img.size[0], img.size[0])
        )
    cam_map = torch.squeeze(cam_map)
    
    cam_map_m = np.full(
        (img.size[1], img.size[0]),
        cam_map.detach().cpu().min()
        )
    start = math.floor((img.size[1] - img.size[0])/2)
    cam_map_m[start : start + img.size[0], :] = cam_map.detach().cpu().numpy()
    
    axs[0, col].axis('off')
    axs[0, col].imshow(img, interpolation='none')
    axs[1, col].axis('off')
    axs[1, col].imshow(img, interpolation='none')
    axs[1, col].imshow(
        cam_map_m,
        alpha=0.6,
        interpolation='none',
        cmap='magma'
        )
    axs[1, col].set_title('#' + str(col + 1), y=-0.2, fontsize=15, pad=-10)
    
    return


# In[53]:


def plotgradCAM_v2(col, img, cam_map):
    """This function plots the Grad-CAM images.
    
    Args:
      col: Defines the column of the subplot
      img: Defines the image to plot
      cam_map: Defines the CAM map
    
    Returns:
      Nothing
    
    Raises:
      Plots Grad-CAM images accordingly
    """
    start = math.floor((img.size[1] - img.size[0])/2)
    img_cropped = img.crop((0, start, img.size[0], start + img.size[0]))
      
    axs[0, col].axis('off')
    axs[0, col].imshow(img_cropped, interpolation='none')
    axs[1, col].axis('off')
    axs[1, col].imshow(img_cropped, interpolation='none')
    axs[1, col].imshow(
        cam_map.detach().cpu(),
        alpha=0.6,
        extent=(0, img.size[0], img.size[0], 0),
        interpolation='bilinear',
        cmap='magma'
        )
    axs[1, col].set_title('#' + str(col + 1), y=-0.2, fontsize=15, pad=-10)
    
    return


# In[54]:


def plotgradCAM_v3(col, dls_t1cslices, x, img, cam_map):
    """This function plots the Grad-CAM images.
    
    Args:
      col: Defines the column of the subplot
      dls_t1cslices: Defines which trained model to use
      x: Defines the first tensorImage
      img: Defines the image to plot
      cam_map: Defines the CAM map
     
    Returns:
      Nothing
     
    Raises:
      Plots Grad-CAM images accordingly
    """
    x_dec = TensorImage(dls_t1cslices.train.decode((x,))[0][0])
    
    axs[0, col].axis('off')
    x_dec.show(ctx=axs[0, col])
    
    axs[1, col].axis('off')
    x_dec.show(ctx=axs[1, col])
    axs[1, col].imshow(
        cam_map.detach().cpu(),
        alpha=0.6,
        extent=(0, 224, 224, 0),
        interpolation='bilinear',
        cmap='magma'
        )
    axs[1, col].set_title('#' + str(col + 1), y=-0.2, fontsize=15, pad=-10)
    
    return


# In[55]:


def calculateandplotgradCAM(
        images,
        cls,
        dls_t1cslices,
        learn_t1cslices34,
        versionplot
        ):
    """This function calculates and plots the Grad-CAM images.
    
    Args:
      listimages: A list of image names that are supposed to be plotted
                  using Grad-CAM
      cls: A list of whether the respective image contains a VS or not
      dls_t1cslices: Defines which trained model to use
      learn_t1cslices34: Defines which loaded learner to use
      versionplot: Defines how to plot the images
    
    Returns:
      Nothing
    
    Raises:
      Calculates and plots Grad-CAM images accordingly
    """
    for i in range(0, len(images)):
        img = PILImage.create(path + images[i])
        x, = first(dls_t1cslices.test_dl([img]))
        
        with HookBwd(learn_t1cslices34.model[0]) as hookg:
            # A Hook is created and attached to the last layer of the
            # CNN body
            with Hook(learn_t1cslices34.model[0]) as hook:
                # A batch is taken and run through the through the model
                output = learn_t1cslices34.model.eval()(x.cuda())
                # The stored activations are accessed.
                act = hook.stored
            
            output[0,cls_corr[i]].backward()
            grad = hookg.stored
        
        w = grad[0].mean(dim=[1, 2], keepdim=True)
        cam_map = (w * act[0]).sum(0)
        
        if versionplot == 1:
            plotgradCAM(i, img, cam_map)
        
        elif versionplot == 2:
            plotgradCAM_v2(i, img, cam_map)
        
        else:
            plotgradCAM_v3(i, dls_t1cslices, x, img, cam_map)
    
    return


# In[56]:


# The learner is loaded. This step is only necessary if the learner has
# not been run previously.
learn_t1cslices34 = load_learner('VSBisectedSlices34.pkl')


# In[57]:


learn_t1cslices34.model.cuda()


# In[58]:


# The path is specified.
path = defaultpath + foldernames_sorted[1] + '/'


# In[59]:


# The correctly classified images are plotted (plot version 2).
plt.figure()
fig, axs = plt.subplots(
    nrows=2,
    ncols=len(images_corr),
    gridspec_kw={'left': 0,'right': 0.9, 'hspace': 0.2}
    )

calculateandplotgradCAM(
    images_corr,
    cls_corr,
    dls_t1cslices,
    learn_t1cslices34,
    2
    )

fig.suptitle('Correctly classified images', x=0.45, fontsize=24)
plt.show()


# In[60]:


# The figure is saved.
fig.savefig('GradCAM_corr_v2_externalvalidation_t1c_pv2.svg')


# In[61]:


# The incorrectly classified images are plotted (plot version 2).
plt.figure()
fig, axs = plt.subplots(
    nrows=2,
    ncols=len(images_incorr),
    gridspec_kw={'left': 0,'right': 0.9, 'hspace': 0.2}
    )

calculateandplotgradCAM(
    images_incorr,
    cls_incorr,
    dls_t1cslices,
    learn_t1cslices34,
    2
    )

fig.suptitle('Incorrectly classified images', x=0.45, fontsize=24)
plt.show()


# In[62]:


# The figure is saved.
fig.savefig('GradCAM_incorr_v2_externalvalidation_t1c_pv2.svg')


# # External validation T1

# In[63]:


# The data is loaded and extracted.
validt1path = untar_data(foldernames_sorted[2])
validt1files = get_image_files(validt1path)


# In[64]:


validt1files


# In[65]:


# A validation data loader is created.
testt1dl = dls_t1cslices.test_dl(validt1files, with_labels=True)


# In[66]:


# ResNet-34: External validation T1
learn_t1cslices34.validate(dl=testt1dl)


# # External validation T1: Assess model

# In[67]:


# Built-in interpretation methods are used.
interp_vs34t1_exvalid = ClassificationInterpretation.from_learner(
    learn_t1cslices34,
    dl=testt1dl
    )


# In[68]:


# The confusion matrix is plotted.
interp_vs34t1_exvalid.plot_confusion_matrix(figsize=(4, 4), dpi=120)


# # External validation T1: Display MRI slices based on prediction vs. ground truth

# In[69]:


# Built-in interpretation methods are used.
interp_vs34t1_exvalid = Interpretation.from_learner(
    learn_t1cslices34,
    dl=testt1dl
    )


# In[70]:


# The predictions compared to the ground truth are obtained.
print(interp_vs34t1_exvalid.targs)
print(interp_vs34t1_exvalid.decoded)


# In[71]:


# The images are assigned into correctly and incorrectly predicted MRI
# slices with and without a VS.
corr_tumor_t1 = []
incorr_tumor_t1 = []

corr_notumor_t1 = []
incorr_notumor_t1 = []

nrimages_t1 = len(interp_vs34t1_exvalid.targs)

if str(validt1files[0]).count('no') > str(validt1files[-1]).count('no'):
    novs = interp_vs34t1_exvalid.targs[0]

else: 
    novs = interp_vs34t1_exvalid.targs[-1]

for i in range(nrimages_t1):
    if interp_vs34t1_exvalid.targs[i] == novs:
        if interp_vs34t1_exvalid.targs[i] == interp_vs34t1_exvalid.decoded[i]:
            corr_notumor_t1.append(validt1files[i])

        else:
            incorr_notumor_t1.append(validt1files[i])
    else:
        
        if interp_vs34t1_exvalid.targs[i] == interp_vs34t1_exvalid.decoded[i]:
            corr_tumor_t1.append(validt1files[i])

        else:
            incorr_tumor_t1.append(validt1files[i])


# In[72]:


# The dimensions and the corresponding ratio of each image are saved.
dim_x = []
dim_y = []
ratio = []

start_corr_tumor_t1 = 0
saveimgdim(corr_tumor_t1)

start_corr_notumor_t1 = len(dim_x)
saveimgdim(corr_notumor_t1)

start_incorr_tumor_t1 = len(dim_x)
saveimgdim(incorr_tumor_t1)

start_incorr_notumor_t1 = len(dim_x)
saveimgdim(incorr_notumor_t1)


# In[73]:


# The number of lines is calculated for the plot regarding the correctly
# and incorrectly classified images.
nrlines_corr_t1 = calcnrlines(len(corr_tumor_t1), len(corr_notumor_t1))
nrlines_incorr_t1 = calcnrlines(len(incorr_tumor_t1), len(incorr_notumor_t1))
nrlines_t1 = nrlines_corr_t1 + nrlines_incorr_t1


# In[74]:


# The images are displayed based on the prediction and ground truth.
plt.figure()
fig, axs = plt.subplots(
    nrows=nrlines_t1 + 1,
    ncols=2*nrcol + 1,
    squeeze=True,
    gridspec_kw={'left': 0,'right': 0.25, 'hspace': 0.05})

x = dim_x[0]
y = math.floor(dim_x[0] / max(ratio))
emptyimg = np.zeros([x, y, 3], dtype=np.uint8)
emptyimg.fill(255)

nrsubplots_corr_t1 = nrlines_corr_t1 * nrcol
plotimages(corr_tumor_t1, nrsubplots_corr_t1, 0, 0, start_corr_tumor_t1)
plotimages(
    corr_notumor_t1,
    nrsubplots_corr_t1,
    0,
    nrcol + 1,
    start_corr_notumor_t1
    )

nrsubplots_incorr_t1 = nrlines_incorr_t1 * nrcol
plotimages(
    incorr_tumor_t1,
    nrsubplots_incorr_t1,
    nrlines_corr_t1 + 1,
    0,
    start_incorr_tumor_t1
    )
plotimages(
    incorr_notumor_t1,
    nrsubplots_incorr_t1,
    nrlines_corr_t1 + 1,
    nrcol + 1,
    start_incorr_notumor_t1
    )

plt.subplots_adjust(
    left=0.125,
    bottom=0.1,
    right=0.9,
    top=0.9,
    wspace=0.05,
    hspace=0.05
    )

plt.show()


# In[75]:


# The figure is saved.
fig.savefig('externalvalidation_t1.svg')

