traits ui array output

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

traits ui array output

billy.robertson
Hi,
I am new to TraitsUI and I am attempting to write a small GUI where the user inputs some numbers into Float type boxes and when they click a solve button it returns the calculated answer. I have imported my own module that gives out an array (named 'orderarray' below) of the form [[[1,2,3],[4]] , [[5,6,7],[8]] , [[9,10,11],[12]]] . I need to display this in the GUI.

Currently I have:

#most of this isn't used. will delete unused later
from enthought.traits.api import *
from enthought.traits.ui.api import *
from enthought.enable.api import *
from enthought.chaco.api import *
from enthought.enable.component_editor import *
from enthought.chaco.tools.api import ZoomTool, \
    LegendTool, DragZoom
from scipy import array
from numpy import arange
#my own module
from SolverModule import *

class GUI(HasTraits):
   
    #variables used
    xwidth = Float(1.0)
    ywidth = Float(1.0)
    xwaistpos = Float(0.0)
    ywaistpos = Float(0.0)
    m2x = Float(1.0)
    m2y = Float(1.0)
    wavelength = Float(0.001064)
    solutions = String()
    solvebutton = Button()
       
    #GUI layout:
    traits_view = View(
    HGroup(
        Item(name='xwidth',label='x width (mm)'),
        Item(name='ywidth',label='y width (mm)')
        ),
    HGroup(
        Item(name='xwaistpos',label='x waist location (mm)'),
        Item(name='ywaistpos',label='y waist location (mm)')
        ),
    HGroup(
        Item(name='m2x',label='M squared, x'),
        Item(name='m2y',label='M squared, y')    
        ),
    HGroup(
        Item(name='wavelength',label='wavelength (mm)')
        ),
    HGroup(
        Item(name='solutions',label='solutions')    
        ),
    HGroup(
        Item(name='solvebutton',label='Solve')
        ),
    Item(name='version', label='Version', style='readonly'),
    resizable = True,
    buttons = [CancelButton],
    title = 'Solver',
    width=300, height=200)
   
    #When 'solve' is clicked    
    def _solvebutton_fired(self):
        #Use SolverModule to create 'orderarray'
        data = Solver(self.xwidth, self.ywidth, self.xwaistpos, self.ywaistpos,
                 self.m2x, self.m2y, self.wavelength)
        data.Solve()
        self.solutions = data.orderarray
   
if __name__ == '__main__':
    viewer = GUI()
    viewer.configure_traits()


The GUI displays fine until I click the solve button then I get some errors including one about the array not being a string.

Any help is appreciated. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: traits ui array output

Didrik Pinte-2
Hi Billy,

On Fri, Aug 26, 2011 at 11:13 AM, billy.robertson
<[hidden email]> wrote:

> Hi,
> I am new to TraitsUI and I am attempting to write a small GUI where the user
> inputs some numbers into Float type boxes and when they click a solve button
> it returns the calculated answer. I have imported my own module that gives
> out an array (named 'orderarray' below) of the form [[[1,2,3],[4]] ,
> [[5,6,7],[8]] , [[9,10,11],[12]]] . I need to display this in the GUI.
>
> Currently I have:
>
> /#most of this isn't used. will delete unused later
> from enthought.traits.api import *
> from enthought.traits.ui.api import *
> from enthought.enable.api import *
> from enthought.chaco.api import *
> from enthought.enable.component_editor import *
> from enthought.chaco.tools.api import ZoomTool, \
>    LegendTool, DragZoom
> from scipy import array
> from numpy import arange
> #my own module
> from SolverModule import *
>
> class GUI(HasTraits):
>
>    #variables used
>    xwidth = Float(1.0)
>    ywidth = Float(1.0)
>    xwaistpos = Float(0.0)
>    ywaistpos = Float(0.0)
>    m2x = Float(1.0)
>    m2y = Float(1.0)
>    wavelength = Float(0.001064)
>    solutions = String()
>    solvebutton = Button()
>
>    #GUI layout:
>    traits_view = View(
>    HGroup(
>        Item(name='xwidth',label='x width (mm)'),
>        Item(name='ywidth',label='y width (mm)')
>        ),
>    HGroup(
>        Item(name='xwaistpos',label='x waist location (mm)'),
>        Item(name='ywaistpos',label='y waist location (mm)')
>        ),
>    HGroup(
>        Item(name='m2x',label='M squared, x'),
>        Item(name='m2y',label='M squared, y')
>        ),
>    HGroup(
>        Item(name='wavelength',label='wavelength (mm)')
>        ),
>    HGroup(
>        Item(name='solutions',label='solutions')
>        ),
>    HGroup(
>        Item(name='solvebutton',label='Solve')
>        ),
>    Item(name='version', label='Version', style='readonly'),
>    resizable = True,
>    buttons = [CancelButton],
>    title = 'Solver',
>    width=300, height=200)
>
>    #When 'solve' is clicked
>    def _solvebutton_fired(self):
>        #Use SolverModule to create 'orderarray'
>        data = Solver(self.xwidth, self.ywidth, self.xwaistpos,
> self.ywaistpos,
>                 self.m2x, self.m2y, self.wavelength)
>        data.Solve()
>        self.solutions = data.orderarray
>
> if __name__ == '__main__':
>    viewer = GUI()
>    viewer.configure_traits()/
>
> The GUI displays fine until I click the solve button then I get some errors
> including one about the array not being a string.

If I understand correctly, the data.orderarray is a numpy array with
the solution of your solver and you are trying to assign its value to
the self.solutions trait attribute of your class. It does not work as
Traits wants the content of self.solutions to be a Python str as you
declared it. You could solve your problem by converting the
data.orderarray into a str :

    self.solutions = str(data.orderarray)

Posting the full traceback could be helpful for us to help you.

-- Didrik
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: traits ui array output

billy.robertson
I changed 'self.solutions = data.orderarray' to 'self.solutions = str(data.orderarray)' but this didn't fix the problem. I am getting this error:

data = Solver(self.xwidth, self.ywidth, self.xwaistpos, self.ywaistpos, self.m2x, self.m2y, self.wavelength)
TypeError: this constructor takes no arguments

The modules __init__ takes (self, xwidth=0.5, ywidth=0.5, xwaistpos=0.0, ywaistpos=0.0, m2x=1.0, m2y=1.0, wavelength=0.001064) and so I am trying to pass my vules to it then perform the Solve function.

I've noticed online that I could possibly do something like solutions=Array() so I could keep the solutions as an array instead of converting to a string, although this didn't work either.

The full trace back was:

Exception occurred in traits notification handler.
Please check the log file for details.
Exception occurred in traits notification handler for object: <__main__.GUI object at 0x075315D0>, trait: solvebutton, old value: <undefined>, new value: 0
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\traits-3.6.0-py2.6-win32.egg\enthought\traits\trait_notifiers.py", line 360, in call_1
    self.handler( object )
  File "C:\Pyhton Programs\SolverGUI.py", line 105, in _solvebutton_fired
    data = Solver(self.xwidth, self.ywidth, self.xwaistpos, self.ywaistpos, self.m2x, self.m2y, self.wavelength)
TypeError: this constructor takes no arguments

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: traits ui array output

billy.robertson
I altered the data = Solve(...) to become data = Solver(xwidth=self.xwidth, ywidth=self.ywidth, xwaistpos=self.xwaistpos, ywaistpos=self.ywaistpos, m2x=self.m2x, m2y=self.m2y, wavelength=self.wavelength) and I no longer get any errors, however the data still does not get displayed.
I added a status box to my GUI and it shows 'done' at the end of the def _solvebutton_fired(self): function so it appears mt solutions are getting created fine but I just cant display them.
Reply | Threaded
Open this post in threaded view
|

Re: traits ui array output

Didrik Pinte-2
On Fri, Aug 26, 2011 at 12:47 PM, billy.robertson
<[hidden email]> wrote:
> I altered the data = Solve(...) to become data = Solver(xwidth=self.xwidth,
> ywidth=self.ywidth, xwaistpos=self.xwaistpos, ywaistpos=self.ywaistpos,
> m2x=self.m2x, m2y=self.m2y, wavelength=self.wavelength) and I no longer get
> any errors, however the data still does not get displayed.
> I added a status box to my GUI and it shows 'done' at the end of the def
> _solvebutton_fired(self): function so it appears mt solutions are getting
> created fine but I just cant display them.

What I would suggest you do is create a function outside of the traits
class that takes care of solving the problem. On top of it I would
write a test to make sure that calling this function does return what
you want to be returned. Then I would integrate it within your gui
class

def solve_problem(xwidth, ywidth, ...):
    # create the solver, solve the problem and returns the results
    solver = Solver(XXX)
    solver.solve()
    return solver.data()

def test_solve_problem():
    # create the inputs
    xwidth = 100
    ywidth = 10
    ...
    result = solve_problem(xwidth, ywidth, ...)

    # make sure the result is the one we do expect
    expected_result = numpy.array([1,2,3,4])
    assert expected_result == result

HTH,

-- Didirk
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev