import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
import scipy.io as sio
from ex7_utility import *
from ex7 import *

## Machine Learning Online Class - Exercise 7: Principle Component Analysis and K-Means Clustering

#  Instructions
#  ------------
# 
#  This file contains code that helps you get started on the
#  exercise. You will need to complete the following functions 
#  in this exericse:
#
#     pca
#     projectData
#     recoverData
#

# ==================== All function declaration ====================

def pca(X):
    m, n = X.shape
    U = np.zeros((n,n))
    S = np.zeros((n,n))
    # ============= YOUR CODE HERE =============
    # Instructions: You should first compute the covariance matrix. Then, you
    #               should use the "svd" function to compute the eigenvectors
    #               and eigenvalues of the covariance matrix. 
    #
    # Note: When computing the covariance matrix, remember to divide by m (the
    #       number of examples).
    # ===========================================
    return U, S

def projectData(X, U, K):
    m, n = X.shape
    Z = np.zeros((m, K))
    # ============= YOUR CODE HERE =============
    # Instructions: Compute the projection of the data using only the top K 
    #               eigenvectors in U (first K columns).
    # ===========================================
    return Z

def recoverData(Z, U, K):
    m, n = Z.shape
    X_rec = np.zeros((m, K))
    # ============= YOUR CODE HERE =============
    # Instructions: You should set centroids to randomly chosen examples from
    #               the dataset X
    # ===========================================
    return X_rec

if __name__ == "__main__":
    plt.close('all')
    plt.ion() # interactive mode

    # ==================== Part 1: Load Example Dataset ====================
    
    print('Visualizing example dataset for PCA.')

    data_file = '../../data/ex7/ex7data1.mat'
    mat_content = sio.loadmat(data_file)

    X = mat_content['X']