def donate1(difficulty=None, password=None, donor='anonymous'): if password is None: try: from microprediction.config_private import DONATION_PASSWORD, DONOR_NAME except: raise Exception("Ask Peter for the donation password ") write_key = new_key(difficulty=8) res = requests.post('https://api.microprediction.org/donations/' + write_key, data={ 'password': password, 'donor': donor }) if res.status_code == 200: if 'password' in res.json()['message']: return {"error": res.json()} else: if difficulty is None: difficulty = random.choice([13]) while True: print("Mining and donating the MUIDs with password " + password + " and donor name " + donor + ". Thanks. Difficulty set to " + str(difficulty), flush=True) write_key = new_key(difficulty=12) print(write_key, flush=True) res = requests.post( 'https://api.microprediction.org/donations/' + write_key, data={ 'password': password, 'donor': donor }) pprint(res.json()) else: return res
def test_cancel_live(): write_key = new_key(difficulty=7) predictor = MicroWriter(write_key=write_key) values = sorted(np.random.randn(predictor.num_predictions)) predictor.submit(name='cop.json', values=values, delay=predictor.DELAYS[0]) res = predictor.cancel(name='cop.json') assert res, "Cancellation of submission failed"
def test_values(): df = example_df() write_key = new_key(difficulty=7) loop = PandasLoop(df=df, origin=time.time(), interval=0.1, write_key=write_key) for k in range(10): assert loop._values(k=k)[0] == k
def test_current_row(): df = example_df() write_key = new_key(difficulty=7) loop = PandasLoop(df=df, origin=time.time(), interval=0.1, write_key=write_key) assert loop._current_row() == 0 assert loop.current_values()[0] == 0 time.sleep(7) assert loop._current_row() == 1 assert loop.current_values()[0] == 1 time.sleep(7) assert loop._current_row() == 2 assert loop.current_values()[0] == 2
def new_key_if_none(write_key, use_environ=False, difficulty=10): """ Create key on the fly but warn user about time this may take :param write_key str :param use_environ bool Highly experimental ... set True at your own risk :param difficulty int :returns write_key """ if use_environ: write_key = write_key or os.environ.get('WRITE_KEY') if write_key is None: print( 'A write_key was not supplied, so burning one now of difficulty ' + str(difficulty) + '. This will take a while, after which your crawler will be initialized. Save the write_key and next time supply a write_key to the constructor. ', flush=True) write_key = new_key(difficulty=difficulty) print( 'Your write_key is ' + write_key + '. Do not lose!! We recommend a quick visit to http://dev.microprediction.org/dashboard.html where you can punch it in' ) if use_environ: os.environ['WRITE_KEY'] = write_key return write_key
def test_maybe(): try: from microprediction.config_private import TRAFFIC_WRITE_KEY except: TRAFFIC_WRITE_KEY = new_key(difficulty=7) assert MicroWriter.is_valid_key(TRAFFIC_WRITE_KEY)
def test_validate(): write_key = new_key(difficulty=7) assert MicroWriter.is_valid_key(write_key)
def test_client(): with Timer() as t: write_key = new_key(difficulty=7) mw = MicroWriter(write_key=write_key) assert (t.elapsed < 15)
from microprediction import new_key from echochamber import EchoCrawler # Example of creating, modify and launch crawler class MyEchoCrawler(EchoCrawler): def candidate_streams(self): sponsors = self.get_sponsors() bobcats_streams = [ name for name, sponsor in sponsors.items() if 'cellose bobcat' in sponsor.lower() ] return [s for s in bobcats_streams if not '~' in s] def candidate_delays(self, name=None): return [d for d in self.delays if d > 100] if __name__ == "__main__": write_key = new_key(difficulty=10) # <--- This will take a while crawler = MyEchoCrawler(n_reservoir=50) crawler.run()
from microprediction.polling import MicroPoll from microprediction import new_key from microprediction.live.nyc_traffic import verrazano_speed try: from microprediction.config_private import TRAFFIC_WRITE_KEY except: TRAFFIC_WRITE_KEY = new_key(difficulty=12) # Could take a while! if __name__ == "__main__": feed = MicroPoll(write_key=TRAFFIC_WRITE_KEY, name='verrazano_speed.json', func=verrazano_speed, interval=1) feed.run()
DF.columns = [ 'helicopter_pitch', 'helicopter_yaw', 'helicopter_theta', 'helicopter_psi' ] # Nicer names, and ensure there are still only four columns DF = DF[[ 'helicopter_theta', 'helicopter_psi' ]] # Just using the two fast moving ones. Could add the others as auxiliary data def logtime(res): """ Primitive logging function """ pprint(res) print(' ', flush=True) def helicopter_stream(write_key): """ Calls into PandasLoop which sends one row of data every 7 minutes """ loop = PandasLoop(write_key=WRITE_KEY, interval=7, origin=1594147541, df=DF, with_copulas=True) loop.publish_callback = logtime loop.run(minutes=50000) if __name__ == "__main__": WRITE_KEY = new_key( difficulty=13 ) # Takes many days ... [email protected] if you have a good use case and don't want to wait print(WRITE_KEY) helicopter_stream(write_key=WRITE_KEY)
from microprediction import MicroCrawler, new_key from pprint import pprint import muid difficulty = 8 print('Generating MUID of difficulty '+str(difficulty)+' - please be patient') write_key = new_key(difficulty=difficulty) print(write_key) print(muid.animal(write_key) + ' is starting up ') crawler = MicroCrawler(write_key=write_key, sleep_time=30, verbose=True ) crawler.run() pprint(crawler.get_performance())
from microprediction import MicroWriter, new_key from microprediction.samplers import exponential_bootstrap import time from pprint import pprint # Example of a home spun crawler # This does not use MicroCrawler class but instead, directly uses MicroWriter. # It enters all the z2~ streams try: from microprediction.config_private import OXEATE_MAMMAL WRITE_KEY = OXEATE_MAMMAL # Replace with your own except ImportError: WRITE_KEY = new_key(difficulty=10) print(WRITE_KEY) ANIMAL = MicroWriter.animal_from_key(WRITE_KEY) print(ANIMAL, flush=True) STOP_LOSS = 25. if __name__ == "__main__": mw = MicroWriter(write_key=WRITE_KEY) LONG = '~' + str(mw.DELAYS[-1]) SHORT = '~' + str(mw.DELAYS[0]) # Give up if things are going badly ... do it nicely for _ in range(5): mw.cancel_worst_active(stop_loss=STOP_LOSS, num=1) time.sleep(1)
""" changes = np.diff(list(reversed(lagged_values)), n=4) counter = dict(Counter(changes)) d = dict(counter) num_total = len(changes) d1 = dict([(change, round(175 * change_count / num_total)) for change, change_count in d.items()]) values = list() for change, rounded_count in d1.items(): values.extend([change] * rounded_count) change_spray = list(range(-50, 50)) values.extend(change_spray) values = values[:self.num_predictions] abs_values = [lagged_values[0] + chg for chg in values] return sorted(abs_values) if __name__ == '__main__': try: from microprediction.config_private import MALAXABLE_FOX crawler = EmpiricalTrafficCrawler(write_key=MALAXABLE_FOX, stop_loss=5, max_active=15, min_lags=100) crawler.run() except ImportError: print('You need a write_key. I' 'm making one for you now. ') from microprediction import new_key print(new_key(difficulty=11))
from microprediction import new_key from echochamber import EchoCrawler # Create and launch crawler try: from echochamber.config_private import TEST_KEY except: TEST_KEY = new_key(difficulty=10) if __name__ == "__main__": crawler = EchoCrawler(write_key=TEST_KEY, n_reservoir=50) crawler.run()
from microprediction import new_key if __name__ == "__main__": write_key = new_key(difficulty=12) # Takes a LONG time, sorry. # By all means hit us up for one [email protected] print(write_key)
def create_a_write_key(): """ Takes a while """ print(new_key(difficulty=10))
from microprediction import new_key, MicroWriter import pprint write_key = "ddf3b83838f7d07f0e48404115eb3ec3" or new_key(difficulty=8) mw = MicroWriter(write_key=write_key) name = 'cop.json' values = [float(x) / 10.1 for x in range(mw.num_predictions)] res = mw.submit(name=name, values=values, delay=mw.delays[0]) pprint.pprint(res)