import os
import numpy as np
import scipy.io as sio
import tifffile

from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split


#Load dataset
def loadData(name,data_path):
    if name == 'IP':
        data = sio.loadmat(os.path.join(data_path, 'Indian_pines_corrected.mat'))['indian_pines_corrected']
        labels = sio.loadmat(os.path.join(data_path, 'Indian_pines_gt.mat'))['indian_pines_gt']
    elif name == 'SA':
        data = sio.loadmat(os.path.join(data_path, 'Salinas_corrected.mat'))['salinas_corrected']
        labels = sio.loadmat(os.path.join(data_path, 'Salinas_gt.mat'))['salinas_gt']
    elif name == 'PU':
        data = sio.loadmat(os.path.join(data_path, 'PaviaU.mat'))['paviaU']
        labels = sio.loadmat(os.path.join(data_path, 'PaviaU_gt.mat'))['paviaU_gt']
    elif name == 'HU13':
        # dict_keys(['__header__', '__version__', '__globals__', 'Houston'])
        #dict_values([b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Wed Jul 17 16:45:01 2019', '1.0', [], array()])
        #data = sio.loadmat(os.path.join(data_path, 'Houston.mat'))
        #labels = sio.loadmat(os.path.join(data_path,'Houston_gt.mat'))
        data = sio.loadmat(os.path.join(data_path, 'Houston.mat'))['Houston']
        labels = sio.loadmat(os.path.join(data_path,'Houston_gt.mat'))['Houston_gt']
    elif name == 'KSC':
        data = sio.loadmat(os.path.join(data_path, 'KSC.mat'))['KSC']
        labels = sio.loadmat(os.path.join(data_path,'KSC_gt.mat'))['KSC_gt']

    return data, labels



# Use tifffile pkg read the hyperspectral img.
# Load .tiff data set and converted to .mat data
def loadTifDataTomat(data_path,save_DataPath,name):
    if name=='HU13':
        totalTif=tifffile.imread(os.path.join(data_path,'2013_IEEE_GRSS_DF_Contest_CASI.tif'))
        trainTif=tifffile.imread(os.path.join(data_path,'train_roi.tif'))
        valTif=tifffile.imread(os.path.join(data_path,'val_roi.tif'))

        print(totalTif.shape,trainTif.shape,valTif.shape)
        #spectral.imshow(totalTif)
        #spectral.imshow(trainTif)
        sio.savemat(os.path.join(save_DataPath,"totalTifHouston13.mat"),{'totalTifHouston13':totalTif})
        sio.savemat(os.path.join(save_DataPath,"trainTifHouston13.mat"),{'trainTifHouston13':trainTif})
        sio.savemat(os.path.join(save_DataPath,"valTifHouston13.mat"),{'valTifHouston13':valTif})



def loadTifMat(data_path,name):
    if name=='HU13':
        data=sio.loadmat(os.path.join(data_path, 'totalTifHouston13.mat'))['totalTifHouston13']
        train=sio.loadmat(os.path.join(data_path, 'trainTifHouston13.mat'))['trainTifHouston13']
        val=sio.loadmat(os.path.join(data_path, 'valTifHouston13.mat'))['valTifHouston13']
        return data,train,val



### Using PCA for removing the spectral redundancy(冗余)
### Reduce the spectral dimension, from high-dimensional to low-dimensional.
def applyPCA(X, numComponents=75):
    newX = np.reshape(X, (-1, X.shape[2]))
    pca = PCA(n_components=numComponents, whiten=True)
    newX = pca.fit_transform(newX)
    newX = np.reshape(newX, (X.shape[0],X.shape[1], numComponents))
    return newX, pca



### Padding zeros
def padWithZeros(X, margin=2):
    newX = np.zeros((X.shape[0] + 2 * margin, X.shape[1] + 2* margin, X.shape[2]))
    x_offset = margin
    y_offset = margin
    newX[x_offset:X.shape[0] + x_offset, y_offset:X.shape[1] + y_offset, :] = X
    return newX



### Create data cube,3D-patch.
def createImageCubes(X, y, windowSize=5, removeZeroLabels = True):
    margin = int((windowSize - 1) / 2)
    zeroPaddedX = padWithZeros(X, margin=margin)
    # split patches
    patchesData = np.zeros((X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2]))
    patchesLabels = np.zeros((X.shape[0] * X.shape[1]))
    patchIndex = 0
    for r in range(margin, zeroPaddedX.shape[0] - margin):
        for c in range(margin, zeroPaddedX.shape[1] - margin):
            patch = zeroPaddedX[r - margin:r + margin + 1, c - margin:c + margin + 1]
            patchesData[patchIndex, :, :, :] = patch
            patchesLabels[patchIndex] = y[r-margin, c-margin]
            patchIndex = patchIndex + 1
    if removeZeroLabels:
        patchesData = patchesData[patchesLabels>0,:,:,:]
        patchesLabels = patchesLabels[patchesLabels>0]
        patchesLabels -= 1
    return patchesData, patchesLabels

# Dataset split.
def splitTrainTestSet(X, y, testRatio, randomState=345):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=testRatio, random_state=randomState,
                                                        stratify=y)
    return X_train, X_test, y_train, y_test
[((2943, 2974), 'numpy.reshape', 'np.reshape', (['X', '(-1, X.shape[2])'], {}), '(X, (-1, X.shape[2]))\n', (2953, 2974), True, 'import numpy as np\n'), ((2985, 3029), 'sklearn.decomposition.PCA', 'PCA', ([], {'n_components': 'numComponents', 'whiten': '(True)'}), '(n_components=numComponents, whiten=True)\n', (2988, 3029), False, 'from sklearn.decomposition import PCA\n'), ((3076, 3133), 'numpy.reshape', 'np.reshape', (['newX', '(X.shape[0], X.shape[1], numComponents)'], {}), '(newX, (X.shape[0], X.shape[1], numComponents))\n', (3086, 3133), True, 'import numpy as np\n'), ((3217, 3289), 'numpy.zeros', 'np.zeros', (['(X.shape[0] + 2 * margin, X.shape[1] + 2 * margin, X.shape[2])'], {}), '((X.shape[0] + 2 * margin, X.shape[1] + 2 * margin, X.shape[2]))\n', (3225, 3289), True, 'import numpy as np\n'), ((3656, 3727), 'numpy.zeros', 'np.zeros', (['(X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2])'], {}), '((X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2]))\n', (3664, 3727), True, 'import numpy as np\n'), ((3748, 3781), 'numpy.zeros', 'np.zeros', (['(X.shape[0] * X.shape[1])'], {}), '(X.shape[0] * X.shape[1])\n', (3756, 3781), True, 'import numpy as np\n'), ((4482, 4567), 'sklearn.model_selection.train_test_split', 'train_test_split', (['X', 'y'], {'test_size': 'testRatio', 'random_state': 'randomState', 'stratify': 'y'}), '(X, y, test_size=testRatio, random_state=randomState,\n    stratify=y)\n', (4498, 4567), False, 'from sklearn.model_selection import train_test_split\n'), ((1751, 1812), 'os.path.join', 'os.path.join', (['data_path', '"""2013_IEEE_GRSS_DF_Contest_CASI.tif"""'], {}), "(data_path, '2013_IEEE_GRSS_DF_Contest_CASI.tif')\n", (1763, 1812), False, 'import os\n'), ((1846, 1886), 'os.path.join', 'os.path.join', (['data_path', '"""train_roi.tif"""'], {}), "(data_path, 'train_roi.tif')\n", (1858, 1886), False, 'import os\n'), ((1918, 1956), 'os.path.join', 'os.path.join', (['data_path', '"""val_roi.tif"""'], {}), "(data_path, 'val_roi.tif')\n", (1930, 1956), False, 'import os\n'), ((2106, 2158), 'os.path.join', 'os.path.join', (['save_DataPath', '"""totalTifHouston13.mat"""'], {}), "(save_DataPath, 'totalTifHouston13.mat')\n", (2118, 2158), False, 'import os\n'), ((2210, 2262), 'os.path.join', 'os.path.join', (['save_DataPath', '"""trainTifHouston13.mat"""'], {}), "(save_DataPath, 'trainTifHouston13.mat')\n", (2222, 2262), False, 'import os\n'), ((2314, 2364), 'os.path.join', 'os.path.join', (['save_DataPath', '"""valTifHouston13.mat"""'], {}), "(save_DataPath, 'valTifHouston13.mat')\n", (2326, 2364), False, 'import os\n'), ((254, 307), 'os.path.join', 'os.path.join', (['data_path', '"""Indian_pines_corrected.mat"""'], {}), "(data_path, 'Indian_pines_corrected.mat')\n", (266, 307), False, 'import os\n'), ((364, 410), 'os.path.join', 'os.path.join', (['data_path', '"""Indian_pines_gt.mat"""'], {}), "(data_path, 'Indian_pines_gt.mat')\n", (376, 410), False, 'import os\n'), ((2473, 2521), 'os.path.join', 'os.path.join', (['data_path', '"""totalTifHouston13.mat"""'], {}), "(data_path, 'totalTifHouston13.mat')\n", (2485, 2521), False, 'import os\n'), ((2570, 2618), 'os.path.join', 'os.path.join', (['data_path', '"""trainTifHouston13.mat"""'], {}), "(data_path, 'trainTifHouston13.mat')\n", (2582, 2618), False, 'import os\n'), ((2665, 2711), 'os.path.join', 'os.path.join', (['data_path', '"""valTifHouston13.mat"""'], {}), "(data_path, 'valTifHouston13.mat')\n", (2677, 2711), False, 'import os\n'), ((481, 529), 'os.path.join', 'os.path.join', (['data_path', '"""Salinas_corrected.mat"""'], {}), "(data_path, 'Salinas_corrected.mat')\n", (493, 529), False, 'import os\n'), ((581, 622), 'os.path.join', 'os.path.join', (['data_path', '"""Salinas_gt.mat"""'], {}), "(data_path, 'Salinas_gt.mat')\n", (593, 622), False, 'import os\n'), ((688, 725), 'os.path.join', 'os.path.join', (['data_path', '"""PaviaU.mat"""'], {}), "(data_path, 'PaviaU.mat')\n", (700, 725), False, 'import os\n'), ((766, 806), 'os.path.join', 'os.path.join', (['data_path', '"""PaviaU_gt.mat"""'], {}), "(data_path, 'PaviaU_gt.mat')\n", (778, 806), False, 'import os\n'), ((1214, 1252), 'os.path.join', 'os.path.join', (['data_path', '"""Houston.mat"""'], {}), "(data_path, 'Houston.mat')\n", (1226, 1252), False, 'import os\n'), ((1294, 1335), 'os.path.join', 'os.path.join', (['data_path', '"""Houston_gt.mat"""'], {}), "(data_path, 'Houston_gt.mat')\n", (1306, 1335), False, 'import os\n'), ((1401, 1435), 'os.path.join', 'os.path.join', (['data_path', '"""KSC.mat"""'], {}), "(data_path, 'KSC.mat')\n", (1413, 1435), False, 'import os\n'), ((1473, 1510), 'os.path.join', 'os.path.join', (['data_path', '"""KSC_gt.mat"""'], {}), "(data_path, 'KSC_gt.mat')\n", (1485, 1510), False, 'import os\n')]