Beispiel #1
0
    def generate_html_string(self):

        pool_sequence_string = '[\n'
        for block in self.block_sequence:
            block_string = block.get_string()
            pool_sequence_string += block_string
            pool_sequence_string += ',\n'
        pool_sequence_string += 2 * INDENT_CHARACTER + ']'

        # Load template
        html_string = utils.load_text(TEMPLATE_LOCATION)
        html_string = html_string.replace("__INSERT_POOL_SEQUENCE__",
                                          pool_sequence_string)

        # Inject instructions HTML
        html_string = html_string.replace("__INSERT_INSTRUCTIONS_HTML__",
                                          self.instructions_string)

        # Load ptap/public/common/*.js files into a string
        javascript_common = utils.make_javascript_common_injection_string()

        # Load the task definition into a string
        javascript_task = utils.load_text(TASK_LOCATION)

        # Join the strings
        javascript_injection = '\n\n\n\n'.join(
            [javascript_common, javascript_task])
        html_string = html_string.replace('__INJECT_JAVASCRIPT_HERE__',
                                          javascript_injection)
        return html_string
Beispiel #2
0
def write_html(block_sequence, url_checker:URLChecker = None):
    # Performs checks on validity

    if url_checker is not None:
        all_urls = []
        for block in block_sequence:
            cur_urls = [block['image_url_prefix'] + u for u in block['image_url_suffix_seq']]
            all_urls.extend(cur_urls)
        url_checker.check(all_urls)

    javascript_block_sequence = convert_object_to_javascript_string(block_sequence)
    if False:

        bool2jsbool = lambda b:'true' if b else 'false'

        INDENT_CHARACTER = '    '

        javascript_block_sequence = '[\n'

        for block_info in block_sequence:
            javascript_expression = 3 * INDENT_CHARACTER + '{\n'
            for k in block_info:
                cur_val = block_info[k]
                if isinstance(cur_val, str):
                    val = f'"{cur_val}"'
                elif isinstance(cur_val, bool):
                    val = bool2jsbool(cur_val)
                elif isinstance(cur_val, tuple):
                    tup = cur_val
                    assert len(tup) == 2
                    assert tup[0] == 'raw'
                    val = cur_val
                else:
                    val = cur_val
                javascript_expression+=(4 * INDENT_CHARACTER + f'"{k}":{val},\n')
            javascript_expression +=3 * INDENT_CHARACTER + '},\n'
            javascript_block_sequence+=javascript_expression

        javascript_block_sequence += '\n]'

    # Load template
    html_string = utils.load_text(TEMPLATE_LOCATION)

    # Inject block sequence
    html_string = html_string.replace('__INSERT_BLOCK_INFO_SEQUENCE_HERE__', javascript_block_sequence)

    # Load ptap/public/common/*.js files into a string
    javascript_common = utils.make_javascript_common_injection_string()

    # Load the task definition into a string
    javascript_task = utils.load_text(TASK_LOCATION)

    # Join the strings
    javascript_injection = '\n\n\n\n'.join([javascript_common, javascript_task])
    html_string = html_string.replace('__INJECT_JAVASCRIPT_HERE__', javascript_injection)
    return html_string
Beispiel #3
0
def create_redirect_page(
        possible_urls:list,
        check_urls: bool,
):

    assert isinstance(possible_urls, list)
    if check_urls:
        passed = True
        for url in tqdm(possible_urls, desc = 'checking redirect URLs'):
            if not utils.check_url_exists(url, raise_exception=False):
                print(f'Could not find {url}')
                passed = False

        if not passed:
            print('Could not verify all URLs')
            raise Exception

    # Encode as strings
    url_array_string = '[\n'
    for url in possible_urls:
        url_array_string+=('\"' + url + '\"' + ',\n')
    url_array_string +=']\n'

    base_string = utils.load_text(os.path.join(loc, 'choose_url_randomly_template.html'))
    html_string = base_string.replace('__INSERT_URLS_ARRAY_HERE__', url_array_string)
    return html_string
def _generate_same_different_html(
    frame_info_sequence,
    ground_truth_is_same_sequence,
    give_reinforcement_sequence,
    reward_duration_msec,
    punish_duration_msec,
    choice_duration_msec,
    minimal_choice_duration_msec,
    post_stimulus_delay_duration_msec,
    intertrial_delay_duration_msec,
    inter_choice_presentation_delay_msec,
    pre_choice_lockout_delay_duration_msec,
    minimal_gt_performance_for_bonus,
    pseudo_usd_per_gt_correct,
):
    """
    frame_info_sequence, // [{'stimulus_frame_url_sequence':[], 'stimulus_frame_duration_msec_sequence':[]}]
    """

    is_different_url = 'https://s3.amazonaws.com/samedifferentbehavior/task_assets/is_different_choice_image.png'
    is_same_url = 'https://s3.amazonaws.com/samedifferentbehavior/task_assets/is_same_choice_image.png'
    """
    Left choice is always "same"
    Right choice is always "different"
    """
    choice0_image_url_sequence = [is_same_url for _ in frame_info_sequence]
    choice1_image_url_sequence = [
        is_different_url for _ in frame_info_sequence
    ]

    assert len(set(ground_truth_is_same_sequence).difference({-1, 0, 1})) == 0

    rewarded_choice_sequence = []
    ground_truth_correct_choice_sequence = []
    for i, apply_reward in enumerate(give_reinforcement_sequence):
        if apply_reward:
            assert ground_truth_is_same_sequence[i] != -1
            if ground_truth_is_same_sequence[i] == True:
                rewarded_choice_sequence.append(0)
                ground_truth_correct_choice_sequence.append(0)
            elif ground_truth_is_same_sequence[i] == False:
                rewarded_choice_sequence.append(1)
                ground_truth_correct_choice_sequence.append(1)
            else:
                raise Exception
        else:
            rewarded_choice_sequence.append(-1)

    query_string = ''

    all_urls = set()
    for frame_info in frame_info_sequence:
        frame_urls = frame_info['stimulus_frame_url_sequence']
        [all_urls.add(url) for url in frame_urls]

    for c_url in choice0_image_url_sequence + choice1_image_url_sequence:
        all_urls.add(c_url)
    all_urls = list(all_urls)

    image_url_prefix = os.path.commonprefix(all_urls)

    frame_info_sequence_suffix_form = []
    for i_trial, trial_frame_info in enumerate(frame_info_sequence):
        frame_urls = trial_frame_info['stimulus_frame_url_sequence']
        frame_durations = trial_frame_info[
            'stimulus_frame_duration_msec_sequence']

        assert len(frame_urls) == len(frame_durations)
        for d in frame_durations:
            assert d > (1 / 60 * 1000)

        cur_frame_info_suffix_form = {
            'stimulus_frame_url_suffix_sequence':
            [url.split(image_url_prefix)[-1] for url in frame_urls],
            'stimulus_frame_duration_msec_sequence':
            list(frame_durations),
        }
        frame_info_sequence_suffix_form.append(
            dict(cur_frame_info_suffix_form))

    choice0_url_suffix_sequence = [
        url.split(image_url_prefix)[-1] for url in choice0_image_url_sequence
    ]
    choice1_url_suffix_sequence = [
        url.split(image_url_prefix)[-1] for url in choice1_image_url_sequence
    ]

    assert isinstance(image_url_prefix, str)
    sequence_vars = [
        frame_info_sequence_suffix_form,
        choice0_url_suffix_sequence,
        choice1_url_suffix_sequence,
        rewarded_choice_sequence,
        ground_truth_correct_choice_sequence,
    ]

    lengths = [len(var) for var in sequence_vars]
    assert len(set(lengths)) == 1, lengths
    ntrials = lengths[0]
    assert ntrials > 0

    positive_msec_scalars = [
        reward_duration_msec,
        punish_duration_msec,
        choice_duration_msec,
        minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec,
    ]

    for scalar in positive_msec_scalars:
        assert scalar >= 0

    max_safety = 0.02
    assert 0 <= minimal_gt_performance_for_bonus < 1
    assert 0 <= pseudo_usd_per_gt_correct < max_safety
    assert isinstance(pseudo_usd_per_gt_correct, float)
    assert isinstance(query_string, str)

    trial_info = dict(
        image_url_prefix=image_url_prefix,
        frame_info_sequence=frame_info_sequence_suffix_form,
        ground_truth_correct_choice_sequence=
        ground_truth_correct_choice_sequence,
        choice0_url_suffix_sequence=choice0_url_suffix_sequence,
        choice1_url_suffix_sequence=choice1_url_suffix_sequence,
        rewarded_choice_sequence=rewarded_choice_sequence,
        reward_duration_msec=reward_duration_msec,
        punish_duration_msec=punish_duration_msec,
        choice_duration_msec=choice_duration_msec,
        minimal_choice_duration_msec=minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec=post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec=intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec=
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec=
        pre_choice_lockout_delay_duration_msec,
        minimal_gt_performance_for_bonus=minimal_gt_performance_for_bonus,
        pseudo_usd_per_gt_correct=pseudo_usd_per_gt_correct,
        query_string=query_string,
    )
    """
    Generate HTML
    """

    javascript_expression = 3 * INDENT_CHARACTER + '{\n'
    for k in trial_info:
        cur_val = trial_info[k]
        if isinstance(cur_val, str):
            val = f'"{cur_val}"'
        elif isinstance(cur_val, bool):
            val = bool2jsbool(cur_val)
        elif isinstance(cur_val, tuple):
            tup = cur_val
            assert len(tup) == 2
            assert tup[0] == 'raw'
            val = cur_val
        else:
            val = cur_val
        javascript_expression += (4 * INDENT_CHARACTER + f'"{k}":{val},\n')
    #javascript_expression += (4*INDENT_CHARACTER + f'"trial_pool":{self.trial_pool.function_string},\n')
    javascript_expression += 3 * INDENT_CHARACTER + '}'

    # Load template
    html_string = utils.load_text(TEMPLATE_LOCATION)
    html_string = html_string.replace("__INSERT_TRIAL_SEQUENCE_HERE__",
                                      javascript_expression)

    # Inject instructions HTML
    html_string = html_string.replace("__INSERT_INSTRUCTIONS_HTML__",
                                      INSTRUCTIONS_STRING)

    # Load ptap/public/common/*.js files into a string
    javascript_common = utils.make_javascript_common_injection_string()

    # Load the task definition into a string
    javascript_task = utils.load_text(TASK_LOCATION)

    # Join the strings
    javascript_injection = '\n\n\n\n'.join(
        [javascript_common, javascript_task])
    html_string = html_string.replace('__INJECT_JAVASCRIPT_HERE__',
                                      javascript_injection)

    return html_string
def _generate_deterministic_mts_trial_sequence_htmls(
    stimulus_image_url_sequence,
    choice0_url_sequence,
    choice1_url_sequence,
    give_feedback_sequence,
    ground_truth_choice_sequence,
    stimulus_duration_msec,
    reward_duration_msec,
    punish_duration_msec,
    choice_duration_msec,
    minimal_choice_duration_msec,
    post_stimulus_delay_duration_msec,
    intertrial_delay_duration_msec,
    inter_choice_presentation_delay_msec,
    pre_choice_lockout_delay_duration_msec,
    min_gt_performance_for_bonus,
    max_bonus_usd,
    block_name,
    query_string,
):
    """
    Core function for getting behavioral data on a series of 2AFC trials from a human subject.

    image_url_prefix, String
    image_url_suffix_sequence, [t]
    choice0_url_suffix_sequence,  [t]
    choice1_url_suffix_sequence,  [t]
    rewarded_choice_sequence, [t]. If an entry is -1, no choice is given a reward.
    ground_truth_choice_sequence, [t]. If an entry is -1, the choice does not affect the hidden performance tracker.
    stimulus_duration_msec, [t]
    reward_duration_msec, [t]. If an entry is zero, no reward feedback is given.
    punish_duration_msec, [t]. If an entry is zero, no punish feedback is given.
    choice_duration_msec, [t]. Max choice time
    minimal_choice_duration_msec, [t]. Imposes a delay until this much time has elapsed. Triggers a GUI element showing the remaining time a choice is made.
    post_stimulus_delay_duration_msec, [t]. The amount of time before the choices pop up.
    usd_upon_block_completion, Float
    size, () in pixels
    block_name, String
    checkpoint_key: String which is used as a key for LocalStorage

    Returns {'coords':coords, 'data_vars':data_vars, 'meta':meta}

    """
    sequence_vars = [
        stimulus_image_url_sequence,
        choice0_url_sequence,
        choice1_url_sequence,
        give_feedback_sequence,
        ground_truth_choice_sequence,
    ]

    lengths = [len(var) for var in sequence_vars]
    assert len(set(lengths)) == 1, lengths
    ntrials = lengths[0]
    assert ntrials > 0

    positive_msec_scalars = [
        stimulus_duration_msec,
        reward_duration_msec,
        punish_duration_msec,
        choice_duration_msec,
        minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec,
    ]

    assert stimulus_duration_msec > 1 / 60 * 1000

    for scalar in positive_msec_scalars:
        assert scalar >= 0

    max_safety = 0.8
    assert 0 <= min_gt_performance_for_bonus < 1
    assert 0 <= max_bonus_usd < max_safety, max_bonus_usd
    assert isinstance(max_bonus_usd, float)
    assert isinstance(block_name, str)
    assert isinstance(block_name, str)
    assert isinstance(query_string, str)

    block_info = dict(
        stimulus_url_sequence=stimulus_image_url_sequence,
        choice0_url_sequence=choice0_url_sequence,
        choice1_url_sequence=choice1_url_sequence,
        give_feedback_sequence=[
            bool2jsbool(v) for v in give_feedback_sequence
        ],
        ground_truth_choice_sequence=ground_truth_choice_sequence,
        stimulus_duration_msec=stimulus_duration_msec,
        reward_duration_msec=reward_duration_msec,
        punish_duration_msec=punish_duration_msec,
        choice_duration_msec=choice_duration_msec,
        minimal_choice_duration_msec=minimal_choice_duration_msec,
        post_stimulus_delay_duration_msec=post_stimulus_delay_duration_msec,
        intertrial_delay_duration_msec=intertrial_delay_duration_msec,
        inter_choice_presentation_delay_msec=
        inter_choice_presentation_delay_msec,
        pre_choice_lockout_delay_duration_msec=
        pre_choice_lockout_delay_duration_msec,
        min_gt_performance_for_bonus=min_gt_performance_for_bonus,
        max_bonus_usd=max_bonus_usd,
        block_name=block_name,
        query_string=query_string,
    )
    """
    Generate HTML
    """

    javascript_expression = 3 * INDENT_CHARACTER + '{\n'
    for k in block_info:
        cur_val = block_info[k]
        if isinstance(cur_val, str):
            val = f'"{cur_val}"'
        elif isinstance(cur_val, bool):
            val = bool2jsbool(cur_val)
        elif isinstance(cur_val, tuple):
            tup = cur_val
            assert len(tup) == 2
            assert tup[0] == 'raw'
            val = cur_val
        else:
            val = cur_val
        javascript_expression += (4 * INDENT_CHARACTER + f'"{k}":{val},\n')
    #javascript_expression += (4*INDENT_CHARACTER + f'"trial_pool":{self.trial_pool.function_string},\n')
    javascript_expression += 3 * INDENT_CHARACTER + '}'

    # Load template
    html_string = utils.load_text(TEMPLATE_LOCATION)
    html_string = html_string.replace("__INSERT_BLOCK_SEQUENCE_HERE__",
                                      '[' + javascript_expression + ']')

    # Inject instructions HTML
    html_string = html_string.replace("__INSERT_INSTRUCTIONS_HTML__",
                                      INSTRUCTIONS_STRING)

    # Load ptap/public/common/*.js files into a string
    javascript_common = utils.make_javascript_common_injection_string()

    # Load the task definition into a string
    javascript_task = utils.load_text(TASK_LOCATION)

    # Join the strings
    javascript_injection = '\n\n\n\n'.join(
        [javascript_common, javascript_task])
    html_string = html_string.replace('__INJECT_JAVASCRIPT_HERE__',
                                      javascript_injection)

    return html_string