Created
November 25, 2019 07:03
-
-
Save hawkrobe/42287f69d582ed0d966a54043e92c362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import pandas as pd | |
| import boto3 | |
| import xmltodict | |
| import csv | |
| import os | |
| import argparse | |
| # looks for a credentials.py defining the ACCESS_ID and SECRET_KEY | |
| import credentials | |
| # parse args | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--source', type=str) | |
| parser.add_argument('--production', '-p', action='store_true', | |
| help="mimicks nosub convention for running on production") | |
| args = parser.parse_args() | |
| MTURK_SANDBOX = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com' | |
| MTURK_PROD = 'https://mturk-requester.us-east-1.amazonaws.com' | |
| class Bonuser : | |
| def __init__(self, client) : | |
| self.client = client | |
| self.bonus_target_file = os.path.join('bonus', 'already_bonused.csv') | |
| self.bonus_multiplier = 1 | |
| self.reason = "Thanks for participating, here's your bonus!" | |
| self.setup_files() | |
| def setup_files (self) : | |
| """ prepare filesystem with a bonus subfolder """ | |
| if not os.path.exists('bonus'): | |
| os.makedirs('bonus') | |
| if not os.path.exists(self.bonus_target_file) : | |
| with open(self.bonus_target_file, 'w') as f: | |
| fieldnames = ['wID', 'aID', 'bonus'] | |
| writer = csv.DictWriter(f, fieldnames=fieldnames) | |
| writer.writeheader() | |
| self.already_bonused_df = pd.read_csv(self.bonus_target_file) | |
| def check_already_bonused (self, info) : | |
| """ """ | |
| df = self.already_bonused_df[['wID', 'aID']] | |
| a = np.array([info['wID'], info['aID']]) | |
| return (df == a).all(1).any() | |
| def handle_assignment(self, info) : | |
| """ grant bonus based on info """ | |
| if not self.check_already_bonused(info) : | |
| print("Granting bonus of {} to {}".format(info['bonus'], info['aID'])) | |
| self.client.send_bonus( | |
| WorkerId = info['wID'], | |
| BonusAmount = info['bonus'], | |
| AssignmentId = info['aID'], | |
| Reason = self.reason | |
| ) | |
| self.record_bonus(info) | |
| def record_bonus (self, info): | |
| """ append newly bonused workers to list of already bonused""" | |
| with open(self.bonus_target_file, 'a') as f: | |
| writer = csv.writer(f) | |
| writer.writerow([info['wID'], info['aID'], info['bonus']]) | |
| # Define client with credentials and initialize bonuser | |
| client = boto3.client( | |
| 'mturk', | |
| aws_access_key_id = credentials.ACCESS_ID, | |
| aws_secret_access_key = credentials.SECRET_KEY, | |
| region_name = 'us-east-1', | |
| endpoint_url = MTURK_PROD if args.production else MTURK_SANDBOX | |
| ) | |
| bonuser = Bonuser(client) | |
| # Read in csv with bonus information | |
| source = pd.read_csv(args.source) | |
| for i, row in source.iterrows() : | |
| try : | |
| bonuser.handle_assignment(row) | |
| except ClientError as e: | |
| print("failed to bonus row: {}".format(row)) | |
| print("unexpected error: {}".format(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment