Example #1
0
    def __init__(self, settings_yaml, texts_yaml):
        """Create the window and stimuli using the settings provided."""
        with open(settings_yaml, 'r') as f:
            exp_info = yaml.load(f)

        self.waits = exp_info.pop('waits')
        self.response_keys = exp_info.pop('response_keys')
        self.survey_url = exp_info.pop('survey_url')

        with open(texts_yaml, 'r') as f:
            self.texts = yaml.load(f)

        self.win = visual.Window(
            fullscr=True,
            units='pix',
            allowGUI=False,
            winType='pyglet',
            color=[.6, .6, .6],
        )

        fixation_point = [0, 100]
        text_kwargs = dict(
            win=self.win,
            pos=fixation_point,
            height=40,
            font='Consolas',
            color='black',
            wrapWidth=int(self.win.size[0] * 0.8),
        )
        self.ready = visual.TextStim(text='READY', **text_kwargs)
        self.question = visual.TextStim(**text_kwargs)
        self.prompt = visual.TextStim(text='?', **text_kwargs)
        self.prompt.setHeight(100)  # increase font size from default

        cues_dir = Path(self.STIM_DIR, 'cues')
        self.cues = load_sounds(cues_dir, include_ext=True)

        mask_kwargs = dict(win=self.win, pos=fixation_point, size=[500, 500])
        self.mask = DynamicMask(**mask_kwargs)

        feedback_dir = Path(self.STIM_DIR, 'feedback')
        self.feedback = {}
        self.feedback[0] = sound.Sound(Path(feedback_dir, 'buzz.wav'))
        self.feedback[1] = sound.Sound(Path(feedback_dir, 'bleep.wav'))

        self.timer = core.Clock()
Example #2
0
    def __init__(self, settings_yaml, texts_yaml):
        with open(settings_yaml, 'r') as f:
            exp_info = yaml.load(f)

        self.waits = exp_info.pop('waits')
        self.response_keys = exp_info.pop('response_keys')
        self.survey_url = exp_info.pop('survey_url')

        with open(texts_yaml, 'r') as f:
            self.texts = yaml.load(f)

        self.win = visual.Window(fullscr=True, units='pix')

        text_kwargs = dict(height=60, font='Consolas', color='black')
        self.fix = visual.TextStim(self.win, text='+', **text_kwargs)
        self.prompt = visual.TextStim(self.win,
                                      text='Yes or No?',
                                      **text_kwargs)

        self.questions = load_sounds(Path(self.STIM_DIR, 'questions'))
        self.cues = load_sounds(Path(self.STIM_DIR, 'cues'))

        size = [400, 400]
        image_kwargs = dict(win=self.win, size=size)
        self.mask = DynamicMask(Path(self.STIM_DIR, 'dynamic_mask'),
                                **image_kwargs)
        self.pics = load_images(Path(self.STIM_DIR, 'pics'), **image_kwargs)
        frame_buffer = 20
        self.frame = visual.Rect(self.win,
                                 width=size[0] + 20,
                                 height=size[1] + 20,
                                 lineColor='black')

        feedback_dir = Path(self.STIM_DIR, 'feedback')
        self.feedback = {}
        self.feedback[0] = sound.Sound(Path(feedback_dir, 'buzz.wav'))
        self.feedback[1] = sound.Sound(Path(feedback_dir, 'bleep.wav'))

        self.timer = core.Clock()
Example #3
0
    def __init__(self, settings_yaml, texts_yaml):
        with open(settings_yaml, 'r') as f:
            exp_info = yaml.load(f)

        self.waits = exp_info.pop('waits')
        self.response_keys = exp_info.pop('response_keys')
        self.survey_url = exp_info.pop('survey_url')

        with open(texts_yaml, 'r') as f:
            self.texts = yaml.load(f)

        self.win = visual.Window(fullscr=True, units='pix', allowGUI=False)

        text_kwargs = dict(win=self.win,
                           height=30,
                           font='Consolas',
                           color='black',
                           wrapWidth=400)
        self.fix = visual.TextStim(text='+', **text_kwargs)
        self.question = visual.TextStim(**text_kwargs)
        self.prompt = visual.TextStim(text='?', **text_kwargs)
        self.word = visual.TextStim(**text_kwargs)

        self.cues = load_sounds(unipath.Path(self.STIM_DIR, 'cues'),
                                include_ext=True)  # key names strawberry_1.wav

        size = [400, 400]
        image_kwargs = dict(win=self.win, size=size)
        self.mask = DynamicMask(**image_kwargs)

        feedback_dir = unipath.Path(self.STIM_DIR, 'feedback')
        self.feedback = {}
        self.feedback[0] = sound.Sound(unipath.Path(feedback_dir, 'buzz.wav'))
        self.feedback[1] = sound.Sound(unipath.Path(feedback_dir, 'bleep.wav'))

        self.timer = core.Clock()
Example #4
0
class Experiment(object):
    STIM_DIR = Path('stimuli')

    def __init__(self, settings_yaml, texts_yaml):
        """Create the window and stimuli using the settings provided."""
        with open(settings_yaml, 'r') as f:
            exp_info = yaml.load(f)

        self.waits = exp_info.pop('waits')
        self.response_keys = exp_info.pop('response_keys')
        self.survey_url = exp_info.pop('survey_url')

        with open(texts_yaml, 'r') as f:
            self.texts = yaml.load(f)

        self.win = visual.Window(
            fullscr=True,
            units='pix',
            allowGUI=False,
            winType='pyglet',
            color=[.6, .6, .6],
        )

        fixation_point = [0, 100]
        text_kwargs = dict(
            win=self.win,
            pos=fixation_point,
            height=40,
            font='Consolas',
            color='black',
            wrapWidth=int(self.win.size[0] * 0.8),
        )
        self.ready = visual.TextStim(text='READY', **text_kwargs)
        self.question = visual.TextStim(**text_kwargs)
        self.prompt = visual.TextStim(text='?', **text_kwargs)
        self.prompt.setHeight(100)  # increase font size from default

        cues_dir = Path(self.STIM_DIR, 'cues')
        self.cues = load_sounds(cues_dir, include_ext=True)

        mask_kwargs = dict(win=self.win, pos=fixation_point, size=[500, 500])
        self.mask = DynamicMask(**mask_kwargs)

        feedback_dir = Path(self.STIM_DIR, 'feedback')
        self.feedback = {}
        self.feedback[0] = sound.Sound(Path(feedback_dir, 'buzz.wav'))
        self.feedback[1] = sound.Sound(Path(feedback_dir, 'bleep.wav'))

        self.timer = core.Clock()

    def run_trial(self, trial):
        """Run a trial using a dict of settings."""
        self.question.setText(trial['question'])

        cue = self.cues[trial['cue_file']]
        cue_dur = cue.getDuration()

        show_mask = (trial['mask_type'] == 'mask')

        ############################
        # BEGIN TRIAL PRESENTATION #
        ############################

        # Show READY prompt
        self.timer.reset()
        self.ready.draw()
        self.win.flip()
        core.wait(self.waits['ready_duration'])

        # Delay between READY offset and question onset
        self.win.flip()
        core.wait(self.waits['ready_offset_to_question_onset'])

        # Show question
        self.question.draw()
        self.win.flip()
        core.wait(self.waits['question_duration'])

        # Delay between question offset and mask onset
        self.win.flip()
        core.wait(self.waits['question_offset_to_mask_interval_onset'])

        # If it's a mask trial, show the mask
        self.timer.reset()
        while self.timer.getTime() < self.waits['mask_interval_onset_to_cue_onset']:
            if show_mask:
                self.mask.draw()
            self.win.flip()
            core.wait(self.waits['mask_refresh'])

        # Play the cue and collect the response
        self.timer.reset()
        event.clearEvents()
        cue.play()
        while self.timer.getTime() < self.waits['max_wait']:
            if show_mask:
                self.mask.draw()
            self.prompt.draw()
            self.win.flip()

            response = event.getKeys(
                keyList=self.response_keys.keys(),
                timeStamped=self.timer,
            )
            if response:
                cue.stop()
                break

        self.win.flip()

        ##########################
        # END TRIAL PRESENTATION #
        ##########################

        # Determine the response type
        try:
            key, rt = response[0]
        except IndexError:
            rt = self.waits['max_wait']
            response = 'timeout'
        else:
            response = self.response_keys[key]

        # Evaluate the response
        is_correct = int(response == trial['correct_response'])
        if trial['block_type'] == 'practice':
            self.feedback[is_correct].play()

        # Update the data for this trial
        trial['response'] = response
        trial['rt'] = rt * 1000.0
        trial['is_correct'] = is_correct

        if response == 'timeout':
            # pause the experiment
            self.show_text('timeout')
        else:
            remaining_time = self.waits['max_wait'] - rt
            core.wait(remaining_time + self.waits['inter_trial_interval'])

        return trial

    def show_text(self, label):
        if label in ['instructions', 'end_of_practice']:
            self._show_text_block(label)
        else:
            text_str = self.texts[label]
            text_stim = visual.TextStim(
                win=self.win,
                text=text_str,
                height=30,
                wrapWidth=self.win.size[0] * 0.8,
                color='black',
                font='Consolas',
            )
            text_stim.draw()
            self.win.flip()
            event.waitKeys(keyList=['space', ])

    def _show_text_block(self, label):
        text_block = sorted(self.texts[label].items())

        text_kwargs = dict(
            win=self.win,
            wrapWidth=self.win.size[0] * 0.8,
            color='black',
            font='Consolas',
        )
        main = visual.TextStim(pos=[0, 200], **text_kwargs)
        example = visual.TextStim(pos=[0, -50], **text_kwargs)
        example.setHeight(30)

        for i, block in text_block:
            tag = block.pop('tag', None)
            advance_keys = [block.get('advance', 'space'), 'q']

            if 'main' in block:
                main.setText(block['main'])

                if tag == 'title':
                    main.setHeight(50)
                else:
                    main.setHeight(25)

                main.draw()

            if 'example' in block:
                example.setText(block['example'])
                example.draw()

            if tag == 'mask':
                img_path = Path('labtools', 'dynamic_mask', 'colored_1.png')
                mask = visual.ImageStim(self.win, str(img_path), pos=[0, -100])
                mask.draw()

            self.win.flip()
            key = event.waitKeys(keyList=advance_keys)[0]

            if key == 'q':
                core.quit()

            if key in ['up', 'down']:
                self.feedback[1].play()