Пример #1
0
func TestMultipleConnectionSpikingNeuronNetwork(t *testing.T) {
	// Create a group of neurons with the default parameters.
	// Connect to a redis client.
	redisConnection, err := redis.Dial("tcp", ":6379")
	if err != nil {
		// Handle error if any...
		panic(err)
	}
	defer redisConnection.Close()

	network := make([]*sn.SpikingNeuron, numberOfSpikingNeurons)

	// Create a feed-forward network.
	for i := 0; i < numberOfSpikingNeurons; i++ {
		// Define the neuron.
		network[i] = sn.NewSpikingNeuron(defaultA, defaultB, defaultC, defaultD, int64(i), redisConnection)

		if i > 0 {
			// Create a connection from one neuron to the next.
			network[i-1].CreateConnection(network[i], defaultWeight, true, 0)
		}
	}

	// This is a hacky way to create an external connection...
	externalConnections := make([]*sn.SpikingNeuron, numberOfExternalConnections)
	for index, _ := range externalConnections {
		externalConnections[index] = sn.NewSpikingNeuron(0, 0, 0, 0, int64(-index), redisConnection)
		externalConnections[index].CreateConnection(network[0], 1.0, true, 0)
	}

	// Create a default simulation.
	simulation := sim.NewSimulation(defaultSteps, defaultTau, defaultStart, defaultStepRise)

	var testNetwork *net.Network

	for input := float64(0); input < maxInput+lazyIncrement; input = input + lazyIncrement {
		if input == 0 {
			input = 1
		}

		for _, externalSource := range externalConnections {
			for _, connection := range externalSource.GetConnections() {
				if connection.IsWriteable() {
					connection.SetOutput(input)
				}
			}
		}

		// Create a new network simulation of connections feeding from/to neurons.
		testNetwork = net.NewNetwork(network)
		testNetwork.Simulate(simulation)

		for plotIndex, neuron := range testNetwork.GetNeurons() {
			plotIndexString := alphabet[plotIndex]
			numberOfConnections := strconv.Itoa(len(neuron.GetConnections()))
			inputString := strconv.FormatFloat(input, 'f', 6, 64)
			title := "Phasic Spiking Neuron " + plotIndexString + " with connections " + numberOfConnections + " @ I = " + inputString
			xLabel := "Time Series"
			yLabel := "Membrane Potential"
			legendLabel := "Membrane Potential over Time"
			fileName := "plots/spiking-neuron-" + plotIndexString + "-" + inputString + "-with-" + numberOfConnections + "-connections.png"
			plots.GeneratePlot(simulation.GetTimeSeries(), neuron.GetOutputs(), title, xLabel, yLabel, legendLabel, fileName)

			neuron.SetSpikeRate(input, float64(neuron.GetSpikes())/defaultMeasureStart)
			neuron.ResetParameters(defaultA, defaultB, defaultC, defaultD)
		}

		if input == 1 {
			input = 0
		}
	}
}
Пример #2
0
// External Source -> Neuron A -> Neuron B -> Neuron C ... example.
func TestSingleConnectionSpikingNeuronNetwork(t *testing.T) {
	// Create a group of neurons with the default parameters.
	// Connect to a redis client.
	redisConnection, err := redis.Dial("tcp", ":6379")
	if err != nil {
		// Handle error if any...
		panic(err)
	}
	defer redisConnection.Close()

	network := make([]*sn.SpikingNeuron, numberOfSpikingNeurons)

	// Create a feed-forward network.
	for i := 0; i < numberOfSpikingNeurons; i++ {
		// Define the neuron.
		network[i] = sn.NewSpikingNeuron(defaultA, defaultB, defaultC, defaultD, int64(i), redisConnection)

		if i > 0 {
			// Create a connection from one neuron to the next.
			network[i-1].CreateConnection(network[i], defaultWeight, true, 0)
		}
	}

	// This is a hacky way to create an external connection...
	externalSource := sn.NewSpikingNeuron(0, 0, 0, 0, int64(-1), redisConnection)
	externalSource.CreateConnection(network[0], 1.0, true, 0)

	// Create a default simulation.
	simulation := sim.NewSimulation(defaultSteps, defaultTau, defaultStart, defaultStepRise)

	var testNetwork *net.Network

	for input := float64(0); input < maxInput+lazyIncrement; input = input + lazyIncrement {
		if input == 0 {
			input = 1
		}

		for _, connection := range externalSource.GetConnections() {
			if connection.IsWriteable() {
				connection.SetOutput(input)
			}
		}

		// Create a new network simulation of connections feeding from/to neurons.
		testNetwork = net.NewNetwork(network)
		testNetwork.Simulate(simulation)

		for plotIndex, neuron := range testNetwork.GetNeurons() {
			plotIndexString := alphabet[plotIndex]
			inputString := strconv.FormatFloat(input, 'f', 6, 64)
			title := "Phasic Spiking Neuron " + plotIndexString + " @ I = " + inputString
			xLabel := "Time Series"
			yLabel := "Membrane Potential"
			legendLabel := "Membrane Potential over Time"
			fileName := "plots/spiking-neuron-" + plotIndexString + "-" + inputString + ".png"
			plots.GeneratePlot(simulation.GetTimeSeries(), neuron.GetOutputs(), title, xLabel, yLabel, legendLabel, fileName)

			neuron.SetSpikeRate(input, float64(neuron.GetSpikes())/defaultMeasureStart)
			neuron.ResetParameters(defaultA, defaultB, defaultC, defaultD)
		}

		if input == 1 {
			input = 0
		}
	}

	// Measure and plot out mean spike rates.
	meansMap := make(map[int64][]float64)

	for _, neuron := range testNetwork.GetNeurons() {
		means := make([]float64, len(inputMeasurements))
		for i, selectedInput := range inputMeasurements {
			neuronSpikeRate := neuron.GetSpikeRate()
			means[i] = neuronSpikeRate[selectedInput]
		}
		meansMap[neuron.GetId()] = means
	}

	plotIndex := int(0)
	for _, value := range meansMap {
		plotIndexString := alphabet[plotIndex]
		title := "Mean Spike Rate of Phasic Spiking Neuron " + plotIndexString
		xLabel := "Input"
		yLabel := "Spike Rate"
		legendLabel := "Spike Rate as a Function of Input"
		fileName := "plots/spiking-neuron-network-mean-spike-rate-" + plotIndexString + ".png"
		plots.GeneratePlot(inputMeasurements, value, title, xLabel, yLabel, legendLabel, fileName)
		plotIndex++
	}
}