- Undocumented Matlab - https://undocumentedmatlab.com -

JBoost – Integrating an external Java library in Matlab

Posted By Yair Altman On June 29, 2011 | No Comments

I would like to welcome guest blogger Adam Chapman, who has recently started a dedicated Matlab blog at http://intelilab.blogspot.com/ [1]. Today, Adam shares his experience in integrating a 3rd-party Java library – in this case, the open-source JBoost library, in a Matlab application.
I had received a couple of requests asking me to create alternating decision trees [2] (ADTs) in Matlab. Due to the self-organising nature of an ADT structure, it would be an extremely slow and frustrating task to code up an ADT building algorithm in any language.
I was cautious of looking for source code that would do the job for me, as I was concerned that interfacing external code with Matlab would be another difficult task.
I had interfaced external VBA and C++ code with Matlab before, but never attempted interfacing Java code. I remember all of those previous interfacing projects being a nightmare to debug and very tricky to code into a stable solution. After some hesitation I decided that I would give the open-source Jboost library [3] a try.
It turns out [4] that “every Matlab installation includes a Java Virtual Machine (JVM), so that you can use the Java interpreter via MATLAB commands, and you can create and run programs that create and access Java objects”.
That’s all very well, but I needed to know exactly how I should call the main() function in Jboost directly from Matlab. In this Article I’ll explain how I replicated the example walkthrough on the Jboost website [5], using the spambase data set as an example.
Reading the Jboost installation notes [6], I found that three paths need to be added to the Java class path. Instead of adding a system environment variable like the Jboost documentation [7] suggests, we can simply use the Matlab’s built-in javaaddpath function:

newpath1=[JBOOST_DIR '\dist\jboost.jar'];
newpath2=[JBOOST_DIR '\lib\jfreechart-1.0.10.jar'];
newpath3=[JBOOST_DIR '\lib\jcommon-1.0.8.jar'];
% add Jboost classes to javaclasspath
% also add JBOOST_DIR to the matlab path so that matlab can look there for non-jave code and data files:

we can check those paths were added by reading back the dynamic java classpath:

>> javaclasspath('-dynamic')

Now I had the paths set up for Matlab and the JVM to find code and files in, I had to work out how to actually run the Jboost main() function.
Matlab’s methodsview function lists information on the methods in a Java class:


pops a new window, from which we can derive that the main function for Jboost expects a java.lang.String array object (which is standard for a main() function in Java):

View of JBoost's class function using methodsview (click for details) [8]
View of JBoost's class function using methodsview (click for details)

(we could also have used Yair’s UIINSPECT utility [9] on the File Exchange, to display even more information on the Java class’s methods, callbacks and properties, but for our simple usage here, the built-in methodsview function is sufficient)
To build a Java string array to input to Jboost, we use the built-in java_array function:

strArray = java_array('java.lang.String', 1);
strArray(1) = java.lang.String('-S');
strArray(2) = java.lang.String('C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\demo\spambase');
strArray(3) = java.lang.String('-CONFIG');
strArray(4) = java.lang.String('spambase.config');
strArray(5) = java.lang.String('-m');
strArray(6) = java.lang.String(‘MATLABTREE.m');

Note how I initialized it with a size of 1 element, and let it grow as new elements are added since the array is very small there is no need to worry about the time saved by pre-allocating. The first 2 elements define the directory that Jboost is to look for data files in, the next 2 define a standard Jboost configuration file and the final 2 elements are rather special: they define the name of the m-file that will contain automatically generated Matlab code, allowing us to use the decision tree in the future without going in and out of Java.
Alternately, we could have simply used Matlab’s cell-array, as in:

strArray = {'-S', 'C:\...', '-CONFIG', 'spambase.config', '-m', 'MATLABTREE.m'};

Now to construct an Alternating Decision Tree with Jboost, just call the Java jboost class containing the main() function, and input the array of parameters that we just constructed:

% input the configuration class into the jboost controller

With all the code so far, Matlab will invoke the Java jboost libraries to train an ADT to detect spam emails based on their contents defined in the spambase dataset. You will see the verbose java print-outs in the Matlab command window while it is running.
The jBoost developers use a Perl script to create an image of the completed ADT. That is even easier to call from Matlab than the Java routines were:


Now if you look in the folder that you defined in the “-d” parameter above, you will see that the Perl script has created a few images of the tree in different formats. The .png file is below. I think it is incredible that an inanimate machine can be programmed to have the intelligence to create something so intricate.

Resulting ADT tree (click for details) [10]
Resulting ADT tree (click for details)

During this small project I realized that calling Java and Perl classes from Matlab is actually pretty easy. However, calling python, which jboost uses for some post-processing routines is actually very hard. I’ve decided to create my own Matlab versions of the error and margin plotting functions that Jboost uses, and will add them to the sourceforge project as soon as I’m happy with them.
I’d like to thank Aaron Arvey and Yair Altman for their friendly support during this project.

Categories: Guest bloggers, Java, Low risk of breaking in future versions

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/jboost-integrating-an-external-java-library-in-matlab

URLs in this post:

[1] http://intelilab.blogspot.com/: http://intelilab.blogspot.com/

[2] alternating decision trees: http://en.wikipedia.org/wiki/Alternating_decision_tree

[3] Jboost library: http://jboost.sourceforge.net/

[4] turns out: http://www.mathworks.com/help/techdoc/matlab_external/f98533.html

[5] the example walkthrough on the Jboost website: http://jboost.sourceforge.net/examples.html

[6] installation notes: http://jboost.sourceforge.net/install.html

[7] documentation: http://jboost.sourceforge.net/doc.html

[8] Image: http://undocumentedmatlab.com/images/JBoost_methodsview.png

[9] UIINSPECT utility: http://www.mathworks.com/matlabcentral/fileexchange/17935-uiinspect-display-methods-properties-callbacks-of-an-object

[10] Image: http://img31.imageshack.us/img31/952/spambase0.png

[11] Matlab-Java memory leaks, performance : https://undocumentedmatlab.com/articles/matlab-java-memory-leaks-performance

[12] Converting Java vectors to Matlab arrays : https://undocumentedmatlab.com/articles/converting-java-vectors-to-matlab-arrays

[13] Assessing Java object size in Matlab : https://undocumentedmatlab.com/articles/assessing-java-object-size-in-matlab

[14] Matlab-Java interface using a static control : https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control

[15] JMI – Java-to-Matlab Interface : https://undocumentedmatlab.com/articles/jmi-java-to-matlab-interface

[16] Using Java Collections in Matlab : https://undocumentedmatlab.com/articles/using-java-collections-in-matlab

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.