import glob
import radiomics
import os
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
import SimpleITK as sitk
import pandas as pd
from FeatureExtract import FeatureExtract

a=glob.glob('ml_subjet_0803/train/*_T1C.nii')

b=[str.replace('_T1C.nii','_T1C_label.nii',) for str in a]



data = pd.DataFrame({'id':[],
'EnergyFeatureValue':[],
'TotalEnergyFeatureValue':[],
'EntropyFeatureValue':[],
'MinimumFeatureValue':[],
'10PercentileFeatureValue':[],
'90PercentileFeatureValue':[],
'MaximumFeatureValue':[],
'MeanFeatureValue':[],
'MedianFeatureValue':[],
'InterquartileRangeFeatureValue':[],
'RangeFeatureValue':[],
'MeanAbsoluteDeviationFeatureValuee':[],
'RobustMeanAbsoluteDeviationFeatureValue':[],
'RootMeanSquaredFeatureValue':[],
'StandardDeviationFeatureValue':[],
'SkewnessFeatureValue(axis=0)':[],
'KurtosisFeatureValue(axis=0)':[],
'VarianceFeatureValue':[],
'UniformityFeatureValue':[],
'calculateDiameters':[],
'VolumeFeatureValue':[],
'SurfaceAreaFeatureValue':[],
'SurfaceVolumeRatioFeatureValue':[],
'SphericityFeatureValue':[],
'Compactness1FeatureValue':[],
'Compactness2FeatureValue':[],
'SphericalDisproportionFeatureValue':[],
'Maximum3DDiameterFeatureValue':[],
'Maximum2DDiameterSliceFeatureValue':[],
'Maximum2DDiameterColumnFeatureValue':[],
'Maximum2DDiameterRowFeatureValue':[],
'MajorAxisFeatureValue':[],
'MinorAxisFeatureValue':[],
'LeastAxisFeatureValue':[],
'ElongationFeatureValue':[],
'FlatnessFeatureValue':[],
'AutocorrelationFeatureValue':[],
'JointAverageFeatureValue':[],
'ClusterProminenceFeatureValue':[],
'ClusterShadeFeatureValue':[],
'ClusterTendencyFeatureValue':[],
'ContrastFeatureValue':[],
'CorrelationFeatureValue':[],
'DifferenceAverageFeatureValue':[],
'DifferenceEntropyFeatureValue':[],
'DifferenceVarianceFeatureValue':[],
'DissimilarityFeatureValue':[],
'JointEnergyFeatureValue':[],
'JointEntropyFeatureValue':[],
'Homogeneity1FeatureValue':[],
'Homogeneity2FeatureValue':[],
'Imc1FeatureValue':[],
'Imc2FeatureValue':[],
'IdmFeatureValue':[],
'IdmnFeatureValue':[],
'IdFeatureValue':[],
'IdnFeatureValue':[],
'InverseVarianceFeatureValue':[],
'MaximumProbabilityFeatureValue':[],
'SumAverageFeatureValue':[],
'SumVarianceFeatureValue':[],
'SumEntropyFeatureValue':[],
'SumSquaresFeatureValue':[],
'SmallAreaEmphasisFeatureValue':[],
'LargeAreaEmphasisFeatureValue':[],
'GrayLevelNonUniformityFeatureValue':[],
'GrayLevelNonUniformityNormalizedFeatureValue':[],
'SizeZoneNonUniformityFeatureValue':[],
'SizeZoneNonUniformityNormalizedFeatureValue':[],
'ZonePercentageFeatureValue':[],
'GrayLevelVarianceFeatureValue':[],
'ZoneVarianceFeatureValue':[],
'ZoneEntropyFeatureValue':[],
'LowGrayLevelZoneEmphasisFeatureValue':[],
'HighGrayLevelZoneEmphasisFeatureValue':[],
'SmallAreaLowGrayLevelEmphasisFeatureValue':[],
'SmallAreaHighGrayLevelEmphasisFeatureValue':[],
'LargeAreaLowGrayLevelEmphasisFeatureValue':[],
'LargeAreaHighGrayLevelEmphasisFeatureValue':[],
'ShortRunEmphasisFeatureValue':[],
'LongRunEmphasisFeatureValue':[],
'GrayLevelNonUniformityFeatureValue':[],
'GrayLevelNonUniformityNormalizedFeatureValue':[],
'RunLengthNonUniformityFeatureValue':[],
'RunLengthNonUniformityNormalizedFeatureValue':[],
'RunPercentageFeatureValue':[],
'GrayLevelVarianceFeatureValue':[],
'RunVarianceFeatureValue':[],
'RunEntropyFeatureValue':[],
'LowGrayLevelRunEmphasisFeatureValue':[],
'HighGrayLevelRunEmphasisFeatureValue':[],
'ShortRunLowGrayLevelEmphasisFeatureValue':[],
'ShortRunHighGrayLevelEmphasisFeatureValue':[],
'LongRunLowGrayLevelEmphasisFeatureValue':[],
'LongRunHighGrayLevelEmphasisFeatureValue':[],
'CoarsenessFeatureValue':[],
'ContrastFeatureValue':[],
'BusynessFeatureValue':[],
'ComplexityFeatureValue':[],
'StrengthFeatureValue':[],
'SmallDependenceEmphasisFeatureValue':[],
'LargeDependenceEmphasisFeatureValue':[],
'GrayLevelNonUniformityFeatureValue':[],
'GrayLevelNonUniformityNormalizedFeatureValue':[],
'DependenceNonUniformityFeatureValue':[],
'DependenceNonUniformityNormalizedFeatureValue':[],
'GrayLevelVarianceFeatureValue':[],
'DependenceVarianceFeatureValue':[],
'DependenceEntropyFeatureValue':[],
'DependencePercentageFeatureValue':[],
'LowGrayLevelEmphasisFeatureValue':[],
'HighGrayLevelEmphasisFeatureValue':[],
'SmallDependenceLowGrayLevelEmphasisFeatureValue':[],
'SmallDependenceHighGrayLevelEmphasisFeatureValue':[],
'LargeDependenceLowGrayLevelEmphasisFeatureValue':[],
'LargeDependenceHighGrayLevelEmphasisFeatureValue':[]},
columns = ['id','EnergyFeatureValue','TotalEnergyFeatureValue','EntropyFeatureValue','MinimumFeatureValue'
,'10PercentileFeatureValue','90PercentileFeatureValue','MaximumFeatureValue','MeanFeatureValue'
,'MedianFeatureValue','InterquartileRangeFeatureValue','RangeFeatureValue','MeanAbsoluteDeviationFeatureValue'
,'RobustMeanAbsoluteDeviationFeatureValue','RootMeanSquaredFeatureValue','StandardDeviationFeatureValue','SkewnessFeatureValue(axis=0)'
,'KurtosisFeatureValue(axis=0)','VarianceFeatureValue','UniformityFeatureValue','calculateDiameters'
,'VolumeFeatureValue','SurfaceAreaFeatureValue','SurfaceVolumeRatioFeatureValue','SphericityFeatureValue'
,'Compactness1FeatureValue','Compactness2FeatureValue','SphericalDisproportionFeatureValue','Maximum3DDiameterFeatureValue'
,'Maximum2DDiameterSliceFeatureValue','Maximum2DDiameterColumnFeatureValue','Maximum2DDiameterRowFeatureValue','MajorAxisFeatureValue'
,'MinorAxisFeatureValue','LeastAxisFeatureValue','ElongationFeatureValue','FlatnessFeatureValue'
,'AutocorrelationFeatureValue','JointAverageFeatureValue','ClusterProminenceFeatureValue','ClusterShadeFeatureValue'
,'ClusterTendencyFeatureValue','ContrastFeatureValue','CorrelationFeatureValue','DifferenceAverageFeatureValue'
,'DifferenceEntropyFeatureValue','DifferenceVarianceFeatureValue','DissimilarityFeatureValue','JointEnergyFeatureValue'
,'JointEntropyFeatureValue','Homogeneity1FeatureValue','Homogeneity2FeatureValue','Imc1FeatureValue'
,'Imc2FeatureValue','IdmFeatureValue','IdmnFeatureValue','IdFeatureValue'
,'IdnFeatureValue','InverseVarianceFeatureValue','MaximumProbabilityFeatureValue','SumAverageFeatureValue'
,'SumVarianceFeatureValue','SumEntropyFeatureValue','SumSquaresFeatureValue','SmallAreaEmphasisFeatureValue'
,'LargeAreaEmphasisFeatureValue','GrayLevelNonUniformityFeatureValue','GrayLevelNonUniformityNormalizedFeatureValue','SizeZoneNonUniformityFeatureValue'
,'SizeZoneNonUniformityNormalizedFeatureValue','ZonePercentageFeatureValue','GrayLevelVarianceFeatureValue','ZoneVarianceFeatureValue'
,'ZoneEntropyFeatureValue','LowGrayLevelZoneEmphasisFeatureValue','HighGrayLevelZoneEmphasisFeatureValue','SmallAreaLowGrayLevelEmphasisFeatureValue'
,'SmallAreaHighGrayLevelEmphasisFeatureValue','LargeAreaLowGrayLevelEmphasisFeatureValue','LargeAreaHighGrayLevelEmphasisFeatureValue','ShortRunEmphasisFeatureValue'
,'LongRunEmphasisFeatureValue','GrayLevelNonUniformityFeatureValue','GrayLevelNonUniformityNormalizedFeatureValue','RunLengthNonUniformityFeatureValue'
,'RunLengthNonUniformityNormalizedFeatureValue','RunPercentageFeatureValue','GrayLevelVarianceFeatureValue','RunVarianceFeatureValue'
,'RunEntropyFeatureValue','LowGrayLevelRunEmphasisFeatureValue','HighGrayLevelRunEmphasisFeatureValue','ShortRunLowGrayLevelEmphasisFeatureValue'
,'ShortRunHighGrayLevelEmphasisFeatureValue','LongRunLowGrayLevelEmphasisFeatureValue','LongRunHighGrayLevelEmphasisFeatureValue','CoarsenessFeatureValue'
,'ContrastFeatureValue','BusynessFeatureValue','ComplexityFeatureValue','StrengthFeatureValue'
,'SmallDependenceEmphasisFeatureValue','LargeDependenceEmphasisFeatureValue','GrayLevelNonUniformityFeatureValue','GrayLevelNonUniformityNormalizedFeatureValue'
,'DependenceNonUniformityFeatureValue','DependenceNonUniformityNormalizedFeatureValue','GrayLevelVarianceFeatureValue','DependenceVarianceFeatureValue'
,'DependenceEntropyFeatureValue','DependencePercentageFeatureValue','LowGrayLevelEmphasisFeatureValue','HighGrayLevelEmphasisFeatureValue'
,'SmallDependenceLowGrayLevelEmphasisFeatureValue','SmallDependenceHighGrayLevelEmphasisFeatureValue','LargeDependenceLowGrayLevelEmphasisFeatureValue','LargeDependenceHighGrayLevelEmphasisFeatureValue'
])


for i in range(len(a)):
    img = sitk.ReadImage(a[i])
    label = sitk.ReadImage(b[i])
    feature=FeatureExtract(img,label)
    id = os.path.split(a[i])[1].split('_')[0]
    df = pd.DataFrame({'id':[id],
        'EnergyFeatureValue':[feature[0]],
        'TotalEnergyFeatureValue':[feature[1]],
        'EntropyFeatureValue':[feature[2]],
        'MinimumFeatureValue':[feature[3]],
        '10PercentileFeatureValue':[feature[4]],
        '90PercentileFeatureValue':[feature[5]],
        'MaximumFeatureValue':[feature[6]],
        'MeanFeatureValue':[feature[7]],
        'MedianFeatureValue':[feature[8]],
        'InterquartileRangeFeatureValue':[feature[9]],
        'RangeFeatureValue':[feature[10]],
        'MeanAbsoluteDeviationFeatureValuee':[feature[11]],
        'RobustMeanAbsoluteDeviationFeatureValue':[feature[12]],
        'RootMeanSquaredFeatureValue':[feature[13]],
        'StandardDeviationFeatureValue':[feature[14]],
        'SkewnessFeatureValue(axis=0)':[feature[15]],
        'KurtosisFeatureValue(axis=0)':[feature[16]],
        'VarianceFeatureValue':[feature[17]],
        'UniformityFeatureValue':[feature[18]],
        'calculateDiameters':[feature[19]],
        'VolumeFeatureValue':[feature[20]],
        'SurfaceAreaFeatureValue':[feature[21]],
        'SurfaceVolumeRatioFeatureValue':[feature[22]],
        'SphericityFeatureValue':[feature[23]],
        'Compactness1FeatureValue':[feature[24]],
        'Compactness2FeatureValue':[feature[25]],
        'SphericalDisproportionFeatureValue':[feature[26]],
        'Maximum3DDiameterFeatureValue':[feature[27]],
        'Maximum2DDiameterSliceFeatureValue':[feature[28]],
        'Maximum2DDiameterColumnFeatureValue':[feature[29]],
        'Maximum2DDiameterRowFeatureValue':[feature[30]],
        'MajorAxisFeatureValue':[feature[31]],
        'MinorAxisFeatureValue':[feature[32]],
        'LeastAxisFeatureValue':[feature[33]],
        'ElongationFeatureValue':[feature[34]],
        'FlatnessFeatureValue':[feature[35]],
        'AutocorrelationFeatureValue':[feature[36]],
        'JointAverageFeatureValue':[feature[37]],
        'ClusterProminenceFeatureValue':[feature[38]],
        'ClusterShadeFeatureValue':[feature[39]],
        'ClusterTendencyFeatureValue':[feature[40]],
        'ContrastFeatureValue':[feature[41]],
        'CorrelationFeatureValue':[feature[42]],
        'DifferenceAverageFeatureValue':[feature[43]],
        'DifferenceEntropyFeatureValue':[feature[44]],
        'DifferenceVarianceFeatureValue':[feature[45]],
        'DissimilarityFeatureValue':[feature[46]],
        'JointEnergyFeatureValue':[feature[47]],
        'JointEntropyFeatureValue':[feature[48]],
        'Homogeneity1FeatureValue':[feature[49]],
        'Homogeneity2FeatureValue':[feature[50]],
        'Imc1FeatureValue':[feature[51]],
        'Imc2FeatureValue':[feature[52]],
        'IdmFeatureValue':[feature[53]],
        'IdmnFeatureValue':[feature[54]],
        'IdFeatureValue':[feature[55]],
        'IdnFeatureValue':[feature[56]],
        'InverseVarianceFeatureValue':[feature[57]],
        'MaximumProbabilityFeatureValue':[feature[58]],
        'SumAverageFeatureValue':[feature[59]],
        'SumVarianceFeatureValue':[feature[60]],
        'SumEntropyFeatureValue':[feature[61]],
        'SumSquaresFeatureValue':[feature[62]],
        'SmallAreaEmphasisFeatureValue':[feature[63]],
        'LargeAreaEmphasisFeatureValue':[feature[64]],
        'GrayLevelNonUniformityFeatureValue':[feature[65]],
        'GrayLevelNonUniformityNormalizedFeatureValue':[feature[66]],
        'SizeZoneNonUniformityFeatureValue':[feature[67]],
        'SizeZoneNonUniformityNormalizedFeatureValue':[feature[68]],
        'ZonePercentageFeatureValue':[feature[69]],
        'GrayLevelVarianceFeatureValue':[feature[70]],
        'ZoneVarianceFeatureValue':[feature[71]],
        'ZoneEntropyFeatureValue':[feature[72]],
        'LowGrayLevelZoneEmphasisFeatureValue':[feature[73]],
        'HighGrayLevelZoneEmphasisFeatureValue':[feature[74]],
        'SmallAreaLowGrayLevelEmphasisFeatureValue':[feature[75]],
        'SmallAreaHighGrayLevelEmphasisFeatureValue':[feature[76]],
        'LargeAreaLowGrayLevelEmphasisFeatureValue':[feature[77]],
        'LargeAreaHighGrayLevelEmphasisFeatureValue':[feature[78]],
        'ShortRunEmphasisFeatureValue':[feature[79]],
        'LongRunEmphasisFeatureValue':[feature[80]],
        'GrayLevelNonUniformityFeatureValue':[feature[81]],
        'GrayLevelNonUniformityNormalizedFeatureValue':[feature[82]],
        'RunLengthNonUniformityFeatureValue':[feature[83]],
        'RunLengthNonUniformityNormalizedFeatureValue':[feature[84]],
        'RunPercentageFeatureValue':[feature[85]],
        'GrayLevelVarianceFeatureValue':[feature[86]],
        'RunVarianceFeatureValue':[feature[87]],
        'RunEntropyFeatureValue':[feature[88]],
        'LowGrayLevelRunEmphasisFeatureValue':[feature[89]],
        'HighGrayLevelRunEmphasisFeatureValue':[feature[90]],
        'ShortRunLowGrayLevelEmphasisFeatureValue':[feature[91]],
        'ShortRunHighGrayLevelEmphasisFeatureValue':[feature[92]],
        'LongRunLowGrayLevelEmphasisFeatureValue':[feature[93]],
        'LongRunHighGrayLevelEmphasisFeatureValue':[feature[94]],
        'CoarsenessFeatureValue':[feature[95]],
        'ContrastFeatureValue':[feature[96]],
        'BusynessFeatureValue':[feature[97]],
        'ComplexityFeatureValue':[feature[98]],
        'StrengthFeatureValue':[feature[99]],
        'SmallDependenceEmphasisFeatureValue':[feature[100]],
        'LargeDependenceEmphasisFeatureValue':[feature[101]],
        'GrayLevelNonUniformityFeatureValue':[feature[102]],
        'GrayLevelNonUniformityNormalizedFeatureValue':[feature[103]],
        'DependenceNonUniformityFeatureValue':[feature[104]],
        'DependenceNonUniformityNormalizedFeatureValue':[feature[105]],
        'GrayLevelVarianceFeatureValue':[feature[106]],
        'DependenceVarianceFeatureValue':[feature[107]],
        'DependenceEntropyFeatureValue':[feature[108]],
        'DependencePercentageFeatureValue':[feature[109]],
        'LowGrayLevelEmphasisFeatureValue':[feature[110]],
        'HighGrayLevelEmphasisFeatureValue':[feature[111]],
        'SmallDependenceLowGrayLevelEmphasisFeatureValue':[feature[112]],
        'SmallDependenceHighGrayLevelEmphasisFeatureValue':[feature[113]],
        'LargeDependenceLowGrayLevelEmphasisFeatureValue':[feature[114]],
        'LargeDependenceHighGrayLevelEmphasisFeatureValue':[feature[115]]},
        columns = ['id','EnergyFeatureValue','TotalEnergyFeatureValue','EntropyFeatureValue','MinimumFeatureValue'
        ,'10PercentileFeatureValue','90PercentileFeatureValue','MaximumFeatureValue','MeanFeatureValue'
        ,'MedianFeatureValue','InterquartileRangeFeatureValue','RangeFeatureValue','MeanAbsoluteDeviationFeatureValue'
        ,'RobustMeanAbsoluteDeviationFeatureValue','RootMeanSquaredFeatureValue','StandardDeviationFeatureValue','SkewnessFeatureValue(axis=0)'
        ,'KurtosisFeatureValue(axis=0)','VarianceFeatureValue','UniformityFeatureValue','calculateDiameters'
        ,'VolumeFeatureValue','SurfaceAreaFeatureValue','SurfaceVolumeRatioFeatureValue','SphericityFeatureValue'
        ,'Compactness1FeatureValue','Compactness2FeatureValue','SphericalDisproportionFeatureValue','Maximum3DDiameterFeatureValue'
        ,'Maximum2DDiameterSliceFeatureValue','Maximum2DDiameterColumnFeatureValue','Maximum2DDiameterRowFeatureValue','MajorAxisFeatureValue'
        ,'MinorAxisFeatureValue','LeastAxisFeatureValue','ElongationFeatureValue','FlatnessFeatureValue'
        ,'AutocorrelationFeatureValue','JointAverageFeatureValue','ClusterProminenceFeatureValue','ClusterShadeFeatureValue'
        ,'ClusterTendencyFeatureValue','ContrastFeatureValue','CorrelationFeatureValue','DifferenceAverageFeatureValue'
        ,'DifferenceEntropyFeatureValue','DifferenceVarianceFeatureValue','DissimilarityFeatureValue','JointEnergyFeatureValue'
        ,'JointEntropyFeatureValue','Homogeneity1FeatureValue','Homogeneity2FeatureValue','Imc1FeatureValue'
        ,'Imc2FeatureValue','IdmFeatureValue','IdmnFeatureValue','IdFeatureValue'
        ,'IdnFeatureValue','InverseVarianceFeatureValue','MaximumProbabilityFeatureValue','SumAverageFeatureValue'
        ,'SumVarianceFeatureValue','SumEntropyFeatureValue','SumSquaresFeatureValue','SmallAreaEmphasisFeatureValue'
        ,'LargeAreaEmphasisFeatureValue','GrayLevelNonUniformityFeatureValue','GrayLevelNonUniformityNormalizedFeatureValue','SizeZoneNonUniformityFeatureValue'
        ,'SizeZoneNonUniformityNormalizedFeatureValue','ZonePercentageFeatureValue','GrayLevelVarianceFeatureValue','ZoneVarianceFeatureValue'
        ,'ZoneEntropyFeatureValue','LowGrayLevelZoneEmphasisFeatureValue','HighGrayLevelZoneEmphasisFeatureValue','SmallAreaLowGrayLevelEmphasisFeatureValue'
        ,'SmallAreaHighGrayLevelEmphasisFeatureValue','LargeAreaLowGrayLevelEmphasisFeatureValue','LargeAreaHighGrayLevelEmphasisFeatureValue','ShortRunEmphasisFeatureValue'
        ,'LongRunEmphasisFeatureValue','GrayLevelNonUniformityFeatureValue','GrayLevelNonUniformityNormalizedFeatureValue','RunLengthNonUniformityFeatureValue'
        ,'RunLengthNonUniformityNormalizedFeatureValue','RunPercentageFeatureValue','GrayLevelVarianceFeatureValue','RunVarianceFeatureValue'
        ,'RunEntropyFeatureValue','LowGrayLevelRunEmphasisFeatureValue','HighGrayLevelRunEmphasisFeatureValue','ShortRunLowGrayLevelEmphasisFeatureValue'
        ,'ShortRunHighGrayLevelEmphasisFeatureValue','LongRunLowGrayLevelEmphasisFeatureValue','LongRunHighGrayLevelEmphasisFeatureValue','CoarsenessFeatureValue'
        ,'ContrastFeatureValue','BusynessFeatureValue','ComplexityFeatureValue','StrengthFeatureValue'
        ,'SmallDependenceEmphasisFeatureValue','LargeDependenceEmphasisFeatureValue','GrayLevelNonUniformityFeatureValue','GrayLevelNonUniformityNormalizedFeatureValue'
        ,'DependenceNonUniformityFeatureValue','DependenceNonUniformityNormalizedFeatureValue','GrayLevelVarianceFeatureValue','DependenceVarianceFeatureValue'
        ,'DependenceEntropyFeatureValue','DependencePercentageFeatureValue','LowGrayLevelEmphasisFeatureValue','HighGrayLevelEmphasisFeatureValue'
        ,'SmallDependenceLowGrayLevelEmphasisFeatureValue','SmallDependenceHighGrayLevelEmphasisFeatureValue','LargeDependenceLowGrayLevelEmphasisFeatureValue','LargeDependenceHighGrayLevelEmphasisFeatureValue'
        ])

    data = data.append(df)
    print('data engraved:',i+1)

data.to_csv('train_data_T1C.csv',index=None)
