Beispiel #1
0
                ##start = pygame.time.get_ticks()
                p.setData(int(15))
                pygame.time.delay(1)
                p.setData(int(0))
                pygame.display.update(rectMultiple)
                p.setData(int(17))
                pygame.time.delay(1)
                p.setData(int(0))
                bubble_display_start = pygame.time.get_ticks()
                ##tools.debug_time("Forced_stim_onset",start)
                el.trialmetadata("forced_fix_onset", bubble_display_start)

                # wait until first bubble is fixated before starting forced_fix_time
                if subtrial_num == 1:
                    tools.sacc_detection(el, chosen_bubble)

                #choice
                #how many bubbles should be displayed
                num_of_bubbles = int(float(subtrial[2]))
                num_of_bubbles = 1
                #tools.debug_time("pre_stim_gen",start)
                #load surface for choosing next fixation location
                used_bubble, more_bubbles = trial.get_image(
                    subtrial_num, surf, bubble_image, all_bubbles,
                    loaded_bubbles, remaining_bubbles, chosen_bubble,
                    num_of_bubbles, dist_mat)
                #tools.debug_time("post_stim_gen",start)

                #keep displaying choosen bubble until disp_time is over
                disp_time = float(subtrial[3])
Beispiel #2
0
def training(surf, el, memory_image, fix_cross):
    #training parameter
    rectXY = surf.get_rect()
    rectXY = (rectXY[2], rectXY[3])
    training_bubble_num = [1, 2, 3, 4, 5]

    training_images = os.listdir(path_to_fixdur_files +
                                 'stimuli/training/single_bubbles/')
    bubble_image = training_images[0]
    trial_num = 1000

    for bubble_image in training_images:

        #start with sequential trial
        # load all bubbles for chosen image
        remaining_bubbles = os.listdir(path_to_fixdur_files +
                                       'stimuli/training/single_bubbles/' +
                                       bubble_image)
        all_bubbles = []
        loaded_bubbles = []
        for bubble in remaining_bubbles:
            loaded_bubbles.append(
                pygame.image.load(path_to_fixdur_files +
                                  'stimuli/training/single_bubbles/' +
                                  bubble_image + '/' + bubble).convert_alpha())
            all_bubbles.append([
                int(bubble.split('_', 1)[1].split('_')[0]),
                int(bubble.split('_', 1)[1].split('_')[1].split('.')[0])
            ])
        remaining_bubbles = list(all_bubbles)
        # load distance mat for chosen image
        dist_mat = np.load(path_to_fixdur_code + 'distances/' + bubble_image +
                           '.npy')
        chosen_bubble = 'No bubble'
        used_bubbles = []

        #wait for next trial
        surf.fill((128, 128, 128))
        pygame.draw.circle(surf, (50, 205, 50),
                           (int(rectXY[0] / 2), int(rectXY[1] / 2)), 10, 5)
        pygame.display.update()
        key = tools.wait_for_key()
        if (key.key == 27):
            pygame.quit()
            sys.exit()

        #drift correction
        surf.fill((128, 128, 128))
        surf.blit(fix_cross,
                  (rectXY[0] / 2 - (np.array(fix_cross.get_size()) / 2)[0],
                   rectXY[1] / 2 - (np.array(fix_cross.get_size()) / 2)[1]))
        pygame.display.update()
        #el.drift((rectXY[0]/2-(np.array(fix_cross.get_size())/2)[0],rectXY[1]/2-(np.array(fix_cross.get_size())/2)[1]))
        el.drift()
        #keep displying fixation cross so it"s still present after ne calibration
        surf.fill((128, 128, 128))
        surf.blit(fix_cross,
                  (rectXY[0] / 2 - (np.array(fix_cross.get_size()) / 2)[0],
                   rectXY[1] / 2 - (np.array(fix_cross.get_size()) / 2)[1]))
        pygame.display.update()
        #start trial
        el.start_trial()
        delay_time = np.random.normal(500, 100)
        pygame.time.delay(int(delay_time))

        el.trial(trial_num)
        el.trialmetadata('BUBBLE_IMAGE', bubble_image)

        for subtrial in range(20):

            #choose starting bubble for first trial randomly
            if subtrial == 0:
                chosen_bubble = [random.choice(remaining_bubbles)]
                remaining_bubbles.remove(chosen_bubble[0])
                rectMultiple = [
                    pygame.Rect((rectXY[0] / 2 -
                                 (np.array(fix_cross.get_size()) / 2)[0],
                                 rectXY[1] / 2 -
                                 (np.array(fix_cross.get_size()) / 2)[1]) +
                                fix_cross.get_size()),
                    pygame.Rect(chosen_bubble[0][0] + 320,
                                chosen_bubble[0][1] + 60, 154, 154)
                ]  # in the beginning we need to actually show a bubble, not hide them

            #start displaying chosen bubble
            surf.fill((128, 128, 128))
            chosen_bubble_image = loaded_bubbles[all_bubbles.index(
                chosen_bubble[0])]
            surf.blit(chosen_bubble_image,
                      (chosen_bubble[0][0] + 320, chosen_bubble[0][1] + 60))
            pygame.display.update(rectMultiple)
            bubble_display_start = pygame.time.get_ticks()

            # wait until first bubble is fixated before starting forced_fix_time
            if subtrial == 0:
                tools.sacc_detection(el, chosen_bubble)

            #choice
            #how many bubbles should be displayed
            num_of_bubbles = random.choice(training_bubble_num)
            #load surface for choosing next fixation location
            used_bubble, more_bubbles = get_image(surf, bubble_image,
                                                  all_bubbles, loaded_bubbles,
                                                  remaining_bubbles,
                                                  chosen_bubble,
                                                  num_of_bubbles, dist_mat)
            # update used bubbles for whole trial
            for bubble in used_bubble:
                used_bubbles.append(bubble)
            # reset bubbles
            remaining_bubbles = list(all_bubbles)
            for bubble in remaining_bubbles:
                if bubble in used_bubbles:
                    remaining_bubbles.remove(bubble)

            #keep displaying choosen bubble until disp_time is over
            disp_time = scipy.random.exponential(300, 1)
            bubble_display_time = 0
            while bubble_display_time < disp_time:
                pygame.time.delay(1)
                bubble_display_time = pygame.time.get_ticks(
                ) - bubble_display_start

            rects1 = [
                pygame.Rect(chosen_bubble[0][0] + 320,
                            chosen_bubble[0][1] + 60, 154, 154)
            ]
            rectMultiple = []
            for l in range(len(used_bubble)):
                rectMultiple.append(
                    pygame.Rect(used_bubble[l][0] + 320,
                                used_bubble[l][1] + 60, 154, 154))

            # draw choice window onto the screen until saccade is over
            pygame.display.update(
                rects1 + rectMultiple
            )  # blit the currently fixated bubbles and all to be displayed bubbles.
            #fix_x,fix_y = tools.wait_for_saccade(el)
            chosen_bubble = tools.sacc_detection(el, used_bubble)
            #chosen_bubble = tools.get_fixated_bubble(used_bubble,fix_x,fix_y)
            chosen_bubble = [chosen_bubble]

            rectMultiple = []
            for l in range(len(used_bubble)):
                if used_bubble[l][0] != chosen_bubble[0]:
                    rectMultiple.append(
                        pygame.Rect(used_bubble[l][0] + 320,
                                    used_bubble[l][1] + 60, 154, 154))
            #metainfos for tracker
            el.trialmetadata("DISPLAYED_BUBBLES", used_bubble)
            el.trialmetadata("CHOSEN_BUBBLE", chosen_bubble)
            el.trialmetadata('BUBBLE_DISPLAY_TIME', disp_time)

        #memory task
        #left_bubble, right bubble, correct = tools.memory_task(all_bubbles,loaded_bubbles,bubble_image,memory_image.copy(),surf)
        surf.fill((128, 128, 128))
        memory_res = tools.memory_task(all_bubbles, loaded_bubbles,
                                       bubble_image, memory_image.copy(), surf)

        #wait for next trial
        surf.fill((128, 128, 128))
        pygame.draw.circle(surf, (50, 205, 50),
                           (int(rectXY[0] / 2), int(rectXY[1] / 2)), 10, 5)
        pygame.display.update()
        key = tools.wait_for_key()
        if (key.key == 27):
            pygame.quit()
            sys.exit()

        #drift correction
        surf.fill((128, 128, 128))
        surf.blit(fix_cross,
                  (rectXY[0] / 2 - (np.array(fix_cross.get_size()) / 2)[0],
                   rectXY[1] / 2 - (np.array(fix_cross.get_size()) / 2)[1]))
        pygame.display.update()
        el.drift()
        el.start_trial()
        delay_time = np.random.normal(500, 100)
        pygame.time.delay(int(delay_time))

        # all bubble displayed trial
        #load bubbles for memory task
        remaining_bubbles = os.listdir(path_to_fixdur_files +
                                       'stimuli/training/single_bubbles/' +
                                       bubble_image)
        all_bubbles = []
        loaded_bubbles = []
        for bubble in remaining_bubbles:
            loaded_bubbles.append(
                pygame.image.load(path_to_fixdur_files +
                                  'stimuli/training/single_bubbles/' +
                                  bubble_image + '/' + bubble).convert_alpha())
            all_bubbles.append([
                int(bubble.split('_', 1)[1].split('_')[0]),
                int(bubble.split('_', 1)[1].split('_')[1].split('.')[0])
            ])
        remaining_bubbles = list(all_bubbles)
        #load and show stimulus
        img_num = random.randrange(0, 10)
        stim = pygame.image.load(path_to_fixdur_files +
                                 'stimuli/multi_bubble_images/' +
                                 bubble_image + '/' + bubble_image + '_' +
                                 str(img_num) + '.png').convert()
        surf.blit(stim, (0, 0))
        pygame.display.update()
        pygame.time.delay(6000)

        #memory task
        #left_bubble, right bubble, correct = tools.memory_task(all_bubbles,loaded_bubbles,bubble_image,memory_image.copy(),surf)
        surf.fill((128, 128, 128))
        memory_res = tools.memory_task(all_bubbles, loaded_bubbles,
                                       bubble_image, memory_image.copy(), surf)
        el.end_trial()