DMelt:General/5 Using Scripts

From jWork.ORG
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member

Running Scripts

DataMelt is designed to work with several programming languages that are integrated (or written) in Java. DataMelt IDE allows to use interactive shells (in case of scripting languages) or the DataMelt editor.

DataMelt Java numerical libraries are designed for programming using several languages:

  • Jython - Probably, this is the most simple and the most powerful approach as it combines the strength of the Python language with most complete and most popular Java. Read the tutorial online or the books
  • JRuby - Similar to Jython is a scripting language, but with different syntax. Read tutorial online
  • BeanShell - another scripting language which have the full access to Java API, but it is mainly designed to call Java classes without its own API. Read about it here
  • Java - as above to build compiled Java bitecode libraries without interactivity. No need to introduce - this is the backbone of the scripting languages above, but you can still program using it. Read Java tutorial online
  • Matlab or Octave - for analytic calculations (interactive). Octave is a high-level language intended for numerical or analytical computations. It is a Matlab-like numerical programming language. You can find about the syntax of Octave-type macros here.

If you have a source file loaded to the editor, one can execute it by pressing the button Run.png on the toolbar of DataMelt. One can also use the [F8] key. Also check out the "Run" toolbar menu for more options. The result depends on the file type:

  1. if you are editing a Java file (the extension ".java"), the program will be compiled and executed. One can also compile Java source files using the "Run" toolbar menu.
  2. if you are working with a BeanShell file (the extension ".bsh"), the program will be executed by pressing [[[:Template:Run.png]]] or [F8]. Text output will go to "BeanShell" (a window below the main editor)
  3. if you are working with Jython or Python files (the extension ".py"), the program will be executed as before, with the text output in the JythonShell (below the editor).
  4. if you are working with JRuby or Ruby files (the extension ".rb"), the program will be executed as before, with the text output in the JRubyShell (below the editor).
  5. if you are working with Matlab or Octave macros (the extension ".m") and using the analytic calculations capabilities of the program, the program will be executed as before, with the text output in the JasymcaShell.

Using JythonShell

For interactive coding and prototyping use the JythonShell (a window below the main editor). Use the standard Jython/Python language Jython.ORG. One advantage using the DataMelt JythonShell is that it has the code assist. To learn about the JythonShell, enter Python commands line by line. Use the help system as "obj." + [Ctrl]+[Space], where "obj" is any object.

Here is an example of a simple calculator:

from math import *
print sin(20)*10*tan(0.5)

you can see the available functions as by executing the command "dir()". The next example shows all the methods of the Python string object:

a.  # press [Ctrl]+[Space] to find all methods

Executing scripts in a GUI mode

To run a Jython script in DataMelt IDE, use the Run button Run.png on the toolbar of DataMelt. One can also use the [F8] key for fast execution of a script. In case of an error, the DataMelt main editor will move the cursor to the appropriate line with error. Press any key to remove the line highlighting (red color).

As exercise, try to run this script: load to the DataMelt IDE and execute the script pressing the key [F8].

from jhplot  import *
f1 = F1D("2*exp(-x*x/50)+sin(pi*x)/x", 1.0, 10.0)
c1 = HPlot("Example of  function")

The script creates an EPS image "example.eps" with the 2D function as shown in this figure

DMelt example: Show a F1D function in the range 1-10

Print streams in JythonShell

As usual, use the Python "print" method to print strings and numbers. The output will be directed to the JythonShell itself. You can also direct output steam and error stream to "System console". Use Java "System.out" and "System.err" for this. Here is an example:

print "Hello, Jython"
print 233 # this is a number

You can also redirect the outputs from Jython macros to the Java system console:

from java.lang import *
System.out.println("OK message")
System.err.println("Error message") # will be shown in RED

Using File Menu

Let us show how to run Jython scripts using File Manu. Simple open a script using File-Open. When you will see Jython file in the editor, click on "run" button.

Running Jython scripts using URL

You can open or execute a Jython script from URL. Go to "File->Open from URL". You will see a dialogue window. Copy and paster URL. For example, in the Firefox, navigate to some link and right-click on the mouse button. Select "Copy and Link location", and then copy it to the DataMelt URL window. Make sure that you copy a complete address which starts from "http". Then click "run". This will execute the script from URL.

Be careful! Only run scripts from trusted sites! You can access the script later from the directory "macros/downloads".

Running a script in a batch mode

On Linux/Unix and Mac platforms, one can run scripts without DataMelt GUI. You can use a bash script included in the installation directory: dmelt_bat> If you have a Jython script "", simply run DataMelt as:


It is assumed that this command is executed inside the DataMelt install directory (where "dmelt.jar" is located). If the Jython code takes an argument, run the above example as:

./ argument

You can always redefine the installation directory by changing SCAVIS_HOME inside file.

The only thing you need to do is to set the variable "JHPLOT_HOME" to the installation location where "" is located. It is assumed that Java is installed (type "java" to check"). Make this script executable, i.e. type "chmod 755".

Now, in order to run the above script, just execute:


This will brings up a canvas and generate a vector-format image "example.eps".

Generating images in a background

Images can be generated without pop-up GUI window if the method "visible(0)" is called for the created canvases (HPlot, HPlotJa etc.). "0" indicates Java "false". Then, the last statement should be "export(file)" applied to canvases.

Jython and JavaBean properties

As I have not seen any topic of the Jython access properties to JavaBean here, I'd like to give a few example of how they are very efficient with the jhplot libraries to make a code very compact and easy to write and read. Maybe what I will describe is obvious, but I guess that not all are aware of those properties.

First, I don't want to describe in details what is a JavaBean (you can find tens of websites that will explain it better than me), but just explain basically that they are Java class that respect some writing conventions. One of this convention, in particular, has a very important interest in Jython: The fields of a Java Class are declared private (possibly protected or friendly) and are accessed via “assessor” methods (getters/setters methods) as following. For instance a field:

private double dou1=1;

will be accessed with two public methods:

public double getDou1();


public void setDou1(double d);

used for reading and writing this double value.

The things is that most of the jHPlot code is written respecting this convention. Let's see now how it could be used to simplify the code. Thus a small piece of Jython code used in the standard "Java way" would be written:

from java.awt import Color
from java.util import Random
from jhplot import HPlotJa, H1D
from jhplot.jadraw import JaText

rand = Random()
val = []
for i in range(500):

h1 = H1D("H1D-1",20, -2.0, 2.0)

c1 = HPlotJa("Canvas-1", 600, 600, 1, 1, True)
c1.setName("Canvas Title")

t1 = JaText("Text-1", 0.3, 0.3, "NDC")

Of course, this works very well but as the Jython interpreter is smart with these accessor methods, it is possible to get a direct access to the private field and the interpreter will by itself use the getter/setter methods. Now if I write the same example as above with the properties, one would have:

from java.awt import Color
from java.util import Random
from jhplot import HPlotJa, H1D
from jhplot.jadraw import JaText

rand = Random()
val = []
for i in range(500):

h2 = H1D("H1D-2",20, -2.0, 2.0)
h2.color =
h2.errX = False
h2.errY = True
h2.penWidthErr = 2

c2 = HPlotJa("Canvas-2", 600, 600, 1, 1, True)
c2.antiAlias = False
c2.GTitle = "Title-2"
c2.nameX = "Xaxis"
c2.nameY = "Yaxis" = "Canvas Title"

t2 = JaText("Text-2", 0.3, 0.3, "NDC")
t2.color = h2.color

One can see that we don't really save typing code , however it is (in my opinion) more clear to read, thanks to the “=” symbols.

Finally there another way to use those JavaBean properties, with once again, the smart Jython interpreter who can read or write the class fields directly in the object instance. Thus the same code can now be written:

from java.awt import Color
from java.util import Random
from jhplot import HPlotJa, H1D
from jhplot.jadraw import JaText

rand = Random()
val = []
for i in range(500):

h3 = H1D("H1D-3", 20, -2.0, 2.0,, errX=False, errY=True, penWidthErr=2)

c3 = HPlotJa("Canvas-3",600,600,1,1,True,antiAlias=False, GTitle="Title-3", nameX="Xaxis", nameY="Yaxis", name="Canvas Title")

t3 = JaText("Text-3", 0.3, 0.3, "NDC", color=h3.color)

One can see now that the code is more compact and each object is easy to identify. Of course these three methods produce exactly the same result.

Although this feature allows to save a lot a time while coding one must, however, be careful with some methods which are sometime named get/setSomething() but which are actually not providing any access to a private field of the class but a random methods doing some stuff. Of course this will not work in that case.

More information can be found here

(contributed by Marc-Antoine)

More information on this topic is in DMelt books