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

# Load training and test data
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)
X20 = X_test[:1000]
yt20 = y_test[:1000]

# Define the MLP Classifier
class MLPClassifier:
    def __init__(self, input_size, hidden_size, output_size, learning_rate=np.random.rand(4)):
        self.W1 = np.random.randn(input_size, hidden_size) * 0.01
        self.b1 = np.zeros((1, hidden_size))
        self.W2 = np.random.randn(hidden_size, output_size) * 0.01
        self.b2 = np.zeros((1, output_size))
        self.learning_rate = learning_rate
    
    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)
    
    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(y_pred + 1e-9)) / 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)
        self.W1 -= self.learning_rate[0] * dW1
        self.b1 -= self.learning_rate[1] * db1
        self.W2 -= self.learning_rate[2] * dW2
        self.b2 -= self.learning_rate[3] * 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 forward_conditional(self, X, thinking_time):
        """
        Calculate forward pass with conditional probability given thinking time.
        P(y|x, t) - probability of class y given input x and thinking time t
        
        Different samples can take different paths based on their thinking time.
        thinking_time can be a scalar (same for all) or array (per-sample)
        """
        if isinstance(thinking_time, (int, float)):
            thinking_time = np.full(X.shape[0], thinking_time)
        
        # Store for potential backward pass
        self.z1 = np.dot(X, self.W1) + self.b1
        self.a1 = self.relu(self.z1)
        
        # Apply thinking time modulation to hidden layer
        # Longer thinking time = more refined representations
        # Modulate activations based on thinking time
        t_normalized = thinking_time.reshape(-1, 1) / 100.0  # Normalize by expected max iterations
        modulation = np.minimum(t_normalized, 1.0)  # Cap at 1.0
        
        # Apply modulation to hidden activations
        a1_modulated = self.a1 * modulation
        
        # Second layer with modulated activations
        self.z2 = np.dot(a1_modulated, self.W2) + self.b2
        
        # Temperature scaling based on thinking time
        # Higher thinking time = lower temperature (more confident)
        temperature = 1.0 / (0.5 + 0.5 * modulation)
        z2_scaled = self.z2 * temperature
        
        output = self.softmax(z2_scaled)
        return output

    def conditional_probability(self, X, thinking_time, y_true=None):
        """
        Calculate P(y|x, t) - conditional probability given input and thinking time.
        Returns probabilities and optionally the loss.
        """
        probabilities = self.forward_conditional(X, thinking_time)
        
        if y_true is not None:
            loss = self.compute_loss(y_true, probabilities)
            return probabilities, loss
        
        return probabilities

# Initialize and train the MLP Classifier
learning_rate = np.random.rand(4)
f = MLPClassifier(input_size=784, hidden_size=100, output_size=10, learning_rate=learning_rate)

i = 0
while True:
    idx = np.random.randint(0, 60000, 100)
    X = X_train[idx]
    yt = y_train[idx]
    #print(i, f.score(X,yt))
    f.update(X, np.eye(10)[yt])
    i += 1
    
    # Example: Calculate conditional probabilities with different thinking times
    if i % 100 == 0:
        # Test with uniform thinking time
        probs_uniform, loss = f.conditional_probability(X[:10], 50, np.eye(10)[yt[:10]])
        print(f"  Iteration {i}: Conditional prob (t=50) - Loss: {loss:.4f}")
        
        # Test with per-sample thinking times (different paths for each sample)
        thinking_times = np.random.randint(10, 100, 10)  # Different thinking time per sample
        probs_variable, loss_variable = f.conditional_probability(
            X[:10], thinking_times, np.eye(10)[yt[:10]]
        )
        print(f"  Iteration {i}: Conditional prob (variable t) - Loss: {loss_variable:.4f}")
        print(f"  Thinking times: {thinking_times}")

