Python or Java ...
March 20, 2009 2:13 PM   Subscribe

Help me choose between Python and Java for a specific type of programming.

Background: I am a researcher in engineering with a wide scope. Mostly, I perform simulations of (mechanical) systems, behavior of which are governed by differential equations. I also develop models for biology/genetics (but not bioinformatics type stuff). I use Fortran (I know) for programming, write results to output file(s) and read them using Excel or Tecplot to visualize. I also use Matlab and Mathematica for quick models. It gets pretty cumbersome after a while, especially when you want to quickly figure out behavior change (of a phenomenon) depending on the input variables. So I thought maybe it is time for me to learn some OOP and write codes that will allow me to change input variables interactively and see the results simultaneously on the screen. Following link best describes the type of interface I am looking for:

http://cmol.nbi.dk/models/igroup/igroup.html

You see what I mean? There are buttons, sliders, text boxes to change variables and the results change immediately in the "graphics" area. As far as I can tell, it is an applet written using Java. Let's drop the ability to write applets aside because I am not looking for that.

First question: can one write a code that would provide similar interface (GUI if you will) using Python?

If so, would it be pretty straight forward after some Python learning (I understand it is hard to answer this question but give it whirl please)?

Java appears a little intimidating to me while my initial experience with Python has been pleasant. However, I could not find any applications/codes on Python sites that fits what I am looking for (suggestions are welcome).

Thanks so much.
posted by eebs to Computers & Internet (27 answers total) 3 users marked this as a favorite
 
I'm guessing here, but might you be interested in Anylogic ? It provides a GUI for developing Agent based models and System Dynamic models (and more, but these two seem most relevant). It is java based and not free, but you can do much of the work using a GUI.

I couldn't load your applet cos I'm on a java free computer but the structure seemed similar to an agent based model. System Dynamics is a really great way of visualizing differential equations.

(I'm not familiar with python)
posted by a womble is an active kind of sloth at 2:24 PM on March 20, 2009


"First question: can one write a code that would provide similar interface (GUI if you will) using Python?"

* Yes. Any GUI features you describe will be available in any language.

"If so, would it be pretty straight forward after some Python learning (I understand it is hard to answer this question but give it whirl please)?"

* Yes. Python is super easy and straight-forward, and there are many, many tools you can use to do tasks which in general look like this. I might consider doing this with SUMMON: http://people.csail.mit.edu/rasmus/summon/. Admittedly I don't work in your field and don't know much about mechanical simulations.

Java is not too intimidating, but a lot of the significant differences between Python and Java for a beginner relate to the software infrastructure concepts which are incorporated into Java but not Python (such as public vs private methods, or interfaces vs superclasses). There's no reason you should have to do this in Java unless there is a toolset you want to use that you already know about. Python has a lot to offer in this direction and I'm sure, if SUMMON isn't the answer for you, that there is a easy-to-learn and well-packaged tool you'll find.
posted by doteatop at 2:34 PM on March 20, 2009 [2 favorites]


I'm not familiar with your problem domain, and I'm not sure if you're looking to port over your math code to Python as well, but Python does have some fairly robust (or at least, long-lived) number-crunching stuff with its NumPy package. It's got plenty of string-handling stuff to read in your files, and it's fairly easy to make GUI applications via wxPython - the documentation can be a little sketchy sometimes, but the example code is amazingly good, and there's a book available as well.

You could certainly build a GUI in Java as well, but you're likely to need to spend a lot longer learning the GUI toolkit (I imagine - I've only really worked on web applications in Java, though). One advantage of Java is that it would probably be much faster, though, being statically compiled and optimized and all.

I'd suggest building out a prototype in Python and getting a feel for whether it is a good fit for what you want your application to do.
posted by whir at 2:49 PM on March 20, 2009


Both Python and Java are suitable for what you want to do, particularly if you just need to get a UI that's functional and doesn't look beautiful / like a part of the host OS. Java comes with a built-in GUI toolkit you would use. Python has a variety of GUI choices, last I checked wxWindows was a good choice and I'd also check out PyGTK.

If you're strictly running on Windows, C#/.NET is also a reasonable choice.
posted by Nelson at 2:49 PM on March 20, 2009


If the bulk of your code is the math/simulation parts, I would stick with matlab and find another way around the GUI layer. First of all, proper gui programming in e.g. Java is a huge pain in the ass. Its possible, but the code volume explodes and its really annoying, boring code to write. Its like type, type , type. Further, IME writing mathematical or simulation code in Java kind of bites. Its just not fun. I've never used Python for this before, but matlab is so sick for this kind of stuff I have a hard time believing Python would be worth it.

How much have you played with matlab's gui libs? I would suggest:
- wringing everything you can out of matlab's GUI lib
- for quick interactive front-ends, get the matlab->excel addin compiler, and build front ends on top of excel.
- if you need some crazy graph-based custom UI layer, get someone to write you a matlab extension in C# that does it, or to adapt existing code.

Building rich UIs in Java or Python is a big, deep codepile to start going down if all you want to do is slickify the front-end of some math-heavy simulation code.
posted by jeb at 2:56 PM on March 20, 2009


I must agree with jeb. GUI programming in Java is a huge hassle and, for your purposes, a waste of time. All of the biology and DE modeling projects I've ever done were in Mathematica and Matlab, for what it's worth.
posted by telegraph at 3:12 PM on March 20, 2009


I haven't used Java but I used a lot of python and from everything I've heard about both, Python would be much more suitable here. I've used wxPython and it's nice. There are also easier options for guis in python: e.g. easygui, but there are others, too. I don't know if this would do what you want, but I've happyily used this for charts: chart director.
posted by rainy at 3:13 PM on March 20, 2009


Response by poster: Thanks much everybody. This is all very helpful.

Summon, wxPython and PyGTK look relevant to my problem (and interesting). Will read more. Also, the fact that nobody told me to go straight into Java tells me to stay away from it (just as I suspected).

@jeb: I see where you're coming from (and I agree with you). Part of the problem is that when I move from my current job, I may not have the wide Matlab access I currently have through my work whereas the resources are freely available for Python. I will definitely try your suggestions.
posted by eebs at 3:17 PM on March 20, 2009


Layout managers in both wxPython and Swing will eventually drive you insane. I would generally recommend Python for the task you're describing with the caveat that as other have noted, doing a GUI in either environment is a lot of work. Java will be much, much better documented than wxPython, but there are one or two books out there on wxPython and you only really need one.

Java is better for large-scale systems, but for projects like this it would be tedious unless you're already really familiar with it.
posted by GuyZero at 3:23 PM on March 20, 2009


If you want to do GUI work with Java, consider SWT, which is easier to work with than Swing framework, IMO. Both Swing and SWT will suit most UI needs.

For scientific computing in Java, there are a wide variety of frameworks for numerical calculation. I have used Colt and JAMA for statistical work, as well as the Apache Project Commons-Math bundle.

Python is younger than Java, and less mature. I love programming in it for short scripts, but for larger, long-term projects, Java is a better choice, IMO.
posted by Blazecock Pileon at 3:47 PM on March 20, 2009


Actually doing java GUIs using the new GroupLayout is a lot less difficult. It's a little confusing in that the X and Y layouts are completely separate, but it's much easier then the previous layout managers, and you can do complex layouts without much nesting of containers.

I've never done layout stuff in Python.

Frankly, and sadly, the easiest way to do GUIs that I've seen is using Microsoft's visual studio products. I tried the new netbeans for java a couple months back, and was buggy and irritating as hell. The GUI designers in visual studio work really well.

Hand coding swing with grouplayouts isn't that hard, though (but it could be a waste of time if you just want to throw a few sliders and buttons up on the screen)
posted by delmoi at 3:49 PM on March 20, 2009


I would propose a different route:

Python would be easier for the straight up programming, but for the GUI, Java would be easier. I know it's a bit tedious, but it's well documented, and you can be sure to eventually reach at your solution.

PyGTK and wxWidgets aren't very robust and the documentation isn't nearly as good. You will pull you hair out.

So, I might suggest using Jython (which is python written in Java) for the programming portions, then switching to Java for the GUI, where you can call your Jython functions.

The best GUI designer, however, is still Visual Studio.
posted by unexpected at 3:56 PM on March 20, 2009


There are buttons, sliders, text boxes to change variables and the results change immediately in the "graphics" area.

I've coded a fancy version of this in Python before, for a proprietary geometry program. It can certainly be done. GUI programming is nice in Python because functions are first-class objects, so you can easily pass them as the callbacks you'll need for asynchronous GUI programming. You can also create anonymous functions (with lambda). These things make GUI programming in Python much more concise than Java.

I like pyGTK better than wxPython; the API seems simpler and more "pythonic". But I use Linux, so I'm not sure how they compare on Windows.

The reasons to use Java would be if you need a robust, professional quality application or if you already know Java better. Numerical libraries in Java are probably better, also, and Python tends to be kind of slow, so maybe use Java if you need to work with a huge dataset.
posted by qxntpqbbbqxl at 4:03 PM on March 20, 2009


re: pyGTK, wxWindows documentation

I've been using PyGTK lately, and find the reference material to be pretty good.

When I last used wxPython (almost 3 years ago), the documentation was difficult. I often had to translate from the original C++ wxWindows docs, which sucked.
posted by qxntpqbbbqxl at 4:06 PM on March 20, 2009


The Matlab gui editor is pretty good and I don't think you'd have any trouble writing a gui that looked like the example you provided. Once you're familiar with it writing gui applications are pretty easy and quick.

While I understand the concern about being locked into a proprietary language, it may be that it's so much faster to solve your problems now in Matlab that you can worry about learning another language when you're no longer at your current job and don't have Matlab access. (That said I write this as an academic who's always had access to Matlab; academic licenses are cheap).
posted by pombe at 4:21 PM on March 20, 2009


Python is younger than Java, and less mature.

Um, what? Python was first released in 1991, Java was first released in 1995. I'm not sure what "mature" means in this context, but I think it is something we could argue about all day long.
posted by i_am_joe's_spleen at 4:37 PM on March 20, 2009


I do a lot of work in jython. The python language + the Java library (or vice-versa, if you want). Bliss.
posted by Netzapper at 4:49 PM on March 20, 2009


You might look at Processing, which is a programming language/library based on Java. It's tailor-made for doing interactive data visualization. It's also designed to be friendly to newbies. (Some tutorials here.)

There are a few good GUI libraries for Processing as well: controlP5 and Interfascia. Not as powerful or extensible as Swing or GTK, but from what you've described, it seems like they might be sufficient.
posted by aparrish at 4:51 PM on March 20, 2009 [1 favorite]


I firmly recommend python, too. And judging from the amount of GUI stuff in your example link, the ease of doing the layout is not that critical, since it is not an awfully big GUI.

As a side note: If you want to GUIfy some of your previous work or want some glue to tie your existing Fortran and Mathlab stuff together and if you're using windows, you might want to take a look at AutoIt.

It is great fun to automate and tie together different GUIless programs or to automate some of the more cumbersome clicking in GUIs.

I really like it, because it's really accessible for beginners. The function reference has nice examples for every function and there's lots of helpful snippets in the forum.
posted by mmkhd at 5:16 PM on March 20, 2009


nthing Python and wxPython. Yes the documentation for wxPython kinda sucks right now, but it has a great demo and a super-helpful mailing list. Like someone mentioned, the wxWidgets docs are actually more helpful, but you have to mentally translate them from C++ to python (not too difficult after a bit). Also matplotlib and NumPy would be of interest.

Once you get setup/installed (make sure you get the wxPython demo), check out this nice demo on the wxPyWiki which is a recreation of a MATLAB GUI.

(also if you've never programmed Java or Python, definitely go with Python... it's much easier to understanding OOP without Java in the way)

posted by i_am_a_Jedi at 5:50 PM on March 20, 2009


Um, what? Python was first released in 1991, Java was first released in 1995. I'm not sure what "mature" means in this context, but I think it is something we could argue about all day long.

You're right, but as a language on par with Java, Python only added features like garbage collection and Unicode support around the end of 2000. It's definitely something to argue about, but feature-wise, I think it is reasonable to suggest that Java has a lot more going for it than Python.

One thing I like about Java is strong typing. Python does not have this, and while this makes writing code faster, it makes debugging anything much more than a script increasingly difficult to manage. YMMV.

If you have the psyco module installed you'll get significant overall speed improvements with Python scripts. Install this and add the following snippet to the top of Python scripts that do any computation with non-native libraries:

try:
    import psyco
    psyco.full()
except ImportError:
    pass


I'd still recommend Java for anything more than short scripts where cross-platform GUI development is concerned, but it's good to have knowledge of both languages.
posted by Blazecock Pileon at 6:05 PM on March 20, 2009


Check out SciPy/NumPy. NumPy wraps some well proven and optimized numeric analysis libraries up for use in Python programs. SciPy builds on NumPy with a bunch of higher-level science and engineering modules. I think there are also bits for visualization and interactive UIs. Keep in mind too, you can use python interactively.

You might also check out Sage. Also built with and scriptable in python. Has a webUI for interactive visualization.
posted by Good Brain at 6:24 PM on March 20, 2009


Python only added features like garbage collection and Unicode support

Unicode, yes. Garbage collection? Python was refcounted from the start. CPython still is.

To the asker: Sage is v. awesome.
posted by Monday, stony Monday at 9:31 PM on March 20, 2009


You may want to check out PyQt.
posted by sien at 11:25 PM on March 20, 2009


OOP may be a red herring here. There's nothing about your problem that requires OOP, and it may get in the way. Java will require you to structure your code in an OO fashion, but it's optional in Python.
posted by silentbicycle at 5:31 AM on March 21, 2009


I thorougly recommend Python + wxPython + numpy/scipy.

Python is a fantastic general-purpose language. Of the languages I know, it's my favorite.

If you use wxPython, you should know that the more detailed wxWidgets documentation is also applicable to wxPython. Also be sure to download the docs and demos package to get a sense of what is possible.

Numpy/scipy, recommended above, can function as a complete replacement for MATLAB (except for plotting) for most users.

For the plotting part, look into matplotlib / pylab, which basically duplicates most of the 2D plotting capabilities of MATLAB, with a similar API. You can also embed it in wxPython and other GUI frameworks, which is one way to acheive these "interactive plot" type applications. The only thing it can't do is 3D plots, so if you need those you'll have to look elsewhere. But it is much more powerful than Excel for plotting.

One of the salient features of Python is that, once you get used to it, productivity is very high. Most people find they can get much more done in much less time with many fewer bugs than in a lot of other languages (C++ and Java, I'm looking at you).

I'm an electrical engineer, if it matters.
posted by musicinmybrain at 9:25 AM on March 21, 2009


Response by poster: Processing looks very interesting (though it kept crashing my Firefox when I was checking out their tutorial/demos).

Here is my strategy: I'll give the Matlab gui editor a go as I already am familiar with other aspects of Matlab. In parallel, I will build knowledge on Python + the suggested modules.

Thank you, appreciate the help.
posted by eebs at 11:52 AM on March 21, 2009 [1 favorite]


« Older How to get the best deal when buying art?   |   How does quiet nerd girl attract type of man she... Newer »
This thread is closed to new comments.