Пример #1
0
 def writeRoutineStartCode(self, buff):
     inits = getInitVals(self.params, 'PsychoPy')
     BaseVisualComponent.writeRoutineStartCode(self, buff)
     code = (
         "# clear any previous roi data\n"
         "%(name)s.reset()\n"
     )
     buff.writeIndentedLines(code % inits)
Пример #2
0
    def writeRoutineStartCodeJS(self, buff):
        if self.params['editable']:
            # replaces variable params with sensible defaults
            inits = getInitVals(self.params, 'PsychoJS')
            # check for NoneTypes
            for param in inits:
                if inits[param] in [None, 'None', '']:
                    inits[param].val = 'undefined'
                    if param == 'text':
                        inits[param].val = ""

            code = ("%(name)s.setText(%(text)s);")
            buff.writeIndentedLines(code % inits)
        BaseVisualComponent.writeRoutineStartCodeJS(self, buff)
Пример #3
0
    def writeRoutineStartCode(self, buff):
        # Give alert if in the same routine as a Keyboard component
        if self.params['editable'].val:
            routine = self.exp.routines[self.parentName]
            for sibling in routine:
                if isinstance(sibling, KeyboardComponent):
                    alert(4405,
                          strFields={
                              'textbox': self.params['name'],
                              'keyboard': sibling.params['name']
                          })

        code = ("%(name)s.reset()")
        buff.writeIndentedLines(code % self.params)
        BaseVisualComponent.writeRoutineStartCode(self, buff)
Пример #4
0
 def writeRoutineEndCode(self, buff):
     BaseVisualComponent.writeRoutineEndCode(self, buff)
     if len(self.exp.flow._loopList):
         currLoop = self.exp.flow._loopList[-1]  # last (outer-most) loop
     else:
         currLoop = self.exp._expHandler
     name = self.params['name']
     if self.params['save'] == 'first click':
         index = "[0]"
     elif self.params['save'] == 'last click':
         index = "[-1]"
     else:
         index = ""
     if self.params['save'] != 'none':
         code = (
             f"{currLoop.params['name']}.addData('{name}.numClicks', {name}.numClicks)\n"
             f"if {name}.numClicks:\n"
             f"   {currLoop.params['name']}.addData('{name}.timesOn', {name}.timesOn{index})\n"
             f"   {currLoop.params['name']}.addData('{name}.timesOff', {name}.timesOff{index})\n"
             f"else:\n"
             f"   {currLoop.params['name']}.addData('{name}.timesOn', \"\")\n"
             f"   {currLoop.params['name']}.addData('{name}.timesOff', \"\")\n"
         )
         buff.writeIndentedLines(code)
Пример #5
0
 def writeFrameCode(self, buff):
     BaseVisualComponent.writeFrameCode(self, buff)
     # do writing of init
     inits = getInitVals(self.params, 'PsychoPy')
     # Get callback from params
     callback = inits['callback']
     if inits['callback']:
         callback = str(callback)
     else:
         callback = ""
     # String to get time
     if inits['timeRelativeTo'] == 'button onset':
         timing = "%(name)s.buttonClock.getTime()"
     elif inits['timeRelativeTo'] == 'experiment':
         timing = "globalClock.getTime()"
     elif inits['timeRelativeTo'] == 'routine':
         timing = "routineTimer.getTime()"
     else:
         timing = "globalClock.getTime()"
     # Assemble code
     code = (f"if %(name)s.status == STARTED:\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(1, relative=True)
     code = (f"# check whether %(name)s has been pressed\n"
             f"if %(name)s.isClicked:\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(1, relative=True)
     code = (f"if not %(name)s.wasClicked:\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(1, relative=True)
     code = (
         f"%(name)s.timesOn.append({timing}) # store time of first click\n"
         f"%(name)s.timesOff.append({timing}) # store time clicked until\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(-1, relative=True)
     code = (f"else:\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(1, relative=True)
     code = (
         f"%(name)s.timesOff[-1] = {timing} # update time clicked until\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(-1, relative=True)
     if self.params['forceEndRoutine'].val:
         code = (
             f"continueRoutine = False  # end routine when %(name)s is clicked\n"
         )
         buff.writeIndentedLines(code % inits)
     if self.params['oncePerClick'].val:
         code = (f"if not %(name)s.wasClicked:\n")
         buff.writeIndentedLines(code % inits)
         buff.setIndentLevel(1, relative=True)
         buff.writeIndentedLines(callback % inits)
         buff.setIndentLevel(-1, relative=True)
     else:
         buff.writeIndentedLines(callback % inits)
     code = (
         f"%(name)s.wasClicked = True  # if %(name)s is still clicked next frame, it is not a new click\n"
     )
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(-1, relative=True)
     code = (f"else:\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(1, relative=True)
     code = (
         f"%(name)s.wasClicked = False  # if %(name)s is clicked next frame, it is a new click\n"
     )
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(-2, relative=True)
     code = (f"else:\n")
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(1, relative=True)
     code = (
         f"%(name)s.buttonClock.reset() # keep clock at 0 if button hasn't started / has finished\n"
         f"%(name)s.wasClicked = False  # if %(name)s is clicked next frame, it is a new click\n"
     )
     buff.writeIndentedLines(code % inits)
     buff.setIndentLevel(-1, relative=True)
Пример #6
0
    def writeFrameCodeJS(self, buff):
        BaseVisualComponent.writeFrameCodeJS(self, buff)
        # do writing of init
        inits = getInitVals(self.params, 'PsychoJS')
        # Get callback from params
        callback = inits['callback']
        if inits['callback'].val:
            callback = translatePythonToJavaScript(str(callback))
        else:
            callback = ""

        # Check for current and last button press
        code = ("if (%(name)s.status === PsychoJS.Status.STARTED) {\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = ("// check whether %(name)s has been pressed\n"
                "if (%(name)s.isClicked) {\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = ("if (!%(name)s.wasClicked) {\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = ("// store time of first click\n"
                "%(name)s.timesOn.push(%(name)s.clock.getTime());\n"
                "// store time clicked until\n"
                "%(name)s.timesOff.push(%(name)s.clock.getTime());\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = ("} else {\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            "// update time clicked until;\n"
            "%(name)s.timesOff[%(name)s.timesOff.length - 1] = %(name)s.clock.getTime();\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = ("}\n")
        buff.writeIndentedLines(code % inits)

        if self.params['oncePerClick'] or self.params['forceEndRoutine']:
            code = ("if (!%(name)s.wasClicked) {\n")
            buff.writeIndentedLines(code % inits)
            buff.setIndentLevel(1, relative=True)
            if self.params['forceEndRoutine']:
                code = ("// end routine when %(name)s is clicked\n"
                        "continueRoutine = false;\n")
                buff.writeIndentedLines(code % inits)
            if self.params['oncePerClick']:
                buff.writeIndentedLines(callback % inits)
            buff.setIndentLevel(-1, relative=True)
            code = ("}\n")
            buff.writeIndentedLines(code % inits)
        if not self.params['oncePerClick']:
            buff.writeIndentedLines(callback % inits)

        # Store current button press as last
        code = (
            "// if %(name)s is still clicked next frame, it is not a new click\n"
            "%(name)s.wasClicked = true;\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = ("} else {\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = ("// if %(name)s is clicked next frame, it is a new click\n"
                "%(name)s.wasClicked = false\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = ("}\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = ("} else {\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            "// keep clock at 0 if %(name)s hasn't started / has finished\n"
            "%(name)s.clock.reset();\n"
            "// if %(name)s is clicked next frame, it is a new click\n"
            "%(name)s.wasClicked = false;\n")
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = ("}\n")
        buff.writeIndentedLines(code % inits)
Пример #7
0
    def writeFrameCode(self, buff):
        """Write the code that will be called every frame
        """
        # do writing of init
        inits = getInitVals(self.params, 'PsychoPy')
        # Write basics
        BaseVisualComponent.writeFrameCode(self, buff)
        buff.setIndentLevel(1, relative=True)
        code = (
            "%(name)s.status = STARTED\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        # String to get time
        if inits['timeRelativeTo'] == 'roi onset':
            timing = "%(name)s.clock.getTime()"
        elif inits['timeRelativeTo'] == 'experiment':
            timing = "globalClock.getTime()"
        elif inits['timeRelativeTo'] == 'routine':
            timing = "routineTimer.getTime()"
        else:
            timing = "globalClock.getTime()"
        # Assemble code
        code = (
            f"if %(name)s.status == STARTED:\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"# check whether %(name)s has been looked in\n"
            f"if %(name)s.isLookedIn:\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"if not %(name)s.wasLookedIn:\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"%(name)s.timesOn.append({timing}) # store time of first look\n"
            f"%(name)s.timesOff.append({timing}) # store time looked until\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = (
            f"else:\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"%(name)s.timesOff[-1] = {timing} # update time looked until\n"
        )
        buff.writeIndentedLines(code % inits)
        if self.params['endRoutineOn'].val == "look at":
            code = (
                "if %(name)s.currentLookTime > %(lookDur)s: # check if they've been looking long enough\n"
            )
            buff.writeIndentedLines(code % inits)
            buff.setIndentLevel(1, relative=True)
            code = (
                    "continueRoutine = False # end routine on sufficiently long look\n"
            )
            buff.writeIndentedLines(code % inits)
            buff.setIndentLevel(-1, relative=True)
        buff.setIndentLevel(-1, relative=True)
        code = (
            f"%(name)s.wasLookedIn = True  # if %(name)s is still looked at next frame, it is not a new look\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
        code = (
            f"else:\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"if %(name)s.wasLookedIn:"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"%(name)s.timesOff[-1] = {timing} # update time looked until\n"
        )
        buff.writeIndentedLines(code % inits)
        if self.params['endRoutineOn'].val == "look away":
            code = (
                "if %(name)s.lastLookTime > %(lookDur)s: # check if last look was long enough\n"
            )
            buff.writeIndentedLines(code % inits)
            buff.setIndentLevel(1, relative=True)
            code = (
                    "continueRoutine = False # end routine after sufficiently long look\n"
            )
            buff.writeIndentedLines(code % inits)
            buff.setIndentLevel(-1, relative=True)
        buff.setIndentLevel(-1, relative=True)
        code = (
            f"%(name)s.wasLookedIn = False  # if %(name)s is looked at next frame, it is a new look\n"

        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-2, relative=True)
        code = (
            f"else:\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(1, relative=True)
        code = (
            f"%(name)s.clock.reset() # keep clock at 0 if roi hasn't started / has finished\n"
            f"%(name)s.wasLookedIn = False  # if %(name)s is looked at next frame, it is a new look\n"
        )
        buff.writeIndentedLines(code % inits)
        buff.setIndentLevel(-1, relative=True)
Пример #8
0
    def writeRoutineStartCodeJS(self, buff):
        BaseVisualComponent.writeRoutineStartCode(self, buff)

        code = ("%(name)s.reset()")
        buff.writeIndentedLines(code % self.params)