def simulate_one_client( input_args ): exp_uid,participant_uid,total_pulls,X_true,assert_200 = input_args getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "Participant {1} has taken {0} pulls".format(t,participant_uid) # test POST getQuery # widget = random.choice([True] + 4*[False]) widget = True getQuery_args_dict = {'args': {'participant_uid': participant_uid, 'widget': widget}, 'exp_uid': exp_uid} query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] targets = query_dict['target_indices'] # print targets for target in targets: if target['label'] == 'center': index_center = target['target_id'] elif target['label'] == 'left': index_left = target['target_id'] elif target['label'] == 'right': index_right = target['target_id'] ts = test_utils.response_delay() # sleep for a bit to simulate response time direction = norm(X_true[index_left]-X_true[index_center])-norm(X_true[index_right]-X_true[index_center]) r = numpy.random.rand() if r<.1: direction = - direction if direction<0.: target_winner = index_left else: target_winner = index_right response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid processAnswer_args_dict["args"]["target_winner"] = target_winner processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer(processAnswer_args_dict) processAnswer_times.append(dt) r = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return r
def simulate_one_client(input_args): exp_uid, participant_uid, total_pulls, true_means, assert_200 = input_args verbose = False test_utils.response_delay() getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "Participant {} has had {} pulls".format(participant_uid, t) # test POST getQuery # getQuery_args_dict = {} getQuery_args_dict['exp_uid'] = exp_uid getQuery_args_dict['args'] = {} # getQuery_args_dict['args']['participant_uid'] = numpy.random.choice(participants) getQuery_args_dict['args']['participant_uid'] = participant_uid query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] query_uid = query_dict['query_uid'] targets = query_dict['target_indices'] target_index = targets[0]['target']['target_id'] # generate simulated reward # # sleep for a bit to simulate response time ts = test_utils.response_delay() target_reward = 1. + sum( numpy.random.rand(2) < true_means[target_index]) # in {1,2,3} # target_reward = numpy.random.choice(labels)['reward'] response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid target_reward = str(target_reward) if np.random.randint( 2) == 0 else target_reward processAnswer_args_dict["args"]['target_reward'] = str(target_reward) processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer( processAnswer_args_dict) processAnswer_times += [dt] t = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return t
def simulate_one_client(input_args): exp_uid, participant_uid, total_pulls, true_weights, assert_200 = input_args getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "participant {} had {} pulls".format(participant_uid, t) # test POST getQuery # widget = True getQuery_args_dict = { 'args': { 'participant_uid': participant_uid, 'widget': widget }, 'exp_uid': exp_uid } query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] target = query_dict['target_indices'] x = numpy.array(eval(target['primary_description'])) # generate simulated reward # # sleep for a bit to simulate response time ts = test_utils.response_delay() target_label = 1 if random.random( ) > 0.5 else -1 #numpy.sign(numpy.dot(x,true_weights)) response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid processAnswer_args_dict["args"]["target_label"] = target_label processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer( processAnswer_args_dict) processAnswer_times += [dt] return_str = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return return_str
def simulate_one_client(input_args): exp_uid,participant_uid,total_pulls,true_means,assert_200 = input_args getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print " Participant {} had {} total pulls: ".format(participant_uid, t) # test POST getQuery # # return a widget 1/5 of the time (normally, use HTML) widget = random.choice([True] + 4*[False]) getQuery_args_dict = {'args': {'participant_uid': participant_uid, 'widget': widget}, 'exp_uid': exp_uid} query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times.append(dt) if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] targets = query_dict['target_indices'] left = targets[0]['target'] right = targets[1]['target'] # sleep for a bit to simulate response time ts = test_utils.response_delay() # print left reward_left = true_means[left['target_id']] + numpy.random.randn()*0.5 reward_right = true_means[right['target_id']] + numpy.random.randn()*0.5 if reward_left > reward_right: target_winner = left else: target_winner = right response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {'args': {'query_uid': query_uid, 'response_time': response_time, 'target_winner': target_winner["target_id"]}, 'exp_uid': exp_uid} processAnswer_json_response, dt = test_utils.processAnswer(processAnswer_args_dict) processAnswer_times += [dt] r = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return r
def simulate_one_client(input_args): exp_uid,participant_uid,total_pulls,true_means,assert_200 = input_args verbose = False test_utils.response_delay() getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "Participant {} has had {} pulls".format(participant_uid, t) # test POST getQuery # getQuery_args_dict = {} getQuery_args_dict['exp_uid'] = exp_uid getQuery_args_dict['args'] = {} # getQuery_args_dict['args']['participant_uid'] = numpy.random.choice(participants) getQuery_args_dict['args']['participant_uid'] = participant_uid query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] query_uid = query_dict['query_uid'] targets = query_dict['target_indices'] target_index = targets[0]['target']['target_id'] # generate simulated reward # # sleep for a bit to simulate response time ts = test_utils.response_delay() target_reward = 1.+sum(numpy.random.rand(2)<true_means[target_index]) # in {1,2,3} # target_reward = numpy.random.choice(labels)['reward'] response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid target_reward = str(target_reward) if np.random.randint(2) == 0 else target_reward processAnswer_args_dict["args"]['target_reward'] = str(target_reward) processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer(processAnswer_args_dict) processAnswer_times += [dt] t = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return t
def simulate_one_client(input_args): exp_uid, participant_uid, total_pulls, assert_200 = input_args getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "Participant {1} has taken {0} pulls".format(t, participant_uid) # test POST getQuery # widget = random.choice([True] + 4 * [False]) widget = True getQuery_args_dict = { 'args': { 'participant_uid': participant_uid, 'widget': widget }, 'exp_uid': exp_uid } query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] ts = test_utils.response_delay() # sleep for a bit to simulate response time response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer( processAnswer_args_dict) processAnswer_times.append(dt) r = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return r
def simulate_one_client(input_args): exp_uid, participant_uid, total_pulls, true_weights, assert_200 = input_args getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "participant {} had {} pulls".format(participant_uid, t) # test POST getQuery # widget = True getQuery_args_dict = {'args': {'participant_uid': participant_uid, 'widget': widget}, 'exp_uid': exp_uid} query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] target = query_dict['target_indices'] x = numpy.array(eval(target['primary_description'])) # generate simulated reward # # sleep for a bit to simulate response time ts = test_utils.response_delay() target_label = numpy.sign(numpy.dot(x,true_weights)) response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid processAnswer_args_dict["args"]["target_label"] = target_label processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer(processAnswer_args_dict) processAnswer_times += [dt] return_str = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return return_str
def simulate_one_client(input_args): exp_uid, participant_uid, total_pulls, assert_200 = input_args getQuery_times = [] processAnswer_times = [] for t in range(total_pulls): print "Participant {1} has taken {0} pulls".format(t, participant_uid) # test POST getQuery # widget = random.choice([True] + 4*[False]) widget = True getQuery_args_dict = {'args': {'participant_uid': participant_uid, 'widget': widget}, 'exp_uid': exp_uid} query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] ts = test_utils.response_delay() # sleep for a bit to simulate response time response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer(processAnswer_args_dict) processAnswer_times.append(dt) r = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return r
def simulate_one_client(input_args): """ method to simulate client behavior :param input_args: list(object): the input arguments :return list of statistics regarding client simulation """ exp_uid, participant_uid, assert_200, pretest_count, training_count, posttest_count, guard_gap = input_args # count for instruction questions introduction_instructions_count = 2 pretest_instructions_count = 1 training_instructions_count = 1 posttest_instructions_count = 1 # total number of questions total_pulls = pretest_count + training_count + posttest_count guard_count = total_pulls / guard_gap total_pulls = total_pulls + guard_count + introduction_instructions_count + pretest_instructions_count + \ training_instructions_count + posttest_instructions_count getQuery_times = [] processAnswer_times = [] start_time = time.time() for t in range(total_pulls): end_time = time.time() print "Participant {1} has taken {0} pulls, time required {2}".format( t, participant_uid, end_time - start_time) start_time = time.time() # test POST getQuery # widget = True getQuery_args_dict = { 'args': { 'participant_uid': participant_uid, 'widget': widget }, 'exp_uid': exp_uid } query_dict, dt = test_utils.getQuery(getQuery_args_dict) getQuery_times += [dt] if widget: query_dict = query_dict['args'] query_uid = query_dict['query_uid'] same = query_dict['same'] ques_type = query_dict['ques_type'] ques_count = query_dict['ques_count'] total_ques_count = query_dict['total_ques_count'] molecules = query_dict['target_indices'] # print targets ''' for target in molecules: print 'here', target['label'] if target['label'] == 'mol1': index_left = target['target_id'] elif target['label'] == 'mol2': index_right = target['target_id'] ''' index_left = molecules[0] index_right = molecules[1] ts = test_utils.response_delay(mean=0.5, std=1.) # sleep for a bit to simulate response time if ques_type == 'instruction': target_winner = 1 else: target_winner = same # just returns the correct answer response_time = time.time() - ts # test POST processAnswer processAnswer_args_dict = {} processAnswer_args_dict["exp_uid"] = exp_uid processAnswer_args_dict["args"] = {} processAnswer_args_dict["args"]["query_uid"] = query_uid processAnswer_args_dict["args"]["target_winner"] = target_winner processAnswer_args_dict["args"][ "participant_uid"] = exp_uid + '_' + participant_uid # next appends exp_uid # with participant_uid processAnswer_args_dict["args"]['response_time'] = response_time processAnswer_json_response, dt = test_utils.processAnswer( processAnswer_args_dict) processAnswer_times.append(dt) r = test_utils.format_times(getQuery_times, processAnswer_times, total_pulls, participant_uid) return r