From 0e530cdcebb9b40bf7f0360683868b185ca7bf23 Mon Sep 17 00:00:00 2001 From: David Shorten Date: Fri, 23 Jul 2021 17:26:18 +1000 Subject: [PATCH] history embeddings done --- ...erEntropyCalculatorSpikingIntegration.java | 95 ++++++++++++------- tester.py | 4 +- 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/java/source/infodynamics/measures/spiking/integration/TransferEntropyCalculatorSpikingIntegration.java b/java/source/infodynamics/measures/spiking/integration/TransferEntropyCalculatorSpikingIntegration.java index f5322a9..54c008d 100644 --- a/java/source/infodynamics/measures/spiking/integration/TransferEntropyCalculatorSpikingIntegration.java +++ b/java/source/infodynamics/measures/spiking/integration/TransferEntropyCalculatorSpikingIntegration.java @@ -72,10 +72,10 @@ public class TransferEntropyCalculatorSpikingIntegration implements * destination only */ - Vector targetEmbeddingsFromSpikes = null; - Vector sourceEmbeddingsFromSpikes = null; - Vector targetEmbeddingsFromSamples = null; - Vector sourceEmbeddingsFromSamples = null; + Vector targetEmbeddingsFromSpikes = null; + Vector sourceEmbeddingsFromSpikes = null; + Vector targetEmbeddingsFromSamples = null; + Vector sourceEmbeddingsFromSamples = null; Vector destPastAndNextTimings = null; @@ -298,10 +298,10 @@ public class TransferEntropyCalculatorSpikingIntegration implements numEventsPerObservationSet = new Vector(); // New - targetEmbeddingsFromSpikes = new Vector(); - sourceEmbeddingsFromSpikes = new Vector(); - targetEmbeddingsFromSamples = new Vector(); - sourceEmbeddingsFromSamples = new Vector(); + targetEmbeddingsFromSpikes = new Vector(); + sourceEmbeddingsFromSpikes = new Vector(); + targetEmbeddingsFromSamples = new Vector(); + sourceEmbeddingsFromSamples = new Vector(); // Send all of the observations through: Iterator sourceIterator = vectorOfSourceSpikeTimes.iterator(); @@ -316,6 +316,10 @@ public class TransferEntropyCalculatorSpikingIntegration implements targetEmbeddingsFromSpikes, sourceEmbeddingsFromSpikes, targetEmbeddingsFromSamples, sourceEmbeddingsFromSamples); } + + for (int i = 0; i < 5; i++) { + System.out.println(Arrays.toString(targetEmbeddingsFromSpikes.elementAt(i))); + } // Now we have collected all the events. // Load up the search structures: @@ -370,8 +374,10 @@ public class TransferEntropyCalculatorSpikingIntegration implements } protected void makeEmbeddingsAtPoints(double[] pointsAtWhichToMakeEmbeddings, double[] sourceSpikeTimes, double[] destSpikeTimes, - Vector targetEmbeddings, Vector sourceEmbeddings) { - System.out.println("foo"); + Vector targetEmbeddings, Vector sourceEmbeddings) { + //System.out.println("foo"); + + Random random = new Random(); int embedding_point_index = 0; int most_recent_dest_index = k; @@ -388,41 +394,58 @@ public class TransferEntropyCalculatorSpikingIntegration implements for (;embedding_point_index < pointsAtWhichToMakeEmbeddings.length; embedding_point_index++) { // Advance the tracker of the most recent dest index - while (most_recent_dest_index < destSpikeTimes.length) { - // Case where we will not be able to evaluate the following else if - if (most_recent_dest_index == destSpikeTimes.length - 2) { - // The final spike is still behind the current embedding point, so advance - if (destSpikeTimes[most_recent_dest_index + 1] < - pointsAtWhichToMakeEmbeddings[embedding_point_index]) { - most_recent_dest_index += 1; - } - break; - } else if (destSpikeTimes[most_recent_dest_index + 1] < pointsAtWhichToMakeEmbeddings[embedding_point_index]) { + while (most_recent_dest_index < (destSpikeTimes.length - 1)) { + if (destSpikeTimes[most_recent_dest_index + 1] < pointsAtWhichToMakeEmbeddings[embedding_point_index]) { most_recent_dest_index++; } else { break; } } // Do the same for the most recent source index - while (most_recent_source_index < sourceSpikeTimes.length) { - // Case where we will not be able to evaluate the following else if - if (most_recent_source_index == sourceSpikeTimes.length - 2) { - // The final spike is still behind the current embedding point, so advance - if (sourceSpikeTimes[most_recent_source_index + 1] < - pointsAtWhichToMakeEmbeddings[embedding_point_index]) { - most_recent_source_index += 1; - } - break; - } else if (sourceSpikeTimes[most_recent_source_index + 1] < pointsAtWhichToMakeEmbeddings[embedding_point_index]) { + while (most_recent_source_index < (sourceSpikeTimes.length - 1)) { + if (sourceSpikeTimes[most_recent_source_index + 1] < pointsAtWhichToMakeEmbeddings[embedding_point_index]) { most_recent_source_index++; } else { break; } } - System.out.println(most_recent_dest_index + " " + most_recent_source_index + " " + embedding_point_index); - System.out.println(destSpikeTimes[most_recent_dest_index] + " " + sourceSpikeTimes[most_recent_source_index] + " " + - pointsAtWhichToMakeEmbeddings[embedding_point_index]); + //System.out.println(most_recent_dest_index + " " + most_recent_source_index + " " + embedding_point_index); + //System.out.println(destSpikeTimes[most_recent_dest_index] + " " + sourceSpikeTimes[most_recent_source_index] + " " + + // pointsAtWhichToMakeEmbeddings[embedding_point_index]); + //System.out.println(k + " " + l); + + double[] destPast = new double[k]; + double[] sourcePast = new double[l]; + destPast[0] = pointsAtWhichToMakeEmbeddings[embedding_point_index] - + destSpikeTimes[most_recent_dest_index]; + sourcePast[0] = pointsAtWhichToMakeEmbeddings[embedding_point_index] - + sourceSpikeTimes[most_recent_source_index]; + if (addNoise) { + destPast[0] += random.nextGaussian()*noiseLevel; + sourcePast[0] += random.nextGaussian()*noiseLevel; + } + for (int i = 1; i < k; i++) { + destPast[i] = destSpikeTimes[most_recent_dest_index - i + 1] - + destSpikeTimes[most_recent_dest_index - i]; + if (addNoise) { + destPast[i - 1] += random.nextGaussian()*noiseLevel; + } + } + for (int i = 1; i < l; i++) { + sourcePast[i] = sourceSpikeTimes[most_recent_source_index - i + 1] - + sourceSpikeTimes[most_recent_source_index - i]; + if (addNoise) { + sourcePast[i - 1] += random.nextGaussian()*noiseLevel; + } + } + + //System.out.println(Arrays.toString(destPast)); + //System.out.println(Arrays.toString(sourcePast)); + //System.out.println(); + + targetEmbeddings.add(destPast); + sourceEmbeddings.add(sourcePast); } } @@ -430,8 +453,8 @@ public class TransferEntropyCalculatorSpikingIntegration implements int timeSeriesIndex, Vector[] eventTimings, Vector destPastAndNextTimings, Vector eventTypeLocator, Vector eventIndexLocator, Vector numEventsPerObservationSet, - Vector targetEmbeddingsFromSpikes, Vector sourceEmbeddingsFromSpikes, - Vector targetEmbeddingsFromSamples, Vector sourceEmbeddingsFromSamples) + Vector targetEmbeddingsFromSpikes, Vector sourceEmbeddingsFromSpikes, + Vector targetEmbeddingsFromSamples, Vector sourceEmbeddingsFromSamples) throws Exception { // addObservationsAfterParamsDetermined(sourceSpikeTimes, destSpikeTimes); @@ -455,6 +478,8 @@ public class TransferEntropyCalculatorSpikingIntegration implements makeEmbeddingsAtPoints(destSpikeTimes, sourceSpikeTimes, destSpikeTimes, targetEmbeddingsFromSpikes, sourceEmbeddingsFromSpikes); makeEmbeddingsAtPoints(randomSampleTimes, sourceSpikeTimes, destSpikeTimes, targetEmbeddingsFromSamples, sourceEmbeddingsFromSamples); + + // Scan to find the indices by which we have k and l spikes for dest and source // respectively diff --git a/tester.py b/tester.py index 2ce6d6c..6c9f084 100755 --- a/tester.py +++ b/tester.py @@ -58,8 +58,8 @@ teCalcClass = JPackage("infodynamics.measures.spiking.integration").TransferEntr teCalc = teCalcClass() teCalc.setProperty("NORMALISE", "true") # Normalise the individual variables teCalc.initialise(1) # Use history length 1 (Schreiber k=1) -teCalc.setProperty("k", "2") -teCalc.setProperty("l", "2") +teCalc.setProperty("k_HISTORY", "2") +teCalc.setProperty("l_HISTORY", "2") teCalc.setProperty("knns", "4") # Use Kraskov parameter K=4 for 4 nearest points # # Perform calculation with correlated source: teCalc.setObservations(JArray(JDouble, 1)(sourceArray), JArray(JDouble, 1)(destArray))