def complete_override(): ''' Debugging route for complete. ''' if not 'uniqueId' in request.args: raise ExperimentError('improper_inputs') else: unique_id = request.args['uniqueId'] mode = request.args['mode'] try: user = Participant.query.\ filter(Participant.uniqueid == unique_id).one() user.status = COMPLETED user.endhit = datetime.now() db_session.add(user) db_session.commit() except: raise ExperimentError('error_setting_worker_complete') else: # send them back to mturk. if (mode == 'sandbox' or mode == 'live'): return render_template('closepopup.html') else: if mode == 'lab': link = unique_id.split(':')[0] try: unique_link = UniqueLink.query.\ filter(UniqueLink.link == link).one() unique_link.status = LINK_SUBMITTED db_session.add(unique_link) db_session.commit() except: current_app.logger.error( "Couldn't change status to SUBMITTED for %s" % link) return render_template('complete.html')
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON bonus = 0 for record in user_data['data']: # for line in data file trial = record['trialdata'] if trial['phase'] == 'TEST': if trial['hit'] == True: bonus += 0.02 user.bonus = bonus db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if 'uniqueId' not in request.args: raise ExperimentError( 'improper_inputs') # original author wants to change this line uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query\ .filter(Participant.uniqueid == uniqueId)\ .one() user_data = loads(user.datastring) # load datastring from JSON bonus = 0 for record in user_data['data']: # for line in data file trial = record['trialdata'] if trial['phase'] == 'TEST': if trial['hit'] is True: bonus += 0.02 user.bonus = bonus db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except Exception as e: current_app.logger.info("Error: %s", repr(e)) abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not request.args.has_key('uniqueId'): raise ExperimentError('improper_inputs') # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON n_trials = 0 for record in user_data['data']: # for line in data file trial = record['trialdata'] cur_trl_type = data_trl.get('trial', '') if cur_trl_type != trl_type: continue if trl_type == 'train': points = int(data_trl['points']) if 'points' in data_trl else np.nan n_trials += 1 bonus = points * .01 user.bonus = bonus db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): current_app.logger.info("accessing route /compute_bonus") # check that request includes a uniqueId if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON bonuses = [] for record in user_data['data']: # for line in data file trial = record['trialdata'] # get part of line holding trial info if trial['phase'] == 'apavPhase' and trial['globalTrial'] == 252: user.bonus = float( trial['scoreAfter']) #set bonus field to the final bonus db_session.add(user) db_session.commit() #commit to database resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not 'uniqueId' in request.args: # i don't like returning HTML to JSON requests... maybe should change this raise ExperimentError('improper_inputs') uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON for record in user_data['data']: # for line in data file trial = record['trialdata'] if trial['phase'] == 'Points Collected': bonus = float( trial['Total Points'] ) / 10. * 0.01 # payout one cent per trial, for now user.bonus = bonus db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): current_app.logger.info("accessing route /compute_bonus") # check that request includes a uniqueId if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON import random r = random.random() whichToBonus = 0 if r > 0.5: whichToBonus = 1 bonus = 0 for record in user_data['data']: # for line in data file trial = record['trialdata'] # get part of line holding trial info if trial[ 'phase'] == 'apav': #check that trial is in test phase, not instructions if trial['trial'] == 243 and trial[ 'habitatNum'] == whichToBonus: bonus = float(trial['scoreAfter']) user.bonus = bonus #set bonus field to new value db_session.add(user) db_session.commit() #commit to database resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascript client if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this unique_id = request.args['uniqueId'] # try: # lookup user in database # in order to check for IP address violations get the user this way and then check for user.ipaddress user = Participant.query. \ filter(Participant.uniqueid == unique_id). \ one() user_data = loads(user.datastring) # load datastring from JSON bonus = 0 for record in user_data['data']: # for line in data file trial = record['trialdata'] if trial['phase'] == 'trial' and trial['included']: bonus += float(trial['choice']) elif trial['phase'] == 'exit': bonus = 0 break user.bonus = "%.2f" % round(bonus, 2) db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp)
def ineligible(): current_app.logger.info("Participant ineligible") try: unique_id = request.args['uniqueId'] current_app.logger.info("Marking ineligible %s" % unique_id) user = Participant.query. \ filter(Participant.uniqueid == unique_id).one() user.status = 8 # INELIGIBLE db_session.add(user) db_session.commit() except exc.SQLAlchemyError: raise ExperimentError('tried_to_quit') raise IneligibilityError()
def mark_bad(): if not 'uniqueId' in request.args: raise ExperimentError('improper_inputs') else: uniqueId = request.args['uniqueId'] user = Participant\ .query.\ filter(Participant.uniqueid == uniqueId).\ one() user.status = BAD db_session.add(user) db_session.commit() resp = {user.uniqueid: user.status} return jsonify(**resp)
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascript client if not request.args.has_key('uniqueId'): raise ExperimentError('improper_inputs') # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON bonus = 0 # find actual reward/max expected reward and multiply by max $ amount to give ($1?) # take average over each collaboration setting? questiondata = user_data['questiondata'] num_iterations = questiondata['num_iterations'] bonuses = [] for robot in questiondata['robot_order']: key = str(robot) + "_hard_collaborate_suggest" data_dict = questiondata[key] # payoffs = np.array(data_dict['arm_payoffs']) # probabilities = np.array(data_dict['arm_probabilities']) # max_exp_reward = max(payoffs * probabilities) * num_iterations payoff_matrix = np.array(data_dict['arms_payoff_matrix']) max_exp_reward = max(payoff_matrix.dot([0, 1, 2, 3, 4])) * num_iterations actual_reward = data_dict['all_total_rewards'][-1] bonuses.append(min(1, 1 * (actual_reward / max_exp_reward))) bonus = np.average(bonuses) if bonus >= 0.5: user.bonus = round(bonus, 2) else: user.bonus = 0.00 db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except Exception as e: print "error updating bonus" print e abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not request.args.has_key('uniqueId'): raise ExperimentError('improper_inputs') # lookup user in database uniqueid = request.args['uniqueId'] user = Participant.query.\ filter(Participant.uniqueid == uniqueid).\ one() final_bonus = 'NONE' # load the bonus information try: all_data = json.loads(user.datastring) question_data = all_data['questiondata'] final_bonus = question_data['final_bonus'] final_bonus = round(float(final_bonus), 2) if final_bonus > MAX_BONUS: raise ValueError('Bonus of {} excedes MAX_BONUS of {}'.format( final_bonus, MAX_BONUS)) user.bonus = final_bonus db_session.add(user) db_session.commit() resp = { 'uniqueId': uniqueid, 'bonusComputed': 'success', 'bonusAmount': final_bonus } except: current_app.logger.error( 'error processing bonus for {}'.format(uniqueid)) current_app.logger.error(format_exc()) resp = { 'uniqueId': uniqueid, 'bonusComputed': 'failure', 'bonusAmount': final_bonus } current_app.logger.info(str(resp)) return jsonify(**resp)
def complete_exp(): if not 'uniqueId' in request.form: raise ExperimentError('improper_inputs') unique_id = request.form['uniqueId'] current_app.logger.info("completed experimente") try: user = Participant.query.\ filter(Participant.uniqueid == unique_id).one() user.status = COMPLETED user.endhit = datetime.datetime.now() db_session.add(user) db_session.commit() resp = {"status": "success"} except exc.SQLAlchemyError: current_app.logger.error("DB error: Unique user not found.") resp = {"status": "error, uniqueId not found"} return jsonify(**resp)
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascript client if not request.args.has_key('uniqueId'): raise ExperimentError('improper_inputs') uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.filter(Participant.uniqueid == uniqueId).one() user_data = loads(user.datastring) # load datastring from JSON user.bonus = user_data['questiondata']['bonus'] db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404)
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads( user.datastring ) # load datastring from JSON [values are all of type str?] #user_data=loads(user.datastring, parse_float=decimal.Decimal) bonus = 6.00 for record in user_data['data']: # for line in data file trial = record['trialdata'] # part of line holding trial data if trial['phase'] == 'task': # for 'task' trials only if trial['rt'] > -1: bonus -= 0.02 if trial['rt'] == -1 and trial[ 'tt'] == 2: #float(trial['tt'])== 2: bonus -= 0.10 if trial['rt'] == -1 and trial['tt'] == 4: bonus -= 0.10 if bonus < 0: # no negative bonuses bonus = 0 user.bonus = bonus db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): current_app.logger.info("accessing route /compute_bonus") # check that request includes a uniqueId if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON user.bonus = float(user_data['questiondata']['finalBonus']) db_session.add(user) db_session.commit() #commit to database resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: # lookup user in database user = Participant.query.\ filter(Participant.uniqueid == uniqueId).\ one() user_data = loads(user.datastring) # load datastring from JSON reward = 0 clicknum = 0 randomtrialnum = math.ceil(random.random() * 60) for record in user_data['data']: # for line in data file trial = record['trialdata'] if trial['phase'] == 'TASK': if trial['trial_num'] == randomtrialnum: reward += trial['reward'] clicknum += 1 zscore = (reward / clicknum - 3) / (0.6 / math.sqrt(clicknum)) bonus = round(5 * (1 - math.pow(math.e, (-zscore / 3))), 2) if bonus < 0: bonus = 0 user.bonus = bonus db_session.add(user) db_session.commit() resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def compute_bonus(): # check that user provided the correct keys # errors will not be that gracefull here if being # accessed by the Javascrip client if not request.args.has_key('uniqueId'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this uniqueId = request.args['uniqueId'] try: print "looking for user: "******"found it. computing his bonuses." data = user_data['data'] for report in data: # for line in data file trial = report['trialdata'] if trial["phase"] == TASK: bonus = trial['total_reward'] if bonus > 5: bonus *= 1.1 bonus = round(bonus + 0.2, 2) print "bonus: ", bonus user.bonus = bonus db_session.add(user) db_session.commit() print "commited to db" resp = {"bonusComputed": "success"} return jsonify(**resp) except: abort(404) # again, bad to display HTML, but...
def init_experiment(): if not request.args.has_key('condition'): raise ExperimentError( 'improper_inputs' ) # i don't like returning HTML to JSON requests... maybe should change this CONDITION = int(request.args['condition']) COUNTERBALANCE = int(request.args['counterbalance']) ## FREE VARS # made with numpy.random.randint(4294967295, size=100) # (number is max allowed on amazon linux) RNGSEEDPOOL =\ npa([3298170796, 2836114699, 599817272, 4120600023, 2084303722, 3397274674, 422915931, 1268543322, 4176768264, 3919821713, 1110305631, 1751019283, 2477245129, 658114151, 3344905605, 1041401026, 232715019, 326334289, 2686111309, 2708714477, 737618720, 1961563934, 2498601877, 210792284, 474894253, 4028779193, 237326432, 3676530999, 529417255, 3092896686, 169403409, 2615770170, 1339086861, 3757383830, 2082288757, 4170457367, 371267289, 3248256753, 1696640091, 2779201988, 492501592, 2278560761, 2146121483, 772692697, 2009991030, 1917110505, 621292942, 1900862326, 3924210345, 2834685808, 2782250785, 3978659517, 230589819, 3266844848, 1789706566, 1926158994, 3334290749, 2564647456, 2780425615, 2453304773, 2867246165, 2853230235, 3943014068, 1849702346, 1006440977, 326290567, 779365638, 2796156127, 2850718974, 4250010213, 1627130691, 3538373920, 1807938670, 2430758838, 1678867555, 515849939, 323252975, 1062571753, 551895230, 1003551997, 902827309, 2496798931, 4165811834, 88322007, 1998993400, 3260624632, 2504021401, 915464428, 2503603945, 1138822767, 1487903826, 3534352433, 2793970570, 3696596236, 3057302268, 2924494158, 1308408238, 2181850436, 2485685726, 1958873721]) MINDEG = 0. # minumum degree of choiceSet MAXDEG = 360. # max degree of choiceSet RANGEDEG = MAXDEG - MINDEG ROTMAGPOOL = npa([15., 30., 45., 60.]) # proxy for 15, 30, 45, 60 degree rots ROTMAG = ROTMAGPOOL[CONDITION] NPERXOPT = [90, 40, 40, 90] # how many trials per block? NTRIAL = sum(NPERXOPT) # total number trials in experiment MAXROTMAG = 60. # maximum rotation considered for these experiments DEGWHEREROTISZERO = None # if none, will be random EDGEBUF = 10. # random degWhereRotIsZero will be between [MINDOMAIN+EDGEBUF, MAXDOMAIN-EDGEBUF] RNGSEED = RNGSEEDPOOL[COUNTERBALANCE] # 'b' for base, 'r' for rot, 'c' for counterrot BLOCKTYPES = ['b', 'r', 'c', 'b'] # if None, all abrupt blocks. # (can be explicitely written as ['a', 'a', 'a', 'a']) AGTYPES = None # params for make_clickArcQueue, which determines startpoint and heading ang # NTARGET = 4 # MINDEGARCPOOL = linspace(0., 360., NTARGET+1)[:-1] # ccw-most part of choice arc MINDEGARCPOOL = npa([0.]) NTARGET = len(MINDEGARCPOOL) MAXDEGARCPOOL = MINDEGARCPOOL + RANGEDEG # cw-most part of choice arc assert NTRIAL % NTARGET == 0 NEPICYCLE = NTRIAL / NTARGET # how many epicycles through each target loc RADWRTXARC = 0.3 # percent of window width that determines dist(start, arc) XORIGIN = 0.5 # x startpoint as percentage of window width YORIGIN = 0.5 # y startpoint as percentage of window height experParams = { # needed for make_mixed_xOptQueue 'domainbounds': [MINDEG, MAXDEG], 'rotmag': ROTMAG, 'nPerXOpt': NPERXOPT, 'radwrtxArc': RADWRTXARC, 'maxrotmag': MAXROTMAG, 'degWhereRotIsZero': DEGWHEREROTISZERO, 'edgebuf': EDGEBUF, 'rngseed': RNGSEED, 'blockTypes': BLOCKTYPES, 'agTypes': AGTYPES, # needed for make_clickArcQueue 'mindegArcPool': MINDEGARCPOOL, 'maxdegArcPool': MAXDEGARCPOOL, 'nEpicycle': NEPICYCLE, 'radwrtxArc': RADWRTXARC, 'xOrigin': XORIGIN, 'yOrigin': YORIGIN } # make experiment params for this subject! # (** means unpack and pass in params in a dict) subParams = rotationExperiment(**experParams) # add experiment params used on client side MSMINTIMEINSTART = 500 # ms to spend in startpoint before choice MSMAXTIMETOCHOICE = None MSSHOWFEEDBACK = 1000 # must be in ['aboveStartPoint', 'clickLocation'] FEEDBACKTYPE = 'aboveStartPoint' SDPERCENTRADWIGGLEFB = 0.1 # sd for wiggling rad wrt radius of choiceArc SDPERCENTDEGWIGGLEFB = 0.1 # sd for wiggling angle wrt RANGEDEG of choiceArc # number of prev scores to show on arc. # must be None if fbtype != 'clickLocation' NLASTTOSHOW = None # how far bt sp and arc should fb be? must be > 0 # must be None if fbtype != 'aboveStartPoint' PERCENTBETWEENSPANDARC = 0.6 if FEEDBACKTYPE == 'aboveStartPoint': assert not NLASTTOSHOW if FEEDBACKTYPE == 'clickLocation': assert not PERCENTBETWEENSPANDARC experParams['msmintimeinstart'] = MSMINTIMEINSTART experParams['msmaxtimetochoice'] = MSMAXTIMETOCHOICE experParams['msshowfeedback'] = MSSHOWFEEDBACK experParams['feedbacktype'] = FEEDBACKTYPE experParams['nlasttoshow'] = NLASTTOSHOW experParams['percentbetweenspandarc'] = PERCENTBETWEENSPANDARC experParams['sdpercentradwigglefb'] = SDPERCENTRADWIGGLEFB experParams['sdpercentdegwigglefb'] = SDPERCENTDEGWIGGLEFB experParams['ntrial'] = NTRIAL # bundle response to send resp = {} for f in subParams: fname = f if f == 'xOptQueue': fname = 'degoptqueue' try: resp[fname] = subParams[f].tolist() except: resp[fname] = subParams[f] for f in experParams: try: # convet numpy array to list if possible resp[f] = experParams[f].tolist() except: resp[f] = experParams[f] resp['inititrial'] = 0 # start at trial 0 resp['rotmag'] = ROTMAG resp['rngseed'] = RNGSEED resp['initscore'] = 0 # start w 0 points resp['mindeg'] = MINDEG resp['maxdeg'] = MAXDEG return jsonify(**resp)