def testSerialization(self):
    """ test to ensure serialization preserves the state of the region
        correctly. """
    anomalyLikelihoodRegion1 = AnomalyLikelihoodRegion()
    inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
    outputs = AnomalyLikelihoodRegion.getSpec()['outputs']

    for _ in xrange(0, 6):
      inputs['rawAnomalyScore'] = numpy.array([random.random()])
      inputs['metricValue'] = numpy.array([random.random()])
      anomalyLikelihoodRegion1.compute(inputs, outputs)
      score1 = outputs['anomalyLikelihood'][0]

    proto1 = AnomalyLikelihoodRegionProto.new_message()
    anomalyLikelihoodRegion1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = AnomalyLikelihoodRegionProto.read(f)

    # # Load the deserialized proto

    anomalyLikelihoodRegion2 = AnomalyLikelihoodRegion.read(proto2)
    self.assertEqual(anomalyLikelihoodRegion1, anomalyLikelihoodRegion2)

    for _ in xrange(6, 500):
      inputs['rawAnomalyScore'] = numpy.array([random.random()])
      inputs['metricValue'] = numpy.array([random.random()])
      anomalyLikelihoodRegion1.compute(inputs, outputs)
      score1 = outputs['anomalyLikelihood'][0]
      anomalyLikelihoodRegion2.compute(inputs, outputs)
      score2 = outputs['anomalyLikelihood'][0]
      self.assertEqual(score1, score2)
 def testParamterError(self):
     """ ensure historicWindowSize is greater than estimationSamples """
     try:
         anomalyLikelihoodRegion = AnomalyLikelihoodRegion(
             estimationSamples=100, historicWindowSize=99)
         self.assertEqual(False, True, "Should have failed with ValueError")
     except ValueError:
         pass
  def testLikelihoodValues(self):
    """ test to see if the region keeps track of state correctly and produces
        the same likelihoods as the AnomalyLikelihood module """
    anomalyLikelihoodRegion = AnomalyLikelihoodRegion()
    anomalyLikelihood = AnomalyLikelihood()

    inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
    outputs = AnomalyLikelihoodRegion.getSpec()['outputs']
    with open (_INPUT_DATA_FILE) as f:
      reader = csv.reader(f)
      reader.next()
      for record in reader:
        consumption = float(record[1])
        anomalyScore = float(record[2])
        likelihood1 = anomalyLikelihood.anomalyProbability(
          consumption, anomalyScore)

        inputs['rawAnomalyScore'] = numpy.array([anomalyScore])
        inputs['metricValue'] = numpy.array([consumption])
        anomalyLikelihoodRegion.compute(inputs, outputs)
        likelihood2 = outputs['anomalyLikelihood'][0]

        self.assertEqual(likelihood1, likelihood2)
    def testLikelihoodValues(self):
        """ test to see if the region keeps track of state correctly and produces
        the same likelihoods as the AnomalyLikelihood module """
        anomalyLikelihoodRegion = AnomalyLikelihoodRegion()
        anomalyLikelihood = AnomalyLikelihood()

        inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
        outputs = AnomalyLikelihoodRegion.getSpec()['outputs']
        with open(_INPUT_DATA_FILE) as f:
            reader = csv.reader(f)
            reader.next()
            for record in reader:
                consumption = float(record[1])
                anomalyScore = float(record[2])
                likelihood1 = anomalyLikelihood.anomalyProbability(
                    consumption, anomalyScore)

                inputs['rawAnomalyScore'] = numpy.array([anomalyScore])
                inputs['metricValue'] = numpy.array([consumption])
                anomalyLikelihoodRegion.compute(inputs, outputs)
                likelihood2 = outputs['anomalyLikelihood'][0]

                self.assertEqual(likelihood1, likelihood2)
    def testSerialization(self):
        """ test to ensure serialization preserves the state of the region
        correctly. """
        anomalyLikelihoodRegion1 = AnomalyLikelihoodRegion()
        inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
        outputs = AnomalyLikelihoodRegion.getSpec()['outputs']
        parameters = AnomalyLikelihoodRegion.getSpec()['parameters']

        # Make sure to calculate distribution by passing the probation period
        learningPeriod = parameters['learningPeriod']['defaultValue']
        reestimationPeriod = parameters['reestimationPeriod']['defaultValue']
        probation = learningPeriod + reestimationPeriod
        for _ in xrange(0, probation + 1):
            inputs['rawAnomalyScore'] = numpy.array([random.random()])
            inputs['metricValue'] = numpy.array([random.random()])
            anomalyLikelihoodRegion1.compute(inputs, outputs)
            score1 = outputs['anomalyLikelihood'][0]

        proto1 = AnomalyLikelihoodRegionProto.new_message()
        anomalyLikelihoodRegion1.write(proto1)

        # Write the proto to a temp file and read it back into a new proto
        with tempfile.TemporaryFile() as f:
            proto1.write(f)
            f.seek(0)
            proto2 = AnomalyLikelihoodRegionProto.read(f)

        # # Load the deserialized proto

        anomalyLikelihoodRegion2 = AnomalyLikelihoodRegion.read(proto2)
        self.assertEqual(anomalyLikelihoodRegion1, anomalyLikelihoodRegion2)

        window = parameters['historicWindowSize']['defaultValue']
        for _ in xrange(0, window + 1):
            inputs['rawAnomalyScore'] = numpy.array([random.random()])
            inputs['metricValue'] = numpy.array([random.random()])
            anomalyLikelihoodRegion1.compute(inputs, outputs)
            score1 = outputs['anomalyLikelihood'][0]
            anomalyLikelihoodRegion2.compute(inputs, outputs)
            score2 = outputs['anomalyLikelihood'][0]
            self.assertEqual(score1, score2)
    def testSerialization(self):
        """ test to ensure serialization preserves the state of the region
        correctly. """
        anomalyLikelihoodRegion1 = AnomalyLikelihoodRegion()
        inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
        outputs = AnomalyLikelihoodRegion.getSpec()['outputs']

        for _ in range(0, 6):
            inputs['rawAnomalyScore'] = numpy.array([random.random()])
            inputs['metricValue'] = numpy.array([random.random()])
            anomalyLikelihoodRegion1.compute(inputs, outputs)
            score1 = outputs['anomalyLikelihood'][0]

        proto1 = AnomalyLikelihoodRegionProto.new_message()
        anomalyLikelihoodRegion1.write(proto1)

        # Write the proto to a temp file and read it back into a new proto
        with tempfile.TemporaryFile() as f:
            proto1.write(f)
            f.seek(0)
            proto2 = AnomalyLikelihoodRegionProto.read(f)

        # # Load the deserialized proto

        anomalyLikelihoodRegion2 = AnomalyLikelihoodRegion.read(proto2)
        self.assertEqual(anomalyLikelihoodRegion1, anomalyLikelihoodRegion2)

        for _ in range(6, 500):
            inputs['rawAnomalyScore'] = numpy.array([random.random()])
            inputs['metricValue'] = numpy.array([random.random()])
            anomalyLikelihoodRegion1.compute(inputs, outputs)
            score1 = outputs['anomalyLikelihood'][0]
            anomalyLikelihoodRegion2.compute(inputs, outputs)
            score2 = outputs['anomalyLikelihood'][0]
            self.assertEqual(score1, score2)
  def testSerialization(self):
    """ test to ensure serialization preserves the state of the region
        correctly. """
    anomalyLikelihoodRegion1 = AnomalyLikelihoodRegion()
    inputs = AnomalyLikelihoodRegion.getSpec()['inputs']
    outputs = AnomalyLikelihoodRegion.getSpec()['outputs']
    parameters = AnomalyLikelihoodRegion.getSpec()['parameters']

    # Make sure to calculate distribution by passing the probation period
    learningPeriod = parameters['learningPeriod']['defaultValue']
    reestimationPeriod = parameters['reestimationPeriod']['defaultValue']
    probation = learningPeriod + reestimationPeriod
    for _ in xrange(0, probation + 1):
      inputs['rawAnomalyScore'] = numpy.array([random.random()])
      inputs['metricValue'] = numpy.array([random.random()])
      anomalyLikelihoodRegion1.compute(inputs, outputs)
      score1 = outputs['anomalyLikelihood'][0]

    proto1 = AnomalyLikelihoodRegionProto.new_message()
    anomalyLikelihoodRegion1.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = AnomalyLikelihoodRegionProto.read(f)

    # # Load the deserialized proto

    anomalyLikelihoodRegion2 = AnomalyLikelihoodRegion.read(proto2)
    self.assertEqual(anomalyLikelihoodRegion1, anomalyLikelihoodRegion2)

    window = parameters['historicWindowSize']['defaultValue']
    for _ in xrange(0, window + 1):
      inputs['rawAnomalyScore'] = numpy.array([random.random()])
      inputs['metricValue'] = numpy.array([random.random()])
      anomalyLikelihoodRegion1.compute(inputs, outputs)
      score1 = outputs['anomalyLikelihood'][0]
      anomalyLikelihoodRegion2.compute(inputs, outputs)
      score2 = outputs['anomalyLikelihood'][0]
      self.assertEqual(score1, score2)