import numpy as np
from scipy.io.wavfile import read

# Load training and test data
try:
    X_train = read('../X_train.wav')[1].reshape(-1, 784)
    y_train = (read('../y_train.wav')[1] * 9).astype(int)
    X_test = read('../X_test.wav')[1].reshape(-1, 784)
    y_test = (read('../y_test.wav')[1] * 9).astype(int)
except Exception:
    # Fallback to simulated data if files are not present
    X_train = np.random.randn(60000, 784)
    y_train = np.random.randint(0, 10, 60000)
    X_test = np.random.randn(1000, 784)
    y_test = np.random.randint(0, 10, 1000)

X20 = X_test[:1000]
yt20 = y_test[:1000]

# Define the CCT-ODE Enhanced MLP Classifier
class MLPClassifier:
    def __init__(self, input_size, hidden_size, output_size, learning_rate=None):
        if learning_rate is None:
            self.learning_rate = np.random.rand(4) * 0.01 + 0.001
        else:
            self.learning_rate = learning_rate
            
        # Initialize weights with standard normal distribution scaled for stable flow
        self.W1 = np.random.randn(input_size, hidden_size) * np.sqrt(2.0 / input_size)
        self.b1 = np.zeros((1, hidden_size))
        self.W2 = np.random.randn(hidden_size, output_size) * np.sqrt(2.0 / hidden_size)
        self.b2 = np.zeros((1, output_size))
    
    def relu(self, x):
        return np.maximum(0, x)
    
    def relu_derivative(self, x):
        return np.where(x > 0, 1, 0)
    
    def softmax(self, x):
        exp_x = np.exp(x - np.max(x, axis=1, keepdims=True))
        return exp_x / (np.sum(exp_x, axis=1, keepdims=True) + 1e-15)
    
    def forward(self, X):
        self.z1 = np.dot(X, self.W1) + self.b1
        self.a1 = self.relu(self.z1)
        self.z2 = np.dot(self.a1, self.W2) + self.b2
        output = self.softmax(self.z2)
        return output
    
    def compute_loss(self, y_true, y_pred):
        m = y_true.shape[0]
        loss = -np.sum(y_true * np.log(np.clip(y_pred, 1e-15, 1.0 - 1e-15))) / m
        return loss
    
    def backward(self, X, y_true, y_pred):
        m = y_true.shape[0]
        
        dz2 = y_pred - y_true
        dW2 = np.dot(self.a1.T, dz2) / m
        db2 = np.sum(dz2, axis=0, keepdims=True) / m
        
        da1 = np.dot(dz2, self.W2.T)
        dz1 = da1 * self.relu_derivative(self.z1)
        dW1 = np.dot(X.T, dz1) / m
        db1 = np.sum(dz1, axis=0, keepdims=True) / m
        
        return dW1, db1, dW2, db2
    
    def update(self, X, y_true):
        y_pred = self.forward(X)
        dW1, db1, dW2, db2 = self.backward(X, y_true, y_pred)
        
        norm_dW1 = np.linalg.norm(dW1) + 1e-15
        norm_dW2 = np.linalg.norm(dW2) + 1e-15
        
        step1 = self.learning_rate[0] / (1.0 + log_scale_step(norm_dW1))
        step2 = self.learning_rate[1] / (1.0 + log_scale_step(norm_dW1))
        step3 = self.learning_rate[2] / (1.0 + log_scale_step(norm_dW2))
        step4 = self.learning_rate[3] / (1.0 + log_scale_step(norm_dW2))
        
        self.W1 -= step1 * dW1
        self.b1 -= step2 * db1
        self.W2 -= step3 * dW2
        self.b2 -= step4 * db2
    
    def predict(self, X):
        probabilities = self.forward(X)
        return np.argmax(probabilities, axis=1)

    def score(self, X, y_true):
        y = self.predict(X)
        return np.mean(y == y_true)

    def calculate_esd(self, matrix):
        """Calculates the Effective Significant Digits of a given weight tensor."""
        abs_mat = np.abs(matrix)
        max_val = np.max(abs_mat)
        # Avoid zero elements to prevent log(0)
        min_val = np.min(abs_mat[abs_mat > 0]) if np.any(abs_mat > 0) else 1e-15
        
        # Logarithmic scale metric for dynamic digit capacity
        esd = np.log10(max_val / min_val)
        return esd

def log_scale_step(norm_val):
    return np.log1p(norm_val)

# Initialize and train the Enhanced Classifier
if __name__ == "__main__":
    learning_rate = np.array([0.05, 0.05, 0.05, 0.05])
    f = MLPClassifier(input_size=784, hidden_size=128, output_size=10, learning_rate=learning_rate)

    i = 0
    print("Beginning Dynamic CCT-ODE Training Sequence with Precision Metrics...")
    while i <= 100000:
        idx = np.random.randint(0, X_train.shape[0], 128)
        X = X_train[idx]
        yt = y_train[idx]
        
        if i % 50 == 0:
            train_acc = f.score(X, yt)
            test_acc = f.score(X20, yt20)
            
            # Extract real-time significant digit resolution metrics
            esd_w1 = f.calculate_esd(f.W1)
            esd_w2 = f.calculate_esd(f.W2)
            
            print(f"Iteration: {i:03d} | Train Acc: {train_acc:.4f} | Test Acc: {test_acc:.4f} | W1 ESD: {esd_w1:.2f} | W2 ESD: {esd_w2:.2f}")
            
        f.update(X, np.eye(10)[yt])
        i += 1
