Program: 4475
Project: 4475
Buggy source:
/** 
 * Process one input sample. This method is called by outer loop code outside the nupic-engine. We  use this instead of the nupic engine compute() because our inputs and  outputs aren't fixed size vectors of reals. <p>
 * @param recordNum <p>Record number of this input pattern. Record numbers normally increase sequentially by 1 each time unless there are missing records in the dataset. Knowing this information ensures that we don't get confused by missing records.
 * @param classification <p>{@link Map} of the classification information:<p>&emsp;"bucketIdx" - index of the encoder bucket <p>&emsp;"actValue" -  actual value doing into the encoder
 * @param patternNZ <p>List of the active indices from the output below. When the output is from the TemporalMemory, this array should be the indices of the active cells.
 * @param learn <p>If true, learn this sample.
 * @param infer <p>If true, perform inference.
 * @return {@link Classification} containing inference results. The Classificationcontains the computed probability distribution (relative likelihood for each bucketIdx starting from bucketIdx 0) for each step in  {@code steps}. Each bucket's likelihood can be accessed individually, or all the buckets' likelihoods can be obtained in the form of a double array. <pre>  {@code //Get likelihood val for bucket 0, 5 steps in future classification.getStat(5, 0); //Get all buckets' likelihoods as double[] where each //index is the likelihood for that bucket //(e.g. [0] contains likelihood for bucketIdx 0) classification.getStats(5);}</pre> The Classification also contains the average actual value for each bucket. The average values for the buckets can be accessed individually, or altogether as a double[]. <pre>  {@code //Get average actual val for bucket 0 classification.getActualValue(0); //Get average vals for all buckets as double[], where //each index is the average val for that bucket //(e.g. [0] contains average val for bucketIdx 0) classification.getActualValues();}</pre> The Classification can also be queried for the most probable bucket (the bucket with the highest associated likelihood value), as well as the average input value that corresponds to that bucket. <pre>  {@code //Get index of most probable bucket classification.getMostProbableBucketIndex(); //Get the average actual val for that bucket classification.getMostProbableValue();}</pre>
 */
@SuppressWarnings("String_Node_Str") public <T>Classification<T> compute(int recordNum,Map<String,Object> classification,int[] patternNZ,boolean learn,boolean infer){
  Classification<T> retVal=new Classification<T>();
  List<T> actualValues=(List<T>)this.actualValues;
  if (recordNumMinusLearnIteration == -1)   recordNumMinusLearnIteration=recordNum - learnIteration;
  learnIteration=recordNum - recordNumMinusLearnIteration;
  if (verbosity >= 1) {
    System.out.println(String.format("String_Node_Str",g_debugPrefix));
    System.out.printf("String_Node_Str",recordNum);
    System.out.printf("String_Node_Str",learnIteration);
    System.out.printf("String_Node_Str",patternNZ.length,patternNZ);
    System.out.println("String_Node_Str" + classification);
  }
  patternNZHistory.append(new Tuple(learnIteration,patternNZ));
  if (ArrayUtils.max(patternNZ) > maxInputIdx) {
    int newMaxInputIdx=Math.max(ArrayUtils.max(patternNZ),maxBucketIdx);
    for (    int nSteps : steps.toArray()) {
      for (int i=maxBucketIdx; i < ArrayUtils.max(patternNZ); i++) {
        weightMatrix.get(nSteps).addCol(new double[maxBucketIdx + 1]);
      }
    }
    maxInputIdx=newMaxInputIdx;
  }
  if (infer) {
    retVal=infer(patternNZ,classification);
  }
  if (learn && classification.get("String_Node_Str") != null) {
    int bucketIdx=(int)classification.get("String_Node_Str");
    Object actValue=classification.get("String_Node_Str");
    if (bucketIdx > maxBucketIdx) {
      for (      int nSteps : steps.toArray()) {
        for (int i=maxBucketIdx; i < bucketIdx; i++) {
          weightMatrix.get(nSteps).addRow(new double[maxBucketIdx + 1]);
        }
      }
      maxBucketIdx=bucketIdx;
    }
    while (maxBucketIdx > actualValues.size() - 1) {
      actualValues.add(null);
    }
    if (actualValues.get(bucketIdx) == null) {
      actualValues.set(bucketIdx,(T)actValue);
    }
 else {
      if (Number.class.isAssignableFrom(actValue.getClass())) {
        Double val=((1.0 - actValueAlpha) * ((Number)actualValues.get(bucketIdx)).doubleValue() + actValueAlpha * ((Number)actValue).doubleValue());
        actualValues.set(bucketIdx,(T)val);
      }
 else {
        actualValues.set(bucketIdx,(T)actValue);
      }
    }
    int iteration=0;
    int[] learnPatternNZ=null;
    for (    Tuple t : patternNZHistory) {
      iteration=(int)t.get(0);
      learnPatternNZ=(int[])t.get(1);
      Map<Integer,double[]> error=calculateError(classification);
      int nSteps=learnIteration - iteration;
      if (steps.contains(nSteps)) {
        for (int row=0; row <= maxBucketIdx; row++) {
          for (          int bit : learnPatternNZ) {
            weightMatrix.get(nSteps).add(row,bit,alpha * error.get(nSteps)[row]);
          }
        }
      }
    }
  }
  if (infer && verbosity >= 1) {
    System.out.println("String_Node_Str");
    System.out.println("String_Node_Str" + Arrays.toString((T[])retVal.getActualValues()));
    for (    int key : retVal.stepSet()) {
      if (retVal.getActualValue(key) == null)       continue;
      Object[] actual=new Object[]{(T)retVal.getActualValue(key)};
      System.out.println(String.format("String_Node_Str",key,pFormatArray(actual)));
      int bestBucketIdx=retVal.getMostProbableBucketIndex(key);
      System.out.println(String.format("String_Node_Str",bestBucketIdx,retVal.getActualValue(bestBucketIdx)));
    }
  }
  return retVal;
}

Fixed version (ground truth):
/** 
 * Process one input sample. This method is called by outer loop code outside the nupic-engine. We  use this instead of the nupic engine compute() because our inputs and  outputs aren't fixed size vectors of reals. <p>
 * @param recordNum <p>Record number of this input pattern. Record numbers normally increase sequentially by 1 each time unless there are missing records in the dataset. Knowing this information ensures that we don't get confused by missing records.
 * @param classification <p>{@link Map} of the classification information:<p>&emsp;"bucketIdx" - index of the encoder bucket <p>&emsp;"actValue" -  actual value doing into the encoder
 * @param patternNZ <p>List of the active indices from the output below. When the output is from the TemporalMemory, this array should be the indices of the active cells.
 * @param learn <p>If true, learn this sample.
 * @param infer <p>If true, perform inference. If false, null will be returned.
 * @return {@link Classification} containing inference results if {@code learn} param is true,otherwise, will return  {@code null}. The Classification contains the computed probability distribution (relative likelihood for each bucketIdx starting from bucketIdx 0) for each step in   {@code steps}. Each bucket's likelihood can be accessed individually, or all the buckets' likelihoods can be obtained in the form of a double array. <pre>  {@code //Get likelihood val for bucket 0, 5 steps in future classification.getStat(5, 0); //Get all buckets' likelihoods as double[] where each //index is the likelihood for that bucket //(e.g. [0] contains likelihood for bucketIdx 0) classification.getStats(5);}</pre> The Classification also contains the average actual value for each bucket. The average values for the buckets can be accessed individually, or altogether as a double[]. <pre>  {@code //Get average actual val for bucket 0 classification.getActualValue(0); //Get average vals for all buckets as double[], where //each index is the average val for that bucket //(e.g. [0] contains average val for bucketIdx 0) classification.getActualValues();}</pre> The Classification can also be queried for the most probable bucket (the bucket with the highest associated likelihood value), as well as the average input value that corresponds to that bucket. <pre>  {@code //Get index of most probable bucket classification.getMostProbableBucketIndex(); //Get the average actual val for that bucket classification.getMostProbableValue();}</pre>
 */
@SuppressWarnings("String_Node_Str") public <T>Classification<T> compute(int recordNum,Map<String,Object> classification,int[] patternNZ,boolean learn,boolean infer){
  Classification<T> retVal=null;
  List<T> actualValues=(List<T>)this.actualValues;
  if (recordNumMinusLearnIteration == -1)   recordNumMinusLearnIteration=recordNum - learnIteration;
  learnIteration=recordNum - recordNumMinusLearnIteration;
  if (verbosity >= 1) {
    System.out.println(String.format("String_Node_Str",g_debugPrefix));
    System.out.printf("String_Node_Str",recordNum);
    System.out.printf("String_Node_Str",learnIteration);
    System.out.printf("String_Node_Str",patternNZ.length,ArrayUtils.intArrayToString(patternNZ));
    System.out.println("String_Node_Str" + classification);
  }
  patternNZHistory.append(new Tuple(learnIteration,patternNZ));
  if (ArrayUtils.max(patternNZ) > maxInputIdx) {
    int newMaxInputIdx=ArrayUtils.max(patternNZ);
    for (    int nSteps : steps.toArray()) {
      for (int i=maxInputIdx; i < newMaxInputIdx; i++) {
        weightMatrix.get(nSteps).addCol(new double[maxBucketIdx + 1]);
      }
    }
    maxInputIdx=newMaxInputIdx;
  }
  if (infer) {
    retVal=infer(patternNZ,classification);
  }
  if (learn && classification.get("String_Node_Str") != null) {
    int bucketIdx=(int)classification.get("String_Node_Str");
    Object actValue=classification.get("String_Node_Str");
    if (bucketIdx > maxBucketIdx) {
      for (      int nSteps : steps.toArray()) {
        for (int i=maxBucketIdx; i < bucketIdx; i++) {
          weightMatrix.get(nSteps).addRow(new double[maxInputIdx + 1]);
        }
      }
      maxBucketIdx=bucketIdx;
    }
    while (maxBucketIdx > actualValues.size() - 1) {
      actualValues.add(null);
    }
    if (actualValues.get(bucketIdx) == null) {
      actualValues.set(bucketIdx,(T)actValue);
    }
 else {
      if (Number.class.isAssignableFrom(actValue.getClass())) {
        Double val=((1.0 - actValueAlpha) * ((Number)actualValues.get(bucketIdx)).doubleValue() + actValueAlpha * ((Number)actValue).doubleValue());
        actualValues.set(bucketIdx,(T)val);
      }
 else {
        actualValues.set(bucketIdx,(T)actValue);
      }
    }
    int iteration=0;
    int[] learnPatternNZ=null;
    for (    Tuple t : patternNZHistory) {
      iteration=(int)t.get(0);
      learnPatternNZ=(int[])t.get(1);
      Map<Integer,double[]> error=calculateError(classification);
      int nSteps=learnIteration - iteration;
      if (steps.contains(nSteps)) {
        for (int row=0; row <= maxBucketIdx; row++) {
          for (          int bit : learnPatternNZ) {
            weightMatrix.get(nSteps).add(row,bit,alpha * error.get(nSteps)[row]);
          }
        }
      }
    }
  }
  if (infer && verbosity >= 1) {
    System.out.println("String_Node_Str");
    System.out.println("String_Node_Str" + Arrays.toString((T[])retVal.getActualValues()));
    for (    int key : retVal.stepSet()) {
      if (retVal.getActualValue(key) == null)       continue;
      Object[] actual=new Object[]{(T)retVal.getActualValue(key)};
      System.out.println(String.format("String_Node_Str",key,pFormatArray(actual)));
      int bestBucketIdx=retVal.getMostProbableBucketIndex(key);
      System.out.println(String.format("String_Node_Str",bestBucketIdx,retVal.getActualValue(bestBucketIdx)));
    }
  }
  return retVal;
}