Program: 4491
Project: 4491
Buggy source:
/** 
 * <p> Complex test that tests the Anomaly function automatically being setup and working. To test this, we do the following: </p><p> <ol> <li>Reset the warm up state vars</li> <li>Warm up prediction inferencing - make sure predictions are trained</li> <li>Throw in an anomalous record</li> <li>Test to make sure the Layer detects the anomaly, and that it is significantly registered</li> </ol> </p>
 */
@Test public void testAnomalySetup(){
  TOTAL=0;
  resetWarmUp();
  final int PRIME_COUNT=35;
  final int NUM_CYCLES=10;
  final int INPUT_GROUP_COUNT=7;
  Parameters p=NetworkTestHarness.getParameters();
  p=p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
  p.setParameterByKey(KEY.RANDOM,new MersenneTwister(42));
  p.setParameterByKey(KEY.SP_PRIMER_DELAY,PRIME_COUNT);
  MultiEncoder me=MultiEncoder.builder().name("String_Node_Str").build();
  Map<String,Object> params=new HashMap<>();
  params.put(KEY_MODE,Mode.PURE);
  params.put(KEY_WINDOW_SIZE,3);
  params.put(KEY_USE_MOVING_AVG,true);
  Anomaly anomalyComputer=Anomaly.create(params);
  final Layer<Map<String,Object>> l=new Layer<>(p,me,new SpatialPooler(),new TemporalMemory(),Boolean.TRUE,anomalyComputer);
  l.subscribe(new Observer<Inference>(){
    @Override public void onCompleted(){
    }
    @Override public void onError(    Throwable e){
      System.out.println("String_Node_Str" + e.getMessage());
      e.printStackTrace();
    }
    @Override public void onNext(    Inference i){
      TOTAL++;
      assertNotNull(i);
      if (testingAnomaly) {
        if (i.getAnomalyScore() > highestAnomaly)         highestAnomaly=i.getAnomalyScore();
      }
    }
  }
);
  Map<String,Object> multiInput=new HashMap<>();
  boolean isWarmedUp=false;
  while (l.getInference() == null || !isWarmedUp) {
    for (double j=0; j < INPUT_GROUP_COUNT; j++) {
      multiInput.put("String_Node_Str",j);
      l.compute(multiInput);
      if (l.getInference() != null && isWarmedUp(l,l.getInference().getAnomalyScore())) {
        isWarmedUp=true;
      }
    }
  }
  boolean exit=false;
  for (int i=0; !exit && i < NUM_CYCLES; i++) {
    for (double j=0; j < INPUT_GROUP_COUNT; j++) {
      if (i == 2) {
        testingAnomaly=true;
        multiInput.put("String_Node_Str",j+=0.5);
        l.compute(multiInput);
        exit=true;
      }
 else {
        multiInput.put("String_Node_Str",j);
        l.compute(multiInput);
      }
    }
  }
  assertTrue(highestAnomaly > 0.2);
}

Fixed version (ground truth):
/** 
 * <p> Complex test that tests the Anomaly function automatically being setup and working. To test this, we do the following: </p><p> <ol> <li>Reset the warm up state vars</li> <li>Warm up prediction inferencing - make sure predictions are trained</li> <li>Throw in an anomalous record</li> <li>Test to make sure the Layer detects the anomaly, and that it is significantly registered</li> </ol> </p>
 */
@Test public void testAnomalySetup(){
  TOTAL=0;
  resetWarmUp();
  final int PRIME_COUNT=35;
  final int NUM_CYCLES=10;
  final int INPUT_GROUP_COUNT=7;
  Parameters p=NetworkTestHarness.getParameters();
  p=p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
  p.setParameterByKey(KEY.RANDOM,new MersenneTwister(42));
  p.setParameterByKey(KEY.SP_PRIMER_DELAY,PRIME_COUNT);
  MultiEncoder me=MultiEncoder.builder().name("String_Node_Str").build();
  Map<String,Object> params=new HashMap<>();
  params.put(KEY_MODE,Mode.PURE);
  params.put(KEY_WINDOW_SIZE,3);
  params.put(KEY_USE_MOVING_AVG,true);
  Anomaly anomalyComputer=Anomaly.create(params);
  final Layer<Map<String,Object>> l=new Layer<>(p,me,new SpatialPooler(),new TemporalMemory(),Boolean.TRUE,anomalyComputer);
  l.subscribe(new Observer<Inference>(){
    @Override public void onCompleted(){
    }
    @Override public void onError(    Throwable e){
      System.out.println("String_Node_Str" + e.getMessage());
      e.printStackTrace();
    }
    @Override public void onNext(    Inference i){
      TOTAL++;
      assertNotNull(i);
      if (testingAnomaly) {
        if (i.getAnomalyScore() > highestAnomaly)         highestAnomaly=i.getAnomalyScore();
      }
    }
  }
);
  Map<String,Object> multiInput=new HashMap<>();
  boolean isWarmedUp=false;
  while (l.getInference() == null || !isWarmedUp) {
    for (double j=0; j < INPUT_GROUP_COUNT; j++) {
      multiInput.put("String_Node_Str",j);
      l.compute(multiInput);
      if (l.getInference() != null && isWarmedUp(l,l.getInference().getAnomalyScore())) {
        isWarmedUp=true;
      }
    }
  }
  boolean exit=false;
  for (int i=0; !exit && i < NUM_CYCLES; i++) {
    for (double j=0; j < INPUT_GROUP_COUNT; j++) {
      if (i == 2) {
        testingAnomaly=true;
        multiInput.put("String_Node_Str",j+=0.5);
        l.compute(multiInput);
        exit=true;
      }
 else {
        multiInput.put("String_Node_Str",j);
        l.compute(multiInput);
      }
    }
  }
  System.out.println("String_Node_Str" + highestAnomaly);
  assertTrue(highestAnomaly > 0.2);
}