def doReadings(self,
                   channelNumbers=[0],
                   tracesDestination=None,
                   progressBar=None):
        self._keyTextPattern.initPair()
        data = self._keyTextPattern.newPair()
        self.key = data[0]
        self.textin = data[1]

        if self.writer:
            self.writer.prepareDisk()
            self.writer.setKnownKey(self.key)

        if self.auxList:
            for aux in self.auxList:
                if aux:
                    aux.traceArm()

        if self.target:
            self.target.init()

        self.currentTrace = 0
        while self.currentTrace < self.maxtraces:
            if self.doSingleReading():
                try:
                    if self.writer:
                        for channelNum in channelNumbers:
                            self.writer.addTrace(
                                self.scope.channels[channelNum].getTrace(),
                                self.textin,
                                self.textout,
                                self.key,
                                channelNum=channelNum)
                except ValueError as e:
                    logging.warning(
                        'Exception caught in adding trace %d, trace skipped.' %
                        self.currentTrace)
                    logging.debug(str(e))
                self.sigTraceDone.emit()
                self.currentTrace += 1
            else:
                util.updateUI()  # Check if it was aborted

            if progressBar is not None:
                if progressBar.wasAborted():
                    break

        if self.auxList:
            for aux in self.auxList:
                if aux:
                    aux.captureComplete()

        if self.writer and self.writer.numTraces() > 0:
            # Don't clear trace as we re-use the buffer
            self.writer.config.setAttr(
                "scopeSampleRate",
                self.scope.channels[channelNumbers[0]].getSampleRate())
            self.writer.closeAll(clearTrace=False)
            if tracesDestination:
                tracesDestination.appendSegment(self.writer)
    def plotInputTrace(self):
        #print "Plotting %d-%d for points %d-%d"%(params[0].value(), params[1].value(), params[2].value(), params[3].value())
        initialPersist = self.persistant
        if not self.persistant:
            self.clearPushed()

        tstart = self.findParam('tracerng').getValue()[0]
        tend = self.findParam('tracerng').getValue()[1]
        pstart = self.findParam('pointrng').getValue()[0]
        pend = self.findParam('pointrng').getValue()[1]
        ttotal = 0

        if tend - tstart + 1 > 1:
            self.setPersistance(True)

        for tnum in range(tstart, tend+1):
            trace = self._traceSource.getTrace(tnum)
            if trace is not None:
                ttotal += 1
                #TODO - Not sure if should add _traceSource.offset() or not?
                self.passTrace(trace[pstart:pend+1], pstart + self._traceSource.offset(), idString = str(tnum))

                if self.findParam('Redraw after Each').getValue():
                    util.updateUI()

        self.setPersistance(initialPersist)
    def difference(self,
                   numkeys,
                   numparts,
                   trace,
                   numpoints,
                   stats,
                   pbDialog=None):

        averageData = stats["mean"]

        if pbDialog:
            pbDialog.setMinimum(0)
            pbDialog.setMaximum(numkeys * numparts)

        SADSeg = np.zeros((numkeys, numpoints))
        for bnum in range(0, numkeys):
            for i in range(0, numparts):
                if pbDialog:
                    pbDialog.updateStatus(numparts * bnum + i)
                    util.updateUI()
                    if pbDialog.wasAborted():
                        return SADSeg
                for j in range(0, numparts):
                    if averageData[bnum][i] is not None and averageData[bnum][
                            j] is not None:
                        SADSeg[bnum] = np.add(
                            SADSeg[bnum],
                            np.abs(
                                np.subtract(averageData[bnum][i],
                                            averageData[bnum][j])))
        return SADSeg
示例#4
0
 def nonblockingSleep(self, stime):
     """Sleep for given number of seconds (~50mS resolution), but don't block GUI while we do it"""
     timer.Timer.singleShot(stime * 1000, self.nonblockingSleep_done)
     self._sleeping = True
     while(self._sleeping):
         time.sleep(0.01)
         util.updateUI()
示例#5
0
    def program(self,
                filename,
                memtype="flash",
                verify=True,
                logfunc=print_fun,
                waitfunc=None):

        f = IntelHex(filename)

        maxsize = self._chip.memtypes[memtype]["size"]
        fsize = f.maxaddr() - f.minaddr()

        if fsize > maxsize:
            raise IOError(
                "File %s appears to be %d bytes, larger than %s size of %d" %
                (filename, fsize, memtype, maxsize))

        logfunc("AVR Programming %s..." % memtype)
        util.updateUI()
        fdata = f.tobinarray(start=0)
        self.writeMemory(0, fdata, memtype)

        logfunc("AVR Reading %s..." % memtype)
        util.updateUI()
        # Do verify run
        rdata = self.readMemory(0, len(fdata))  # memtype ?

        for i in range(0, len(fdata)):
            if fdata[i] != rdata[i]:
                raise IOError("Verify failed at 0x%04x, %x != %x" %
                              (i, fdata[i], rdata[i]))

        logfunc("Verified %s OK, %d bytes" % (memtype, fsize))
    def capture(self):
        timeout = False
        status = self.getStatus()
        starttime = datetime.datetime.now()

        # Wait for a trigger, letting the UI run when it can
        while ((status & STATUS_ARM_MASK) == STATUS_ARM_MASK) | ((status & STATUS_FIFO_MASK) == 0):
            status = self.getStatus()
            
            # Wait for a moment before re-running the loop
            time.sleep(0.05)
            diff = datetime.datetime.now() - starttime

            # If we've timed out, don't wait any longer for a trigger
            if (diff.total_seconds() > self._timeout):
                print("Timeout in OpenADC api(), trigger FORCED")
                timeout = True
                self.triggerNow()

            # Give the UI a chance to update (does nothing if not using UI)
            util.updateUI()

        self.setSettings(self.settings() & ~SETTINGS_ARM)
        
        # If using large offsets, system doesn't know we are delaying api
        nosampletimeout = 100
        while (self.getBytesInFifo() == 0) and nosampletimeout:
            time.sleep(0.05)
            nosampletimeout -= 1

        if nosampletimeout == 0:
            print "WARNING: No samples received from ADC. Either very long offset, or no ADC clock."
            print "         If you need such a long offset, manually update 'nosampletimeout' limit in source code."
        
        return timeout
    def program(self, filename, memtype="flash", verify=True):
        Programmer.lastFlashedFile = filename
        f = IntelHex(filename)

        startaddr = self.xmega._chip.memtypes[memtype]["offset"]
        maxsize = self.xmega._chip.memtypes[memtype]["size"]
        fsize = f.maxaddr() - f.minaddr()

        if fsize > maxsize:
            raise IOError(
                "File %s appears to be %d bytes, larger than %s size of %d" %
                (filename, fsize, memtype, maxsize))

        self.log("XMEGA Programming %s..." % memtype)
        util.updateUI()
        fdata = f.tobinarray(start=0)
        self.xmega.writeMemory(startaddr, fdata, memtype)  # , erasePage=True

        self.log("XMEGA Reading %s..." % memtype)
        util.updateUI()
        # Do verify run
        rdata = self.xmega.readMemory(startaddr, len(fdata), memtype)

        for i in range(0, len(fdata)):
            if fdata[i] != rdata[i]:
                raise IOError("Verify failed at 0x%04x, %x != %x" %
                              (i, fdata[i], rdata[i]))

        self.log("Verified %s OK, %d bytes" % (memtype, fsize))
示例#8
0
    def program(self, filename, memtype="flash", verify=True):
        Programmer.lastFlashedFile = filename
        f = IntelHex(filename)

        maxsize = self.avr._chip.memtypes[memtype]["size"]
        fsize = f.maxaddr() - f.minaddr()

        if fsize > maxsize:
            raise IOError("File %s appears to be %d bytes, larger than %s size of %d" % (filename, fsize, memtype, maxsize))

        self.log("AVR Programming %s..." % memtype)
        util.updateUI()
        fdata = f.tobinarray(start=0)
        self.avr.writeMemory(0, fdata, memtype)

        self.log("AVR Reading %s..." % memtype)
        util.updateUI()
        # Do verify run
        rdata = self.avr.readMemory(0, len(fdata))  # memtype ?

        for i in range(0, len(fdata)):
            if fdata[i] != rdata[i]:
                raise IOError("Verify failed at 0x%04x, %x != %x" % (i, fdata[i], rdata[i]))

        self.log("Verified %s OK, %d bytes" % (memtype, fsize))
示例#9
0
 def nonBlockingSleep(self, stime):
     """Sleep for given number of seconds (~50mS resolution), but don't block GUI while we do it"""
     timer.Timer().singleShot(stime * 1000, self.nonblockingSleep_done)
     self._sleeping = True
     while(self._sleeping):
         time.sleep(0.01)
         util.updateUI()
    def doReadings(self,
                   channelNumbers=None,
                   tracesDestination=None,
                   progressBar=None):
        if channelNumbers is None:
            channelNumbers = [0]

        self._pattern.initPair(self.maxtraces)

        if self._writer:
            self._writer.prepareDisk()

        if self._target:
            self._target.init()

        self.currentTrace = 0
        previous_ok = True
        while self.currentTrace < self.maxtraces:
            if self.doSingleReading(previous_ok):
                try:
                    if self._scope and self._writer:
                        self._writer.setKnownKey(self.key)
                        for channelNum in channelNumbers:
                            self._writer.addTrace(
                                self._scope.channels[channelNum].getTrace(),
                                self.textin,
                                self.textout,
                                self.key,
                                channelNum=channelNum)
                    previous_ok = True
                except ValueError as e:
                    logging.warning(
                        'Exception caught in adding trace %d, trace skipped.' %
                        self.currentTrace)
                    logging.debug(str(e))
                    previous_ok = False
                self.sigTraceDone.emit()
                self.currentTrace += 1
            else:
                previous_ok = False
                util.updateUI()  # Check if it was aborted

            if progressBar is not None:
                if progressBar.wasAborted():
                    break

        if self._aux_dict is not None:
            for func in self._aux_dict['after_capture']:
                func(self._scope, self._target, self._writer)

        if self._writer and self._writer.numTraces() > 0:
            # Don't clear trace as we re-use the buffer
            self._writer.config.setAttr(
                "scopeSampleRate",
                self._scope.channels[channelNumbers[0]].getSampleRate())
            self._writer.closeAll(clearTrace=False)
            if tracesDestination:
                tracesDestination.appendSegment(self._writer)
    def plotInputTrace(self, _=None):
        #print "Plotting %d-%d for points %d-%d"%(params[0].value(), params[1].value(), params[2].value(), params[3].value())
        initialPersist = self.persistant
        if not self.persistant:
            self.clearPushed()

        tstart = self.findParam('tracerng').getValue()[0]
        tend = self.findParam('tracerng').getValue()[1]
        pstart = self.findParam('pointrng').getValue()[0]
        pend = self.findParam('pointrng').getValue()[1]
        yaxisScaleFactor = self.findParam(['Y Axis',
                                           'Scale Factor']).getValue()
        yaxisOffsetFactor = self.findParam(['Y Axis',
                                            'Offset Factor']).getValue()

        if tend < tstart or pend < pstart:
            return

        try:
            if tend - tstart + 1 > 1:
                self.setPersistance(True)

            yUnit = self.findParam(['Y Axis', 'Unity']).getValue()
            xUnit = self.findParam('X Axis').getValue()
            xScale = self.findParam('X Axis').getValueKey()
            self.pw.setLabel('bottom', text=xScale, units=xUnit)
            self.pw.setLabel('left', units=yUnit)
            xaxis = range(pstart + self._traceSource.offset(),
                          pend + self._traceSource.offset() + 1)
            if xScale == 'Time':
                tmp = float(self._traceSource.getSampleRate())
                if tmp == 0:
                    raise Warning(
                        'X Axis of type "Time" in "%s" can\'t have "Sample Rate" with value=0. '
                        'Change it to its actual value in the Trace Manager.' %
                        self.getName())
                xaxis = [v / tmp for v in xaxis]

            for tnum in range(tstart, tend + 1):
                trace = self._traceSource.getTrace(tnum)
                if trace is not None:
                    if yaxisScaleFactor != 1.0 or yaxisOffsetFactor != 0.0:
                        trace = [
                            yaxisOffsetFactor + x * yaxisScaleFactor
                            for x in trace
                        ]
                    #TODO - Not sure if should add _traceSource.offset() or not?
                    self.passTrace(trace[pstart:pend + 1],
                                   pstart + self._traceSource.offset(),
                                   idString=str(tnum),
                                   xaxis=xaxis)

                    if self.findParam('Redraw after Each').getValue():
                        util.updateUI()
        finally:
            self.setPersistance(initialPersist)
示例#12
0
    def doReadings(self, channelNumbers=[0], tracesDestination=None, progressBar=None):
        self._keyTextPattern.initPair()
        data = self._keyTextPattern.newPair()
        self.key = data[0]
        self.textin = data[1]

        if self.writer:
            self.writer.prepareDisk()
            self.writer.setKnownKey(self.key)

        if self.auxList:
            for aux in self.auxList:
                if aux:
                    aux.traceArm()

        if self.target:
            self.target.init()

        self.currentTrace = 0
        while self.currentTrace < self.maxtraces:
            if self.doSingleReading():
                try:
                    if self.writer:
                        for channelNum in channelNumbers:
                            self.writer.addTrace(
                                self.scope.channels[channelNum].getTrace(),
                                self.textin,
                                self.textout,
                                self.key,
                                channelNum=channelNum,
                            )
                except ValueError as e:
                    logging.warning("Exception caught in adding trace %d, trace skipped." % self.currentTrace)
                    logging.debug(str(e))
                self.sigTraceDone.emit()
                self.currentTrace += 1
            else:
                util.updateUI()  # Check if it was aborted

            if progressBar is not None:
                if progressBar.wasAborted():
                    break

        if self.auxList:
            for aux in self.auxList:
                if aux:
                    aux.captureComplete()

        if self.writer and self.writer.numTraces() > 0:
            # Don't clear trace as we re-use the buffer
            self.writer.config.setAttr("scopeSampleRate", self.scope.channels[channelNumbers[0]].getSampleRate())
            self.writer.closeAll(clearTrace=False)
            if tracesDestination:
                tracesDestination.appendSegment(self.writer)
    def difference(self,
                   numkeys,
                   numparts,
                   trace,
                   numpoints,
                   stats,
                   pbDialog=None):
        means = stats["mean"]
        var = stats["variance"]
        num = stats["number"]

        if pbDialog:
            pbDialog.setMinimum(0)
            pbDialog.setMaximum(numkeys * numparts)

        # TODO: This is because we run through loop multiple times
        # When numparts == 2 (default for rand vs. fixed), we actually
        # run loop 4 times I think, but with all same data (i.e. part1 vs part2, part2 vs part1)
        # need to verify this...
        scalingFactor = 1.0 / (numparts * numparts)

        SADSeg = np.zeros((numkeys, numpoints))
        for bnum in range(0, numkeys):
            for i in range(0, numparts):
                if pbDialog:
                    pbDialog.updateStatus(numparts * bnum + i)
                    util.updateUI()
                    if pbDialog.wasAborted():
                        return SADSeg
                for j in range(0, numparts):
                    if means[bnum][i] is not None and means[bnum][
                            j] is not None:

                        ttest = np.subtract(means[bnum][i], means[bnum][j])
                        ttest /= np.sqrt((var[bnum][i] / num[bnum][i]) +
                                         (var[bnum][j] / num[bnum][j]))

                        # if t-test is NaN indicates perhaps exact same data
                        ttest = np.nan_to_num(ttest)

                        SADSeg[bnum] = np.add(SADSeg[bnum],
                                              np.abs(ttest) * scalingFactor)

        if pbDialog:
            pbDialog.updateStatus(numkeys * numparts)

        return SADSeg
示例#14
0
    def capture(self):
        # Wait?
        while self.visaInst.ask("*OPC?\n") != "1":
            time.sleep(0.1)
            util.updateUI()
            logging.info('2Waiting...')

        # print command
        self.visaInst.write(":WAVeform:DATA?")
        data = self.visaInst.read_raw()

        # Find '#' which is start of frame
        start = data.find('#')

        if start < 0:
            raise IOError('Error in header')

        start += 1
        hdrlen = data[start]
        hdrlen = int(hdrlen)

        # print hdrlen

        start += 1
        datalen = data[start:(start + hdrlen)]
        datalen = int(datalen)
        # print datalen

        start += hdrlen

        # Each is two bytes
        wavdata = bytearray(data[start:(start + datalen)])

        self.datapoints = []

        for j in range(0, datalen, 2):
            data = wavdata[j] | (wavdata[j + 1] << 8)

            if (data & 0x8000):
                data += -65536

            self.datapoints.append(data)

        self.dataUpdated.emit(0, self.datapoints, 0, 0)
        return False
示例#15
0
    def capture(self):
        # Wait?
        while self.visaInst.ask("*OPC?\n") != "1":
            time.sleep(0.1)
            util.updateUI()
            logging.info("2Waiting...")

        # print command
        self.visaInst.write(":WAVeform:DATA?")
        data = self.visaInst.read_raw()

        # Find '#' which is start of frame
        start = data.find("#")

        if start < 0:
            raise IOError("Error in header")

        start += 1
        hdrlen = data[start]
        hdrlen = int(hdrlen)

        # print hdrlen

        start += 1
        datalen = data[start : (start + hdrlen)]
        datalen = int(datalen)
        # print datalen

        start += hdrlen

        # Each is two bytes
        wavdata = bytearray(data[start : (start + datalen)])

        self.datapoints = []

        for j in range(0, datalen, 2):
            data = wavdata[j] | (wavdata[j + 1] << 8)

            if data & 0x8000:
                data += -65536

            self.datapoints.append(data)

        self.dataUpdated.emit(0, self.datapoints, 0, 0)
        return False
示例#16
0
    def plotInputTrace(self, _=None):
        #print "Plotting %d-%d for points %d-%d"%(params[0].value(), params[1].value(), params[2].value(), params[3].value())
        initialPersist = self.persistant
        if not self.persistant:
            self.clearPushed()

        tstart = self.findParam('tracerng').getValue()[0]
        tend = self.findParam('tracerng').getValue()[1]
        pstart = self.findParam('pointrng').getValue()[0]
        pend = self.findParam('pointrng').getValue()[1]
        yaxisScaleFactor = self.findParam(['Y Axis', 'Scale Factor']).getValue()
        yaxisOffsetFactor = self.findParam(['Y Axis', 'Offset Factor']).getValue()

        if tend < tstart or pend < pstart:
            return

        try:
            if tend - tstart + 1 > 1:
                self.setPersistance(True)

            yUnit = self.findParam(['Y Axis','Unity']).getValue()
            xUnit = self.findParam('X Axis').getValue()
            xScale = self.findParam('X Axis').getValueKey()
            self.pw.setLabel('bottom', text=xScale, units=xUnit)
            self.pw.setLabel('left', units=yUnit)
            xaxis = range(pstart + self._traceSource.offset(), pend + self._traceSource.offset() + 1)
            if xScale == 'Time':
                tmp = float(self._traceSource.getSampleRate())
                if tmp == 0:
                    raise Warning('X Axis of type "Time" in "%s" can\'t have "Sample Rate" with value=0. '
                                  'Change it to its actual value in the Trace Manager.' % self.getName())
                xaxis = [v/tmp for v in xaxis]

            for tnum in range(tstart, tend+1):
                trace = self._traceSource.getTrace(tnum)
                if trace is not None:
                    if yaxisScaleFactor != 1.0 or yaxisOffsetFactor != 0.0:
                        trace = [yaxisOffsetFactor + x * yaxisScaleFactor for x in trace]
                    #TODO - Not sure if should add _traceSource.offset() or not?
                    self.passTrace(trace[pstart:pend+1], pstart + self._traceSource.offset(), idString = str(tnum), xaxis=xaxis)

                    if self.findParam('Redraw after Each').getValue():
                        util.updateUI()
        finally:
            self.setPersistance(initialPersist)
    def difference(self,
                   numkeys,
                   numparts,
                   trace,
                   numpoints,
                   stats,
                   pbDialog=None):
        means = stats["mean"]
        var = stats["variance"]
        num = stats["number"]

        if pbDialog:
            pbDialog.setMinimum(0)
            pbDialog.setMaximum(numkeys * numparts)

        #T-Test makes sense between 2 partitions, todo extend
        if numparts != 2:
            raise AttributeError("T-Test works on with 2-partition options")

        SADSeg = np.zeros((numkeys, numpoints))
        for bnum in range(0, numkeys):
            i = 0
            j = 1
            if pbDialog:
                pbDialog.updateStatus(numparts * bnum + i)
                util.updateUI()
                if pbDialog.wasAborted():
                    return SADSeg

            if means[bnum][i] is not None and means[bnum][j] is not None:
                ttest = np.subtract(means[bnum][i], means[bnum][j])
                ttest /= np.sqrt((var[bnum][i] / num[bnum][i]) +
                                 (var[bnum][j] / num[bnum][j]))

                # if t-test is NaN indicates perhaps exact same data
                ttest = np.nan_to_num(ttest)

                SADSeg[bnum] = np.add(SADSeg[bnum], np.abs(ttest))

        if pbDialog:
            pbDialog.updateStatus(numkeys * numparts)

        return SADSeg
示例#18
0
    def difference(self, numkeys, numparts, trace, numpoints, stats, pbDialog=None):

        averageData = stats["mean"]

        if pbDialog:
            pbDialog.setMinimum(0)
            pbDialog.setMaximum(numkeys * numparts)

        SADSeg = np.zeros((numkeys, numpoints))
        for bnum in range(0, numkeys):
            for i in range(0, numparts):
                if pbDialog:
                    pbDialog.updateStatus(numparts * bnum + i)
                    util.updateUI()
                    if pbDialog.wasAborted():
                        return SADSeg
                for j in range(0, numparts):
                    if averageData[bnum][i] is not None and averageData[bnum][j] is not None:
                        SADSeg[bnum] = np.add(SADSeg[bnum], np.abs(np.subtract(averageData[bnum][i], averageData[bnum][j])))
        return SADSeg
示例#19
0
    def difference(self, numkeys, numparts, trace, numpoints, stats, pbDialog=None):
        means = stats["mean"]
        var = stats["variance"]
        num = stats["number"]

        if pbDialog:
            pbDialog.setMinimum(0)
            pbDialog.setMaximum(numkeys * numparts)

        # TODO: This is because we run through loop multiple times
        # When numparts == 2 (default for rand vs. fixed), we actually
        # run loop 4 times I think, but with all same data (i.e. part1 vs part2, part2 vs part1)
        # need to verify this...
        scalingFactor = 1.0 / (numparts * numparts)

        SADSeg = np.zeros((numkeys, numpoints))
        for bnum in range(0, numkeys):
            for i in range(0, numparts):
                if pbDialog:
                    pbDialog.updateStatus(numparts * bnum + i)
                    util.updateUI()
                    if pbDialog.wasAborted():
                        return SADSeg
                for j in range(0, numparts):
                    if means[bnum][i] is not None and means[bnum][j] is not None:

                        ttest = np.subtract(means[bnum][i], means[bnum][j])
                        ttest /= np.sqrt((var[bnum][i]/num[bnum][i]) + (var[bnum][j]/num[bnum][j]))

                        # if t-test is NaN indicates perhaps exact same data
                        ttest = np.nan_to_num(ttest)

                        SADSeg[bnum] = np.add(SADSeg[bnum], np.abs(ttest) * scalingFactor)

        if pbDialog:
            pbDialog.updateStatus(numkeys * numparts)

        return SADSeg
    def writeMemory(self, addr, data):
        lng = len(data)
        data = list(data)

        offs = 0
        while lng > 256:
            logging.debug("Write %(len)d bytes at 0x%(addr)X" % {
                'addr': addr,
                'len': 256
            })
            self.cmdWriteMemory(addr, data[offs:offs + 256])
            updateUI()
            if self.slow_speed:
                self.delay_func(1)
            offs += 256
            addr += 256
            lng -= 256
        if lng:
            logging.debug("Write %(len)d bytes at 0x%(addr)X" % {
                'addr': addr,
                'len': lng
            })
            self.cmdWriteMemory(addr, data[offs:])
示例#21
0
    def difference(self, numkeys, numparts, trace, numpoints, stats, pbDialog=None):
        means = stats["mean"]
        var = stats["variance"]
        num = stats["number"]

        if pbDialog:
            pbDialog.setMinimum(0)
            pbDialog.setMaximum(numkeys * numparts)

        #T-Test makes sense between 2 partitions, todo extend
        if numparts != 2:
            raise AttributeError("T-Test works on with 2-partition options")

        SADSeg = np.zeros((numkeys, numpoints))
        for bnum in range(0, numkeys):
            i = 0
            j = 1
            if pbDialog:
                pbDialog.updateStatus(numparts * bnum + i)
                util.updateUI()
                if pbDialog.wasAborted():
                    return SADSeg

            if means[bnum][i] is not None and means[bnum][j] is not None:
                ttest = np.subtract(means[bnum][i], means[bnum][j])
                ttest /= np.sqrt((var[bnum][i]/num[bnum][i]) + (var[bnum][j]/num[bnum][j]))

                # if t-test is NaN indicates perhaps exact same data
                ttest = np.nan_to_num(ttest)

                SADSeg[bnum] = np.add(SADSeg[bnum], np.abs(ttest))

        if pbDialog:
            pbDialog.updateStatus(numkeys * numparts)

        return SADSeg
    def plotInputTrace(self, _=None):
        #print "Plotting %d-%d for points %d-%d"%(params[0].value(), params[1].value(), params[2].value(), params[3].value())
        initialPersist = self.persistant
        if not self.persistant:
            self.clearPushed()

        #--- Get valid trace/point ranges.  Extra effort is necessary because we allow the widgets to survive temporary de-sync

        if not self._traceSource:
            return

        lastTrace = self._traceSource.numTraces() - 1
        lastPoint = self._traceSource.numPoints() - 1

        traceRange = self.findParam('tracerng').getValue()
        pointRange = self.findParam('pointrng').getValue()

        tstart = max(0, traceRange[0])
        tend = min(lastTrace, traceRange[1] if traceRange[1] >= 0 else 7)
        pstart = max(0, pointRange[0])
        pend = min(lastPoint, pointRange[1] if pointRange[1] >= 0 else 0)

        if tend < tstart or pend < pstart:
            return

        #---

        yaxisScaleFactor = self.findParam(['Y Axis',
                                           'Scale Factor']).getValue()
        yaxisOffsetFactor = self.findParam(['Y Axis',
                                            'Offset Factor']).getValue()

        try:
            if tend - tstart + 1 > 1:
                self.setPersistance(True)

            yUnit = self.findParam(['Y Axis', 'Unity']).getValue()
            xUnit = self.findParam('X Axis').getValue()
            xScale = self.findParam('X Axis').getValueKey()
            self.pw.setLabel('bottom', text='<h2>' + xScale, units=xUnit)
            self.pw.setLabel('left', units=yUnit)
            xaxis = range(pstart + self._traceSource.offset(),
                          pend + self._traceSource.offset() + 1)
            if xScale == 'Time':
                tmp = float(self._traceSource.getSampleRate())
                if tmp == 0:
                    raise Warning(
                        'X Axis of type "Time" in "%s" can\'t have "Sample Rate" with value=0. '
                        'Change it to its actual value in the Trace Manager.' %
                        self.getName())
                xaxis = [v / tmp for v in xaxis]

            for tnum in range(tstart, tend + 1):
                trace = self._traceSource.getTrace(tnum)
                if trace is not None:
                    if yaxisScaleFactor != 1.0 or yaxisOffsetFactor != 0.0:
                        trace = [
                            yaxisOffsetFactor + x * yaxisScaleFactor
                            for x in trace
                        ]
                    #TODO - Not sure if should add _traceSource.offset() or not?
                    self.passTrace(trace[pstart:pend + 1],
                                   pstart + self._traceSource.offset(),
                                   idString=str(tnum),
                                   xaxis=xaxis)

                    if self.findParam('Redraw after Each').getValue():
                        util.updateUI()
        finally:
            self.setPersistance(initialPersist)
    def generatePartitionStats(self,
                               partitionData={
                                   "partclass": None,
                                   "partdata": None
                               },
                               saveFile=False,
                               loadFile=False,
                               tRange=(0, -1),
                               progressBar=None):

        traces = self._traces

        if tRange[1] < 0:
            tRange = (tRange[0], traces.numTraces() + 1 + tRange[1])

        self.partObject.setPartMethod(partitionData["partclass"])

        self.numKeys = len(
            self.partObject.partMethod.getPartitionNum(traces, 0))

        numPoints = traces.numPoints()

        if loadFile:
            cfgsecs = self.api.project().getDataConfig(
                sectionName="Trace Statistics",
                subsectionName="Total Trace Statistics")
            foundsecs = []
            for cfg in cfgsecs:
                desiredsettings = {}
                desiredsettings["tracestart"] = tRange[0]
                desiredsettings["traceend"] = tRange[1]
                desiredsettings[
                    "partitiontype"] = self.partObject.partMethod.__class__.__name__
                if self.api.project().checkDataConfig(cfg, desiredsettings):
                    foundsecs.append(cfg)
        else:
            foundsecs = []

        if len(foundsecs) > 1:
            IOError("Too many sections!!!")
        elif len(foundsecs) == 1:
            fname = self.api.project().convertDataFilepathAbs(
                foundsecs[0]["filename"])
            stats = np.load(fname)
        else:
            # Array to hold average + stddev of all traces/partitions
            A_k = []
            A_j = []
            Q_k = []
            dataArrays = [A_k, A_j, Q_k]
            ACnt = []
            for bnum in range(0, self.numKeys):
                for d in dataArrays:
                    d.append([])
                ACnt.append([])
                for i in range(0,
                               self.partObject.partMethod.getNumPartitions()):
                    for d in dataArrays:
                        d[bnum].append(np.zeros(numPoints))
                    ACnt[bnum].append(0)

            # Get segment list
            segList = traces.getSegmentList()

            if progressBar:
                progressBar.setWindowTitle("Phase 1: Trace Statistics")
                progressBar.setMaximum(
                    bnum)  # len(segList['offsetList']) * self.numKeys)
                progressBar.show()

            # TODO: Double-check this fix
            # for tsegn, segtrace in enumerate(segList['offsetList']):
            tsegn = 0
            if progressBar: progressBar.setText("Segment %d" % tsegn)

            # Average data needs to be calculated
            # Require partition list
            partData = partitionData["partdata"]

            # print "Calculating Average + Std-Dev"
            # Std-Dev calculation:
            # A[0] = 0
            # A[k] = A[k-1] + (x[k] - A[k-1]) / k
            # Q[0] = 0
            # Q[k] = Q[k-1] + (x[k] - A[k-1])(x[k] - A[k])
            for bnum in range(0, self.numKeys):
                progressBar.updateStatus(tsegn * self.numKeys + bnum)
                if progressBar.wasAborted():
                    break
                for i in range(0,
                               self.partObject.partMethod.getNumPartitions()):
                    util.updateUI()
                    tlist = partData[bnum][i]
                    if len(tlist) > 0:
                        for tnum in tlist:
                            if tnum > tRange[0] and tnum < tRange[1]:
                                t = traces.getTrace(tnum)
                                ACnt[bnum][i] += 1
                                A_k[bnum][i] = A_k[bnum][i] + (
                                    t - A_j[bnum][i]) / ACnt[bnum][i]
                                Q_k[bnum][i] = Q_k[bnum][i] + (
                                    (t - A_j[bnum][i]) * (t - A_k[bnum][i]))
                                A_j[bnum][i] = A_k[bnum][i]

            if progressBar.wasAborted():
                progressBar.hide()
                return

            # Finally get variance
            for bnum in range(0, self.numKeys):
                for i in range(0,
                               self.partObject.partMethod.getNumPartitions()):
                    # TODO: Should be using population variance or sample variance (e.g. /n or /n-1)?
                    #      Since this is taken over very large sample sizes I imagine it won't matter
                    #      ultimately.
                    Q_k[bnum][i] = Q_k[bnum][i] / max(ACnt[bnum][i] - 1, 1)

            # Average is in A_k
            stats = {"mean": A_k, "variance": Q_k, "number": ACnt}

            # Wasn't cancelled - save this to project file for future use if requested
            if saveFile:
                progressBar.setText("Saving Mean/Variance Partitions")
                cfgsec = self.api.project().addDataConfig(
                    sectionName="Trace Statistics",
                    subsectionName="Total Trace Statistics")
                cfgsec["tracestart"] = tRange[0]
                cfgsec["traceend"] = tRange[1]
                cfgsec[
                    "partitiontype"] = self.partObject.partMethod.__class__.__name__
                fname = self.api.project().getDataFilepath(
                    'tracestats-%s-%d-%s.npz' %
                    (cfgsec["partitiontype"], tRange[0], tRange[1]),
                    'analysis')

                # Save mean/variance for trace
                np.savez(fname["abs"], mean=A_k, variance=Q_k, number=ACnt)
                cfgsec["filename"] = fname["rel"]

        progressBar.hide()
        return stats
示例#24
0
def nonBlockingDelay(delay_ms):
    DC = _DelayCallback()
    Timer().singleShot(delay_ms, DC.done)
    while DC.running:
        time.sleep(0.01)
        util.updateUI()
 def updateUI(self):
     updateUI()
    def plotInputTrace(self, _=None):
        #print "Plotting %d-%d for points %d-%d"%(params[0].value(), params[1].value(), params[2].value(), params[3].value())

        # Workaround: if source is set to None, don't plot anything
        if self._traceSource is None:
            self.clearPushed()
            return

        initialPersist = self.persistant
        if not self.persistant:
            self.clearPushed()

        plotlist, plotinfo = self.getPlotList()
        pstart = self.findParam('pointrng').getValue()[0]
        pend = self.findParam('pointrng').getValue()[1]
        dsmode = self.findParam('dsmode').getValue()
        yaxisScaleFactor = self.findParam(['Y Axis',
                                           'Scale Factor']).getValue()
        yaxisOffsetFactor = self.findParam(['Y Axis',
                                            'Offset Factor']).getValue()

        tstat_enabled = self.findParam(['tstat', 'enable']).getValue()
        ttstart = self.findParam(['tstat', 'range']).getValue()[0]
        ttend = self.findParam(['tstat', 'range']).getValue()[1]

        #Sanity check
        if pend < pstart or plotlist is None or len(plotlist) == 0:
            return

        try:
            progress_bar = None

            if len(plotlist) > 1 or tstat_enabled:
                self.setPersistance(True)

            yUnit = self.findParam(['Y Axis', 'Unity']).getValue()
            xUnit = self.findParam('X Axis').getValue()
            xScale = self.findParam('X Axis').getValueKey()
            self.pw.setLabel('bottom', text=xScale, units=xUnit)
            self.pw.setLabel('left', units=yUnit)
            xaxis = range(pstart + self._traceSource.offset(),
                          pend + self._traceSource.offset() + 1)
            if xScale == 'Time':
                tmp = float(self._traceSource.getSampleRate())
                if tmp == 0:
                    raise Warning(
                        'X Axis of type "Time" in "%s" can\'t have "Sample Rate" with value=0. '
                        'Change it to its actual value in the Trace Manager.' %
                        self.getName())
                xaxis = [v / tmp for v in xaxis]

            for i, tnum in enumerate(plotlist):
                trace = self._traceSource.getTrace(tnum)
                if trace is not None:
                    if yaxisScaleFactor != 1.0 or yaxisOffsetFactor != 0.0:
                        trace = [
                            yaxisOffsetFactor + x * yaxisScaleFactor
                            for x in trace
                        ]
                    #TODO - Not sure if should add _traceSource.offset() or not?
                    traceclr = plotinfo[i]['color']
                    self.passTrace(trace[pstart:pend + 1],
                                   pstart + self._traceSource.offset(),
                                   idString=str(tnum),
                                   xaxis=xaxis,
                                   dsmode=dsmode,
                                   color=traceclr)

                    if self.findParam('Redraw after Each').getValue():
                        util.updateUI()

            if tstat_enabled:
                ttrace = [0] * (pend - pstart + 1)
                tstat_leakage = self.findParam(['tstat', 'type']).getValue()
                if tstat_leakage == 'Text In':
                    b_list = np.array([
                        self._traceSource.getTextin(tnum)[0]
                        for tnum in range(ttstart, ttend + 1)
                    ])
                elif tstat_leakage == 'Text Out':
                    b_list = np.array([
                        self._traceSource.getTextout(tnum)[0]
                        for tnum in range(ttstart, ttend + 1)
                    ])
                elif tstat_leakage == 'Key':
                    b_list = np.array([
                        self._traceSource.getKnownKey(tnum)[0]
                        for tnum in range(ttstart, ttend + 1)
                    ])
                else:
                    raise NotImplementedError(
                        "Can't calculate t-statistics against input type %s" %
                        tstat_leakage)
                x_list = np.array([bin(x).count('1') for x in b_list])
                s_x = np.sum(x_list)
                mu_x = s_x / len(x_list)

                progress_bar = ProgressBarGUI("Computing T-Statistics",
                                              "Calculating T-Statistic:")
                progress_bar.setStatusMask("Current Point = %d", 0)
                progress_bar.setMaximum(pend + 1 - pstart)
                progress_bar.show()
                for i in range(pstart, pend + 1):
                    y_list = np.array([
                        self._traceSource.getTrace(tnum)[i]
                        for tnum in range(ttstart, ttend + 1)
                    ])
                    s_y = np.sum(y_list)
                    mu_y = s_y / len(y_list)
                    s_xx = np.sum(np.dot(x_list, x_list))
                    s_xy = np.sum(np.dot(x_list, y_list))

                    b_1 = (s_xy - mu_y * s_x) / (s_xx - s_x**2 / len(x_list))
                    b_0 = mu_y - b_1 * mu_x

                    y_est = b_0 + b_1 * x_list
                    err = y_est - y_list
                    s_ee = np.sum(np.dot(err, err))
                    se = np.sqrt(s_ee / (len(x_list) - 2) /
                                 (s_xx - s_x**2 / len(x_list)))

                    t_stat = b_1 / se
                    ttrace[i] = t_stat

                    prog = i + 1 - pstart
                    progress_bar.updateStatus(prog, prog)
                    if progress_bar.wasAborted():
                        break

                self.passTrace(ttrace[pstart:pend + 1],
                               pstart + self._traceSource.offset(),
                               idString='ttest',
                               xaxis=xaxis,
                               dsmode=dsmode,
                               color=0.0)
        except NotImplementedError as e:
            # This happens if we can't get text in/out or key from a trace source
            logging.info("Couldn't plot t-statistic; error message:%s" % e)
        finally:
            self.setPersistance(initialPersist)
            if progress_bar is not None:
                progress_bar.close()
示例#27
0
    def generatePartitionStats(self, partitionData={"partclass":None, "partdata":None}, saveFile=False, loadFile=False,  tRange=(0, -1), progressBar=None):

        traces = self._traces

        if tRange[1] < 0:
            tRange = (tRange[0], traces.numTraces() + 1 + tRange[1])

        self.partObject.setPartMethod(partitionData["partclass"])

        self.numKeys = len(self.partObject.partMethod.getPartitionNum(traces, 0))

        numPoints = traces.numPoints()

        if loadFile:
            cfgsecs = self.api.project().getDataConfig(sectionName="Trace Statistics", subsectionName="Total Trace Statistics")
            foundsecs = []
            for cfg in cfgsecs:
                desiredsettings = {}
                desiredsettings["tracestart"] = tRange[0]
                desiredsettings["traceend"] = tRange[1]
                desiredsettings["partitiontype"] = self.partObject.partMethod.__class__.__name__
                if self.api.project().checkDataConfig(cfg, desiredsettings):
                    foundsecs.append(cfg)
        else:
            foundsecs = []

        if len(foundsecs) > 1:
            IOError("Too many sections!!!")
        elif len(foundsecs) == 1:
            fname = self.api.project().convertDataFilepathAbs(foundsecs[0]["filename"])
            stats = np.load(fname)
        else:
            # Array to hold average + stddev of all traces/partitions
            A_k = []
            A_j = []
            Q_k = []
            dataArrays = [A_k, A_j, Q_k]
            ACnt = []
            for bnum in range(0, self.numKeys):
                for d in dataArrays:
                    d.append([])
                ACnt.append([])
                for i in range(0, self.partObject.partMethod.getNumPartitions()):
                    for d in dataArrays:
                        d[bnum].append(np.zeros(numPoints))
                    ACnt[bnum].append(0)

            # Get segment list
            segList = traces.getSegmentList()

            if progressBar:
                progressBar.setWindowTitle("Phase 1: Trace Statistics")
                progressBar.setMaximum(bnum)  # len(segList['offsetList']) * self.numKeys)
                progressBar.show()

            # TODO: Double-check this fix
            # for tsegn, segtrace in enumerate(segList['offsetList']):
            tsegn = 0
            if progressBar: progressBar.setText("Segment %d" % tsegn)

            # Average data needs to be calculated
            # Require partition list
            partData = partitionData["partdata"]


            # print "Calculating Average + Std-Dev"
            # Std-Dev calculation:
            # A[0] = 0
            # A[k] = A[k-1] + (x[k] - A[k-1]) / k
            # Q[0] = 0
            # Q[k] = Q[k-1] + (x[k] - A[k-1])(x[k] - A[k])
            for bnum in range(0, self.numKeys):
                progressBar.updateStatus(tsegn * self.numKeys + bnum)
                if progressBar.wasAborted():
                    break
                for i in range(0, self.partObject.partMethod.getNumPartitions()):
                    util.updateUI()
                    tlist = partData[bnum][i]
                    if len(tlist) > 0:
                        for tnum in tlist:
                            if tnum > tRange[0] and tnum < tRange[1]:
                                t = traces.getTrace(tnum)
                                ACnt[bnum][i] += 1
                                A_k[bnum][i] = A_k[bnum][i] + (t - A_j[bnum][i]) / ACnt[bnum][i]
                                Q_k[bnum][i] = Q_k[bnum][i] + ((t - A_j[bnum][i]) * (t - A_k[bnum][i]))
                                A_j[bnum][i] = A_k[bnum][i]

            if progressBar.wasAborted():
                progressBar.hide()
                return

            # Finally get variance
            for bnum in range(0, self.numKeys):
                    for i in range(0, self.partObject.partMethod.getNumPartitions()):
                        # TODO: Should be using population variance or sample variance (e.g. /n or /n-1)?
                        #      Since this is taken over very large sample sizes I imagine it won't matter
                        #      ultimately.
                        Q_k[bnum][i] = Q_k[bnum][i] / max(ACnt[bnum][i] - 1, 1)

            # Average is in A_k
            stats = {"mean":A_k, "variance":Q_k, "number":ACnt}

            # Wasn't cancelled - save this to project file for future use if requested
            if saveFile:
                progressBar.setText("Saving Mean/Variance Partitions")
                cfgsec = self.api.project().addDataConfig(sectionName="Trace Statistics", subsectionName="Total Trace Statistics")
                cfgsec["tracestart"] = tRange[0]
                cfgsec["traceend"] = tRange[1]
                cfgsec["partitiontype"] = self.partObject.partMethod.__class__.__name__
                fname = self.api.project().getDataFilepath('tracestats-%s-%d-%s.npz' % (cfgsec["partitiontype"], tRange[0], tRange[1]), 'analysis')

                # Save mean/variance for trace
                np.savez(fname["abs"], mean=A_k, variance=Q_k, number=ACnt)
                cfgsec["filename"] = fname["rel"]

        progressBar.hide()
        return stats
示例#28
0
 def write(self, s):
     self._output.write(s)
     updateUI()