00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 package de.picana.generator;
00014
00015 import de.picana.control.*;
00016 import de.picana.logging.*;
00017
00018 import java.util.*;
00019 import java.io.*;
00020
00021
00028 public class Gauss2ARFF extends Task {
00029
00030 private String out_filename;
00031 private String relation;
00032 private int num_instances;
00033 private boolean invert;
00034 private Vector attributes;
00035
00036
00037 class Attr {
00038 public String name;
00039 public double min;
00040 public double max;
00041
00042 Attr(String name, double min, double max) {
00043 this.name = name;
00044 this.min = min;
00045 this.max = max;
00046 }
00047 }
00048
00049
00051 public Gauss2ARFF() {
00052 }
00053
00054 public void init(ParameterSet params, Logger logger) {
00055
00056 super.init(params, logger);
00057
00058 attributes = new Vector();
00059
00060 out_filename = (String)params.getParameter("out");
00061 relation = (String)params.getParameter("relation");
00062
00063 num_instances = 1000;
00064 try {
00065 num_instances = Integer.parseInt((String)params.getParameter("num_instances"));
00066 } catch (NumberFormatException nfe) {}
00067
00068 invert = false;
00069 try {
00070 invert = (Boolean.valueOf((String)params.getParameter("invert"))).booleanValue();
00071 } catch (NumberFormatException nfe) {}
00072
00073 ParameterSet attribute_set = (ParameterSet)params.getParameter("attributes");
00074 Enumeration attribute_names = attribute_set.getParameterNames();
00075 while (attribute_names.hasMoreElements()) {
00076 String name = (String)attribute_names.nextElement();
00077 ParameterSet attribute = (ParameterSet)attribute_set.getParameter(name);
00078
00079 double min = 0.0;
00080 try {
00081 min = Double.parseDouble((String)attribute.getParameter("min"));
00082 } catch (NumberFormatException nfe) {}
00083
00084 double max = 0.0;
00085 try {
00086 max = Double.parseDouble((String)attribute.getParameter("max"));
00087 } catch (NumberFormatException nfe) {}
00088
00089 attributes.addElement(new Attr(attribute.getName(), min, max));
00090 }
00091 }
00092
00093 public void start() throws TaskException {
00094
00095 try {
00096 logger.info(LOGSRC, "Started.");
00097
00098 Random rand = new Random();
00099
00100 FileOutputStream output = new FileOutputStream(new File(out_filename));
00101 PrintWriter pw = new PrintWriter(output);
00102
00103 pw.println("% ARFF file for picture data from PNG files");
00104 pw.println("%");
00105 pw.println("@relation " + relation);
00106 pw.println();
00107 for (int i=0; i < attributes.size(); i++) {
00108 Attr a = (Attr)attributes.elementAt(i);
00109 pw.println("@attribute " + a.name + " numeric");
00110 }
00111 pw.println();
00112 pw.println("@data");
00113 pw.println("%");
00114 pw.println("% x instances");
00115 pw.println("%");
00116
00117 for (int n=0; n < num_instances; n++) {
00118 for (int i=0; i < attributes.size(); i++) {
00119 Attr a = (Attr)attributes.elementAt(i);
00120 boolean found = false;
00121 double g = 0.0;
00122 while (!found) {
00123 if (!invert) {
00124 g = rand.nextGaussian() + 0.5;
00125 if ((g >= 0.0) && (g <= 1.0))
00126 found = true;
00127 } else {
00128 g = rand.nextGaussian();
00129 if ((g >= -0.5) && (g < 0.0)) {
00130
00131 g = Math.abs(g);
00132
00133 found = true;
00134 } else if ((g >= 0.0) && (g <= 0.5)) {
00135
00136 g = (1-g);
00137
00138 found = true;
00139 }
00140 }
00141 }
00142 pw.print(g * (a.max-a.min) + a.min);
00143 if (i != attributes.size()-1) {
00144 pw.print(",");
00145 }
00146 }
00147 pw.println();
00148 }
00149
00150 pw.close();
00151
00152 logger.info(LOGSRC, "Stopped.");
00153
00154 } catch (Exception e) {
00155 throw new TaskException(e.toString());
00156 }
00157 }
00158
00159 public void stop() {
00160 }
00161 public void pause() {
00162 }
00163 public void resume() {
00164 }
00165 }