Spiking Neuron Network Simulator  1.0
Simulation and training of spiking neuron networks, primarily theta neurons
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
TestHelper.cs
Go to the documentation of this file.
1 namespace SpikingNeuronNetwork.Test
2 {
3  using Lib;
4  using Lib.Interfaces;
5  using Microsoft.VisualStudio.TestTools.UnitTesting;
6  using System;
7  using System.Collections.Generic;
8  using System.Linq;
9 
13  public class TestHelper
14  {
18  public static double Epsilon = 1e-4;
19 
28  public static void CompareSpikeTrains(IReadOnlyCollection<Spike> spikeTrain1, IReadOnlyList<Spike> spikeTrain2, double threshold)
29  {
30  //Find Mean-Squared in Spike Times Between the Different Methods
31  Assert.AreEqual(spikeTrain1.Count, spikeTrain2.Count,
32  "The Two Spike Trains Contain Different Numbers of Spikes! Unable to Compare.\n\tTrain 1: " + String.Join(", ", spikeTrain1) + "\n\tTrain 2: " + String.Join(", ", spikeTrain2));
33 
34  if (spikeTrain1.Count == 0)
35  {
36  Console.WriteLine("The Two Spike Trains Produced No Output Spikes!");
37  Console.WriteLine();
38  }
39  else
40  {
41  double meanSquaredError = 0;
42  meanSquaredError +=
43  spikeTrain1.Select((t, j) => Math.Pow(t.Time - spikeTrain2[j].Time, 2)).Sum();
44  meanSquaredError /= spikeTrain1.Count;
45  Assert.IsTrue(meanSquaredError < threshold,
46  "Output Spike Mean-Squared Error Between The Two Spike Trains is greater than " + threshold);
47  Console.WriteLine("Output Spike Mean-Squared Error Between The Two Spike Trains: ");
48  Console.WriteLine("\t" + meanSquaredError);
49  Console.WriteLine();
50  }
51  }
52 
61  public static void CompareNumericalAndEventDrivenSpikeTrains(ISpikingNeuron neuron, List<Spike> inputSpikeTimes, int maxNumOutputSpikes = 3)
62  {
63  inputSpikeTimes.RemoveAll(x => x.NeuronIndex >= neuron.NumInputs);
64 
65  // Display Neuron Properties
66  Console.WriteLine(neuron);
67 
68  // Simulate Theta Neuron Numerically
69  neuron.Method = SimulationMethod.Numerical;
70  var outputSpikeTimesNumerical = neuron.RunThetaNeuron(inputSpikeTimes, maxNumOutputSpikes).ToList();
71 
72  //Simulate Theta Neuron with Event-Driven Methodology
73  neuron.ResetState();
74  neuron.Method = SimulationMethod.EventDriven;
75  var outputSpikeTimesEventDriven = neuron.RunThetaNeuron(inputSpikeTimes, maxNumOutputSpikes).ToList();
76  CompareSpikeTrains(outputSpikeTimesNumerical, outputSpikeTimesEventDriven, 0.01);
77  }
78  }
79 }
static void CompareNumericalAndEventDrivenSpikeTrains(ISpikingNeuron neuron, List< Spike > inputSpikeTimes, int maxNumOutputSpikes=3)
For a single spiking neuron, compares the output spikes (up to maxNumOutputSpikes) produced by runnin...
Definition: TestHelper.cs:61
Set of helper methods for various spiking neuron network tests
Definition: TestHelper.cs:13
static void CompareSpikeTrains(IReadOnlyCollection< Spike > spikeTrain1, IReadOnlyList< Spike > spikeTrain2, double threshold)
Finds the mean squared error in spike times between two spike trains. Asserts that the MSE is within ...
Definition: TestHelper.cs:28