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
SpikingNeuronNetworkRunningTests.cs
Go to the documentation of this file.
1 namespace SpikingNeuronNetwork.Test
2 {
3  using Lib;
4  using Lib.NeuronModels;
5  using Microsoft.VisualStudio.TestTools.UnitTesting;
6  using System;
7  using System.Collections.Generic;
8  using System.Globalization;
9  using System.Linq;
10 
14  [TestClass]
16  {
20  [TestMethod]
22  {
23  const int numInputs = 3;
24  var numNeuronsPerLayer = new[] { 1, 2 };
25  var network = new SpikingNeuronNetwork(numInputs, numNeuronsPerLayer, typeof(ThetaNeuron), 1);
26  Assert.IsTrue(network.IsLayered);
27  Assert.IsTrue(new List<int> { 3 }.SequenceEqual(network.GetNeuronIndicesByLayer(0)));
28  Assert.IsTrue(new List<int> { 4, 5 }.SequenceEqual(network.GetNeuronIndicesByLayer(1)));
29  network = new SpikingNeuronNetwork(numInputs, numNeuronsPerLayer.Sum(), typeof(ThetaNeuron), 1);
30  Assert.IsFalse(network.IsLayered);
31  }
32 
37  [TestMethod]
39  {
40  const int numInputs = 1;
41  const int numNeurons = 1;
42  const double weightIni = 0.15;
43  const double baselineCurrent = 0.001;
44  var inputSpikeTimes = new List<Spike>
45  {
46  new Spike { Time = 1, NeuronIndex = 0},
47  };
48 
49  SpikeTrainTestHelper(numInputs, numNeurons, weightIni, baselineCurrent, inputSpikeTimes);
50  }
51 
56  [TestMethod]
58  {
59  const int numInputs = 2;
60  const int numNeurons = 1;
61  const double weightIni = 0.1;
62  const double baselineCurrent = 0.001;
63  var inputSpikeTimes = new List<Spike>
64  {
65  new Spike { Time = 2, NeuronIndex = 0},
66  new Spike { Time = 1, NeuronIndex = 1},
67  };
68 
69  SpikeTrainTestHelper(numInputs, numNeurons, weightIni, baselineCurrent, inputSpikeTimes);
70  }
71 
76  [TestMethod]
78  {
79  const int numInputs = 3;
80  const int numNeurons = 1;
81  const double weightIni = 0.01;
82  const double baselineCurrent = 0.001;
83  var inputSpikeTimes = new List<Spike>
84  {
85  new Spike { Time = 2, NeuronIndex = 0},
86  new Spike { Time = 1, NeuronIndex = 1},
87  new Spike { Time = 2.1, NeuronIndex = 2}
88  };
89 
90  SpikeTrainTestHelper(numInputs, numNeurons, weightIni, baselineCurrent, inputSpikeTimes);
91  }
92 
97  [TestMethod]
99  {
100  const int numInputs = 1;
101  const int numNeurons = 1;
102  const double weightIni = -0.15;
103  const double baselineCurrent = -0.001;
104  var inputSpikeTimes = new List<Spike>
105  {
106  new Spike { Time = 0.01, NeuronIndex = 0},
107  };
108 
109  SpikeTrainTestHelper(numInputs, numNeurons, weightIni, baselineCurrent, inputSpikeTimes);
110  }
111 
116  [TestMethod]
118  {
119  const int numInputs = 1;
120  const int numNeurons = 1;
121  const double weightIni = -0.15;
122  const double baselineCurrent = -0.001;
123  const int currentNeuron = numInputs + numNeurons - 1;
124  var inputSpikeTimes = new List<Spike>
125  {
126  new Spike { Time = 0.01, NeuronIndex = 0},
127  };
128 
129  var thetaNeuronNetwork = new SpikingNeuronNetwork(numInputs, numNeurons, typeof(ThetaNeuron), weightIni, new Dictionary<string, string> { {"Io", baselineCurrent.ToString(CultureInfo.InvariantCulture)}, {"Method", "EventDriven"} });
130  thetaNeuronNetwork.RunSpikingNeuronNetwork(inputSpikeTimes);
131  inputSpikeTimes.First().Time -= 0.005;
132  var neuronFiringHistories = thetaNeuronNetwork.RunSpikingNeuronNetwork(inputSpikeTimes);
133 
134  Assert.AreEqual(1, neuronFiringHistories.Count);
135  Assert.AreEqual(currentNeuron, neuronFiringHistories.First().Key);
136  Assert.AreEqual(-1, neuronFiringHistories.First().Value.StartTime);
137  Assert.AreEqual(-1, neuronFiringHistories.First().Value.EndTime);
138  Assert.AreEqual(0, neuronFiringHistories.First().Value.OutputSpikes.Count);
139  Assert.AreEqual(0, neuronFiringHistories.First().Value.SpikeStats.Count);
140  }
141 
145  [TestMethod]
146  [ExpectedException(typeof(ArgumentException))]
148  {
149  const int numInputs = 2;
150  const int numNeurons = 1;
151  const double weightIni = 0.1;
152  // ReSharper disable ObjectCreationAsStatement
153  new SpikingNeuronNetwork(numInputs, numNeurons, typeof(double), weightIni);
154  // ReSharper restore ObjectCreationAsStatement
155  }
156 
157  #region Helper Methods
158 
170  private static void SpikeTrainTestHelper(int numInputs, int numNeurons, double weightIni, double baselineCurrent, List<Spike> inputSpikeTimes)
171  {
172  var currentNeuron = numInputs + numNeurons - 1;
173  var thetaNeuronNetwork = new SpikingNeuronNetwork(numInputs, numNeurons, typeof(ThetaNeuron), weightIni, new Dictionary<string, string> { {"Io", baselineCurrent.ToString(CultureInfo.InvariantCulture)}, {"Method", "EventDriven"} });
174  var neuronFiringHistories = thetaNeuronNetwork.RunSpikingNeuronNetwork(inputSpikeTimes);
175 
176  // Compare Running With Stand-Alone Neuron, numerically
177  var outputSpikeTimesNetwork = neuronFiringHistories[currentNeuron].OutputSpikes;
178  var thetaNeuron = (ThetaNeuron)thetaNeuronNetwork.GetStandAloneNeuron(currentNeuron); //new ThetaNeuron(numInputs, weightIni) {Method = ThetaNeuron.SimulationMethod.Numerical};
179  thetaNeuron.Method = SimulationMethod.Numerical;
180  thetaNeuron.ResetState();
181  var outputSpikeTimesStandAlone = thetaNeuron.RunThetaNeuron(inputSpikeTimes).ToList();
182  TestHelper.CompareSpikeTrains(outputSpikeTimesNetwork, outputSpikeTimesStandAlone, 0.01);
183  }
184 
185  #endregion
186  }
187 }
void ThetaNeuronNetworkBasicRunAllInputSpikesComeBeforeNetworkTime()
Tests ability to run a Spiking Neuron Network containing a single neuron and one input that takes pla...
void ThetaNeuronNetworkBasicRunNoOutputSpikesTest()
Tests ability to run a Spiking Neuron Network containing a single neuron and one input which produces...
void ThetaNeuronNetworkLayeredTest()
Tests ability to setup a layered spiking neuron network
void ThetaNeuronNetworkBasicRunCompareOneInputTest()
Tests ability to run a Spiking Neuron Network containing a single neuron and one input ...
void ThetaNeuronNetworkBadNeuronTypeTest()
Tests that a spiking neuron network is not created with an invalid spiking neuron type ...
ISpikingNeuron GetStandAloneNeuron(int neuronIndex)
Gets the stand alone neuron.
void ThetaNeuronNetworkBasicRunCompareThreeInputsTest()
Tests ability to run a Spiking Neuron Network containing a single neuron and three inputs ...
void ThetaNeuronNetworkBasicRunCompareTwoInputsTest()
Tests ability to run a Spiking Neuron Network containing a single neuron and two inputs ...