-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_methods.py
More file actions
123 lines (107 loc) · 5.57 KB
/
display_methods.py
File metadata and controls
123 lines (107 loc) · 5.57 KB
1
2
3
4
5
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from psychopy import visual, core
from psychopy.hardware import keyboard
class DisplayMethod:
"""Abstract class for displaying slide
Defines common attributes to all display methods:
- duration: [int] in seconds
- screen: [psychopy visual.Window object]
- stimulus_path: [string] path to image file to display
- stimulus_size: [tuple] Width and Height of image file to display. Will use units defined in main script
- clock: psychopy core.Clock object to count time for slide presentation
Also defines informal interface for method to show slide.
"""
def __init__(self, duration, screen, stimulus_path, stimulus_size):
self.duration = duration
self.screen = screen
self.stimulus = visual.ImageStim(win=self.screen) # create empty psychopy visual stimulus object
if stimulus_path: # if image path is provided, add path and size to visual stimulus object
self.stimulus.image = stimulus_path
self.stimulus.size = stimulus_size
self.clock = core.Clock()
def show_slide(self): # TODO: think of maybe changing this name to not being confused with Slide class method
pass
class DispStatic(DisplayMethod):
"""Class for displaying a static slide, with or without visual stimulus, for a fixed duration.
No response is recorded.
"""
def __init__(self, duration, screen, stimulus_path, stimulus_size):
super().__init__(duration, screen, stimulus_path, stimulus_size)
def show_slide(self):
"""Overrides informal interface to display slide for a fixed duration."""
self.clock.reset()
t = self.clock.getTime()
while t < self.duration:
self.stimulus.draw()
self.screen.flip()
t = self.clock.getTime()
class DispStaticResponse(DisplayMethod):
"""Class for displaying a static slide, with or without visual stimulus, for a fixed duration.
Key presses is recorded and stored.
Implements attributes:
- key_list: [list] list of possible keys for response
- response_key: [string] stores eventual key pressed
- response_rt: [float] stores eventual key press reaction time
- keyboard: psychopy keyboard.Keyboard object to detect and record key press.
"""
def __init__(self, duration, screen, stimulus_path, stimulus_size, key_list):
super().__init__(duration, screen, stimulus_path, stimulus_size)
self.key_list = key_list
self.response_key = None
self.response_rt = None
self.keyboard = keyboard.Keyboard()
self.keyboard.clock = self.clock # assign clock to keyboard object to detect reaction time
def show_slide(self):
"""Overrides informal interface to display slide for a fixed duration.
If any key in 'key_list' is pressed, key name and reaction time is stored.
"""
self.keyboard.clearEvents() # flush eventual previous key presses
self.clock.reset()
t = self.clock.getTime()
while t < self.duration:
self.stimulus.draw()
self.screen.flip()
# Check that no key has already been pressed. If not, detect key press among the allowed keys.
# Store name and reaction time of first key press detected
if not self.response_key:
key = self.keyboard.getKeys(self.key_list, clear=True)
if key:
self.response_key = key[0].name
self.response_rt = key[0].rt
t = self.clock.getTime()
class DispDynamicResponse(DisplayMethod):
# TODO: this might be child of "DispStaticResponse"!
"""Class for displaying a dynamic slide, with or without visual stimulus.
Slide is displayed for a maximum duration or until one of allowed keys is pressed.
Key press is recorded and stored.
Implements attributes:
- key_list: [list] list of possible keys for response
- response_key: [string] stores eventual key pressed
- response_rt: [float] stores eventual key press reaction time
- keyboard: psychopy keyboard.Keyboard object to detect and record key press
"""
def __init__(self, duration, screen, stimulus_path, stimulus_size, key_list):
super().__init__(duration, screen, stimulus_path, stimulus_size)
self.key_list = key_list
self.response_key = None
self.response_rt = None
self.keyboard = keyboard.Keyboard()
self.keyboard.clock = self.clock # assign clock to keyboard object to detect reaction time
def show_slide(self):
"""Overrides informal interface to display slide until one key is pressed.
If any key in 'key_list' is pressed, key name and reaction time is stored.
"""
self.keyboard.clearEvents() # flush eventual previous key presses
self.clock.reset()
t = self.clock.getTime()
while t < self.duration:
self.stimulus.draw()
self.screen.flip()
# Check that no key has already been pressed. If not, detect key press among the allowed keys.
# Store name and reaction time of first key press detected, and stop displaying slide.
if not self.response_key:
key = self.keyboard.getKeys(self.key_list, clear=True)
if key:
self.response_key = key[0].name
self.response_rt = key[0].rt
break # in case of key press, stop displaying slide
t = self.clock.getTime()