Ejemplo n.º 1
0
def test(full=False):
    """Test and validates the written html files"""
    trials_org = pk.load(open(os.path.join(TMPDIR, TRIALS)))

    fns_sandbox = sorted(glob.glob(os.path.join(
        TMPDIR_SANDBOX, '*.html')))
    fns_production = sorted(glob.glob(os.path.join(
        TMPDIR_PRODUCTION, '*.html')))

    print '* Testing sandbox...'
    ut.validate_html_files(fns_sandbox,
            rules=ut.PREP_RULE_SIMPLE_RSVP_SANDBOX,
            trials_org=trials_org)
    print '* Testing production...'
    ut.validate_html_files(fns_production,
            rules=ut.PREP_RULE_SIMPLE_RSVP_PRODUCTION,
            trials_org=trials_org)

    if full:
        ids = [e[0].split('/')[-1] for e in trials_org]
        ids = np.unique(ids)
        print '* Making sure all images are there:', len(ids)
        _, bucket = mt.connect_s3(section_name='MTurkCredentials_esolomon',
                bucketname=S3BUCKET_FULLOBJT)
        for i, e in enumerate(ids):
            assert mt.exists_s3(bucket, e)
            if i % 100 == 0:
                print ' ->', i
Ejemplo n.º 2
0
def bonus(sandbox=True):
    """Publish to the sandbox"""
    bucket = mt.connect_s3().get_bucket(S3BUCKET)
    k = Key(bucket)

    def is_in_sloppy(x, iterable):
        for a in iterable:
            if a in x:
                return a
        return None

    if BONUST0 is None:
        states = pk.load(open(os.path.join(TMPDIR, STATES)))
        s_t0 = str(states['t0'])
        pref = DATAFNPREFIX % s_t0
    else:
        pref = DATAFNPREFIX % BONUST0
        k.key = pref + STATES
        states = pk.loads(k.get_contents_as_string())
        s_t0 = str(states['t0'])
        assert s_t0 == BONUST0

    fn_hs = 'hitidslog_' + 'sandbox.pkl' if sandbox else 'production.pkl'
    k.key = pref + fn_hs
    hitids = pk.loads(k.get_contents_as_string())
    bonuses = states['bonuses']
    unbonused = bonuses.keys()

    exp = mt.Experiment(None, None, sandbox=sandbox,
        max_assignments=MAX_PAGE_SIZE,
        reward=0.,
        collection_name=None,   # disables db connection
        meta=None,
        )

    rs = [e for hid in hitids for e in exp.getHITdata(hid)]
    for r in rs:
        w0 = r['WorkerID']
        w = is_in_sloppy(w0, bonuses)
        if w is None:
            print '*** Potential cheater:', r['WorkerID']
            continue

        k.key = pref + w + '.pkl'
        if k.exists():
            unbonused.remove(w)
            continue  # already gave a bonus

        aid = r['AssignmentID']
        try:
            exp.conn.approve_assignment(aid)
        except mturk.connection.MTurkRequestError:
            print 'Already approved?', aid

        b = Price(bonuses[w])
        exp.conn.grant_bonus(w0, aid, b, 'Compensation')

        pkl = pk.dumps(r)
        k.set_contents_from_string(pkl)

        unbonused.remove(w)
        print '* Granting $%4.2f bonus to %s' % (bonuses[w], w0)

    if len(unbonused) > 0:
        print '* Not yet bonused:', unbonused
    else:
        print '* All invited workers got bonuses.'