Catching OutOfMemoryError in Discrete calculator creation for all other discrete calculators (only MI was done in the previous commit). Also simplified constructor calls (with code re-use) for conditional TE.

This commit is contained in:
jlizier 2018-08-25 22:27:29 +10:00
parent 93c767999e
commit dc08e1c372
11 changed files with 115 additions and 66 deletions

View File

@ -106,7 +106,14 @@ public class BlockEntropyCalculatorDiscrete extends EntropyCalculatorDiscrete {
// Create storage for counts of observations.
// We're recreating stateCount, which was created in super()
// however the super() didn't create it for blocksize > 1
stateCount = new int[MathsUtils.power(base, blocksize)];
try {
stateCount = new int[MathsUtils.power(base, blocksize)];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a Runtimexception
throw new RuntimeException("Requested memory for the base " +
base + " and k=" + blocksize+ " is too large for the JVM at this time", e);
}
// Create constants for tracking stateValues
maxShiftedValue = new int[base];

View File

@ -100,9 +100,16 @@ public class CombinedActiveEntRateCalculatorDiscrete {
if (changedSizes) {
// Create storage for counts of observations
jointCount = new int[base][MathsUtils.power(base, history)];
prevCount = new int[MathsUtils.power(base, history)];
nextCount = new int[base];
try {
jointCount = new int[base][MathsUtils.power(base, history)];
prevCount = new int[MathsUtils.power(base, history)];
nextCount = new int[base];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a Runtimexception
throw new RuntimeException("Requested memory for the base " +
base + " and k=" + history + " is too large for the JVM at this time", e);
}
} else {
// Just set counts to zeros without recreating the space
MatrixUtils.fill(jointCount, 0);

View File

@ -117,10 +117,18 @@ public class ConditionalMutualInformationCalculatorDiscrete
this.condBase = condBase;
// Create storage for extra counts of observations
firstSecondCondCount = new int[base1][base2][condBase];
firstCondCount = new int[base1][condBase];
secondCondCount = new int[base2][condBase];
condCount = new int[condBase];
try {
firstSecondCondCount = new int[base1][base2][condBase];
firstCondCount = new int[base1][condBase];
secondCondCount = new int[base2][condBase];
condCount = new int[condBase];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as an Exception
throw new RuntimeException("Requested memory for the MI bases (" +
base1 + ", " + base2 + ", " + condBase +
") is too large for the JVM at this time", e);
}
}
@Override

View File

@ -185,6 +185,15 @@ public class ConditionalTransferEntropyCalculatorDiscrete
base_power_k = MathsUtils.power(base, k);
base_power_num_others = MathsUtils.power(base_others, numOtherInfoContributors);
// Relaxing this assumption so we can use this calculation as
// a time-lagged conditional MI at will:
//if (k < 1) {
// throw new RuntimeException("History k " + history + " is not >= 1 a ContextOfPastMeasureCalculator");
//}
// Which time step do we start taking observations from?
// Normally this is k (to allow k previous time steps)
// but if k==0 (becoming a lagged MI), it's 1.
startObservationTime = Math.max(k, 1);
// check that we can convert the base tuple into an integer ok
@ -196,10 +205,18 @@ public class ConditionalTransferEntropyCalculatorDiscrete
}
// Create storage for counts of observations
sourceDestPastOthersCount = new int[base][base][base_power_k][base_power_num_others];
sourcePastOthersCount = new int[base][base_power_k][base_power_num_others];
destPastOthersCount = new int [base][base_power_k][base_power_num_others];
pastOthersCount = new int[base_power_k][base_power_num_others];
try {
sourceDestPastOthersCount = new int[base][base][base_power_k][base_power_num_others];
sourcePastOthersCount = new int[base][base_power_k][base_power_num_others];
destPastOthersCount = new int [base][base_power_k][base_power_num_others];
pastOthersCount = new int[base_power_k][base_power_num_others];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base " +
base + ", k=" + k + " num of others " + numOtherInfoContributors +
" with base " + base_others + ") is too large for the JVM at this time", e);
}
// Create constants for tracking prevValues
maxShiftedValue = new int[base];
@ -223,44 +240,7 @@ public class ConditionalTransferEntropyCalculatorDiscrete
public ConditionalTransferEntropyCalculatorDiscrete
(int base, int history, int numOtherInfoContributors) {
super(base);
k = history;
base_others = base; // If unspecified, the base of the conditional variables is the same as src and tgt
this.numOtherInfoContributors = numOtherInfoContributors;
base_power_k = MathsUtils.power(base, k);
base_power_num_others = MathsUtils.power(base, numOtherInfoContributors);
// Relaxing this assumption so we can use this calculation as
// a time-lagged conditional MI at will:
//if (k < 1) {
// throw new RuntimeException("History k " + history + " is not >= 1 a ContextOfPastMeasureCalculator");
//}
// Which time step do we start taking observations from?
// Normally this is k (to allow k previous time steps)
// but if k==0 (becoming a lagged MI), it's 1.
startObservationTime = Math.max(k, 1);
// check that we can convert the base tuple into an integer ok
if (k > Math.log(Integer.MAX_VALUE) / log_base) {
throw new RuntimeException("Base and history combination too large");
}
if (numOtherInfoContributors < 1) {
throw new RuntimeException("Number of other info contributors < 1 for CompleteTECalculator");
}
// Create storage for counts of observations
sourceDestPastOthersCount = new int[base][base][base_power_k][base_power_num_others];
sourcePastOthersCount = new int[base][base_power_k][base_power_num_others];
destPastOthersCount = new int [base][base_power_k][base_power_num_others];
pastOthersCount = new int[base_power_k][base_power_num_others];
// Create constants for tracking prevValues
maxShiftedValue = new int[base];
for (int v = 0; v < base; v++) {
maxShiftedValue[v] = v * MathsUtils.power(base, k-1);
}
this(base, history, numOtherInfoContributors, base);
}
@Override

View File

@ -114,9 +114,16 @@ public abstract class ContextOfPastMeasureCalculatorDiscrete extends
noObservationStorage = dontCreateObsStorage;
if (!dontCreateObsStorage) {
// Create storage for counts of observations
nextPastCount = new int[base][base_power_k];
pastCount = new int[base_power_k];
nextCount = new int[base];
try {
nextPastCount = new int[base][base_power_k];
pastCount = new int[base_power_k];
nextCount = new int[base];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base " +
base + " and k=" + k + " is too large for the JVM at this time", e);
}
}
}

View File

@ -94,7 +94,15 @@ public class EntropyCalculatorDiscrete extends InfoMeasureCalculatorDiscrete
super(base);
// Create storage for counts of observations
stateCount = new int[base];
try {
stateCount = new int[base];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base (" +
base + ") is too large for the JVM at this time", e);
}
}
@Override

View File

@ -80,8 +80,16 @@ public class MultiInformationCalculatorDiscrete extends InfoMeasureCalculatorDis
super(base);
this.numVars = numVars;
jointStates = MathsUtils.power(base, numVars);
jointCount = new int[jointStates];
marginalCounts = new int[numVars][base];
try {
jointCount = new int[jointStates];
marginalCounts = new int[numVars][base];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base " +
base + " with " + numVars +
" variables is too large for the JVM at this time", e);
}
}
@Override

View File

@ -123,8 +123,8 @@ public class MutualInformationCalculatorDiscrete extends InfoMeasureCalculatorDi
jCount = new int[base2];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as an Exception
throw new Exception("Requested memory for the MI bases (" +
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the MI bases (" +
base1 + ", " + base2 + ") is too large for the JVM at this time", e);
}
}

View File

@ -135,9 +135,17 @@ public class PredictiveInformationCalculatorDiscrete {
}
// Create storage for counts of observations
jointCount = new int[base_power_k][base_power_k];
prevCount = new int[base_power_k];
nextCount = new int[base_power_k];
try {
jointCount = new int[base_power_k][base_power_k];
prevCount = new int[base_power_k];
nextCount = new int[base_power_k];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base " +
numDiscreteValues + " with k=" + blockLength +
") is too large for the JVM at this time", e);
}
// Create constants for tracking prevValues and nextValues
maxShiftedValue = new int[numDiscreteValues];

View File

@ -179,9 +179,17 @@ public class SeparableInfoCalculatorDiscrete extends ContextOfPastMeasureCalcula
}
if (!dontCreateObsStorage) {
// Create storage for extra counts of observations
sourceNumValueNextPastCount = new int[numInfoContributors][base][base][base_power_k];
sourcesNextPastCount = new int[base_power_sources][base][base_power_k];
sourceNumValuePastCount = new int[numInfoContributors][base][base_power_k];
try {
sourceNumValueNextPastCount = new int[numInfoContributors][base][base][base_power_k];
sourcesNextPastCount = new int[base_power_sources][base][base_power_k];
sourceNumValuePastCount = new int[numInfoContributors][base][base_power_k];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base " +
base + " with k=" + k + " and " + numInfoContributors +
" sources is too large for the JVM at this time", e);
}
}
}

View File

@ -244,8 +244,16 @@ public class TransferEntropyCalculatorDiscrete extends ContextOfPastMeasureCalcu
}
// Create storage for extra counts of observations
sourceNextPastCount = new int[base_power_l][base][base_power_k];
sourcePastCount = new int[base_power_l][base_power_k];
try {
sourceNextPastCount = new int[base_power_l][base][base_power_k];
sourcePastCount = new int[base_power_l][base_power_k];
} catch (OutOfMemoryError e) {
// Allow any Exceptions to be thrown, but catch and wrap
// Error as a RuntimeException
throw new RuntimeException("Requested memory for the base " +
base + ", k=" + k + ", l=" + sourceHistoryEmbedLength +
" is too large for the JVM at this time", e);
}
// Which time step do we start taking observations from?
// These two integers represent the earliest next time step, in the cases where the destination