Exemplo n.º 1
0
 def setUp(self):
     self.pred = Prediction(HALFLIFE)
     with tmpdatadir_context():
         self.b = MemBlock.read(333931)
         extra_entries = MemBlock.read(333932)
     self.pred.update_predictions(self.b, transientstats)
     self.pred.update_predictions(extra_entries, transientstats)
Exemplo n.º 2
0
    def test_A(self):
        TESTFEERATES = range(0, 55000, 5000)
        # _dum, txref_rates = txref.get_byterates(feerates=FEERATES)
        refbyteratefn = txref.get_byteratefn()
        with tmpdatadir_context() as datadir:
            # RectEstimator
            self.gen_blockrange = (0, 100)
            self.tmpdbfile = os.path.join(datadir, '_tmp.db')
            self.populate_testdb()

            tr = RectEstimator(maxsamplesize=100000)
            print("Starting estimation from generated...")
            tr.start(self.gen_blockrange, dbfile=self.tmpdbfile)
            print("Rect estimation from generated:")
            print("===============================")
            print("Test:")
            print(repr(tr))
            print(tr)
            print("Ref:")
            print(repr(txref))
            print(txref)

            # _dum, byterates = tr.get_byterates(feerates=FEERATES)
            testbyteratefn = tr.get_byteratefn()
            # for test, target in zip(byterates, txref_rates):
            for feerate in TESTFEERATES:
                test = testbyteratefn(feerate)
                ref = refbyteratefn(feerate)
                diff = abs(log(test) - log(ref))
                self.assertLess(diff, 0.2)
                print("Diff is {}".format(diff))
            diff = abs(log(tr.txrate) - log(txref.txrate))
            print("txrate log diff is {}".format(diff))
            self.assertLess(diff, 0.1)

            # ExpEstimator
            tr = ExpEstimator(86400)
            print("Starting estimation from generated...")
            tr.start(self.gen_blockrange[-1]-1, dbfile=self.tmpdbfile)
            print("Exp estimation from generated:")
            print("===============================")
            print("Test:")
            print(repr(tr))
            print(tr)
            print("Ref:")
            print(repr(txref))
            print(txref)

            # _dum, byterates = tr.get_byterates(feerates=FEERATES)
            # for test, target in zip(byterates, txref_rates):
            testbyteratefn = tr.get_byteratefn()
            for feerate in TESTFEERATES:
                test = testbyteratefn(feerate)
                ref = refbyteratefn(feerate)
                diff = abs(log(test) - log(ref))
                self.assertLess(diff, 0.2)
                print("Diff is {}".format(diff))
            diff = abs(log(tr.txrate) - log(txref.txrate))
            print("txrate log diff is {}".format(diff))
            self.assertLess(diff, 0.1)
Exemplo n.º 3
0
    def test_E(self):
        # DB checks
        with tmpdatadir_context():
            pred = self.pred
            b = self.b
            blocktime = b.time
            for txpredict in pred.predicts.values():
                if txpredict:
                    txpredict.entrytime = blocktime - 600
            pred_db = Prediction.from_db(HALFLIFE)
            self.assertIsNone(pred_db.pval_ecdf)
            pred.process_blocks([b])
            pred_db = Prediction.from_db(HALFLIFE, conditions="waittime>600")
            # No pvals
            self.assertEqual(str(pred_db), "No valid ECDF.")

            b.blockheight += DEFAULT_BLOCKS_TO_KEEP - 1
            pred.update_predictions(b, transientstats)
            for txpredict in pred.predicts.values():
                if txpredict:
                    txpredict.entrytime = blocktime - 300
            pred.process_blocks([b])

            # Check stat is unchanged on load
            pred_db = Prediction.from_db(HALFLIFE)
            for p, p_db in zip(pred.pval_ecdf, pred_db.pval_ecdf):
                self.assertAlmostEqual(p[1], p_db[1])
            self.assertEqual(pred_db.pval_ecdf.pdistance,
                             pred.pval_ecdf.pdistance)

            # Check the circular db deletes
            heights = pred.get_heights()
            self.assertEqual(heights,
                             [333931, 333931 + DEFAULT_BLOCKS_TO_KEEP - 1])

            b.blockheight += 1
            pred.update_predictions(b, transientstats)
            for txpredict in pred.predicts.values():
                if txpredict:
                    txpredict.entrytime = blocktime - 100
            pred.process_blocks([b])

            heights = pred.get_heights()
            self.assertEqual(heights, [
                333931 + DEFAULT_BLOCKS_TO_KEEP - 1,
                333931 + DEFAULT_BLOCKS_TO_KEEP
            ])
            pred_db = Prediction.from_db(HALFLIFE)
            self.assertTrue(
                any([
                    abs(p[1] - p_db[1]) >= 0.00001
                    for p, p_db in zip(pred.pval_ecdf, pred_db.pval_ecdf)
                ]))
            self.assertNotAlmostEqual(pred.pval_ecdf.pdistance,
                                      pred_db.pval_ecdf.pdistance)
Exemplo n.º 4
0
    def test_C(self):
        """Test app."""
        with tmpdatadir_context():
            peo = PoolsOnlineEstimator(333954, None, 2016)
            sleep(1)
            peo.loadingthread.join()
            self.assertIsNotNone(peo.poolsestimate)
            self.assertFalse(peo.poolsestimate)

            peo = PoolsOnlineEstimator(333954, None, 2016, minblocks=1)
            sleep(1)
            peo.loadingthread.join()
            self.assertIsNotNone(peo.poolsestimate)
            self.assertTrue(peo.poolsestimate)
            print(peo.poolsestimate)
Exemplo n.º 5
0
    def test_B(self):
        """Test estimation."""
        with tmpdatadir_context():
            pe = PoolsEstimatorNP()
            pe.start((333931, 333954))

            # A fake memblock with zero entries.
            empty_memblock = MemBlock()
            empty_memblock.blockheight = 333954
            empty_memblock.height = 333953
            empty_memblock.blocksize = 0
            empty_memblock.time = pe.blockstats[333953][2] + 20
            empty_memblock.entries = {}
            pe.update(empty_memblock)
        print(pe)
Exemplo n.º 6
0
 def test_saveload(self):
     with tmpdatadir_context() as datadir:
         savefile = os.path.join(datadir, '_test_tmp.pickle')
         save_obj(pe, savefile)
         pe_load = load_obj(savefile)
         self.assertEqual(pe_load, pe)
 def test_saveload(self):
     with tmpdatadir_context() as datadir:
         savefile = os.path.join(datadir, '_test_tmp.pickle')
         save_obj(pe, savefile)
         pe_load = load_obj(savefile)
         self.assertEqual(pe_load, pe)