def _test_deterministic(self, forest, fixtureName):
        utilityAdapter = UtilityAdapter()
        accVec = AccelerationVector3D(self.data[fixtureName])
        prev_confidences = None
        for i in xrange(0, 100):
            confidences = forest.classifySignal(accVec.readings)
            if prev_confidences is not None:
                self.assertEqual(confidences, prev_confidences)

            prev_confidences = confidences
    def test_opencv_classify_vs_features(self):
        accVec = AccelerationVector3D(self.data['androidAccelerations'])

        opencvForest = OpenCVRandomForest(self.configFilename,
                                          self.modelFilename)
        features = opencvForest.prepareFeaturesFromSignal(accVec.readings)
        confidences = opencvForest.classifyFeatures(features)

        directConfidences = opencvForest.classifySignal(accVec.readings)
        self.assertEqual(confidences, directConfidences)

        reversedDirect = opencvForest.classifySignal(
            list(reversed(accVec.readings)))
        self.assertEqual(directConfidences, reversedDirect)
 def _test_tsd_predictions_agree_using_av3(self, tsd, process, forest):
     for index, sdc in enumerate(tsd['data']):
         accVec = AccelerationVector3D(sdc['accelerometerAccelerations'])
         Ppredictions = process.call('classifyAccelerometerSignal',
                                     readings=accVec.readings)
         if Ppredictions.get('error'):
             self.fail(Ppredictions['detail'])
         Fconfidences = forest.classifySignal(accVec.readings)
         Fpredictions = {
             str(k): v
             for k, v in izip(forest.classLabels(), Fconfidences)
         }
         self.assertEqual(
             Ppredictions, Fpredictions,
             "confidences differ for tsd__trip={} sdc_index={}, P={} F={}".
             format(tsd['trip_pk'], index, Ppredictions, Fpredictions))
    def test_java_vs_python_classify_signal(self):
        javaProcess = self.createJavaProcess()
        opencvForest = OpenCVRandomForest(self.configFilename,
                                          self.modelFilename)
        accVec = AccelerationVector3D(self.data['androidAccelerations'])

        javaPredictions = javaProcess.call('classifyAccelerometerSignal',
                                           readings=accVec.readings)
        if javaPredictions.get('error'):
            self.fail(javaPredictions['detail'])

        confidences = opencvForest.classifySignal(accVec.readings)
        opencvPredictions = {
            str(k): v
            for k, v in izip(opencvForest.classLabels(), confidences)
        }
        self.assertEqual(javaPredictions, opencvPredictions)
    def test_offset_resample_results_opencv(self):
        forest = OpenCVRandomForest(self.configFilename, self.modelFilename)
        prev_ordered = None

        accVec = AccelerationVector3D(self.data['androidAccelerations'])
        for offset in np.arange(0, 14e-3, 1e-4):
            predictions = self._offset_resample_results(forest, accVec, offset)
            nonzero_predictions = {
                k: v
                for k, v in predictions.items() if predictions[k] > 0
            }
            ordered = sorted(nonzero_predictions.keys(),
                             key=lambda k: nonzero_predictions[k],
                             reverse=True)
            if prev_ordered is not None:
                self.assertEqual(prev_ordered, ordered)
            prev_ordered = ordered
    def test_spline_congruent(self):
        "These differ a little bit more than linear, perhaps because alg uses doubles internally"
        utilityAdapter = UtilityAdapter()
        accVec = AccelerationVector3D(self.data['androidAccelerations'])
        spacing = 1. / 20.
        samples = 64
        npInterpolated = self.np_interpolateSplineRegular(
            accVec.seconds, accVec.norms, spacing, samples)
        cInterpolated = utilityAdapter.interpolateSplineRegular(
            accVec.seconds, accVec.norms, spacing, samples)
        # self.plot_interpolations(accVec.seconds, accVec.norms, spacing, samples, np=npInterpolated, c=cInterpolated)

        self.assertEqual(len(npInterpolated), len(cInterpolated))
        try:
            for npValue, cValue in izip(npInterpolated, cInterpolated):
                self.assertAlmostEqual(npValue, cValue, delta=5e-6)
        except AssertionError:
            print "np: {}".format(npInterpolated)
            print "c : {}".format(cInterpolated)
            raise
    def test_compare_fft_results(self):
        sampleSize = 64
        appleFFT = AppleFFTPythonAdapter(sampleSize)
        opencvFFT = OpenCVFFTPythonAdapter(sampleSize)

        accVec = AccelerationVector3D(self.data['androidAccelerations'])
        norms = accVec.norms
        try:
            for index, (appleValue, opencvValue) in enumerate(
                    izip(appleFFT.fft(norms), opencvFFT.fft(norms))):
                if index < sampleSize / 2:
                    fractional_difference = (appleValue -
                                             opencvValue) / opencvValue
                    self.assertLess(abs(fractional_difference), 1e-4)
        except AssertionError:
            print "Mismatch at index {}".format(index)
            for index, (appleValue, opencvValue) in enumerate(
                    izip(appleFFT.fft(norms), opencvFFT.fft(norms))):
                print "i: {: 2} apple: {: 12.6f} opencv: {: 12.6f}".format(
                    index, appleValue, opencvValue)
            raise
    def test_python_compare_rf_features(self):
        self.assertNotEqual(AppleRandomForest, OpenCVRandomForest)
        appleForest = AppleRandomForest(self.configFilename,
                                        self.modelFilename)
        opencvForest = OpenCVRandomForest(self.configFilename,
                                          self.modelFilename)

        accVec = AccelerationVector3D(self.data['androidAccelerations'])

        appleFeatures = appleForest.prepareFeaturesFromSignal(accVec.readings)
        opencvFeatures = opencvForest.prepareFeaturesFromSignal(
            accVec.readings)

        self.assertEqual(len(appleFeatures), len(opencvFeatures))

        for index, (appleF,
                    opencvF) in enumerate(izip(appleFeatures, opencvFeatures)):
            self.assertAlmostEqual(
                appleF,
                opencvF,
                msg='feature {} should be equal'.format(index),
                places=6)