Skip to content

sweetbean.stimulus.Image

Image

Bases: _KeyboardResponseStimulus

shows an image

Source code in sweetbean/stimulus/Image.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Image(_KeyboardResponseStimulus):
    """
    shows an image
    """

    type = "jsPsychImageKeyboardResponse"

    def __init__(
        self,
        duration=None,
        stimulus="",
        choices=None,
        correct_key="",
        side_effects=None,
    ):
        """
        Arguments:
            duration: time in ms the stimulus is presented
            stimulus: the path to the image
            choices: the keys that will be recorded if pressed
            correct_key: the correct key to press
            side_effects: Optional side-effect configuration passed to the runtime. This expects
                a list of SideEffect definitions (see SweetBean docs) which can be
                used to update global data like overall score or trial counter.
        """
        super().__init__(locals(), side_effects)

    def _add_special_param(self):
        pass

    def _set_before(self):
        pass

    def _get_prompt_l(self):
        try:
            with open("image_prompts.json") as f:
                image_prompts = json.load(f)
        except FileNotFoundError:
            raise FileNotFoundError(
                "The file 'image_prompts.json' was not found. "
                "If you are using the Image stimulus, you need to provide a file "
                "'image_prompts.json' in the same directory as your experiment script. "
                "It should contain a dictionary with image paths as keys and prompts "
                "as values."
            )
        except json.JSONDecodeError:
            raise ValueError(
                "The file 'image_prompts.json' contains invalid JSON."
                "If you are using the Image stimulus, you need to provide a file "
                "'image_prompts.json' in the same directory as your experiment script. "
                "It should contain a dictionary with image paths as keys and prompts "
                "as values."
            )
        if not self.l_args["stimulus"] in image_prompts:
            raise ValueError(
                f"The image {self.l_args['stimulus']} is not in the image_prompts.json file."
                "If you are using the Image stimulus, you need to provide a file "
                "'image_prompts.json' in the same directory as your experiment script. "
                "It should contain a dictionary with image paths as keys and prompts "
                "as values."
            )

        return image_prompts[self.l_args["stimulus"]]

__init__(duration=None, stimulus='', choices=None, correct_key='', side_effects=None)

Parameters:

Name Type Description Default
duration

time in ms the stimulus is presented

None
stimulus

the path to the image

''
choices

the keys that will be recorded if pressed

None
correct_key

the correct key to press

''
side_effects

Optional side-effect configuration passed to the runtime. This expects a list of SideEffect definitions (see SweetBean docs) which can be used to update global data like overall score or trial counter.

None
Source code in sweetbean/stimulus/Image.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def __init__(
    self,
    duration=None,
    stimulus="",
    choices=None,
    correct_key="",
    side_effects=None,
):
    """
    Arguments:
        duration: time in ms the stimulus is presented
        stimulus: the path to the image
        choices: the keys that will be recorded if pressed
        correct_key: the correct key to press
        side_effects: Optional side-effect configuration passed to the runtime. This expects
            a list of SideEffect definitions (see SweetBean docs) which can be
            used to update global data like overall score or trial counter.
    """
    super().__init__(locals(), side_effects)