Issue with configure_traits when using Enthought Canopy - python-2.7

I was following the tutorial "Writing a graphical applications for scientific programming using TraitsUI
http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html
and tested the following code snippet:
from enthought.traits.api import *
from enthought.traits.ui.api import *
class Camera(HasTraits):
""" Camera object """
gain = Enum(1, 2, 3,
desc="the gain index of the camera",
label="gain", )
exposure = CInt(10,
desc="the exposure time, in ms",
label="Exposure", )
def capture(self):
""" Captures an image on the camera and returns it """
print "capturing an image at %i ms exposure, gain: %i" % (
self.exposure, self.gain )
if __name__ == "__main__":
camera = Camera()
camera.configure_traits()
camera.capture()
If I run this at the command line it works as advertised. A GUI pops up. You adjust the parameters, and when you click "OK" it returns the modified values. But when I run the same code from within the Canopy editor by clicking the run button the default parameters print immediately; then the window pops up. When you then adjust the parameters in the GUI and click "OK" the GUI exits but the new parameter values don't print.
It is as if somehow camera.capture() is running before camera.configure_traits.

First, I would suggest using this newer version of the tutorial: http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html
The one you linked to references materials for TraitsUI version 3, whereas the one above is for the version you're likely using (version 4). The newer tutorial uses the newer module names, traitsui.api instead of enthought.traits.ui.api for example.
As to why Canopy displays the values immediately, this is the expected behavior when running the program:
if __name__ == "__main__":
camera = Camera()
camera.configure_traits()
camera.capture()
When run as __main__ (i.e., not imported as a module by another script), the script does these three things in order: creates an instance of Camera(), pops up the GUI (configure_traits), and then executes the capture method that prints the current values (which are "1" and "10" by default).
The OK/Cancel buttons are not hooked into setting these values. As a test, try changing the exposure or gain but instead of clicking the buttons, try inspecting these attributes from within Canopy's IPython prompt while the GUI is still open: camera.gain or camera.exposure should return the newly set values.

Related

Matplotlib Qt4 GUI programming - replace plt.figure() with OO equivalent

I have an App made using Qt4 Designer which inserts a matplotlib figure into a container widget.
The code to generate the figure comes from another module, obspy:
self.st.plot(fig = self.rawDataPlot)
https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html
Normally, this would create and show a matplotlib figure for the st object's data, which is time-series. When the fig parameter is specified this tells self.st.plot to plot to an existing matplotlib figure instance.
The code I have to generate the figure and then position it in my GUI widget is:
def addmpl(self, fig, layout, window): # code to add mpl figure to Qt4 widget
self.canvas = FigureCanvas(fig)
layout.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas,
window, coordinates=True)
layout.addWidget(self.toolbar)
self.rawDataPlot = plt.figure() # code to create a mpl figure instance
self.st.plot(fig = self.rawDataPlot) # plot time-series data to existing matplotlib figure instance
self.addmpl(self.rawDataPlot, self.mplvl, self.mplwindow) # add mpl figure to Qt4 widget
What I want to do is instantiate a matplot figure (for use by self.st.plot) but in a way which avoids using plt.figure(), as I have read that this is bad practice when using object-oriented programming.
If I replace plt.figure() with Figure() (from matplotlib.figure.Figure()) I get an error:
AttributeError: 'NoneType' object has no attribute 'draw'
As it stands, the App runs fine if I use plt.figure(), but is there a clean way to avoid using is and is it even necessary for my case?
PS, the code snippets here are taken from a larger source, but I think it gets the point across..
In principle both methods should work.
Whether you set self.rawDataPlot = plt.figure() or self.rawDataPlot = Figure() does not make a huge difference, assuming the imports are correct.
So the error is most probably triggered within the self.st.plot() function. (In general, if you report errors, append the traceback.)
Looking at the source of obspy.core.stream.Stream.plot there is a keyword argument
:param draw: If True, the figure canvas is explicitly re-drawn, which
ensures that existing figures are fresh. It makes no difference
for figures that are not yet visible.
Defaults to True.
That means that apparently the plot function tries to draw the canvas, which in the case of providing a Figure() hasn't yet been set.
A good guess would therfore be to call
self.rawDataPlot = Figure()
self.st.plot(fig = self.rawDataPlot, draw=False)
and see if the problem persists.

Convert Code from CodeSkulptor to PyCharm

I am learning python from coursera. They are using CodeSkulptor(http://www.codeskulptor.org/) as IDE.
I want to write the same program in Pycharm.
I am unaware of inbuilt GUI packages in PyCharm(Python 2.7).
I want to convert below code. Please provide me some useful URL's/Important info.
Here is my code:
# define event handlers for control panel
def foo():print "hello world!!!"
# create frame
f = simplegui.create_frame("Guess The Number",300,300)
f.add_button("Range [0,100)", foo, 150)
f.add_button("Range [0,1000)", foo, 150)
f.add_input("Enter a guess", foo, 150)
You can read the documentation here
https://pypi.python.org/pypi/SimpleGUITk
Instead of
import simplegui
you should use
import simpleguitk as simplegui
Reference: How to integrate SimpleGUI with Python 2.7 and 3.0 shell
Please check this link also

automated creation of pdf with Matplotlib blocks the code execution

I have a Python code that contains a function where a figure is created in order to be saved as a pdf (it never shows on the screen during execution). For some reason, the execution of this subroutine keeps the figure open and blocks the following operations in the code. I tried to use the cla(), clf() and clear() functions but I could not get it to work...
Here is a partial view of the subroutine:
def trace_pdf(a,b,c,d):
x = np.linspace(0,100,a)
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.plot(b,c,'b', label='BA',linewidth=3.5)
ax2.set_title('a pdf like no other')
fig2.savefig('file.pdf', format='pdf')
fig2.clf()
fig2.clear()
I do not see why my code is blocked... (I checked that if I comment the call to the trace_pdf function, everything works fine).
So here is what I did to fix my problem. I decided to try to run my function as an independent process so I added to my code:
from multiprocessing import Process, Queue
def trace_pdf(a,b,c,d):
x = np.linspace(0,100,a)
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.plot(b,c,'b', label='BA',linewidth=3.5)
ax2.set_title('a pdf like no other')
fig2.savefig('file.pdf', format='pdf')
plt.close()
trace_pdf = Process(target=trace_pdf, args=(a,b,c,d))
trace_pdf.start()
This way, the plt_close() call does not impact the main graphic interface as I believe its action is limited to the separate process... For details regarding running a function as an independent process I used this post.

Disable or Catch VTK warnings in vtkOutputWindow when embedding Mayavi

I'd like to either disable the VTK warning window or, better yet, catch them to handle with my application's logging system. My application is using an embedded mayavi view, and I don't want error windows popping up that I have no control over. The following code demonstrates the warning window.
import numpy as np
from mayavi import mlab
x1 = np.array([1, 1, 2, 3])
y1 = np.array([1, 1, 4, 2])
z1 = np.array([1, 1, 5, 1])
mlab.plot3d(x1, y1, z1)
mlab.show()
Ok, I've done some research and discovered that vtk.vtkObject.GlobalWarningDisplayOff() will disable the window completely, which is nice. Better yet the followingcode will log the warnings to a file (found it here):
def redirect_vtk_messages ():
""" Can be used to redirect VTK related error messages to a
file."""
import tempfile
tempfile.template = 'vtk-err'
f = tempfile.mktemp('.log')
log = vtkpython.vtkFileOutputWindow()
log.SetFlush(1)
log.SetFileName(f)
log.SetInstance(log)
So while this is nice, I'm still unable to pipe the warnings directly into a logging handler. I'd rather not have to have a vtk_log file next to my regular log files. Also I might want to handle the warnings in my GUI somehow, or give the user options on how to handle them and constantly watching a log file for changes seems like a poor way to do that.
Any suggestions on a robust pythonic way to handle vtk warnings in an application which embeds mayavi/vtk?
I don't know whether this will work in the Mayavi environment, but this works for Python wrappings to VTK
# pipe vtk output errors to file
errOut = vtk.vtkFileOutputWindow()
errOut.SetFileName("VTK Error Out.txt")
vtkStdErrOut = vtk.vtkOutputWindow()
vtkStdErrOut.SetInstance(errOut)
I guess this partially answer your question, but you could implement an error observer in python as explained here http://public.kitware.com/pipermail/vtkusers/2012-June/074703.html and add it to the vtk class you are interested.
In c++ I find much simpler to redirect the output to stderr (this example is for windows):
vtkSmartPointer<vtkWin32OutputWindow> myOutputWindow = vtkSmartPointer<vtkWin32OutputWindow>::New();
myOutputWindow->SetSendToStdErr(true);
vtkOutputWindow::SetInstance(myOutputWindow);
In python I tried
ow = vtk.vtkOutputWindow()
ow.SendToStdErrOn()
it sends the error to console, but I still see the vtk window and it doesn't really seem catching the errors.
Another option could be to recompile vtk with VTK_USE_DISPLAY turned off ( http://osdir.com/ml/python-enthought-devel/2009-11/msg00164.html). I am not going to try this because I am using the vtk distribution already compiled in paraview
You can create a subclass deriving from vtkOutputWindow and implement your message handling in the method void DisplayText(const char* someText). I did this is in a C++ project to redirect all output to cerr and even suppress specific warnings.
An approach I found similar to #SciCompLover's answer that suppresses the output window while also printing to the console:
import vtk
vtk_out = vtk.vtkOutputWindow()
vtk_out.SetInstance(vtk_out)
Tested on Mayavi 4.7.1 with VTK 8.2.0 on Windows and MacOS.

Adobe Adam and Eve (C++ ASL): how to bind Eve variable so to get it updated inside C++ application?

So we know how to compile it, we have seen its demos and loved it. We have seen probably only one real life opensource project based on it. So I look at the samples and see only 3 quite long C++ applications that can be ofmy intrest ASL\test\adam_tutorial\, ASL\test\adam_smoke\, ASL\test\eve_smoke\. But I still do not get how htving simple Eve file with:
dialog(name: "Clipping Path")
{
column(child_horizontal: align_fill)
{
popup(name: "Path:", bind: #path, items:
[
{ name: "None", value: empty },
{ name: "Path 1", value: 1 },
{ name: "Path 2", value: 2 }
]);
edit_number(name: "Flatness:", digits: 9, bind: #flatness);
}
button(name: "OK", default: true, bind: #result);
}
in it, Adam file bound to it (theoretically, because I do not quite get how to bind Eve to adam and see no tutorialon how to do this), with
sheet clipping_path
{
output:
result <== { path: path, flatness: flatness };
interface:
unlink flatness : 0.0 <== (path == empty) ? 0.0 : flatness;
path : 1;
}
in it, make each time flatness variableis changed some C++ function of mine called (A simple one couting new flatness value for example)
So How to implement such thing with Adobe Adam and Eve and Boost ofcourse?
Update
We have tried to do it here and it worked but not in a live feedback way - only on dialog close action. And than here but due to our compile evrething on linux absession we have paused our development in ASL programming and started investing time into ASL compilation on Linux OS.
A good place to ask questions about ASL is on the ASL developer mailing list: http://sourceforge.net/mail/?group_id=132417.
You might want to look at the "Begin" test app . Although this only runs Mac and Win it does demonstrate how to wire things up.
The basic idea is that when a layout description (Eve) is parsed it will call your add_view_proc http://stlab.adobe.com/structadobe_1_1eve__callback__suite__t.html#a964b55af7417ae24aacbf552d1efbda4 with the arguments expression. Normally you use bind_layout_proc for the callback which will handle the argument evaluation for your and call a simplified callback that takes a dictionary with the arguments.
When your callback is invoked, you would typically create an appropriate widget and associate the dictionary to the widget or extract the arguments of interest from the dictionary and store them in a struct. Using the bind argument, you can setup callbacks with the associated sheet (Adam), using the monitor_xxxx functions on sheet_t. Usually you'll use monitor_value and monitor_enabled. When called, you set the value or enabled state on the widget. When the widgets value is changed by the user, and widget is invoked (it may be through an event handler, or a callback, or whatever mechanism your UI toolkit supports) you call sheet_t::set() to set the value of the cell and then sheet_t::update() to cause the sheet to recalculate.
That's about it - When trying to get Adam/Eve going with a new UI framework - start small. I usually start with just a window containing two checkboxs and wire up Eve first. Once that is going add Adam and a simple sheet connecting two boolean cells so you can see if things are happening correctly. Once you have that going you'll find it's pretty simple to get much more complex UIs wired up.