""" Example usage of the entropy module. """ from src.entropy import count_spike_occurrences, calculate_empirical_probabilities from src.entropy import compute_neuron_entropy, aggregate_network_entropy from src.utils.common import SpikeState import numpy as np import matplotlib.pyplot as plt def main(): print("Entropy Calculation Example") print("--------------------------") # Generate random spike data for 5 neurons np.random.seed(42) # For reproducibility num_neurons = 5 batch_size = 100 # Generate data with different distributions to show entropy differences spike_data = [] for i in range(num_neurons): if i == 0: # Neuron 0: Balanced distribution probs = [1/3, 1/3, 1/3] elif i == 1: # Neuron 1: Mostly neutral probs = [0.1, 0.8, 0.1] elif i == 2: # Neuron 2: Mostly positive probs = [0.1, 0.2, 0.7] elif i == 3: # Neuron 3: Mostly negative probs = [0.7, 0.2, 0.1] else: # Neuron 4: Completely neutral (deterministic) probs = [0, 1, 0] # Generate spikes based on probabilities spikes = np.random.choice([-1, 0, 1], size=batch_size, p=probs) spike_data.append(spikes.tolist()) # Process each neuron print(f"\nProcessing {num_neurons} neurons with batch size {batch_size}...") entropies = [] for i in range(num_neurons): # Count spike occurrences counts = count_spike_occurrences(i, spike_data[i], batch_size) print(f"\nNeuron {i} spike counts:") for state, count in counts.items(): print(f" {state.name}: {count}") # Calculate probabilities epsilon = 1e-9 probabilities = calculate_empirical_probabilities(counts, batch_size, epsilon) print(f"Neuron {i} probabilities:") for state, prob in probabilities.items(): print(f" {state.name}: {prob:.4f}") # Compute entropy entropy = compute_neuron_entropy(probabilities) print(f"Neuron {i} entropy: {entropy:.4f}") entropies.append(entropy) # Aggregate entropy network_entropy = aggregate_network_entropy(entropies) print(f"\nNetwork entropy: {network_entropy:.4f}") # Theoretical maximum entropy max_entropy = np.log(3) print(f"Theoretical maximum entropy: {max_entropy:.4f}") print(f"Network entropy as % of maximum: {(network_entropy/max_entropy)*100:.2f}%") # Visualize the entropies try: plt.figure(figsize=(10, 6)) # Bar plot for individual neuron entropies plt.subplot(1, 2, 1) plt.bar(range(num_neurons), entropies) plt.axhline(y=max_entropy, color='r', linestyle='--', label=f'Max Entropy = {max_entropy:.2f}') plt.xlabel('Neuron ID') plt.ylabel('Entropy') plt.title('Entropy by Neuron') plt.xticks(range(num_neurons)) plt.legend() # Pie chart for spike distribution (for first neuron) plt.subplot(1, 2, 2) labels = [f"{state.name}" for state in SpikeState] counts_neuron0 = [counts[state] for state in SpikeState] plt.pie(counts_neuron0, labels=labels, autopct='%1.1f%%') plt.title(f'Spike Distribution for Neuron 0\nEntropy: {entropies[0]:.2f}') plt.tight_layout() plt.savefig('entropy_example.png') print("\nVisualization saved as 'entropy_example.png'") except Exception as e: print(f"\nCould not create visualization: {e}") if __name__ == "__main__": main()