from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random import json import intervention.bcra as bcra import numpy import pandas as pd import time class opening(Page): form_model = 'player' def before_next_page(self): self.player.page_pass_time = time.time() + 10 class instruction(Page): form_model = 'player' def error_message(self, values): if time.time() < self.player.page_pass_time: return "You cannot pass this page yet. Please review the form carefully and wait at least 10 seconds " \ "before you can proceed to the next page." class risk_calc0(Page): form_model = 'player' class part2(Page): form_model = 'player' class risk_calc1(Page): form_model = 'player' form_fields = [ 'risk_calc_q3', 'risk_calc_q4', 'risk_calc_q1', 'risk_calc_q2', 'risk_calc_q5', 'risk_calc_q9' ] def before_next_page(self): self.player.prolific_id = self.participant.label class risk_calc2(Page): form_model = 'player' def get_form_fields(self): if self.player.risk_calc_q5 == 1: if self.player.risk_calc_q9 == 1: return ['risk_calc_q6', 'risk_calc_q7', 'risk_calc_q8', 'risk_calc_q10', 'risk_calc_q11'] else: return ['risk_calc_q6', 'risk_calc_q7', 'risk_calc_q8', 'risk_calc_q11'] else: if self.player.risk_calc_q9 == 1: return ['risk_calc_q8', 'risk_calc_q10', 'risk_calc_q11'] else: return ['risk_calc_q8', 'risk_calc_q11'] def before_next_page(self): # create data as a list dt1 = [self.player.risk_calc_q3, self.player.risk_calc_q3+5, self.player.risk_calc_q6, self.player.risk_calc_q7, self.player.risk_calc_q8, self.player.risk_calc_q10, self.player.risk_calc_q11, self.player.risk_calc_q4] dt2 = [self.player.risk_calc_q3, 90, self.player.risk_calc_q6, self.player.risk_calc_q7, self.player.risk_calc_q8, self.player.risk_calc_q10, self.player.risk_calc_q11, self.player.risk_calc_q4] # create a dataframe with input data df = pd.DataFrame([dt1], columns=['T1', 'T2', 'N_Biop', 'HypPlas', 'AgeMen', 'Age1st', 'N_Rels', 'Race']) df = df.append(pd.Series(dt2, index=['T1', 'T2', 'N_Biop', 'HypPlas', 'AgeMen', 'Age1st', 'N_Rels', 'Race']), ignore_index=True) # change the data to the format to be inputted to bcra functions risk = bcra.absolute_risk(df) self.player.brisk_five = round(risk[0], 2) self.player.brisk_life = round(risk[1], 2) self.player.participant.vars['brisk_five'] = self.player.brisk_five self.player.participant.vars['brisk_life'] = self.player.brisk_life class intervention_q1(Page): form_model = 'player' form_fields = ['confirmation_q1', 'confirmation_q2', 'confirmation_q5'] def error_message(self, values): solutions = dict(confirmation_q1=100, confirmation_q2=22, confirmation_q5=8) if values != solutions: return "One or more answers were incorrect. Please try again" class intervention_q2(Page): form_model = 'player' form_fields = ['confirmation_q3', 'confirmation_q4'] def error_message(self, values): solutions = dict(confirmation_q3=15, confirmation_q4=1) if values != solutions: return "One or more answers were incorrect. Please try again" page_sequence = [opening, instruction, risk_calc0, risk_calc1, risk_calc2, part2, intervention_q1, intervention_q2]