def testToPyspLocalSetPixelWorld(self): ''' set the mandelbrot pixel in world coordinates ''' print "testZmqIfc.testToPyspLocalSetPixelWorld" hsh = PySpectra.toPyspLocal({'command': ['cls', 'delete']}) self.assertEqual(hsh['result'], 'done') hsh = PySpectra.toPyspLocal({'command': ['setWsViewport dina5s']}) self.assertEqual(hsh['result'], 'done') # # title: set pixels using world coordinates # hsh = PySpectra.toPyspLocal( {'command': ['setTitle "set pixels using world coordinates"']}) self.assertEqual(hsh['result'], 'done') (xmin, xmax) = (-2., -0.5) (ymin, ymax) = (0, 1.5) (width, height) = (100, 100) maxiter = 25 # # create the image # hsh = { 'Image': { 'name': "MandelBrot", 'xMin': xmin, 'xMax': xmax, 'width': width, 'yMin': ymin, 'yMax': ymax, 'height': height } } hsh = PySpectra.toPyspLocal(hsh) self.assertEqual(hsh['result'], 'done') # # fill the image, pixel by pixel # r1 = np.linspace(xmin, xmax, width) r2 = np.linspace(ymin, ymax, height) for i in range(width): for j in range(height): res = mandelbrot(r1[i] + 1j * r2[j], maxiter) hsh = { 'command': [ 'setPixelWorld MandelBrot %g %g %g' % (r1[i], r2[j], res) ] } hsh = PySpectra.toPyspLocal(hsh) self.assertEqual(hsh['result'], 'done') hsh = PySpectra.toPyspLocal({'command': ['display']}) self.assertEqual(hsh['result'], 'done') PySpectra.processEventsLoop(1) print "testZmqIfc.testToPyspLocalSetPixelWorld DONE" return
def testDisplaySingle( self): print "testMplGraphics.testDisplaySingle" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() sinus = PySpectra.Scan( name = 'sinus', xMin = 0., xMax = 6.0, nPts = 101, dType = np.float64, at = (2,2,3), lineColor = 'red', lineStyle = 'solid') sinus.y = np.sin( sinus.y) PySpectra.mtpltlb.graphics.display( ['sinus']) PySpectra.mtpltlb.graphics.processEventsLoop( 1) print "testMplGraphics.testDisplaySingle, DONE"
def testToPyspLocalCompleteImageByImage(self): ''' use Image to transfer a complete image at once ''' print "testZmqIfc.testToPyspLocalCompleteImageByImage" hsh = PySpectra.toPyspLocal({'command': ['cls', 'delete']}) self.assertEqual(hsh['result'], 'done') hsh = PySpectra.toPyspLocal({'command': ['setWsViewport dina5s']}) self.assertEqual(hsh['result'], 'done') # # setTitle # hsh = PySpectra.toPyspLocal({ 'command': ['setTitle "use Imageto transfer the complete image at once"'] }) self.assertEqual(hsh['result'], 'done') (xmin, xmax) = (-2., -0.5) (ymin, ymax) = (0, 1.5) (width, height) = (100, 100) maxiter = 25 # # fill the image, pixel by pixel # r1 = np.linspace(xmin, xmax, width) r2 = np.linspace(ymin, ymax, height) data = np.ndarray((width, height), np.float64) for i in range(width): for j in range(height): res = mandelbrot(r1[i] + 1j * r2[j], maxiter) data[i][j] = int(res) hsh = PySpectra.toPyspLocal({ 'Image': { 'name': "MandelBrot", 'data': data, 'xMin': xmin, 'xMax': xmax, 'yMin': ymin, 'yMax': ymax, } }) self.assertEqual(hsh['result'], 'done') hsh = PySpectra.toPyspLocal({'command': ['cls', 'display']}) self.assertEqual(hsh['result'], 'done') o = PySpectra.getGqe("Mandelbrot") self.assertEqual(o.height, 100) self.assertEqual(o.width, 100) self.assertEqual(o.xMin, xmin) self.assertEqual(o.xMax, xmax) self.assertEqual(o.yMin, ymin) self.assertEqual(o.yMax, ymax) PySpectra.processEventsLoop(2) print "testZmqIfc.testToPyspLocalCompleteImageByImage DONE" return
def test_title(self): print "testIFC.test_title" PySpectra.cls() PySpectra.delete() PySpectra.command("create s1") PySpectra.command("setTitle hallo") ret = PySpectra.getTitle() self.assertEqual(ret, "hallo") print "testIFC.test_title DONE" return
def cls(): ''' clear screen: allow for a new plot ''' #print( "mpl_graphics.cls") if Fig is None: _initGraphic() Fig.clear() Fig.canvas.flush_events() #plt.draw() if Canvas is not None: try: Canvas.draw() except Exception as e: print("mpl_graphics.cls: caught exception from Canvas.draw") print(repr(e)) # # clear the plotItems # gqeList = PySpectra.getGqeList() for scan in gqeList: scan.plotItem = None if type(scan) == PySpectra.Scan: scan.plotDataItem = None scan.lastIndex = 0
def cb_timerZMQ(self): """ checks whether there is a request on the ZMQ socket, from toPyspMonitor() - mvsa or another client fetches data from the pyspMonitor - a client sends data to be displayed by the pyspMonitor """ self.timerZMQ.stop() lst = zmq.select([self.sckt], [], [], 0.01) argout = {} # # if we received some input, we do another select to see, if # more input is arriving. this way the pyspMonitor appears # to be very responsive # while self.sckt in lst[0]: msg = self.sckt.recv() hsh = json.loads(msg) #print( "hsh %s " % repr( hsh)) if self.flagIsBusy: argout = { 'result': "pyspMonitor: rejecting dct while scanning" } else: argout = PySpectra.toPyspLocal(hsh) msg = json.dumps(argout) self.sckt.send(bytearray(msg, encoding="utf-8")) lst = zmq.select([self.sckt], [], [], 0.1) # # mandelbrot 20x20: if we change 10 to 1, time from 15s to 10s # self.timerZMQ.start(TIMEOUT_ZMQ)
def close(): if spectraInstalled and useSpectra: argout = True else: argout = PySpectra.close() return
def testSinusScan(self): graPyspIfc.setSpectra(False) graPyspIfc.cls() graPyspIfc.delete() sinus = graPyspIfc.Scan(name="sinus", lineColor='blue', xMin=-5, xMax=5., yMin=-1.5, yMax=1.5, nPts=21, yLabel='sin') sinus.y = numpy.sin(sinus.x) sinus.display() self.assertEqual(sinus.getCurrent(), 20) PySpectra.processEventsLoop(1) if utils.getHostname() != definitions.hostTK: return graPyspIfc.setSpectra(True) graPyspIfc.cls() graPyspIfc.delete() sinus = graPyspIfc.Scan(name="sinus", lineColor='blue', xMin=-5, xMax=5., yMin=-1.5, yMax=1.5, nPts=21, yLabel='sin') sinus.y = numpy.sin(sinus.x) sinus.display() time.sleep(2) self.assertEqual(sinus.getCurrent(), 20) graPyspIfc.close()
def test_toPyspLocalScan(self): import random print "testIFC.test_toPyspLocalScan" ret = PySpectra.toPyspLocal({'command': ["cls", "delete"]}) self.assertEqual(ret['result'], 'done') ret = PySpectra.toPyspLocal({ 'command': [ "setTitle \"An important title\"", "setComment \"An interesting comment\"" ] }) self.assertEqual(ret['result'], 'done') max = 101 name = "TestScan" ret = PySpectra.toPyspLocal({ 'Scan': { 'name': name, 'xMin': 0., 'xMax': 100., 'yMin': 0., 'yMax': 1., 'symbol': '+', 'symbolColor': 'red', 'symbolSize': 7, 'lineColor': 'blue', 'nPts': max, 'autoscaleX': False, 'autoscaleY': True } }) self.assertEqual(ret['result'], 'done') o = PySpectra.getGqe(name) self.assertEqual(o.nPts, max) self.assertEqual(o.symbol, '+') self.assertEqual(o.symbolColor, 'red') self.assertEqual(o.symbolSize, 7) self.assertEqual(o.lineColor, 'blue') self.assertEqual(o.lastIndex, -1) self.assertEqual(o.currentIndex, (max - 1)) for i in range(max): pos = float(i) posY = random.random() * 10 PySpectra.toPyspLocal({ 'command': ['setXY %s %d %s %s' % (name, i, repr(pos), repr(posY))] }) PySpectra.toPyspLocal({'command': ["display"]}) self.assertEqual(o.currentIndex, i) time.sleep(0.1) print "testIFC.test_toPyspLocalScan DONE" return
def test_y2my(self): print "testIFC.test_y2my" PySpectra.cls() PySpectra.delete() PySpectra.command("create s1") PySpectra.command("y2my s1") lst = PySpectra.getGqeList() self.assertEqual(lst[1].name, "s1_y2my") self.assertEqual(len(lst), 2) print "testIFC.test_y2my DONE" return
def testSSA(self): print "testPySpectra.testSSA" PySpectra.cls() PySpectra.delete() PySpectra.setTitle("test SSA") g = utils.createGauss(name="gauss", xMin=-5., xMax=5., nPts=101, lineColor='red', x0=0., sigma=1., amplitude=1.) g.ssa() self.assertEqual(len(g.textList), 5) # SSA results # midpoint: -6.84879e-06 # peak-x: 0 # cms: -9.65309e-05 # fwhm: 2.3552 # self.assertTrue(g.textList[0].text == 'SSA results') lst = g.textList[1].text.split(':') self.assertTrue(lst[0] == 'midpoint') self.assertTrue(abs(float(lst[1])) < 0.0001) lst = g.textList[2].text.split(':') self.assertTrue(lst[0] == 'peak-x') self.assertTrue(abs(float(lst[1])) < 0.0001) lst = g.textList[3].text.split(':') self.assertTrue(lst[0] == 'cms') self.assertTrue(abs(float(lst[1])) < 0.0001) lst = g.textList[4].text.split(':') self.assertTrue(lst[0] == 'fwhm') self.assertTrue(abs(float(lst[1])) < 2.356) self.assertTrue(abs(float(lst[1])) > 2.350) PySpectra.display() PySpectra.processEventsLoop(1) return
def _setWsViewport(line): ''' setWsViewPort dina4 dina4, dina4p, dina4s, dina5, dina5p, dina5s, dina6, dina6p, dina6s, ''' lst = None if line: lst = line.split(' ') if len(lst) > 1: raise ValueError("ifc.setWsViewport: expecting zero or one arguments") if len(lst) == 0: PySpectra.setWsViewport(None) else: PySpectra.setWsViewport(lst[0]) return "done"
def setWsViewport(size=None): ''' size: DINA4, DINA4P, DINA4S, DINA5, DINA5P, DINA5S, DINA6, DINA6L, DINA6S ''' if size is None: return if Fig is None: _initGraphic() if size.upper() == "DINA4" or size.upper() == "DINA4L": w = 29.7 h = 21 elif size.upper() == "DINA4P": w = 21 h = 29.7 elif size.upper() == "DINA4S": w = 29.7 h = 29.7 elif size.upper() == "DINA5" or size.upper() == "DINA5L": w = 21 h = 14.85 elif size.upper() == "DINA5P": w = 14.85 h = 21.0 elif size.upper() == "DINA5S": w = 21.0 h = 21.0 elif size.upper() == "DINA6" or size.upper() == "DINA6L": w = 14.85 h = 10.5 elif size.upper() == "DINA6P": w = 10.5 h = 14.85 elif size.upper() == "DINA6S": w = 14.85 h = 14.85 else: raise ValueError("graphics.setWsViewport: no valid size, %s" % size) fig = matplotlib.pyplot.gcf() fig.set_size_inches(w / 2.54, h / 2.54, forward=True) PySpectra.setWsViewportFixed(True) return
def _setTitle(line): ''' set the title of the whole plot ''' argout = "done" if not PySpectra.setTitle(line): argout = "trouble from PySpectra.setTitle()" return argout
def testCheckTargetWithinLimits(self): print("testPySpectra.testCheckTargetWithinLimits") if utils.getHostname() != definitions.hostTK: return PySpectra.cls() PySpectra.delete() g = PySpectra.Scan(name="gauss", xMin=-5., xMax=5., nPts=101, lineColor='red') mu = 0. sigma = 1. g.y = 1 / (sigma * np.sqrt(2. * np.pi)) * np.exp(-(g.y - mu)**2 / (2. * sigma**2)) g.x += 50 g.motorNameList = ["eh_mot66"] proxy = PyTango.DeviceProxy(g.motorNameList[0]) # # [ 0.0, 149.4232] # self.assertEqual( g.checkTargetWithinLimits(g.motorNameList[0], 51., proxy, flagConfirm=False), True) self.assertEqual( g.checkTargetWithinLimits(g.motorNameList[0], -151., proxy, flagConfirm=False), False) self.assertEqual( g.checkTargetWithinLimits(g.motorNameList[0], 151., proxy, flagConfirm=False), False) return
def testSetLimits(self): print "testPySpectra.testSetLimits" PySpectra.cls() PySpectra.delete() PySpectra.setTitle("test setLimits") scan = PySpectra.Scan( name='t1', xMin=0, xMax=10, nPts=11, ) self.assertEqual(scan.xMin, 0.) self.assertEqual(scan.xMax, 10.) self.assertEqual(scan.yMin, 0.) self.assertEqual(scan.yMax, 10.) scan.xMin = -1. scan.xMax = -1. scan.yMin = -1. scan.yMax = -1. scan.setLimits() self.assertEqual(scan.xMin, 0.) self.assertEqual(scan.xMax, 10.) self.assertEqual(scan.yMin, 0.) self.assertEqual(scan.yMax, 10.5)
def testGrid( self): ''' using showGridX, showGridY ''' print "testMplGraphics.testGrid" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() PySpectra.setTitle( "check grids") sinus = PySpectra.Scan( name = 'sinus', xMin = 0., showGridX = True, xMax = 6.0, nPts = 101, lineColor = 'red') cos = PySpectra.Scan( name = 'cos', xMin = 0., showGridY = True, xMax = 6.0, nPts = 101, lineColor = 'red') tan = PySpectra.Scan( name = 'tan', xMin = 0., showGridY = True, showGridX = True, xMax = 6.0, nPts = 101, lineColor = 'red') sinus.y = np.sin( sinus.y) cos.y = np.cos( cos.y) tan.y = np.tan( tan.y) PySpectra.mtpltlb.graphics.display() PySpectra.mtpltlb.graphics.display() #PySpectra.show() PySpectra.mtpltlb.graphics.processEventsLoop( 1) print "testMplGraphics.testGrid, DONE"
def testDisplaySymbol( self): print "testMplGraphics.testDisplaySymbol" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() sinus = PySpectra.Scan( name = 'sinus', xMin = 0., xMax = 6.0, nPts = 101, dType = np.float64, at = (2,2,3), symbolColor = 'red', symbol = 'o', symbolSize = 10) sinus.y = np.sin( sinus.y) PySpectra.mtpltlb.graphics.display( ['sinus']) #PySpectra.show() PySpectra.mtpltlb.graphics.processEventsLoop( 1) print "testMplGraphics.testDisplaySymbol. DONE"
def _setComment(line): ''' setComment "some comment" set the comment string for the whole plot ''' argout = "done" if not PySpectra.setComment(line): argout = "trouble from PySpectra.setComment()" return argout
def test_show(self): print "testIFC.test_show" PySpectra.cls() PySpectra.delete() PySpectra.command("create s1") PySpectra.command("show s1") print "testIFC.test_show DONE" return
def test_pdf(self): print "testIFC.test_pdf" PySpectra.command("cls") PySpectra.command("delete") PySpectra.command("create s1") PySpectra.command("createPDF testPDF") self.assertEqual(os.path.exists('testPDF.pdf'), True) #os.remove( 'testPDF.pdf') print "testIFC.test_pdf DONE" return
def test_create1(self): print "testIFC.test_create1" PySpectra.cls() PySpectra.delete() PySpectra.command("create s1 0 10 100") lst = PySpectra.getGqeList() self.assertEqual(len(lst), 1) self.assertEqual(lst[0].name, "s1") self.assertEqual(lst[0].nPts, 100) self.assertEqual(lst[0].xMin, 0.) self.assertEqual(lst[0].xMax, 10.) PySpectra.delete() print "testIFC.test_create1 DONE" return
def display(): """ in PySpectra there is mostly autoscale when displaying things therefore we do it when calling spectra as well """ if spectraInstalled and useSpectra: argout = Spectra.gra_command("display/autoscale/vp") else: argout = PySpectra.display() return argout
def testWsViewport( self): ''' ''' print "testGrphics.testWsViewport" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() PySpectra.setTitle( "go through the viewports") sinus = PySpectra.Scan( name = 'sinus', xMin = 0., xMax = 6.0, nPts = 101, lineColor = 'red', doty = True) sinus.y = np.sin( sinus.y) for elm in [ 'DINA4', 'DINA4P', 'DINA5', 'DINA5P', 'DINA6', 'DINA6P']: PySpectra.mtpltlb.graphics.setWsViewport( elm) PySpectra.mtpltlb.graphics.display() PySpectra.mtpltlb.graphics.processEventsLoop( 1) print "testGrphics.testWsViewport, DONE"
def testScanningAutoscaleX( self): ''' using setX and setY ''' print "testGrphics.testScanningAutoscaleX_v1" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() PySpectra.setTitle( "x-axis is re-scaled") sinus = PySpectra.Scan( name = 'sinus', xMin = 0., xMax = 6.0, nPts = 101, autoscaleX = True, lineColor = 'red') for i in range( sinus.nPts): sinus.setX( i, i/10.) sinus.setY( i, math.sin( i/10.)) PySpectra.mtpltlb.graphics.display( ['sinus']) time.sleep( 0.01) print "testGrphics.testScanningAutoscaleX_v1, DONE"
def testDoty( self): ''' using showGridX, showGridY ''' print "testMplGraphics.testDoty" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() PySpectra.setTitle( "check x-axis doty") sinus = PySpectra.Scan( name = 'sinus', xMin = 0., xMax = 6.0, nPts = 101, lineColor = 'red', doty = True) sinus.y = np.sin( sinus.y) PySpectra.mtpltlb.graphics.display() #PySpectra.show() PySpectra.mtpltlb.graphics.processEventsLoop( 1) print "testMplGraphics.testDoty, DONE"
def createGauss( name = "gauss", xMin = -5, xMax = 5., nPts = 101, lineColor = 'red', x0 = 0., sigma = 1., amplitude = 1.): g = PySpectra.Scan( name = name, xMin = xMin, xMax = xMax, nPts = nPts, lineColor = lineColor) g.y = amplitude/(sigma*np.sqrt(2.*np.pi))*np.exp( -(g.y-x0)**2/(2.*sigma**2)) g.yMin = yMin( g) g.yMax = yMax( g) return g
def testDisplayFour( self): print "testMplGraphics.testDisplayFour" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() for i in range( 1, 5): s = PySpectra.Scan( name = 't%d' % i, xMin = 0., xMax = 6.0, nPts = 101, dType = np.float64, xLabel = 'rad', yLabel = 'Signal', at = (2,2,i), lineColor = 'red', lineStyle = 'solid', lineWidth = 2.) s.y = np.tan( s.y) PySpectra.mtpltlb.graphics.display() PySpectra.mtpltlb.graphics.processEventsLoop( 1) print "testMplGraphics.testDisplayFour, DONE"
def createStep( name = "step", xMin = -10, xMax = 10., nPts = 101, lineColor = 'red', amplitude = 3.): g = PySpectra.Scan( name = name, xMin = xMin, xMax = xMax, nPts = nPts, lineColor = lineColor) g.y = (-np.arctan( g.x) + 1.5)*amplitude g.yMin = yMin( g) g.yMax = yMax( g) return g
def testScanningReverseAutoscaleX( self): ''' scanning in reverse direction ''' print "testGrphics.testScanningReverseAutoscaleX" PySpectra.mtpltlb.graphics.cls() PySpectra.delete() PySpectra.setTitle( "reverse scannning, re-scale") sinus = PySpectra.Scan( name = 'sinus', xMin = 0., xMax = 6.0, nPts = 101, autoscaleX = True, lineColor = 'red') sinus.xMax = 10. for i in range( sinus.nPts): x = 10. - i/10. sinus.setX( i, x) sinus.setY( i, math.sin( i/10.)) PySpectra.mtpltlb.graphics.display( ['sinus']) time.sleep( 0.01) print "testGrphics.testScanningReverseAutoscaleX, DONE"