3

i try to get an Image out of Mathematica. I try to evaluate some Mathematica Code that uses Methods in Packages to generate Graphics. If i paste the Code to a Mathematica Notebook the Graphic is generated correctly.

So my Question:

How do I get this graphics to Java ???

Here is my Sample Code:

ml = MathLinkFactory.createKernelLink("-linkmode launch -linkname 'F:\\APPS\\Wolfram
Research\\Mathematica\\7.0\\mathkernel.exe'");

ml.addPacketListener(new MyPacketListener());
ml.discardAnswer();
PacketListener stdoutPrinter = new PacketPrinter(System.out);
ml.addPacketListener(stdoutPrinter);
// In stringList there is all the INPUT for Mathematica
for (int i = 0; stringList.size() > i; i++) 
{
  System.out.println("Input" + "[" + i + "]" + stringList.get(i));
  ml.evaluate(stringList.get(i));
  ml.discardAnswer();   
 }
ml.close();



class MyPacketListener implements PacketListener {
public boolean packetArrived(PacketArrivedEvent evt)
        throws MathLinkException {
    if (evt.getPktType() == MathLink.TEXTPKT) {
        KernelLink ml = (KernelLink) evt.getSource();
        System.out.println(ml.getString());
    }
    return true;
 }

The Output is:

<<CIP`ExperimentalData`
<<CIP`MLR`
dataSet = CIP`ExperimentalData`GetQSPRDataSet02[];
CIP`Graphics`ShowDataSetInfo[{"IoPairs", "InputComponents", "OutputComponents"},  
    dataSet];
Number of IO pairs = 2169


Number of input components = 130

Number of output components = 1

mlrInfo = CIP`MLR`FitMlr[dataSet];
mlrInfoInInputForm = InputForm[mlrInfo];
pointSize = 0.025;
CIP`MLR`ShowMlrSingleRegression[{"ModelVsDataPlot", "CorrelationCoefficient"}, 
  dataSet, mlrInfo, GraphicsOptionPointSize -> pointSize];

(*-Graphics-*)
(*
Out 1 : Correlation coefficient = 0.999373
*)
pointSize = 0.01;
CIP`MLR`ShowMlrSingleRegression[{"AbsoluteSortedResidualsPlot", 
   "AbsoluteResidualsStatistics", "RMSE"}, 
   dataSet, mlrInfo, GraphicsOptionPointSize -> pointSize];

(*-Graphics-

Definition of 'Residual (absolute)': Data - Model

                                                                        -1
Out 1 : Residual (absolute): Mean/Median/Maximum Value = 1.4 / 9.84 × 10   / 

             1
>   1.79 × 10

Root mean squared error (RMSE) = 2.063

*)

How do I get these -Graphics- ?

Thanks for the Help!

mike3996
  • 17,047
  • 9
  • 64
  • 80
Jujo
  • 79
  • 1
  • 5

4 Answers4

4

The J/Link User Guide has nice examples of code for sending graphics from Mathematica to Java: http://reference.wolfram.com/mathematica/JLink/tutorial/CallingJavaFromMathematica.html#29556

Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93
  • 1
    @Jujo I have tried too, it works! Which version of *Mathematica* do you use and for which platform? – Alexey Popkov Jun 17 '11 at 07:41
  • 1
    I use the Mathematica 7 Version, on a x64 Windows 7 machine. The problem is, that these Graphics are not the actual output, they will be created indirectly through the cip package. So if there is any possibility to get these graphics, tell me ;) – Jujo Jun 18 '11 at 09:17
2

Why don't you Export it to a file which you can then read back in your Java program? The format is:

Export["filename.gif",yourPicture]

Instead of .gif, various other formats (.png, jpg, .eps, .tif) are possible too.

Edit

I'm not a Java user, but the code you show in the first block doesn't seem to be able to produce the output you claim is shown in the second code block. The second block looks like the output of an interactive Mathematica session, right? In this session, input and output are mixed. I guess if you put the input part of the second block in the variable stringList in the Java program you'll generate the output from within the Java program.

If you can change the assignment to stringList to use Export to a file, then you should be able to get the graphics via this detour in your Java program.

Update
After downloading the package and working with your example, it seems that the figure output by ShowMlrSingleRegression is produced as a kind of side effect, like what happens when you use Print in a Do loop. It is not the actual output of the function, so one cannot refer to it or assign it to a variable. This makes the Export workaround and any other actually, rather difficult. Clearly, this package was not designed with usage over JavaLink in mind.

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • This seems, to be a workarround for my problem. Is it possible to save any Grahpics Mathematica will throw out ? So that i do not have to know, if the packages generate graphics or not ?! – Jujo Jun 16 '11 at 17:11
  • @Jujo mathematica can save pretty much any graphic to a file (in a number of formats). The second question in the comment is a bit unclear... – acl Jun 16 '11 at 17:39
  • How can i save these Graphics, if they are only a side effect ? – Jujo Jun 17 '11 at 07:21
  • In an interactive session you can simply do a cut and paste (even paste into an Export command). I don't think that you could do this over mathlink (but then, I haven't used it much). You might consider going into the CIP package and adding/changing commands so that it yields the figure as an output. Probably not very difficult. – Sjoerd C. de Vries Jun 17 '11 at 07:59
  • Yeah i think Mathlink can't do this. So i will try to change the CIP Commands. Thanks – Jujo Jun 17 '11 at 08:22
1

Tell Mathematica to save the image to a file on disk. The name of said file can be generated first in Java by File.createTempFile(...).

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

You have to find a library that will interpret mathematica in java and draw some picture from the mathematica output, or, if it doesn't exist, create your own.

Maybe you could check this library...

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • There are methods to generate Images from Mathematice and get this into Java like evaluateToImage(). But my Problem is, that the Methods in the loaded Packages generate my Image, and not the Java method. – Jujo Jun 16 '11 at 17:07