from otree.api import * import numpy as np from time import time import random import roman import json import os doc = """ Test app for generating the choice table. """ VAR_LIST = [800, 400, 200, 100, 10] # Assign treatments. This is based off # of the id_in_group value of the player. # Even ids are not control and odds are. def creating_session(subsession): for p in subsession.get_players(): is_control = p.id_in_group % 2 p.is_control = is_control def get_attr_values(v, n): return np.random.normal(loc=50, scale=np.sqrt(v), size=n).astype(int) def sample_var_list(variances=VAR_LIST): va = random.sample(variances, 5) return va def get_col_names(variances=VAR_LIST): var_dict = {v: roman.toRoman(i) for i, v in enumerate(reversed(sorted(variances)), start=1)} col_names = ["Options/Attributes"] + [var_dict[v] for v in variances] return col_names def get_numbers(labels=["A", "B", "C", "D", "E"], variances=VAR_LIST): """ Modify this function to generate the numbers. :return: The return object is a dict where the key is the label and the value is a list of numbers for that table row. """ num_attrs = 5 num_options = len(labels) # Loop over the variances and generate the values for each one. # This particular syntax is called a list comprehension # it is quite useful for transforming an iterable of one thing into another. # Here we are transforming the list of variances into a list of lists of the # random attribute values. number_lists = [get_attr_values(v, num_options) for v in variances] # As you thought to do, I'm transposing the matrix. # First the concatenate function joins all the lists from number_list into # one long list. # Then I reshape the list into the 5x5 matrix of values. Then transpose it. matrix = np.concatenate(number_lists).reshape((num_attrs, num_options)).T # Like this list comprehension above, the dict comprehension can transform one # iterable into another. Here, I'm changing a list of integers (the range function) # into the return map of {