-
Notifications
You must be signed in to change notification settings - Fork 3
/
MapperStim.py
executable file
·147 lines (110 loc) · 7.31 KB
/
MapperStim.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from __future__ import division
from psychopy import visual, core, misc, event, filters
import numpy as np
from scipy.signal import convolve2d
from IPython import embed as shell
from math import *
import random, sys
sys.path.append( 'exp_tools' )
# sys.path.append( os.environ['EXPERIMENT_HOME'] )
class MapperStim(object):
def __init__(self, screen, trial, session,task):
# parameters
self.num_elements = session.standard_parameters['num_elements'] * (1/session.standard_parameters['bar_width_ratio'])
self.trial = trial
self.session = session
self.screen = screen
self.size_pix = session.standard_parameters['stim_size'] * session.screen_pix_size[1]
self.period = session.standard_parameters['mapper_stim_in_TR'] * session.standard_parameters['TR']
# self.refresh_frequency = refresh_frequency
self.task_rate = session.standard_parameters['task_rate']
self.task = task
self.phase = 0
# bookkeeping variables
self.eccentricity_bin = -1
self.redraws = 0
self.frames = 0
self.last_stimulus_present_for_task = 0
# construct timecourses of tasks
# task_rate is in task_rate seconds per occurrence. we add 2x refresh frequency to avoid transients in the first second(s) and those following too quickly, and add an insane number to avoid tasks in the last second(s).
# psychopy stimuli
self.populate_stimulus()
# make this stimulus array a session variable, in order to have to create it only once...
if not hasattr(session, 'element_array'):
self.session.element_array = visual.ElementArrayStim(screen, nElements = self.num_elements, sizes = self.element_sizes, sfs = self.element_sfs, xys = self.element_positions, colors = self.colors, colorSpace = 'rgb')
# set this to its default no-answer necessary value of None - this is tested for in PRFTrial when incorporating responses
self.last_sampled_staircase = None
def populate_stimulus(self):
# what eccentricity bin are we in? phase runs from 0 to 1, so we take the ecc on a linear scale for now
# self.eccentricity_bin = floor(np.abs(self.phase-0.5) * 2.0 * self.session.nr_staircases_ecc)
if self.task == np.where(self.session.tasks=='no_color_no_speed')[0][0]:
average_color_value = np.mean([self.session.standard_parameters['RG_color'],self.session.standard_parameters['BY_color']])
red = np.array([-average_color_value,-average_color_value,-average_color_value])
green = np.array([average_color_value,average_color_value,average_color_value])
yellow = np.array([-average_color_value,-average_color_value,-average_color_value])
blue = np.array([average_color_value,average_color_value,average_color_value])
self.slow_speed = 0.0
self.fast_speed = 0.0
elif self.task == np.where(self.session.tasks=='yes_color_no_speed')[0][0]:
red = np.array([ self.session.standard_parameters['RG_color'],- self.session.standard_parameters['RG_color'],0])
green = np.array([- self.session.standard_parameters['RG_color'], self.session.standard_parameters['RG_color'],0])
yellow = np.array([ self.session.standard_parameters['BY_color'], self.session.standard_parameters['BY_color'],- self.session.standard_parameters['BY_color']])
blue = np.array([- self.session.standard_parameters['BY_color'],- self.session.standard_parameters['BY_color'], self.session.standard_parameters['BY_color']])
self.slow_speed = 0.0
self.fast_speed = 0.0
elif self.task == np.where(self.session.tasks=='no_color_yes_speed')[0][0]:
average_color_value = np.mean([self.session.standard_parameters['RG_color'],self.session.standard_parameters['BY_color']])
red = np.array([-average_color_value,-average_color_value,-average_color_value])
green = np.array([average_color_value,average_color_value,average_color_value])
yellow = np.array([-average_color_value,-average_color_value,-average_color_value])
blue = np.array([average_color_value,average_color_value,average_color_value])
self.fast_speed = self.session.standard_parameters['fast_speed']
self.slow_speed = self.session.standard_parameters['slow_speed']
elif self.task == np.where(self.session.tasks=='yes_color_yes_speed')[0][0]:
red = np.array([ self.session.standard_parameters['RG_color'],- self.session.standard_parameters['RG_color'],0])
green = np.array([- self.session.standard_parameters['RG_color'], self.session.standard_parameters['RG_color'],0])
yellow = np.array([ self.session.standard_parameters['BY_color'], self.session.standard_parameters['BY_color'],- self.session.standard_parameters['BY_color']])
blue = np.array([- self.session.standard_parameters['BY_color'],- self.session.standard_parameters['BY_color'], self.session.standard_parameters['BY_color']])
self.fast_speed = self.session.standard_parameters['fast_speed']
self.slow_speed = self.session.standard_parameters['slow_speed']
elif self.task == np.where(self.session.tasks=='fix_no_stim')[0][0]:
red,green,yellow,blue = self.session.screen.background_color,self.session.screen.background_color,self.session.screen.background_color,self.session.screen.background_color
self.slow_speed = 0.0
self.fast_speed = 0.0
# Now set the actual stimulus parameters
self.colors = np.concatenate((np.ones((self.num_elements/4.0,3)) * red, # red/green - red
np.ones((self.num_elements/4.0,3)) * green, # red/green - green
np.ones((self.num_elements/4.0,3)) * blue, # blue/yellow - blue
np.ones((self.num_elements/4.0,3)) * yellow)) # blue/yellow - yellow
np.random.shuffle(self.colors)
self.element_positions = np.random.rand(self.num_elements, 2) * np.array([self.size_pix, self.size_pix]) - np.array([self.size_pix/2.0, (self.size_pix)/2.0])
self.element_speeds = np.concatenate((np.ones(np.round(self.num_elements*self.session.fast_ratio)) * self.fast_speed,
np.ones(np.round(self.num_elements*self.session.slow_ratio)) * self.slow_speed))
np.random.shuffle(self.element_speeds)
self.element_sfs = np.ones((self.num_elements)) * self.session.standard_parameters['element_spatial_frequency']
self.element_sizes = np.ones((self.num_elements)) * self.session.standard_parameters['element_size']
self.element_phases = np.zeros(self.num_elements)
self.element_orientations = np.random.rand(self.num_elements) * 720.0 - 360.0
def draw(self, phase = 0):
self.phase = phase
self.frames += 1
if self.frames == 1:#self.redraws < (self.phase * self.period * self.refresh_frequency):
# self.redraws = self.redraws + 1
# self.populate_stimulus()
self.session.element_array.setSfs(self.element_sfs)
self.session.element_array.setSizes(self.element_sizes)
self.session.element_array.setColors(self.colors)
self.session.element_array.setOris(self.element_orientations)
self.session.element_array.setXYs(np.array(np.matrix(self.element_positions)))
log_msg = 'stimulus draw for phase %f, at %f'%(phase, self.session.clock.getTime())
self.trial.events.append( log_msg )
if self.session.tracker:
self.session.tracker.log( log_msg )
# if fmod(self.phase * self.period * self.refresh_frequency, 1.0) > 0.5:
self.session.element_array.setPhases(self.element_speeds * self.phase * self.period + self.element_phases)
if self.session.tasks[self.task] != 'fix_no_stim':
self.session.element_array.draw()
self.session.fixation_outer_rim.draw()
self.session.fixation_rim.draw()
self.session.fixation.draw()
self.session.mask_stim.draw()