Creating Plots using the Boundary Visualizer from the Weka API

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating Plots using the Boundary Visualizer from the Weka API

ja.m.van.eijk
Dear Posters,

For my school project, I am trying to create a Java application that creates and saves figures like the ones created with the Boundary Visualizer. In order to do this, I studied several forums, the documentation for the BoundaryPlotter (https://weka.sourceforge.io/doc.dev/weka/knowledgeflow/steps/BoundaryPlotter.html) and the documentation of the BoundaryVisualizer(https://weka.sourceforge.io/doc.dev/weka/gui/boundaryvisualizer/BoundaryVisualizer.html) among other websites.

However, after looking into the different examples and code, I couldn't find the solution. So in the end my question would be, How can you create and save figures from the Boundary Visualizer for a requested dataset/classifier using just Java code?

Thank you for reading my post, and I wish you a nice day!
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating Plots using the Boundary Visualizer from the Weka API

Peter Reutemann-3
The BoundaryVisualizer class allows you to set classifier and dataset.

Have a look at the code of the class itself (best to use a debugger to see what's happening):

https://github.com/Waikato/weka-trunk/blob/master/weka/src/main/java/weka/gui/boundaryvisualizer/BoundaryVisualizer.java

Cheers, Peter

On December 5, 2020 11:24:03 AM GMT+13:00, [hidden email] wrote:

>Dear Posters,
>
>For my school project, I am trying to create a Java application that
>creates and saves figures like the ones created with the Boundary
>Visualizer. In order to do this, I studied several forums, the
>documentation for the BoundaryPlotter
>(https://weka.sourceforge.io/doc.dev/weka/knowledgeflow/steps/BoundaryPlotter.html)
>and the documentation of the
>BoundaryVisualizer(https://weka.sourceforge.io/doc.dev/weka/gui/boundaryvisualizer/BoundaryVisualizer.html)
>among other websites.
>
>However, after looking into the different examples and code, I couldn't
>find the solution. So in the end my question would be, How can you
>create and save figures from the Boundary Visualizer for a requested
>dataset/classifier using just Java code?
>
>Thank you for reading my post, and I wish you a nice day!
>_______________________________________________
>Wekalist mailing list -- [hidden email]
>Send posts to [hidden email]
>To unsubscribe send an email to [hidden email]
>To subscribe, unsubscribe, etc., visit
>https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
>List etiquette:
>http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html

--
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 577-5304
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating Plots using the Boundary Visualizer from the Weka API

ja.m.van.eijk
When using the boundaryVisualizer class I am trying to set the classifier and instances via their set methods and use the .getGraphics method to get the image as a Grapics2d object which I can then render to a jpg (if that is the correct approach).

However, when setting the Instances, I get a long error message resulting in a NullPointerException. It seems like the set Instances method requires a Graphics object which is not provided.

So in the end, I was wondering if this method of getting an image as a jpg would work and how I could fix this issue with my Instances.

Thank you once again, and a nice day!

-ps
The error:
```
java.lang.NullPointerException: Cannot invoke "java.awt.Graphics.drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)" because "g" is null
        weka.gui.boundaryvisualizer.BoundaryPanel.update(BoundaryPanel.java:774)
        weka.gui.boundaryvisualizer.BoundaryPanel.setColors(BoundaryPanel.java:902)
        weka.gui.boundaryvisualizer.BoundaryVisualizer.configureForClassAttribute(BoundaryVisualizer.java:1084)
        weka.gui.boundaryvisualizer.BoundaryVisualizer.access$1100(BoundaryVisualizer.java:100)
        weka.gui.boundaryvisualizer.BoundaryVisualizer$10.actionPerformed(BoundaryVisualizer.java:959)
        java.desktop/javax.swing.JComboBox.fireActionEvent(JComboBox.java:1264)
        java.desktop/javax.swing.JComboBox.setSelectedItem(JComboBox.java:589)
        java.desktop/javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
        weka.gui.boundaryvisualizer.BoundaryVisualizer.setInstances(BoundaryVisualizer.java:1006)
```

My code:
```
    public void visualizeBoundaries() throws Exception {
        //System.out.println(instances);
        OneR oneR = new OneR();

        BoundaryVisualizer boundaryVisualizer = new BoundaryVisualizer();
        boundaryVisualizer.setClassifier(oneR);
        boundaryVisualizer.setInstances(instances);
        Graphics2D figure = (Graphics2D) boundaryVisualizer.getGraphics();

        System.out.println("##########I GOT HERE WITHOUT ERRORS##########");

        // Convert Graphics Object to BufferedImg

        // Save BufferedImg as jpg

    }
```
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating Plots using the Boundary Visualizer from the Weka API

Peter Reutemann
You would need to create a graphics context, e.g., a panel that is
being displayed on screen.
If you look at the main method of the BoundaryVisualizer class, you
can see that it calls the createNewVisualizerWindow method to create a
frame, using classifier and dataset.

You should be able to use the built-in component writer functionality
in Weka to create a PNG from such a panel:

import java.io.File;
import adams.gui.print.JComponentWriter
import adams.gui.print.PNGWriter

BoundaryVisualizer bvPanel = ...;
// configure and display visualizer

PNGWriter writer = new PNGWriter();
// set more options
File outFile = new File("/some/where/out.png");
JComponentWriter.toOutput(writer, bvPanel, outFile);

HTH

Cheers, Peter

On Mon, Dec 7, 2020 at 9:53 AM <[hidden email]> wrote:

>
> When using the boundaryVisualizer class I am trying to set the classifier and instances via their set methods and use the .getGraphics method to get the image as a Grapics2d object which I can then render to a jpg (if that is the correct approach).
>
> However, when setting the Instances, I get a long error message resulting in a NullPointerException. It seems like the set Instances method requires a Graphics object which is not provided.
>
> So in the end, I was wondering if this method of getting an image as a jpg would work and how I could fix this issue with my Instances.
>
> Thank you once again, and a nice day!
>
> -ps
> The error:
> ```
> java.lang.NullPointerException: Cannot invoke "java.awt.Graphics.drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)" because "g" is null
>         weka.gui.boundaryvisualizer.BoundaryPanel.update(BoundaryPanel.java:774)
>         weka.gui.boundaryvisualizer.BoundaryPanel.setColors(BoundaryPanel.java:902)
>         weka.gui.boundaryvisualizer.BoundaryVisualizer.configureForClassAttribute(BoundaryVisualizer.java:1084)
>         weka.gui.boundaryvisualizer.BoundaryVisualizer.access$1100(BoundaryVisualizer.java:100)
>         weka.gui.boundaryvisualizer.BoundaryVisualizer$10.actionPerformed(BoundaryVisualizer.java:959)
>         java.desktop/javax.swing.JComboBox.fireActionEvent(JComboBox.java:1264)
>         java.desktop/javax.swing.JComboBox.setSelectedItem(JComboBox.java:589)
>         java.desktop/javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
>         weka.gui.boundaryvisualizer.BoundaryVisualizer.setInstances(BoundaryVisualizer.java:1006)
> ```
>
> My code:
> ```
>     public void visualizeBoundaries() throws Exception {
>         //System.out.println(instances);
>         OneR oneR = new OneR();
>
>         BoundaryVisualizer boundaryVisualizer = new BoundaryVisualizer();
>         boundaryVisualizer.setClassifier(oneR);
>         boundaryVisualizer.setInstances(instances);
>         Graphics2D figure = (Graphics2D) boundaryVisualizer.getGraphics();
>
>         System.out.println("##########I GOT HERE WITHOUT ERRORS##########");
>
>         // Convert Graphics Object to BufferedImg
>
>         // Save BufferedImg as jpg
>
>     }
> ```
> _______________________________________________
> Wekalist mailing list -- [hidden email]
> Send posts to [hidden email]
> To unsubscribe send an email to [hidden email]
> To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
> List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html



--
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 577-5304
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating Plots using the Boundary Visualizer from the Weka API

ja.m.van.eijk
Create a panel using the code from the createNewVisualizerWindow method did indeed do the trick and a png was successfully made, thank you! :)

The only thing that is still a little bit unclear to me, is how to control the panel with code. When the pop-up comes, it allows me to change parameters, like selecting whether or not to include the data points and to start the actual coloring process. So my final question would be, how can I start this coloring process and adjust parameters using the code.

Once again thank you for all the advice.
And I hope you have a good day!
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating Plots using the Boundary Visualizer from the Weka API

ja.m.van.eijk
After reading my post again I wanted to elaborate a bit more on the question by adding some actual images. Currently, when saving the image, I get the entire window, which I can use to crop out the actual plot in the bottom left. ( The current plot,  https://i.imgur.com/uxRWHtk.png)

What I was wondering is, would it be possible to get a window where the image is already colored and add the data points after the coloring process, without pressing any buttons?

 Have a nice day!
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating Plots using the Boundary Visualizer from the Weka API

Peter Reutemann
> After reading my post again I wanted to elaborate a bit more on the question by adding some actual images. Currently, when saving the image, I get the entire window, which I can use to crop out the actual plot in the bottom left. ( The current plot,  https://i.imgur.com/uxRWHtk.png)
>
> What I was wondering is, would it be possible to get a window where the image is already colored and add the data points after the coloring process, without pressing any buttons?

Instead of trying to get the BoundaryVisualizer to do what you want,
how about you just copy its code and tweak it to suit your needs? Then
you can hide buttons and other elements, start/stop plotting via code
and also write only the JComponent to an image that contains the
actual plot.

Cheers, Peter
--
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 577-5304
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to [hidden email]
To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html