Beispiel #1
0
    def testNextPrevImage(self):

        print "testPySpectra.testNextPrevImage"
        PySpectra.delete()
        PySpectra.Image(name="t1", data=np.empty((100, 100)))
        PySpectra.Image(name="t2", data=np.empty((100, 100)))
        PySpectra.Image(name="t3", data=np.empty((100, 100)))
        PySpectra.Image(name="t4", data=np.empty((100, 100)))
        self.assertEqual(PySpectra.nextImage().name, 't1')
        self.assertEqual(PySpectra.nextImage().name, 't2')
        self.assertEqual(PySpectra.nextImage().name, 't3')
        self.assertEqual(PySpectra.nextImage().name, 't4')
        self.assertEqual(PySpectra.nextImage().name, 't1')
        PySpectra.delete()
        PySpectra.Image(name="t1", data=np.empty((100, 100)))
        PySpectra.Image(name="t2", data=np.empty((100, 100)))
        PySpectra.Image(name="t3", data=np.empty((100, 100)))
        PySpectra.Image(name="t4", data=np.empty((100, 100)))
        self.assertEqual(PySpectra.prevImage().name, 't1')
        self.assertEqual(PySpectra.prevImage().name, 't4')
        self.assertEqual(PySpectra.prevImage().name, 't3')
        self.assertEqual(PySpectra.prevImage().name, 't2')
        self.assertEqual(PySpectra.prevImage().name, 't1')

        print "testPySpectra.testNextPrevImage, DONE"
Beispiel #2
0
    def testImageMB1( self): 
        print "testPySpectra.testImageMB1"

        PySpectra.mtpltlb.graphics.cls()
        PySpectra.delete()

        (xmin, xmax) = (-2., 1)
        (ymin, ymax) = (-1.5, 1.5)
        (width, height) = (500, 500)
        maxiter = 20

        r1 = np.linspace(xmin, xmax, width)
        r2 = np.linspace(ymin, ymax, height)
        n3 = np.empty((width,height))
        for i in range(width):
            for j in range(height):
                n3[i,j] = self.mandelbrot(r1[i] + 1j*r2[j],maxiter)
            
        m = PySpectra.Image( name = "FirstImage", data = n3, 
                             xMin = xmin, xMax = xmax, width = width,  
                             yMin = ymin, yMax = ymax, height = height, 
                             xLabel = "eh_mot01", yLabel = "eh_mot02")

        self.assertEqual( m.xMin, xmin)
        self.assertEqual( m.xMax, xmax)
        self.assertEqual( m.yMin, ymin)
        self.assertEqual( m.yMax, ymax)
        self.assertEqual( m.height, height)
        self.assertEqual( m.width, width)

        self.assertEqual( m.data.shape[0], width)
        self.assertEqual( m.data.shape[1], height)

        PySpectra.mtpltlb.graphics.display()
        PySpectra.mtpltlb.graphics.processEventsLoop( 1)
Beispiel #3
0
def example_ImageRandom():

    import random

    PySpectra.setWsViewport('DINA5S')

    PySpectra.cls()
    PySpectra.delete()

    (xmin, xmax) = (-2., 1)
    (ymin, ymax) = (-1.5, 1.5)
    (width, height) = (500, 500)

    r1 = np.linspace(xmin, xmax, width)
    r2 = np.linspace(ymin, ymax, height)
    n3 = np.empty((width, height))
    for i in range(width):
        for j in range(height):
            n3[i, j] = i + random.random() * j + 100.

    m = PySpectra.Image(name="ImageRandom",
                        data=n3,
                        xMin=xmin,
                        xMax=xmax,
                        width=width,
                        yMin=ymin,
                        yMax=ymax,
                        height=height,
                        xLabel="x-Axis",
                        yLabel="y-Axis")

    PySpectra.cls()
    PySpectra.display()
    return
Beispiel #4
0
def example_ImageMB():

    PySpectra.setWsViewport('DINA5S')

    PySpectra.cls()
    PySpectra.delete()

    (xmin, xmax) = (-2., 0.5)
    (ymin, ymax) = (-1.25, 1.25)
    (width, height) = (750, 750)
    maxiter = 512

    m = PySpectra.Image(name="MandelbrotSet",
                        flagAxes=True,
                        maxIter=maxiter,
                        xMin=xmin,
                        xMax=xmax,
                        width=width,
                        yMin=ymin,
                        yMax=ymax,
                        height=height)
    m.flagZoomMbSlow = False
    m.zoomMb()
    PySpectra.cls()
    PySpectra.display()
    return
Beispiel #5
0
def _putData(hsh):
    '''
    a plot is created based on a dictionary 
    the use case: some data are sent pyspMonitor
    '''

    argout = 'n.n.'
    if 'title' in hsh:
        PySpectra.setTitle(hsh['title'])

    try:
        if 'columns' in hsh:
            PySpectra.delete()
            PySpectra.cls()
            argout = utils.createScansByColumns(hsh)
        elif 'gqes' in hsh:
            PySpectra.delete()
            PySpectra.cls()
            try:
                argout = utils.createScansByGqes(hsh)
            except Exception as e:
                print("zmqIfc: %s" % repr(e))
        elif 'images' in hsh:
            for h in hsh['images']:
                PySpectra.Image(**h)
            argout = "done"
        else:
            raise Exception("zmqIfc._putData", "expecting 'columns', 'gqes'")
    except Exception as e:
        argout = "zmqIfc._putData: %s" % repr(e)

    return argout
Beispiel #6
0
def example_ScanningMesh():
    '''    
    '''
    PySpectra.cls()
    PySpectra.delete()

    (xmin, xmax) = (-2., 1)
    (ymin, ymax) = (-1.5, 1.5)
    (width, height) = (20, 20)
    maxiter = 20

    r1 = np.linspace(xmin, xmax, width)
    r2 = np.linspace(ymin, ymax, height)
    n3 = np.zeros((width, height))

    m = PySpectra.Image(name="MandelbrotSet",
                        colorMap='Greys',
                        estimatedMax=20,
                        xMin=xmin,
                        xMax=xmax,
                        width=width,
                        yMin=ymin,
                        yMax=ymax,
                        height=height)

    PySpectra.setTitle("Simulate a mesh scan")
    PySpectra.setWsViewport("DINA5")
    sinus = PySpectra.Scan(name='sinus',
                           xMin=0.,
                           xMax=6.0,
                           nPts=width * height,
                           autoscaleX=False,
                           lineColor='red')
    cosinus = PySpectra.Scan(name='cosinus',
                             xMin=0.,
                             xMax=6.0,
                             nPts=width * height,
                             autoscaleX=False,
                             lineColor='red')

    PySpectra.display()

    (iI, jI) = (0, 0)
    for i in range(sinus.nPts):
        x = float(i) * 6.28 / float(sinus.nPts)
        sinus.setX(i, x)
        cosinus.setX(i, x)
        sinus.setY(i, math.sin(x))
        cosinus.setY(i, math.cos(x))
        res = mandelbrot(r1[iI] + 1j * r2[jI], maxiter)
        m.data[iI][jI] = res
        iI += 1
        if iI == width:
            iI = 0
            jI += 1
        PySpectra.display()
    PySpectra.cls()
    PySpectra.display()
    return
Beispiel #7
0
    def testWriteReadImage(self):
        print "testPySpectra.testWrite"
        PySpectra.cls()
        PySpectra.delete()

        (xmin, xmax) = (-2., 1)
        (ymin, ymax) = (-1.5, 1.5)
        (width, height) = (200, 200)
        maxiter = 100

        m = PySpectra.Image(name="MandelbrotSet",
                            colorMap='Greys',
                            estimatedMax=maxiter,
                            xMin=xmin,
                            xMax=xmax,
                            width=width,
                            yMin=ymin,
                            yMax=ymax,
                            height=height)

        m.zoomMb(flagDisplay=False)

        ret = PySpectra.write(['MandelbrotSet'])

        PySpectra.cls()
        PySpectra.delete()
        PySpectra.setTitle("create Mandelbrotset; write; read; display")

        self.assertEqual(os.path.exists(ret), True)

        PySpectra.read(ret)

        PySpectra.setTitle("The Mandelbrotset")
        PySpectra.display()
        PySpectra.processEventsLoop(2)

        lst = PySpectra.getGqeList()
        self.assertEqual(len(lst), 1)

        ima = lst[0]
        self.assertEqual(ima.name, "MandelbrotSet")
        self.assertEqual(type(ima), PySpectra.PySpectra.Image)
        self.assertEqual(ima.width, width)
        self.assertEqual(ima.height, height)

        self.assertEqual(ima.xMin, xmin)
        self.assertEqual(ima.xMax, xmax)
        self.assertEqual(ima.yMin, ymin)
        self.assertEqual(ima.yMax, ymax)

        ima.zoomMb(targetIX=50, targetIY=100, flagDisplay=False)
        PySpectra.display()
        PySpectra.processEventsLoop(1)

        ima.zoomMb(targetIX=50, targetIY=100, flagDisplay=False)
        PySpectra.display()
        PySpectra.processEventsLoop(1)

        return
Beispiel #8
0
    def testDoubles(self):

        print "testPySpectra.testDoubles"

        PySpectra.delete()

        with self.assertRaises(ValueError) as context:
            t1 = PySpectra.Scan(name="t1", xMin=-5., xMax=5., nPts=101)
            t2 = PySpectra.Scan(name="t1", xMin=-5., xMax=5., nPts=101)

        #print("testDoubles: caught %s" % repr( context.exception))
        self.assertTrue(
            "GQE.Scan.__init__(): t1 exists already" in context.exception)
        PySpectra.delete()
        with self.assertRaises(ValueError) as context:
            t1 = PySpectra.Image(name="t1", data=np.empty((100, 100)))
            t1 = PySpectra.Image(name="t1", data=np.empty((100, 100)))

        #print repr( context.exception)
        self.assertTrue(
            "GQE.Image.__init__(): t1 exists already" in context.exception)
Beispiel #9
0
    def testImageMB2( self): 
        print "testGraphics.testImageMB2"

        PySpectra.setWsViewport( 'DINA5S')

        PySpectra.cls()
        PySpectra.delete()

        (xmin, xmax) = (-2., 1)
        (ymin, ymax) = (-1.5, 1.5)
        (width, height) = (300, 300)
        maxiter = 20
        #
        # create the image by supplying the limits
        #
        m = PySpectra.Image( name = "MandelbrotSet2", 
                             xMin = xmin, xMax = xmax, width = width,  
                             yMin = ymin, yMax = ymax, height = height, 
                             xLabel = "eh_mot01", yLabel = "eh_mot02")

        r1 = np.linspace(xmin, xmax, width)
        r2 = np.linspace(ymin, ymax, height)
        for i in range(width):
            for j in range(height):
                res = self.mandelbrot(r1[i] + 1j*r2[j],maxiter)
                m.setPixelWorld( x = r1[i], y = r2[j], value = res)

            PySpectra.cls()
            PySpectra.display()
        PySpectra.processEventsLoop(1)

        self.assertEqual( m.xMin, xmin)
        self.assertEqual( m.xMax, xmax)
        self.assertEqual( m.yMin, ymin)
        self.assertEqual( m.yMax, ymax)
        self.assertEqual( m.height, height)
        self.assertEqual( m.width, width)
        #
        # to understand '+ 1'consider : x [-2, 1], width 100
        #  if x == 1 -> ix == 100, so we need '+ 1'
        #
        self.assertEqual( m.data.shape[0], width)
        self.assertEqual( m.data.shape[1], height)
        return 
Beispiel #10
0
    def testImageMB1( self): 
        print "testGraphics.testImageMB1"

        PySpectra.setWsViewport( 'DINA5S')

        PySpectra.cls()
        PySpectra.delete()

        (xmin, xmax) = (-2., 1)
        (ymin, ymax) = (-1.5, 1.5)
        (width, height) = (500, 500)
        maxiter = 20

        r1 = np.linspace(xmin, xmax, width)
        r2 = np.linspace(ymin, ymax, height)
        n3 = np.empty((width, height))
        for i in range(width):
            for j in range(height):
                n3[i,j] = self.mandelbrot(r1[i] + 1j*r2[j],maxiter)
        #
        # create the image by supplying data
        #
        m = PySpectra.Image( name = "MandelbrotSet1", data = n3,
                             xMin = xmin, xMax = xmax, width = width,  
                             yMin = ymin, yMax = ymax, height = height, 
                             xLabel = "eh_mot01", yLabel = "eh_mot02")

        PySpectra.display()

        self.assertEqual( m.xMin, xmin)
        self.assertEqual( m.xMax, xmax)
        self.assertEqual( m.yMin, ymin)
        self.assertEqual( m.yMax, ymax)
        self.assertEqual( m.height, height)
        self.assertEqual( m.width, width)
        #
        # to understand '+ 1'consider : x [-2, 1], width 100
        #  if x == 1 -> ix == 100, so we need '+ 1'
        #
        self.assertEqual( m.data.shape[0], width)
        self.assertEqual( m.data.shape[1], height)

        PySpectra.processEventsLoop( 2)
Beispiel #11
0
    def testImageMB2( self): 
        print "testPySpectra.testImageMB2"

        PySpectra.mtpltlb.graphics.cls()
        PySpectra.mtpltlb.graphics.setWsViewport( 'DINA5S')
        PySpectra.delete()

        (xmin, xmax) = (-2., 1)
        (ymin, ymax) = (-1.5, 1.5)
        (width, height) = (20, 20)
        maxiter = 50

        m = PySpectra.Image( name = "FirstImage", xMin = xmin, xMax = xmax, width = width,  
                            yMin = ymin, yMax = ymax, height = height)

        r1 = np.linspace(xmin, xmax, width)
        r2 = np.linspace(ymin, ymax, height)
        for i in range(width):
            for j in range(height):
                res = self.mandelbrot(r1[i] + 1j*r2[j],maxiter)
                m.setPixelWorld( x = r1[i], y = r2[j], value = res)
            PySpectra.mtpltlb.graphics.cls()
            PySpectra.mtpltlb.graphics.display()
        PySpectra.mtpltlb.graphics.processEventsLoop( 1.)
Beispiel #12
0
def toPyspLocal(hsh):
    '''
    toPyspLocal executes dictionaries which are received 
      - from toPyspMonitor()
      - from a Queue, from pyspDoor
      - from an application directly, maybe to simulate the toPyspMonitor interface

       Command, see PySpectra.ipython.ifc?
         {'command': ['delete']}
           delete all internal data

         {'command': ['cls']}
           clear the screen

         {'command': ['delete', 'cls']}
           deletes all internal data and clears the screen

         {'command': ['display']}
           display all GQEs

         { 'command': ['setArrowCurrent sig_gen position 1.234']}
         { 'command': ['setArrowCurrent sig_gen show']}
         { 'command': ['setArrowCurrent sig_gen hide']}
           handle the arrow pointing to the current position. sig_gen is
           the Scan containing data from the SignalCounter (MS env. variable)

         { 'command': ['setArrowSetPoint sig_gen position 1.234']}
         { 'command': ['setArrowSetPoint sig_gen show']}
         { 'command': ['setArrowSetPoint sig_gen hide']}
           handle the arrow pointing to the setpoint

         { 'command': ['setArrowMisc sig_gen position 1.234']}
         { 'command': ['setArrowMisc sig_gen show']}
         { 'command': ['setArrowMisc sig_gen hide']}
           handle the arrow pointing to a position defined e.g. by mvsa

         {'command': [u'setTitle "ascan exp_dmy01 0.0 1.0 3 0.2"']}
           set the title for the whole widget
         {'command': [u'setComment "tst_01366.fio, Wed Dec 18 10:02:09 2019"']}
           set the comment for the whole widget

         {'spock': [ 'umv eh_mot66 51']}

       Misc:
         {'isAlive': True}
           return values:
             { 'result': 'done'}
             { 'result': 'notAlive'}

         {'getDoorState': True}
           returns:
             { 'result': 'ON'}

       Scan, see PySpectra.Scan?
         {'Scan': {'name': 'eh_c01', 'xMax': 1.0, 'autoscaleX': False, 'lineColor': 'red', 'xMin': 0.0, 'nPts': 6}}
           create an empty scan. Notice, the inner dictionary is passed to the Scan constructor

         {'Scan': {'yMax': 2.0, 'symbol': '+', 'autoscaleY': False, 'autoscaleX': False, 
                   'xMax': 1.0, 'nPts': 24, 'symbolColor': 'red', 'name': 'MeshScan', 
                   'symbolSize': 5, 'lineColor': 'None', 'xMin': 0.0, 'yMin': 1.0}}
           create an empty scan setting more attributes.

         {'Scan': {'name': 'eh_mca01', 'flagMCA': True, 'lineColor': 'blue', 
                   'y': array([  0., ..., 35.,  30.]), 
                   'x': array([  0.0, ..., 2.047e+03])}}
           create an MCA scan, which is re-used

         {'command': ['setY eh_c01 0 71.41']}
           set a y-value of eh_c01, index 0

         {'command': ['setX eh_c01 0 0.0']}
           set a x-value of eh_c01, index 0

         {'command': ['setXY MeshScan 0 0.0 1.0']}
           set the x- and y-value by a single call, index is 0

         {'putData': { 'columns': [{'data': [0.0, ..., 0.96], 'name': 'eh_mot01'}, 
                                  {'data': [0.39623, ... 0.01250], 'name': 'eh_c01'}, 
                                  {'showGridY': False, 'symbolColor': 'blue', 'showGridX': False, 
                                   'name': 'eh_c02', 'yLog': False, 'symbol': '+', 
                                   'data': [0.1853, ... 0.611], 
                                   'xLog': False, 'symbolSize': 5}], 
                      'title': 'a title', 
                      'comment': 'a comment'}}
           The data are sent as a list of dictionaries containing columns. The first column 
           is the common x-axis. All columns have to have the same length. 
           In this example, the Scans eh_c01 and eh_c02 are created. The common x-axis is given by eh_mot01

         { 'putData': {'gqes': [ {'x': x, 'y': tan, 'name': 'tan'},
                                 {'x': x, 'y': cos, 'name': 'cos'},
                                 {'x': x, 'y': sin, 'name': 'sin', 
                                  'showGridY': False, 'symbolColor': 'blue', 'showGridX': True, 
                                  'yLog': False, 'symbol': '+', 
                                  'xLog': False, 'symbolSize':5}],
                       'title': 'a title', 
                       'comment': 'a comment'}}
           The data are sent as a list of dictionaries containg the x- and y-data and other
           parameters describing the Scans.

       Image, see PySPectra.PySpectra.Image?
         {'Image': {'name': 'MandelBrot', 
                    'height': 5, 'width': 5, 
                    'xMax': -0.5, 'xMin': -2.0, 
                    'yMin': 0, 'yMax': 1.5}}
           create an empty image

         { 'Image': { 'name': "MandelBrot", 'data': data, 
                      'xMin': xmin, 'xMax': xmax, 
                      'yMin': ymin, 'yMax': ymax}})
           pass the entire image data

         { 'putData': { 'images': [{'name': "Mandelbrot", 'data': data,
                                    'xMin': xmin, 'xMax': xmax, 
                                    'yMin': ymin, 'yMax': ymax}]}}
           data is a numpy array, e.g.: data = np.ndarray( (width, height), np.float64)

         {'command': ['setPixelImage Mandelbrot 1 3 200']}
           set a pixel value. the position is specified by indices

         {'command': ['setPixelWorld Mandelbrot 0.5 1.5 200']}
           set a pixel value. the position is specified by world coordinate

         Text
         {'command': ['setText MeshScan comment string "Sweep: 1/4" x 0.05 y 0.95']}
           create a text GQE for the scan MeshScan, the name of the text is comment

       Retrieve data:
         {'getData': True}
           {'getData': {'EH_C02': {'y': [0.3183, ... 0.6510], 'x': [0.0, ... 0.959]}, 
                        'EH_C01': {'y': [0.0234, ... 0.4918], 'x': [0.0, ... 0.959]}}, 
            'result': 'done'}

    '''
    #print( "zmqIfc.toPyspLocal: %s" % repr( hsh))
    argout = {}
    #
    # command
    #
    if 'command' in hsh:
        argout['result'] = _execCommand(hsh)
    #
    # spock
    #
    elif 'spock' in hsh:
        argout['result'] = _execSpockCommand(hsh)
    #
    # doorState
    #
    elif 'getDoorState' in hsh:
        argout['result'] = _getDoorState()
    #
    # putData
    #
    elif 'putData' in hsh:
        argout['result'] = _putData(hsh['putData'])
    #
    # getData
    #
    elif 'getData' in hsh:
        try:
            argout['getData'] = PySpectra.getData()
            argout['result'] = 'done'
        except Exception as e:
            argout['getData'] = {}
            argout['result'] = "zmqIfc.toPyspLocal: error, %s" % repr(e)
    #
    # the 'isAlive' question comes from a toPyspMonitor() client, not from the door
    #
    elif 'isAlive' in hsh:
        argout['result'] = 'done'
    #
    # image
    #
    elif 'Image' in hsh:
        try:
            #
            # '**hsh': unpacked dictionary
            #
            PySpectra.Image(**hsh['Image'])
            argout['result'] = 'done'
        except Exception as e:
            argout['result'] = "zmqIfc.toPyspLocal: error, %s" % repr(e)
    #
    # Scan
    #
    elif 'Scan' in hsh:
        #
        if 'flagMCA' in hsh['Scan'] and hsh['Scan']['flagMCA'] is True:
            _storeMCAData(hsh['Scan'])
            argout['result'] = 'done'
        else:
            try:
                #
                # '**hsh' unpacked dictionary
                #
                PySpectra.Scan(**hsh['Scan'])
                argout['result'] = 'done'
            except Exception as e:
                argout['result'] = "zmqIfc.toPyspLocal: error, %s" % repr(e)
    #
    # ... else
    #
    else:
        argout[
            'result'] = "zmqIfc.toPyspLocal: error, failed to identify %s" % repr(
                hsh)
    #
    # getDoorState is answered with a value, not with 'done'
    #
    if not 'getDoorState' in hsh:
        if argout['result'].upper() != "DONE":
            print("zmqIfc.toPyspLocal: no 'DONE', instead received '%s' " %
                  argout['result'])
            print("zmqIfc.toPyspLocal: input: %s " % repr(hsh))

    #print( "zmqIfc.toPyspLocal: return %s" % repr( argout))
    return argout