-
Notifications
You must be signed in to change notification settings - Fork 892
/
__init__.py
203 lines (186 loc) · 10.2 KB
/
__init__.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Part of the PsychoPy library
Copyright (C) 2015 Jonathan Peirce
Distributed under the terms of the GNU General Public License (GPL).
"""
from __future__ import absolute_import, print_function
from builtins import super # provides Py3-style super() using python-future
from os import path
from psychopy.experiment.components import BaseComponent, Param, _translate
import numpy as np
# the absolute path to the folder containing this path
thisFolder = path.abspath(path.dirname(__file__))
iconFile = path.join(thisFolder, 'variable.png')
tooltip = _translate('Variable: create a new variable')
# only use _localized values for label values, nothing functional:
_localized = {'name': _translate('Name'),
'startExpValue': _translate('Experiment start value'),
'startRoutineValue': _translate('Routine start value'),
'startFrameValue': _translate('Frame start value'),
'saveStartExp': _translate('Save exp start value'),
'saveStartRoutine': _translate('Save routine start value'),
'saveFrameValue': _translate('Save frame value'),
'saveEndRoutine': _translate('Save routine end value'),
'saveEndExp': _translate('Save exp end value')}
class VariableComponent(BaseComponent):
"""An class for creating variables in builder."""
def __init__(self, exp, parentName,
name='var1', startExpValue = '',
startRoutineValue='',
startFrameValue=''):
super().__init__(exp, parentName, name)
categories = ['Custom']
self.type = 'Variable'
self.url = "http://www.psychopy.org/builder/components/variable.html"
self.order += ['startExpValue', 'saveStartExp', 'startRoutineValue', 'saveStartRoutine', 'startFrameValue',
'saveFrameValue', 'saveEndRoutine', 'saveEndExp']
# set parameters
hnt = _translate("The start value. A variable can be set to any value.")
self.params['startExpValue'] = Param(
startExpValue, valType='code', allowedTypes=[], updates='constant',
hint=hnt,
label=_localized['startExpValue'])
hnt = _translate("Set the value for the beginning of each routine.")
self.params['startRoutineValue'] = Param(
startRoutineValue, valType='code', allowedTypes=[], updates='constant',
hint=hnt,
label=_localized['startRoutineValue'])
hnt = _translate("Set the value for the beginning of every screen refresh.")
self.params['startFrameValue'] = Param(
startFrameValue, valType='code', allowedTypes=[],
hint=hnt,
label=_localized['startFrameValue'])
# Save options
hnt = _translate('Save the experiment start value in data file.')
self.params['saveStartExp'] = Param(
False, valType='bool',
updates='constant',
hint=hnt,
label=_localized['saveStartExp'],
categ='Save')
hnt = _translate('Save the experiment end value in data file.')
self.params['saveEndExp'] = Param(
False, valType='bool',
updates='constant',
hint=hnt,
label=_localized['saveEndExp'],
categ='Save')
hnt = _translate('Save the routine start value in data file.')
self.params['saveStartRoutine'] = Param(
False, valType='bool',
updates='constant',
hint=hnt,
label=_localized['saveStartRoutine'],
categ='Save')
hnt = _translate('Save the routine end value in data file.')
self.params['saveEndRoutine'] = Param(
True, valType='bool',
updates='constant',
hint=hnt,
label=_localized['saveEndRoutine'],
categ='Save')
hnt = _translate('Save choice of frame value in data file.')
self.params['saveFrameValue'] = Param(
'never', valType='str',
allowedVals=['first', 'last', 'all', 'never'],
updates='constant',
hint=hnt,
label=_localized['saveFrameValue'],
categ='Save')
def writeInitCode(self, buff):
"""Write variable initialisation code."""
code = ("# Set experiment start values for variable component %(name)s\n")
if self.params['startExpValue'] == '':
code += ("%(name)s = ''\n")
else:
code += ("%(name)s = %(startExpValue)s\n")
# Create variable container
code += ("%(name)sContainer = []\n")
buff.writeIndented(code % self.params)
#
def writeRoutineStartCode(self, buff):
"""Write the code that will be called at the start of the routine."""
if not self.params['startRoutineValue'] == '':
code = ("%(name)s = %(startRoutineValue)s # Set routine start values for %(name)s\n")
if self.params['saveStartRoutine'] == True:
code += ("thisExp.addData('%(name)s.routineStartVal', %(name)s) # Save exp start value\n")
buff.writeIndentedLines(code % self.params)
def writeFrameCode(self, buff):
"""Write the code that will be called at the start of the frame."""
if not self.params['startFrameValue'] == '':
basestring = (str, bytes)
# Create dict for hold start and end types and converting them from types to variables
timeTypeDict = {'time (s)': 't', 'frame N': 'frameN', 'condition': self.params['startVal'].val,
'duration (s)': 't','duration (frames)': 'frameN'}
# Useful values for string creation
startType = timeTypeDict[self.params['startType'].val]
endType = timeTypeDict[self.params['stopType'].val]
code = ''
# Create default string
frameCode = ("%(name)s = %(startFrameValue)s # Set frame start values for %(name)s\n" % self.params)
if not self.params['saveFrameValue'] == 'Never':
frameCode += ("%(name)sContainer.append(%(name)s) # Save frame values\n" % self.params)
# Check for start or end values, and commence conditional timing string creation
if self.params['startVal'].val or self.params['stopVal'].val:
if self.params['startType'].val == 'time (s)':
# if startVal is an empty string then set to be 0.0
if (isinstance(self.params['startVal'].val, basestring) and
not self.params['startVal'].val.strip()):
self.params['startVal'].val = '0.0'
# Begin string construction for start values
if startType == 't':
code = (('if ' + startType + ' >= %(startVal)s') % self.params)
elif startType == 'frameN':
code = (('if ' + startType + ' >= %(startVal)s') % self.params)
elif self.params['startType'].val == 'condition':
code = ('if bool(%(startVal)s)' % self.params)
# Begin string construction for end values
if not self.params['stopVal'].val:
code += (':\n' % self.params)
# Duration types must be calculated
elif u'duration' in self.params['stopType'].val:
if 'frame' in self.params['startType'].val and 'frame' in self.params['stopType'].val \
or '(s)' in self.params['startType'].val and '(s)' in self.params['stopType'].val:
endTime = str((float(self.params['startVal'].val) + float(self.params['stopVal'].val)))
else: # do not add mismatching value types
endTime = self.params['stopVal'].val
code += (' and ' + endType + ' <= ' + endTime + ':\n' % (self.params))
elif endType == 't' :
code += (' and ' + endType + ' <= %(stopVal)s:\n' % (self.params))
elif endType == 'frameN' :
code += (' and ' + endType + ' <= %(stopVal)s:\n' % (self.params))
elif self.params['stopType'].val == 'condition':
code += (' and bool(%(stopVal)s):\n' % self.params)
code += ''.join([' ' + lines + '\n' for lines in frameCode.splitlines()])
else:
code = frameCode
buff.writeIndentedLines(code)
def writeRoutineEndCode(self, buff):
"""Write the code that will be called at the end of the routine."""
code = ''
if self.params['saveStartExp'] == True and not self.params['startExpValue'] == '':
code = ("thisExp.addData('%(name)s.expStartVal', %(startExpValue)s) # Save exp start value\n")
if self.params['saveEndRoutine'] == True and not self.params['startRoutineValue'] == '':
code += ("thisExp.addData('%(name)s.routineEndVal', %(name)s) # Save end routine value\n")
if not self.params['startFrameValue'] == '':
if self.params['saveFrameValue'] == 'last':
code += ("thisExp.addData('%(name)s.frameEndVal', %(name)sContainer[-1]) # Save end frame value\n")
elif self.params['saveFrameValue'] == 'first':
code += ("thisExp.addData('%(name)s.frameStartVal', %(name)sContainer[0]) # Save start frame value\n")
elif self.params['saveFrameValue'] == 'all':
code += ("thisExp.addData('%(name)s.allFrameVal', %(name)sContainer) # Save all frame value\n")
buff.writeIndentedLines(code % self.params)
def writeExperimentEndCode(self, buff):
"""Write the code that will be called at the end of the experiment."""
code=''
writeData = []
# For saveEndExp, check whether any values were initiated.
for vals in ['startExpValue', 'startRoutineValue', 'startFrameValue']:
if not self.params[vals] == '':
writeData.append(True)
# Write values to file if requested, and if any variables defined
if self.params['saveEndExp'] == True and np.any(writeData):
code = ("thisExp.addData('%(name)s.endExpVal', %(name)s) # Save end experiment value\n")
buff.writeIndentedLines(code % self.params)