Пример #1
0
def prep(n_reps=50, n_trials_per_chunk=100, with_repl=False, same_across=True,
        rseed=0, nc_objs=64, nc_imgs=1000):
    """Prepare web files for publishing"""
    meta = pk.load(open(METAPATH))
    models64 = rl.ps64_models
    assert len(models64) == nc_objs
    ids = []

    # -- make trials
    print '* Creating trials...'
    rng = np.random.RandomState(rseed)
    trials = []
    for obj in models64:
        si_obj = np.nonzero(meta['obj'] == obj)[0]
        assert len(si_obj) == nc_imgs
        si = None

        for adj in ADJS:
            if si is not None and same_across:
                # same images are shown across all adjs
                pass
            else:
                if with_repl:
                    # with replacement
                    n = len(si_obj)
                    si = rng.randint(n, size=n_reps)
                    si = si_obj[si]
                else:
                    # without replacement
                    rng.shuffle(si_obj)
                    si = si_obj[:n_reps]
                assert len(si) == n_reps

            for i in si:
                assert meta[i]['obj'] == obj
                imgurl = S3PREFIX_FULLOBJT + meta[i]['id'] + '.png'
                trials.append([imgurl, adj])
                ids.append(meta[i]['id'] + '.png')

    assert len(ids) == n_reps * nc_objs * len(ADJS)
    ids = np.unique(ids)
    assert len(ids) == n_reps * nc_objs
    assert len(trials) % n_trials_per_chunk == 0

    # -- prep files
    print '* Writing files...'
    rng = np.random.RandomState(rseed + 1)
    rng.shuffle(trials)

    for label, rules, dstdir in [
            ('sandbox', ut.PREP_RULE_SIMPLE_RSVP_SANDBOX, TMPDIR_SANDBOX),
            ('production', ut.PREP_RULE_SIMPLE_RSVP_PRODUCTION,
                TMPDIR_PRODUCTION)]:
        print '  ->', label
        ut.prep_web_simple(trials, HTMLSRC, dstdir, dstpatt=HTMLDST,
                rules=rules, auxfns=OTHERSRC,
                n_per_file=100, verbose=True)

    # save trials for future reference
    pk.dump(trials, open(os.path.join(TMPDIR, TRIALS), 'wb'))
Пример #2
0
def prep(l_wid_min=11):
    """Prepare web files for publishing"""
    workers = []
    bonuses = {}
    t0 = int(time.time())

    def get_alnumspc(inp):
        inp = [e for e in inp.strip() if e.isalnum() or e.isspace()]
        return ''.join(inp)

    def get_float(inp):
        inp = [e for e in inp.strip() if e.isdigit() or e == '.']
        return ''.join(inp)

    def isyes(inp):
        return inp.strip().lower() in ['', 'y', 'yes']

    # -- fill out info
    print
    print '* Fill out the followings:'

    # compensation level
    print
    print 'NOTE: The default reward/HIT is $0 to avoid unneeded attraction'
    print 'of uninvited workers.  HOWEVER, BY DOING SO, YOU MUST GRANT BONUS'
    print 'TO THE INVITED WORKERS ONCE THEY FINISH THE HITS. Otherwise, they'
    print 'will only receive $0 --- and be upset.'
    inp = raw_input('HIT reward in USD? [default=%f] ' % DEF_COMPENSATION)
    inp = get_float(inp)
    compamt = DEF_COMPENSATION
    if inp != '':
        compamt = float(inp)

    # worker ids / bonus amount
    print
    print 'NOTE: If you decide to grant bonus later (probably you would),'
    print 'you must specify the bonus amount for individual workers now.'
    inp = raw_input('Will you grant bonuses? [default=y] ')

    print
    print 'NOTE: the # of workers cannot exceed %d.' % MAX_PAGE_SIZE
    if isyes(inp):
        # worker ids + specify bonus amount
        print 'Enter bonus amount and corresponding worker ids separted by space.'   # noqa
        print 'Enter -- to finish.'
        print 'Example:'
        print '2.5 ABCDEFGHIJKLM1 ABCDEFGHIJKLM2 ABCDEFGHIJKLM3'
        print '1.5 ABCDEFGHIJKLM4 ABCDEFGHIJKLM5 ABCDEFGHIJKLM6'
        print '--'
        print
        while True:
            # traditional worker id input
            inp = raw_input('>>> ').strip()
            if inp == '--':
                break
            if inp == '':
                continue
            b = float(inp.split()[0])
            inp = get_alnumspc(inp)
            for w in inp.split()[1:]:
                if l_wid_min > 0 and len(w) < l_wid_min:
                    print '* Bad worker id:', w
                    continue
                workers.append(w)
                bonuses[w] = b
    else:
        # only worker ids
        inp = raw_input('Enter space-separated worker ids: ')
        inp = get_alnumspc(inp)
        bad = False
        for w in inp.split():
            if l_wid_min > 0 and len(w) < l_wid_min:
                print '* Bad worker id:', w
                bad = True
                continue
            workers.append(w)
        if bad:
            print '* Bad worker id(s). Aborting...'
            return False

    if len(workers) <= 0:
        print '* No valid workers. Aborting...'
        return False
    elif len(workers) > MAX_PAGE_SIZE:
        print '* Too many workers. Aborting...'
        return False

    # how many HITs?
    n_assignments = len(workers) * 30  # x30 for stupid uninvited ones' accept
    inp = raw_input('The # of total assignments? [default=%d] ' %
            n_assignments)
    inp = get_float(inp)
    if inp != '':
        n_assignments = int(float(inp))
    n_assignments = min(MAX_PAGE_SIZE, n_assignments)

    # title?
    inp = raw_input('Title? [default=%s] ' % DEF_TITLE).strip()
    title = DEF_TITLE if inp == '' else inp

    print '* Summary:'
    print '  - workers:', workers
    print '  - n_assignments:', n_assignments
    print '  - title:', title
    print '  - compensation:', compamt
    print '  - bonuses:'
    for k in bonuses:
        print '              %s -> $%4.2f' % (k, bonuses[k])
    print

    inp = raw_input('Proceed? [default=y] ')
    if not isyes(inp):
        print '* Aborting...'
        return False

    # -- do the work
    print '* Writing files...'
    dstfns_production = []
    dstfns_sandbox = []
    htmldst = 'compensate_' + str(t0) + '_n%d.html'

    for label, rules, dstdir, dstfns in [
            ('sandbox', PREP_RULE_SIMPLE_SANDBOX,
                TMPDIR_SANDBOX, dstfns_sandbox),
            ('production', PREP_RULE_SIMPLE_PRODUCTION,
                TMPDIR_PRODUCTION, dstfns_production)]:
        print '  ->', label
        ds = ut.prep_web_simple(workers, HTMLSRC, dstdir, dstpatt=htmldst,
                rules=rules, auxfns=OTHERSRC,
                n_per_file=len(workers), verbose=True)
        dstfns.extend(ds)

    # save trials for future reference
    pk.dump({
        'workers': workers,
        'htmldst': htmldst,
        't0': t0,
        'workers': workers,
        'n_assignments': n_assignments,
        'compamt': compamt,
        'title': title,
        'dstfns_production': dstfns_production,
        'dstfns_sandbox': dstfns_sandbox,
        'bonuses': bonuses,
        },
            open(os.path.join(TMPDIR, STATES), 'wb'))
    return True