Menu

WXPYTHON INTERFACE FOR SPy.

Help
2012-04-30
2013-01-25
  • Nobody/Anonymous

    I am trying to create a GUI interface that could do what Spy does through command line.Wx Python even handler works fine.But while loading the the HSi images takes infinite amount of time…with the message "Initializing graphic handlers"
    Please help.
    Regards

    import wx
    import os
    from spectral import *
    from spectral.graphics.hypercube import hypercube
    import matplotlib
    import OpenGL
    import PIL

    class Frame(wx.Frame):
        def __init__(self, title):
            wx.Frame.__init__(self, None, title=title, size=(1000,70),style=wx.MINIMIZE_BOX|wx.CLOSE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|wx.CAPTION|wx.CLIP_CHILDREN)
            self.Bind(wx.EVT_CLOSE, self.OnClose)
            panel=wx.Panel(self,-1)
            self.button=wx.Button(panel,label="Open",pos=(0,0),size=(50,30))
            self.button1=wx.Button(panel,label="Save",pos=(51,0),size=(50,30))
            self.button2=wx.Button(panel,label="ROI",pos=(102,0),size=(50,30))
            self.button3=wx.Button(panel,label="Tone",pos=(153,0),size=(50,30))
            self.slider=wx.Slider(panel,pos=(204,0))
            self.button4=wx.Button(panel,label="Header",pos=(305,0),size=(50,30))
            self.button5=wx.Button(panel,label="Cube",pos=(356,0),size=(50,30))
            self.SetBackgroundColour((11, 11, 11))
            self.Bind(wx.EVT_BUTTON, self.OnCubeClick,self.button5)
            self.Bind(wx.EVT_BUTTON, self.OnHeadClick,self.button4)
            self.Bind(wx.EVT_BUTTON, self.OnSaveClick,self.button1)
            self.Bind(wx.EVT_BUTTON, self.OnButtonClick,self.button)
            self.loc=wx.TextCtrl(panel,pos=(700,0), size=(300,-1))
           
           
           

        def OnButtonClick(self,event):
            wild="HSi Files|*.lan*|All Files|*.*"
            dlg=wx.FileDialog(self,message="Choose a File",wildcard=wild,style=wx.FD_OPEN)
       
            if dlg.ShowModal() == wx.ID_OK:
                self.loc.SetValue(dlg.GetPath())
                dlg.Destroy()
                #self.Onview()

        def Onview(self):
           
            filepath=self.loc.GetValue()
            img=image(filepath)
            view(img)
               

        def OnHeadClick(self,event):
            fileo=self.loc.GetValue()
            img=image(fileo)
            img.__class__
            print img

        def OnCubeClick(self,event):
            fileo=self.loc.GetValue()
           
            img= image(fileo).load()
            (m, c) = cluster(img, 20)
            view_indexed(m)

        def OnSaveClick(self,event):
            wild="HSi Files|*.lan*|All Files|*.*"
            dlg=wx.FileDialog(self,message="Save AS",wildcard=wild,style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
            if dlg.ShowModal() == wx.ID_OK:
                path=dlg.GetPath()
                self.Save(path)
                self.file=path
                dlg.Destroy()
               
            
        def OnClose(self, event):
            dlg = wx.MessageDialog(self,
                "Do you really want to close BBvw ?",
                "Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
            result = dlg.ShowModal()
            dlg.Destroy()
            if result == wx.ID_OK:
                self.Destroy()

    app = wx.App(redirect=True)
    top = Frame("BBvw")
    top.Show()
    app.MainLoop()

     
  • Thomas Boggs

    Thomas Boggs - 2012-04-30

    SPy starts wx in a separate thread so that the python command line will still be available while displaying graphical windows.  Starting the separate wx thread takes some time to complete so you may be experiencing a race condition.  You can get around this by explicitly initializing the wx thread and adding a small delay to allow it to complete.

    I replaced the line

    from spectral import *
    

    in your code with the following and it appeared to function properly:

    from spectral import *
    init_graphics()
    import time
    time.sleep(1)
    

    Also, since SPy depends on OpenGL, matplotlib, and PIL, you may want to import those prior to spectral.  That may make the time delay unnecessary.

    -thomas

     
  • Nobody/Anonymous

    It works.
    I  missed the trick ,
    I was using initGraphics() method.(also mentioned in documentation)
    Still, when i double click the opened image plot refuses to display .
    Thnks

     
  • Thomas Boggs

    Thomas Boggs - 2012-04-30

    It works for me. Using the code you emailed, I made the changes I noted above, uncommented the lines in the OnButtonClick on Onview methods, and the image appeared as expected when I opened the 92AV3C.lan file.  When I double-clicked on the image, the pixel spectrum was displayed in a matplotlib window.

    If you haven't already done so perhaps try calling the same sequence of SPy calls from the python interpreter to see if you get any errors.

    -thomas

     
  • Nobody/Anonymous

    I get a blank window when I double click the image when using the IDLE.
    Windows7 64bit,64 bit hardware, 32bit python and everything else.

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-01

    What is "the IDLE"?  The IDLE python IDE?  If so, please check that you can open an image with SPy from a python terminal (console or xterm) and double-click to open a spectrum plot.  If that doesn't work, then the problem is something related to your matplotlib installation (perhaps a problem with the backend installed).

     
  • Nobody/Anonymous

    I guess you are right

    import matplotlib
    import numpy
    print matplotlib
    data = numpy.loadtxt('C:\Users\zero\Desktop\example.fiat')
    ra = data
    dec = data
    mag = data
    color (scatter(ra,dec,c=mag,cmap=cm.hsv))


    Error
    Traceback (most recent call last):
      File "C:/Users/zero/Desktop/New folder/mat.py", line 1, in <module>
        import matplotlib
      File "C:/Users/zero/Desktop/New folder\matplotlib.py", line 2, in <module>
        import matplotlib.pyplot as plt
    ImportError: No module named pyplot


    I tried the above code.
    Also , matplotlib seems to be installed.
    print matplotlib
    <module 'matplotlib' from 'C:\Python27\lib\site-packages\matplotlib\__init__.pyc'>


    I will reinstall matplotlib.
    Should I install pylab?

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-01

    It looks like you have a problem with your matplotlib installation since pyplot should  be included as a submodule.  I suggest starting with a web search for "No module named pyplot".  If your matplotlib is installed properly, you should be able to run any of the basic examples in the matplotlib tutorial (http://matplotlib.sourceforge.net/users/pyplot_tutorial.html).

     
  • Nobody/Anonymous

    Dear thomas,

                            /Referring to  code in post 1/
                             Once the image is opened I close the image window using the X mark .
                             I noticed that even though I do this,The image is still loaded in memory.
                             How to get around this?
                             After I use the above code for multiple times my entire memory is full of pythonw processes

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-04

    I am not able to reproduce that.  I've tried opening the file multiple times, opening plots and hypercubes, then closing them.  Memory usage always drops down to about 80 MB.  I also tried with a 134 MB image and opened it (plus plots and hypercube) about a dozen times without exiting the app.  The memory jumped up to about 400 MB with everything open but always went back down to about 80 MB after they were closed.  There are only 4 threads running after I close the image windows (no additional pythonw processes), even after opening the file numerous times.

     
  • Nobody/Anonymous

    Sir,

           Thanks for checking with my problem.I have no clue why this is happening with my system.
           I will try to thread the GUI.Or use PIL to display JPGs.
           For the audio component I am using scipy over scikits.audiolabs.For 64 bit windows running 32 bit modules and python         and finding the correct matches between them is quite demanding. easy_install runs into errors.
            Could you suggest a way to save a child window(image window)  from a button on the main panel?

            When could I expect the spectral-cluster view…I am excited about that inclusion into Spec Py.

           I am grateful for your kind support.

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-05

    There must be a simple way to grab a wx frame's image buffer to save the data but I don't know off-hand how.  I suggest searching the web for that.  If you want to save a SPy raster image, there is a save_image function to do that.  If you just want to get the pixel data to be displayed from SPy without using SPy to do the rendering, then you can use the get_image_display_data function after importing it as follows:

    from spectral.graphics.graphics import get_image_display_data
    

    The ND data display will probably be available in a few weeks but I can't give a specific date.

     
  • Nobody/Anonymous

    Sir,
           I installed the MKL, numpy,scipy dependencies and then
           I have reinstalled matplotlib, from Christopher Gohlke website.
           The plot function refuses to run,I get to see a  blank screen,
           The hypercube fuction fails to exceute and indicates a problem in the self.read_band method in line 238 ,239
            Please help.


    this code is working.
           import numpy
    import pylab
    t = numpy.arange(0.0, 1.0+0.01, 0.01)
    s = numpy.cos(2*2*numpy.pi*t)
    pylab.plot(t, s)
    pylab.xlabel('time (s)')
    pylab.ylabel('voltage (mV)')
    pylab.title('About as simple as it gets, folks')
    pylab.grid(True)
    pylab.savefig('simple_plot')
    pylab.show()


    I have improved code , but It is more or less like the one in post 1

    Regards    
       

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-08

    You haven't given enough info for me to help you.  I would need to see an actual error message (there are read_band functions in numerous files).  Before doing anything with your wx app, determine whether the various SPy plotting functions work from the python command line (as in the User Guide); otherwise, it will be difficult to determine if the problem is with SPy, one of its dependencies, or your app.

     
  • Nobody/Anonymous



    I tried the same in command line and they dont seem to work.
    Please see the attached images.

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-09

    Regarding calling hypercube , I would call init_graphics first, as described in my initial reply.  Regarding the image not displaying correctly, I don't know why you are having that problem.  You could again try calling init_graphics first but I would be surprised if that was the problem.  I also recommend selecting the RGB bands to display for that image:

    view(I, [29, 19, 9])
    

    If that doesn't work, I would try reading one of the bands and seeing if it will display with matplotlib (using its imshow function).

     
  • Nobody/Anonymous

    called init_graphics as you mentioned but I get the same traceback.

    Traceback (most recent call last):
      File "C:/Users/zero/Desktop/t1.py", line 5, in <module>
        hypercube(I, bands=)
      File "C:\Python27\lib\site-packages\spectral\graphics\hypercube.py", line 457, in hypercube
        from spectral.graphics.spywxpython import viewer
    ImportError: cannot import name viewer

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-09

    If you are typing these commands interactively at the python command prompt, then the output you are showing doesn't make sense to me.  Please start your python interpreter and type the following commands (one at a time) to see if you get the same output:

    >>> import spectral
    >>> from spectral.graphics.spywxpython import viewer
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: cannot import name viewer
    >>> spectral.init_graphics()
    >>> from spectral.graphics.spywxpython import viewer
    >>> viewer
    <spectral.graphics.spywxpython.SpyWxPythonThreadStarter instance at 0x105f2cc20>
    >>>
    
     
  • Nobody/Anonymous

    >>> import spectral
    >>> from spectral.graphics.spywxpython import viewer

    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        from spectral.graphics.spywxpython import viewer
    ImportError: cannot import name viewer
    >>> spectral.init_graphics()
    >>> from spectral.graphics.spywxpython import viewer
    >>> viewer
    <spectral.graphics.spywxpython.SpyWxPythonThreadStarter instance at 0x025A9AD0>

    That seems OK.

     
  • Thomas Boggs

    Thomas Boggs - 2012-05-10

    If that last import returns a viewer object as shown, then displaying a hypercube should work after calling init_graphics.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.