Example #1
0
    def writeStartTestCodeJS(self, buff):
        """Test whether we need to start
        """
        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'
            code = ("if (t >= %(startVal)s "
                    "&& %(name)s.status === PsychoJS.Status.NOT_STARTED) {\n")
        elif self.params['startType'].val == 'frame N':
            code = ("if (frameN >= %(startVal)s "
                    "&& %(name)s.status === PsychoJS.Status.NOT_STARTED) {\n")
        elif self.params['startType'].val == 'condition':
            code = ("if ((%(startVal)s) "
                    "&& %(name)s.status === PsychoJS.Status.NOT_STARTED) {\n")
        else:
            msg = "Not a known startType (%(startType)s) for %(name)s"
            raise CodeGenerationException(msg % self.params)

        buff.writeIndented(code % self.params)

        buff.setIndentLevel(+1, relative=True)
        code = (
            "// keep track of start time/frame for later\n"
            "%(name)s.tStart = t;  // (not accounting for frame time here)\n"
            "%(name)s.frameNStart = frameN;  // exact frame index\n\n")
        buff.writeIndentedLines(code % self.params)
Example #2
0
    def writeStopTestCode(self, buff):
        """Test whether we need to stop
        """
        buff.writeIndentedLines("if %(name)s.status == STARTED:\n" %
                                self.params)
        buff.setIndentLevel(+1, relative=True)

        if self.params['stopType'].val == 'time (s)':
            code = ("# is it time to stop? (based on local clock)\n"
                    "if tThisFlip > %(stopVal)s-frameTolerance:\n")
        # duration in time (s)
        elif (self.params['stopType'].val == 'duration (s)'):
            code = (
                "# is it time to stop? (based on global clock, using actual start)\n"
                "if tThisFlipGlobal > %(name)s.tStartRefresh + %(stopVal)s-frameTolerance:\n"
            )
        elif self.params['stopType'].val == 'duration (frames)':
            code = ("if frameN >= (%(name)s.frameNStart + %(stopVal)s):\n")
        elif self.params['stopType'].val == 'frame N':
            code = "if frameN >= %(stopVal)s:\n"
        elif self.params['stopType'].val == 'condition':
            code = "if bool(%(stopVal)s):\n"
        else:
            msg = ("Didn't write any stop line for startType=%(startType)s, "
                   "stopType=%(stopType)s")
            raise CodeGenerationException(msg % self.params)

        buff.writeIndentedLines(code % self.params)
        buff.setIndentLevel(+1, relative=True)
        code = ("# keep track of stop time/frame for later\n"
                "%(name)s.tStop = t  # not accounting for scr refresh\n"
                "%(name)s.frameNStop = frameN  # exact frame index\n"
                "win.timeOnFlip(%(name)s, 'tStopRefresh')"
                "  # time at next scr refresh\n")
        buff.writeIndentedLines(code % self.params)
Example #3
0
    def writeStartTestCode(self, buff):
        """Test whether we need to start
        """
        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'
            code = ("if t >= %(startVal)s "
                    "and %(name)s.status == NOT_STARTED:\n")
        elif self.params['startType'].val == 'frame N':
            code = ("if frameN >= %(startVal)s "
                    "and %(name)s.status == NOT_STARTED:\n")
        elif self.params['startType'].val == 'condition':
            code = ("if (%(startVal)s) "
                    "and %(name)s.status == NOT_STARTED:\n")
        else:
            msg = "Not a known startType (%(startType)s) for %(name)s"
            raise CodeGenerationException(msg % self.params)

        buff.writeIndented(code % self.params)

        buff.setIndentLevel(+1, relative=True)
        code = ("# keep track of start time/frame for later\n"
                "%(name)s.tStart = t  # not accounting for scr refresh\n"
                "%(name)s.frameNStart = frameN  # exact frame index\n"
                "win.timeOnFlip(%(name)s, 'tStartRefresh')"
                "  # time at next scr refresh\n")
        buff.writeIndentedLines(code % self.params)
Example #4
0
    def writeStartTestCode(self, buff):
        """Test whether we need to start
        """
        if self.params['syncScreenRefresh']:
            tCompare = 'tThisFlip'
        else:
            tCompare = 't'
        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'
            code = ("if {params[name]}.status == NOT_STARTED and "
                    "{t} >= {params[startVal]}-frameTolerance:\n")
        elif self.params['startType'].val == 'frame N':
            code = ("if {params[name]}.status == NOT_STARTED and "
                    "frameN >= {params[startVal]}:\n")
        elif self.params['startType'].val == 'condition':
            code = ("if {params[name]}.status == NOT_STARTED and "
                    "{params[startVal]}:\n")
        else:
            msg = "Not a known startType (%(startType)s) for %(name)s"
            raise CodeGenerationException(msg % self.params)

        buff.writeIndented(code.format(params=self.params, t=tCompare))

        buff.setIndentLevel(+1, relative=True)
        code = ("# keep track of start time/frame for later\n"
                "{params[name]}.frameNStart = frameN  # exact frame index\n"
                "{params[name]}.tStart = t  # local t and not account for scr refresh\n"
                "{params[name]}.tStartRefresh = tThisFlipGlobal  # on global time\n")
        if self.type != "Sound":  # for sounds, don't update to actual frame time
                code += ("win.timeOnFlip({params[name]}, 'tStartRefresh')"
                         "  # time at next scr refresh\n")
        buff.writeIndentedLines(code.format(params=self.params))
Example #5
0
    def writeStopTestCode(self, buff):
        """Test whether we need to stop
        """
        params = self.params
        buff.writeIndentedLines(f"if {params['name']}.status == STARTED:\n")
        buff.setIndentLevel(+1, relative=True)

        if self.params['stopType'].val == 'time (s)':
            code = (f"# is it time to stop? (based on local clock)\n"
                    f"if tThisFlip > {params['stopVal']}-frameTolerance:\n"
                    )
        # duration in time (s)
        elif (self.params['stopType'].val == 'duration (s)'):
            code = (f"# is it time to stop? (based on global clock, using actual start)\n"
                    f"if tThisFlipGlobal > {params['name']}.tStartRefresh + {params['stopVal']}-frameTolerance:\n")
        elif self.params['stopType'].val == 'duration (frames)':
            code = (f"if frameN >= ({params['name']}.frameNStart + {params['stopVal']}):\n")
        elif self.params['stopType'].val == 'frame N':
            code = f"if frameN >= {params['stopVal']}:\n"
        elif self.params['stopType'].val == 'condition':
            code = f"if bool({params['stopVal']}):\n"
        else:
            msg = (f"Didn't write any stop line for startType={params['startType']}, "
                   f"stopType={params['stopType']}")
            raise CodeGenerationException(msg)

        buff.writeIndentedLines(code)
        buff.setIndentLevel(+1, relative=True)
        code = (f"# keep track of stop time/frame for later\n"
                f"{params['name']}.tStop = t  # not accounting for scr refresh\n"
                f"{params['name']}.frameNStop = frameN  # exact frame index\n"
                f"win.timeOnFlip({params['name']}, 'tStopRefresh')"
                f"  # time at next scr refresh\n")
        buff.writeIndentedLines(code)
Example #6
0
    def writeStartTestCodeJS(self, buff):
        """Test whether we need to start
        """
        params = self.params
        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, str) and
                    not self.params['startVal'].val.strip()):
                self.params['startVal'].val = '0.0'
            code = (f"if (t >= {params['startVal']} "
                    f"&& {params['name']}.status === PsychoJS.Status.NOT_STARTED) {{\n")
        elif self.params['startType'].val == 'frame N':
            code = (f"if (frameN >= {params['startVal']} "
                    f"&& {params['name']}.status === PsychoJS.Status.NOT_STARTED) {{\n")
        elif self.params['startType'].val == 'condition':
            code = (f"if (({params['startVal']}) "
                    f"&& {params['name']}.status === PsychoJS.Status.NOT_STARTED) {{\n")
        else:
            msg = f"Not a known startType ({params['startVal']}) for {params['name']}"
            raise CodeGenerationException(msg)

        buff.writeIndented(code)

        buff.setIndentLevel(+1, relative=True)
        code = (f"// keep track of start time/frame for later\n"
                f"{params['name']}.tStart = t;  // (not accounting for frame time here)\n"
                f"{params['name']}.frameNStart = frameN;  // exact frame index\n\n")
        buff.writeIndentedLines(code)
Example #7
0
File: _base.py Project: sj/psychopy
    def writeStopTestCode(self, buff):
        """Test whether we need to stop
        """
        if self.params['stopType'].val == 'time (s)':
            code = ("frameRemains = %(stopVal)s "
                    "- win.monitorFramePeriod * 0.75"
                    "  # most of one frame period left\n"
                    "if %(name)s.status == STARTED and t >= frameRemains:\n")
        # duration in time (s)
        elif (self.params['stopType'].val == 'duration (s)' and
              self.params['startType'].val == 'time (s)'):
            code = ("frameRemains = %(startVal)s + %(stopVal)s"
                    "- win.monitorFramePeriod * 0.75"
                    "  # most of one frame period left\n"
                    "if %(name)s.status == STARTED and t >= frameRemains:\n")
        # start at frame and end with duration (need to use approximate)
        elif self.params['stopType'].val == 'duration (s)':
            code = ("if %(name)s.status == STARTED and t >= (%(name)s.tStart "
                    "+ %(stopVal)s):\n")
        elif self.params['stopType'].val == 'duration (frames)':
            code = ("if %(name)s.status == STARTED and frameN >= "
                    "(%(name)s.frameNStart + %(stopVal)s):\n")
        elif self.params['stopType'].val == 'frame N':
            code = "if %(name)s.status == STARTED and frameN >= %(stopVal)s:\n"
        elif self.params['stopType'].val == 'condition':
            code = "if %(name)s.status == STARTED and bool(%(stopVal)s):\n"
        else:
            msg = ("Didn't write any stop line for startType=%(startType)s, "
                   "stopType=%(stopType)s")
            raise CodeGenerationException(msg % self.params)

        buff.writeIndentedLines(code % self.params)
        buff.setIndentLevel(+1, relative=True)
Example #8
0
    def writeStartTestCode(self, buff):
        """Test whether we need to start
        """
        if self.params['syncScreenRefresh']:
            tCompare = 'tThisFlip'
        else:
            tCompare = 't'
        params = self.params
        t = tCompare
        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, str)
                    and not self.params['startVal'].val.strip()):
                self.params['startVal'].val = '0.0'
            code = (f"if {params['name']}.status == NOT_STARTED and "
                    f"{t} >= {params['startVal']}-frameTolerance:\n")
        elif self.params['startType'].val == 'frame N':
            code = (f"if {params['name']}.status == NOT_STARTED and "
                    f"frameN >= {params['startVal']}:\n")
        elif self.params['startType'].val == 'condition':
            code = (f"if {params['name']}.status == NOT_STARTED and "
                    f"{params['startVal']}:\n")
        else:
            msg = f"Not a known startType ({params['startVal']}) for {params['name']}"
            raise CodeGenerationException(msg % self.params)

        buff.writeIndented(code)

        buff.setIndentLevel(+1, relative=True)
        params = self.params
        code = (
            f"# keep track of start time/frame for later\n"
            f"{params['name']}.frameNStart = frameN  # exact frame index\n"
            f"{params['name']}.tStart = t  # local t and not account for scr refresh\n"
            f"{params['name']}.tStartRefresh = tThisFlipGlobal  # on global time\n"
        )
        if self.type != "Sound":
            # for sounds, don't update to actual frame time because it will start
            # on the *expected* time of the flip
            code += (f"win.timeOnFlip({params['name']}, 'tStartRefresh')"
                     f"  # time at next scr refresh\n")
        if self.params['saveStartStop']:
            code += f"# add timestamp to datafile\n"
            if self.type == 'Sound' and self.params['syncScreenRefresh']:
                # use the time we *expect* the flip
                code += f"thisExp.addData('{params['name']}.started', tThisFlipGlobal)\n"
            elif 'syncScreenRefresh' in self.params and self.params[
                    'syncScreenRefresh']:
                # use the time we *detect* the flip (in the future)
                code += f"thisExp.timestampOnFlip(win, '{params['name']}.started')\n"
            else:
                # use the time ignoring any flips
                code += f"thisExp.addData('{params['name']}.started', t)\n"
        buff.writeIndentedLines(code)
Example #9
0
    def writeStopTestCode(self, buff):
        """Test whether we need to stop
        """
        params = self.params
        buff.writeIndentedLines(f"if {params['name']}.status == STARTED:\n")
        buff.setIndentLevel(+1, relative=True)

        # If start time is blank ad stop is a duration, raise alert
        if self.params['stopType'] in ('duration (s)', 'duration (frames)'):
            if ('startVal' not in self.params) or (self.params['startVal']
                                                   in ("", "None", None)):
                alerttools.alert(4120,
                                 strFields={'component': self.params['name']})

        if self.params['stopType'].val == 'time (s)':
            code = (f"# is it time to stop? (based on local clock)\n"
                    f"if tThisFlip > {params['stopVal']}-frameTolerance:\n")
        # duration in time (s)
        elif (self.params['stopType'].val == 'duration (s)'):
            code = (
                f"# is it time to stop? (based on global clock, using actual start)\n"
                f"if tThisFlipGlobal > {params['name']}.tStartRefresh + {params['stopVal']}-frameTolerance:\n"
            )
        elif self.params['stopType'].val == 'duration (frames)':
            code = (
                f"if frameN >= ({params['name']}.frameNStart + {params['stopVal']}):\n"
            )
        elif self.params['stopType'].val == 'frame N':
            code = f"if frameN >= {params['stopVal']}:\n"
        elif self.params['stopType'].val == 'condition':
            code = f"if bool({params['stopVal']}):\n"
        else:
            msg = (
                f"Didn't write any stop line for startType={params['startType']}, "
                f"stopType={params['stopType']}")
            raise CodeGenerationException(msg)

        buff.writeIndentedLines(code)
        buff.setIndentLevel(+1, relative=True)
        code = (
            f"# keep track of stop time/frame for later\n"
            f"{params['name']}.tStop = t  # not accounting for scr refresh\n"
            f"{params['name']}.frameNStop = frameN  # exact frame index\n")
        if self.params['saveStartStop']:
            code += f"# add timestamp to datafile\n"
            if 'syncScreenRefresh' in self.params and self.params[
                    'syncScreenRefresh']:
                # use the time we *detect* the flip (in the future)
                code += f"thisExp.timestampOnFlip(win, '{params['name']}.stopped')\n"
            else:
                # use the time ignoring any flips
                code += f"thisExp.addData('{params['name']}.stopped', t)\n"
        buff.writeIndentedLines(code)
Example #10
0
    def writeStopTestCodeJS(self, buff):
        """Test whether we need to stop
        """
        params = self.params
        if self.params['stopType'].val == 'time (s)':
            code = (
                f"frameRemains = {params['stopVal']} "
                f" - psychoJS.window.monitorFramePeriod * 0.75;"
                f"  // most of one frame period left\n"
                f"if (({params['name']}.status === PsychoJS.Status.STARTED || {params['name']}.status === PsychoJS.Status.FINISHED) "
                f"&& t >= frameRemains) {{\n")
        # duration in time (s)
        elif (self.params['stopType'].val == 'duration (s)'
              and self.params['startType'].val == 'time (s)'):
            code = (
                f"frameRemains = {params['startVal']} + {params['stopVal']}"
                f" - psychoJS.window.monitorFramePeriod * 0.75;"
                f"  // most of one frame period left\n"
                f"if ({params['name']}.status === PsychoJS.Status.STARTED "
                f"&& t >= frameRemains) {{\n")
        # start at frame and end with duratio (need to use approximate)
        elif self.params['stopType'].val == 'duration (s)':
            code = (
                f"if ({params['name']}.status === PsychoJS.Status.STARTED "
                f"&& t >= ({params['name']}.tStart + {params['stopVal']})) {{\n"
            )
        elif self.params['stopType'].val == 'duration (frames)':
            code = (
                f"if ({params['name']}.status === PsychoJS.Status.STARTED "
                f"&& frameN >= ({params['name']}.frameNStart + {params['stopVal']})) {{\n"
            )
        elif self.params['stopType'].val == 'frame N':
            code = (f"if ({params['name']}.status === PsychoJS.Status.STARTED "
                    f"&& frameN >= {params['stopVal']}) {{\n")
        elif self.params['stopType'].val == 'condition':
            code = (f"if ({params['name']}.status === PsychoJS.Status.STARTED "
                    f"&& Boolean({params['stopVal']})) {{\n")
        else:
            msg = (f"Didn't write any stop line for startType="
                   f"{params['startType']}, "
                   f"stopType={params['stopType']}")
            raise CodeGenerationException(msg)

        buff.writeIndentedLines(code)
        buff.setIndentLevel(+1, relative=True)
Example #11
0
    def writeStopTestCodeJS(self, buff):
        """Test whether we need to stop
        """
        if self.params['stopType'].val == 'time (s)':
            code = ("frameRemains = %(stopVal)s "
                    " - psychoJS.window.monitorFramePeriod * 0.75;"
                    "  // most of one frame period left\n"
                    "if (%(name)s.status === PsychoJS.Status.STARTED "
                    "&& t >= frameRemains) {\n")
        # duration in time (s)
        elif (self.params['stopType'].val == 'duration (s)'
              and self.params['startType'].val == 'time (s)'):
            code = (
                "frameRemains = %(startVal)s + %(stopVal)s"
                " - psychoJS.window.monitorFramePeriod * 0.75;"
                "  // most of one frame period left\n"
                "if ((%(name)s.status === PsychoJS.Status.STARTED || %(name)s.status === PsychoJS.Status.FINISHED) "
                "&& t >= frameRemains) {\n")
        # start at frame and end with duratio (need to use approximate)
        elif self.params['stopType'].val == 'duration (s)':
            code = (
                "if ((%(name)s.status === PsychoJS.Status.STARTED || %(name)s.status === PsychoJS.Status.FINISHED) "
                "&& t >= (%(name)s.tStart + %(stopVal)s)) {\n")
        elif self.params['stopType'].val == 'duration (frames)':
            code = (
                "if ((%(name)s.status === PsychoJS.Status.STARTED || %(name)s.status === PsychoJS.Status.FINISHED) "
                "&& frameN >= (%(name)s.frameNStart + %(stopVal)s)) {\n")
        elif self.params['stopType'].val == 'frame N':
            code = (
                "if ((%(name)s.status === PsychoJS.Status.STARTED || %(name)s.status === PsychoJS.Status.FINISHED) "
                "&& frameN >= %(stopVal)s) {\n")
        elif self.params['stopType'].val == 'condition':
            code = (
                "if ((%(name)s.status === PsychoJS.Status.STARTED || %(name)s.status === PsychoJS.Status.FINISHED) "
                "&& Boolean(%(stopVal)s)) {\n")
        else:
            msg = ("Didn't write any stop line for startType="
                   "%(startType)s, "
                   "stopType=%(stopType)s")
            raise CodeGenerationException(msg % self.params)

        buff.writeIndentedLines(code % self.params)
        buff.setIndentLevel(+1, relative=True)