Пример #1
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)
Пример #2
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)