/
__init__.py
193 lines (162 loc) · 7.74 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Part of the PsychoPy library
# Copyright (C) 2018 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
from psychopy.hardware import qmix
# The absolute path to the folder containing this path.
thisFolder = path.abspath(path.dirname(__file__))
iconFile = path.join(thisFolder, 'pump.png')
tooltip = _translate('Pump: deliver liquid stimuli via a Cetoni neMESYS syringe pump')
_localized = {'pumpIndex': _translate('Pump index'),
'syringeType': _translate('Syringe type'),
'pumpAction': _translate('Pump action'),
'flowRate': _translate('Flow rate'),
'flowRateUnit': _translate('Flow rate unit'),
'switchValveWhenDone': _translate('Switch valve after dosing'),
'syncToScreen': _translate('Sync to screen')}
class QmixPumpComponent(BaseComponent):
"""Operate a Cetoni neMESYS syringe pump"""
categories = ['Custom']
def __init__(self, exp, parentName, name='pump',
startType='time (s)', startVal=0.0,
stopType='duration (s)', stopVal=1.0,
startEstim='', durationEstim='',
pumpIndex=0,
syringeType='50 mL glass',
pumpAction='dispense',
flowRate=1.0,
flowRateUnit='mL/s',
switchValveWhenDone=True,
syncToScreen=True):
super(QmixPumpComponent, self).__init__(
exp, parentName, name,
startType=startType, startVal=startVal,
stopType=stopType, stopVal=stopVal,
startEstim=startEstim, durationEstim=durationEstim)
self.type = 'QmixPump'
self.url = 'http://www.psychopy.org/builder/components/pump.html'
self.categories = ['I/O']
self.exp.requireImport(importName='qmix',
importFrom='psychopy.hardware')
code = ('# Initialize all pumps so they are ready to be used when we\n'
'# need them later. This enables us to dynamically select\n'
'# pumps during the experiment without worrying about their\n'
'# initialization.\n'
'qmix._init_all_pumps()')
self.exp.runOnce(code)
# Order in which the user-settable parameters will be displayed
# in the component's properties window.
self.order = ['pumpIndex', 'syringeType', 'pumpAction',
'flowRate', 'flowRateUnit', 'switchValveWhenDone',
'syncToScreen']
self.params['pumpIndex'] = Param(
pumpIndex,
valType='code',
hint=_translate('The index of the pump(s) (first pump is 0).'),
label=_localized['pumpIndex'])
self.params['syringeType'] = Param(
syringeType,
valType='str',
allowedVals=qmix.syringeTypes,
hint=_translate('Syringe type and dimensions'),
label=_localized['syringeType'])
self.params['pumpAction'] = Param(
pumpAction,
valType='str',
allowedVals=['aspirate', 'dispense'],
hint=_translate('Whether the syringe should be filled (aspirate) '
'or emptied (dispense'),
label=_localized['pumpAction'])
self.params['flowRate'] = Param(
flowRate,
valType='code',
hint='The flow rate',
label=_localized['flowRate'])
self.params['flowRateUnit'] = Param(
flowRateUnit,
valType='str',
allowedVals=qmix.flowRateUnits,
hint='The unit of the flow rate',
label=_localized['flowRateUnit'])
self.params['switchValveWhenDone'] = Param(
switchValveWhenDone, valType='bool',
allowedVals=[True, False],
hint=_translate('Switch the valve after pump operation'),
label=_localized['switchValveWhenDone'])
self.params['syncToScreen'] = Param(
syncToScreen, valType='bool',
allowedVals=[True, False],
hint=_translate('Sync pump onset to the screen refresh'),
label=_localized['syncToScreen'])
def writeRoutineStartCode(self, buff):
"""Write the code that will be called at the start of the routine.
"""
code = ('\n'
'# Select the correct pre-initialized pump, and set the \n'
'# syringe type according to the Pumo Component properties.\n'
'_pumpInstance = qmix.pumps[%(pumpIndex)s]\n'
'%(name)s = qmix._PumpWrapperForBuilderComponent(_pumpInstance)\n'
'%(name)s.syringeType = %(syringeType)s\n'
'%(name)s.flowRateUnit = %(flowRateUnit)s\n'
'%(name)s.status = None\n'
% self.params)
buff.writeIndentedLines(code)
def writeFrameCode(self, buff):
"""Write the code that will be called every frame.
"""
buff.writeIndented("# *%s* updates\n" % (self.params['name']))
self.writeStartTestCode(buff)
buff.writeIndented("%(name)s.status = STARTED\n" % self.params)
if self.params['syncToScreen'].val:
if self.params['pumpAction'] == 'aspirate':
code = ('win.callOnFlip(%(name)s.fill, '
'flowRate=%(flowRate)s)\n') % self.params
else:
code = 'win.callOnFlip(%(name)s.empty, flowRate=%(flowRate)s)\n' % self.params
else:
if self.params['pumpAction'] == 'aspirate':
code = '%(name)s.fill(flowRate=%(flowRate)s)\n' % self.params
else:
code = '%(name)s.empty(flowRate=%(flowRate)s)\n' % self.params
buff.writeIndentedLines(code)
buff.setIndentLevel(-1, relative=True)
# Test for stop (only if there was some setting for duration or
# stop).
if self.params['stopVal'].val not in ['', None, -1, 'None']:
self.writeStopTestCode(buff)
buff.writeIndented("%(name)s.status = FINISHED\n" % self.params)
if self.params['syncToScreen'].val:
if self.params['switchValveWhenDone'].val:
code = ('win.callOnFlip(%(name)s.stop)\n'
'win.callOnFlip(%(name)s.switchValvePosition)\n'
% self.params)
else:
code = 'win.callOnFlip(%(name)s.stop)\n' % self.params
else:
if self.params['switchValveWhenDone'].val:
code = ('%(name)s.stop()\n'
'%(name)s.switchValvePosition()\n'
% self.params)
else:
code = '%(name)s.stop()\n' % self.params
buff.writeIndentedLines(code)
buff.setIndentLevel(-2, relative=True)
def writeRoutineEndCode(self, buff):
# Make sure that we stop the pumps even if the routine has been
# ended prematurely.
if self.params['switchValveWhenDone'].val:
code = ('\nif %(name)s.status == STARTED:\n'
' %(name)s.stop()\n'
' %(name)s.switchValvePosition()\n\n'
% self.params)
else:
code = ('\nif %(name)s.status == STARTED:\n'
' %(name)s.stop()\n\n' % self.params)
buff.writeIndentedLines(code)
# get parent to write code too (e.g. store onset/offset times)
super().writeRoutineEndCode(buff)