def test_get_competitions(): api = NumerAPI() # get all competions res = api.get_competitions() assert isinstance(res, list) assert len(res) > 80
def test_download_current_dataset(): api = NumerAPI() path = api.download_current_dataset(unzip=True) assert os.path.exists(path) directory = path.replace(".zip", "") filename = "numerai_tournament_data.csv" assert os.path.exists(os.path.join(directory, filename))
def test_stake(): api = NumerAPI() with pytest.raises(ValueError) as err: # while this won't work because we are not authorized, it still tells # us if the request is formatted correctly api.stake(3, 2) # error should warn about not beeing logged in. assert "You must be authenticated" in str(err.value)
def output(self): """ Saves outputs of this task--which is a csv file of the predictions made for the given data. """ self.apc = NumerAPI() fn = 'predictions_{0}_LogisticRegression.csv'.format( self.apc.get_current_round()) return luigi.LocalTarget(os.path.join(self.output_path, fn))
def run(self): napi = NumerAPI() while True: try: napi.download_current_dataset(dest_filename=self.output().path, unzip=False) break except Exception as exception: time.sleep(10) continue
def login(self): public_id = os.environ['PUBLIC_ID'] private_secret = os.environ['PRIVATE_SECRET'] while True: try: self.napi = NumerAPI(public_id, private_secret) break except Exception: self.logger.exception('Login failed') time.sleep(10)
def output(self): """ Manages the files to be written and determines their existence. This is determined by checking all the listed files below. If any of them does not exist, :py:func:`run` is evoked. :returns: A ``dict`` with the following keys: * ``zipfile``: original file as downloaded (``numerai_dataset_xxx.zip``) * ``training_data.csv``: the training data (``numerai_training_data.csv``) * ``tournament_data.csv``: the tournament data (``numerai_tournament_data.csv``) * ``example_predictions.csv``: example predictions (``example_predictions.csv``) Note that ``example_model.py`` and ``example_model.r`` are not referenced, as these are to no use for us. """ self.apc = NumerAPI() current_round = self.apc.get_current_round() dataset_name = "numerai_dataset_{0}.zip".format(current_round) dataset_dir = "numerai_dataset_{0}".format(current_round) assert self.apc.download_current_dataset(dest_path=self.output_path, dest_filename=dataset_name, unzip=True) # see numerapi download_current_dataset dataset_path = os.path.join(self.output_path, dataset_dir) test_data_path = os.path.join(dataset_path, 'numerai_training_data.csv') tournament_data_path = os.path.join(dataset_path, 'numerai_tournament_data.csv') example_data_path = os.path.join(dataset_path, 'example_predictions.csv') out = { 'zipfile': luigi.LocalTarget(os.path.join(self.output_path, dataset_name)), 'training_data.csv': luigi.LocalTarget(test_data_path), 'tournament_data.csv': luigi.LocalTarget(tournament_data_path), 'example_predictions.csv': luigi.LocalTarget(example_data_path) } print(out) return out
def main(): # set example username and round example_public_id = "somepublicid" example_secret_key = "somesecretkey" # some API calls do not require logging in napi = NumerAPI(verbosity="info") # download current dataset napi.download_current_dataset(unzip=True) # get competitions all_competitions = napi.get_competitions() # get leaderboard for the current round leaderboard = napi.get_leaderboard() # leaderboard for a historic round leaderboard_67 = napi.get_leaderboard(round_num=67) # provide api tokens napi = NumerAPI(example_public_id, example_secret_key) # upload predictions submission_id = napi.upload_predictions("mypredictions.csv") # check submission status napi.submission_status()
def test_error_handling(): api = NumerAPI() # String instead of Int with pytest.raises(ValueError): api.get_leaderboard("foo") # round that doesn't exist with pytest.raises(ValueError): api.get_leaderboard(-1) # unauthendicated request with pytest.raises(ValueError): # set wrong token api.token = ("foo", "bar") api.submission_id = 1 api.submission_status()
def __init__(self, path, public_id, secret): """ Creates a new SubmissionTarget. :param: path (str): local path to the predictions csv file :param: public_id (str): public_id as reported by the numer.ai website when creating API credentials :param: secret (str): secret as reported by the numer.ai website when creating API credentials """ self.path = path self.fn = os.path.split(path)[1] self.apc = NumerAPI(public_id, secret)
def getCSV(hmpath=homepath()): napi = NumerAPI(verbosity="info") ps = Parser(hmpath) napi.download_current_dataset(dest_path=ps.datapath, unzip=True) r, d, ff = os.walk(ps.datapath) dp = op.join(r, d[0]) for fn in os.listdir(dp): if fn.endswith('.csv'): os.rename(op.join(dp, fn), op.join(r, fn)) os.removedirs(dp) fff = [f for f in ff if f.endswith(".zip")] for fb in fff: os.remove(fb) return ps
def test_get_submission_ids(): api = NumerAPI() ids = api.get_submission_ids() assert len(ids) > 0 assert isinstance(ids, dict)
def test_get_staking_leaderboard(): api = NumerAPI() stakes = api.get_staking_leaderboard(82) # 115 people staked that round assert len(stakes) == 115
def test_get_leaderboard(): api = NumerAPI() lb = api.get_leaderboard(67) assert len(lb) == 1425
from h2o.grid.grid_search import H2OGridSearch from numerapi.numerapi import NumerAPI from flatten_json import flatten from time import sleep from slacker import Slacker # In[186]: #setup slackbot token = 'slack_token' slack = Slacker(token) # In[187]: #setup numerai API napi = NumerAPI() napi.credentials = ('email', 'password') username = '******' # In[188]: h2o.init() #h2o.remove_all() # In[189]: #download dataset napi.download_current_dataset(dest_path='.', unzip=True) # In[190]:
def main(): # set example username and round example_username = "******" example_round = 51 # set up paths for download of dataset and upload of predictions now = datetime.now().strftime("%Y%m%d") dataset_parent_folder = "./dataset" dataset_name = "numerai_dataset_{0}/example_predictions.csv".format(now) dataset_path = "{0}/{1}".format(dataset_parent_folder, dataset_name) # most API calls do not require logging in napi = NumerAPI(verbosity="info") # log in credentials = napi.login() print(json.dumps(credentials, indent=2)) # download current dataset dl_succeeded = napi.download_current_dataset( dest_path=dataset_parent_folder, unzip=True) print("download succeeded: " + str(dl_succeeded)) # get competitions (returned data is too long to print practically) # all_competitions = napi.get_all_competitions() # current_competition = napi.get_competition() # example_competition = napi.get_competition(round_id=example_round) # get user earnings per round user_earnings = napi.get_earnings_per_round() print("user earnings:") print(user_earnings) example_earnings = napi.get_earnings_per_round(username=example_username) print("example earnings:") print(example_earnings) # get scores for user personal_scores = napi.get_scores_for_user() print("personal scores:") print(personal_scores) other_scores = napi.get_scores_for_user(username=example_username) print("other scores:") print(other_scores) # get user information current_user = napi.get_user() print("current user:"******"example user:"******"submission:") print(json.dumps(submission, indent=2)) # upload predictions ul_succeeded = napi.upload_predictions(dataset_path) print("upload succeeded: " + str(ul_succeeded))
def download_new_dataset(): napi = NumerAPI() print("Downloading the current dataset...") napi.download_current_dataset(dest_path=os.path.join(definitions.DATA_DIR, 'raw'), unzip=True)
def test_get_current_round(): api = NumerAPI() current_round = api.get_current_round() assert current_round >= 82
def test_raw_query(): api = NumerAPI() query = "query {dataset}" result = api.raw_query(query) assert isinstance(result, dict) assert "data" in result
def fixture_for_api(): public_id = os.environ.get('NUMERAI_PUBLIC_ID', None) secret_key = os.environ.get('NUMERAI_SECRET_KEY', None) return NumerAPI(public_id=public_id, secret_key=secret_key, verbosity='DEBUG')