def display_responder_outcome(outcome_label, response_input): # get the response response = response_input.data #randomly select a proposal proposal_inputs = Blank.query.filter(Blank.var == 'Proposal', Blank.data != None).all() if proposal_inputs: # randomly choose a proposal offer = random.choice(proposal_inputs).data else: # no proposals are available # e.g. if this is the first participant offer = randint(0, POT) proposal = POT - offer, offer # compute the payoff accept = response <= proposal[1] payoff = proposal if accept else (0, 0) # record results as embedded data outcome_label.page.embedded = [ Embedded('Proposal', proposal[1]), Embedded('Accept', int(accept)), Embedded('ProposerPayoff', payoff[0]), Embedded('ResponderPayoff', payoff[1]) ] # describe the outcome of the round outcome_label.label = f'''
def _assess_response(question, correct, intuitive): """ Add embedded data to the CRT question's page indicating if the answer was 1) correct, 2) intuitive. """ page = question.page prev_assessment = [ e for e in page.embedded if e.var in [question.var + 'Correct', question.var + 'Intuitive'] ] [page.embedded.remove(e) for e in prev_assessment] data = question.data data_null = data in (None, '') correct_answer = 0 if data_null else int(correct == type(correct)(data)) intuitive_answer = (0 if data_null else int( intuitive == type(intuitive)(data))) current_user.g['CRT_Total'] += 1 current_user.g['CRT_Correct'] += correct_answer current_user.g['CRT_Intuitive'] += intuitive_answer page.embedded += [ Embedded(question.var + 'Correct', None if data_null else correct_answer, data_rows=-1), Embedded(question.var + 'Intuitive', None if data_null else intuitive_answer, data_rows=-1) ]
def fcast(origin=None): questions = texts.fcast_questions.copy() n_bins_list = [choice(N_BINS) for q in questions] fcast_pages = [ Page( gen_dashboard( '/fcast/', label=label, n_bins=n_bins, var='Forecast', record_order=True ), timer='ForecastTime', embedded=[Embedded('Variable', var), Embedded('NBins', n_bins)] ) for (var, label), n_bins in zip(questions, n_bins_list) ] shuffle(fcast_pages) for i, page in enumerate(fcast_pages): page.questions.insert( 0, Label( '<p><b>Question {} of {}</b></p>'.format(i+1, len(fcast_pages)) ) ) return Branch( *fcast_pages, Page( Label(texts.completion), terminal=True ) )
def _record_score(question, score): """ Record the Berlin score as embedded data and make the score accessible through `current_user.g['BerlinScore']`. """ question.page.embedded = [Embedded('BerlinScore', score, data_rows=-1)] current_user.g['BerlinScore'] = score
def rate_articles(start_branch): def select_articles(part): if part.meta['Condition'] == 'random': return random.sample(ARTICLES, k=N_ARTICLES) df = pd.DataFrame(part.get_data()) df['Ideal'] = df['Condition'] == 'ideal' df = df.append((len(ARTICLES) - 1) * [df], ignore_index=True) df['ArticleName'] = ARTICLE_NAMES X = df[cols] y_pred = reg.predict(X) article_ratings = list(zip(ARTICLES, y_pred)) article_ratings.sort(key=lambda x: x[1], reverse=True) return [article for article, rating in article_ratings][:N_ARTICLES] # def gen_rate_articles_page(name, headline, url): # page = Page( # RangeInput( # ''' # <p>From 0 (not at all) to 10 (very much), how much do you think you'd enjoy reading the following article?</p> # <p><b>{}</b></p> # '''.format(headline), # min=0, max=10, required=True, var='Enjoy' # ) # ) # if start_branch.part.meta['Ideal']: # page.questions.insert( # 0, # Label( # ''' # First, think of the sort of news an ideal version of yourself would enjoy reading. # ''' # ) # ) # return page def gen_rate_articles_page(name, headline, url): return Page( RangeInput(''' <p>Take a few minutes to read <a href="{}" target="_blank">this article</a>.</p> {} <p>From 0 (not at all ) to 10 (very much), how much did you enjoy reading that article?</p> '''.format(url, headline), min=0, max=10, required=True, var='Enjoy')) # articles = random.sample(ARTICLES, k=N_ARTICLES) articles = select_articles(start_branch.part) start_branch.embedded.append( Embedded('ArticleName', [name for name, headline, url in articles])) return Branch(*[gen_rate_articles_page(*article) for article in articles], navigate=end)
def make_first_estimate_questions(): """ :return: first estimate questions :rtype: list of hemlock.Blank """ fcast_keys = fcast_selector.next() fcast_keys = (list(fcast_keys) if isinstance(fcast_keys, tuple) else [fcast_keys]) shuffle(fcast_keys) current_user.embedded.append(Embedded('Forecast', fcast_keys)) return [(key, make_fcast_questions(key, context, first_estimate=True)) for key in fcast_keys]
def _record_score(page, item_bank): # records the aggregate score for each personality trait def compute_scores(): scores = {} questions = [q for q in page.questions if q.var and (q.data != '')] for q in questions: trait = item_bank[q.var[len('Big5'):]][0] if trait not in scores: scores[trait] = [] scores[trait].append(q.data) return { trait_abbreviations[trait]: sum(score) / len(score) for trait, score in scores.items() } scores = compute_scores() page.embedded = [ Embedded(trait, score, data_rows=-1) for trait, score in scores.items() ] g = page.part.g if 'Big5' not in g: g['Big5'] = {} g['Big5'].update(scores)
def _immigration_status(citizen_q, residence_q): immigrant = int(citizen_q.data != residence_q.data) current_user.embedded.append(Embedded('Immigrant', immigrant, data_rows=-1))
def _record_male(gender_q): current_user.embedded = [ e for e in current_user.embedded if e.var != 'Male' ] current_user.embedded.append( Embedded('Male', int(gender_q.data == 'Male'), data_rows=-1))