# -*- coding: utf-8 -*-
"""
Created on Thu Mar 10 13:38:24 2022

@author: katja
"""

from __future__ import print_function

import os
import glob
import pandas as pd


head = ['ID','Visit','Image','Mask']

#Initialize datadict
datadict = pd.DataFrame([], columns=head)

path = r"C:/Users/strah/OneDrive/Namizje/Raziskave/Melanoma/Retrospective/Patients/*/*.nii.gz"
print("path: ", path)

outDir = r'C:/Users/strah/OneDrive/Namizje/Raziskave/Melanoma/Retrospective'

PETNames = [] 
MaskNames = [] 

for filename in glob.iglob(path, recursive=True):
    file=os.path.basename(filename)
    print(file)
    name = file.split('.')[0]
    modal= "_".join(name.split('_')[2:])
    print(modal)
    if (modal =='PET_notCropped_2mmVoxel'):
        PETNames.append(filename)
    elif (modal == 'Segm_v5'):
        MaskNames.append(filename)
        
j=0

for (PETname,MaskName) in zip(PETNames,MaskNames):
    base = os.path.basename(PETname)
    print(PETname)
    name = base.split('.')[0]
    ID= "-".join(name.split('-')[0:5])
    allelse="-".join(name.split('-')[5:])
    Visit="_".join(allelse.split('_')[0:2])
    print(Visit)
   
    #MaskName = os.path.join(MaskDir,'*-label.nrrd')
    
    print('Now processing:', ID)
    
    datadict.loc[j] = [ID, Visit, PETname, MaskName]
    j+=1

datadict.to_csv(os.path.join(outDir, 'cases_retroMManon.csv'))