00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 package de.picana.converter;
00014
00015 import de.picana.control.*;
00016 import de.picana.logging.*;
00017 import de.picana.clusterer.*;
00018
00019 import org.apache.velocity.VelocityContext;
00020 import org.apache.velocity.Template;
00021 import org.apache.velocity.app.VelocityEngine;
00022
00023 import weka.core.*;
00024
00025 import java.util.*;
00026 import java.io.*;
00027 import java.text.*;
00028
00029
00036 public class Stats2File extends Task {
00037
00038 private String statfile;
00039 private String modelfile;
00040 private String centroidfile;
00041 private String trainingfile;
00042 private String templatefile;
00043 private String resultfile;
00044 private ParameterSet context_set;
00045
00046 private VectorFreqList model;
00047 private VectorFreqList centroids;
00048
00049
00051 public class VectorFreqList {
00052
00053 private Vector set;
00054 private Hashtable attributes;
00055
00056 public VectorFreqList() {
00057 this.set = new Vector();
00058 attributes = new Hashtable();
00059 }
00060
00061 public void addVector(MLVector vec) {
00062 this.set.addElement(vec);
00063 }
00064
00065 public void addAttribute(String att) {
00066 attributes.put(att, new Integer(attributes.size()));
00067 }
00068
00069 public double value(int inst, int att) {
00070 return ((MLVector)set.elementAt(inst)).value[att];
00071 }
00072
00073 public String value(int inst, String att) {
00074 Integer a = (Integer)attributes.get(att);
00075 return String.valueOf(((MLVector)set.elementAt(inst)).value[a.intValue()]);
00076 }
00077
00078 public int freq(int inst) {
00079 return ((MLVector)set.elementAt(inst)).freq;
00080 }
00081
00082 public Enumeration attributes() {
00083 return attributes.keys();
00084 }
00085
00086 public int numAttributes() {
00087 return attributes.size();
00088 }
00089
00090 public int numVectors() {
00091 return set.size();
00092 }
00093 }
00094
00095
00097 public class Functions {
00098
00099 private DecimalFormat decf;
00100
00101 public Functions() {
00102 decf = (DecimalFormat)DecimalFormat.getInstance(Locale.ENGLISH);
00103 decf.applyPattern("####0.000000");
00104 }
00105
00106 public String decf(String value) {
00107 double d = 0.0;
00108 try {
00109 d = Double.parseDouble(value);
00110 } catch (NumberFormatException nfe) {}
00111 return decf.format(d);
00112 }
00113
00114 public String getDateTimeString() {
00115 return Logger.getDateTimeString();
00116 }
00117
00118 public String getTimeString(String time) {
00119 long l = 0;
00120 try {
00121 l = Long.parseLong(time);
00122 } catch (NumberFormatException nfe) {}
00123 return Task.getTimeString(l);
00124 }
00125
00126 public String getFilename(String path) {
00127 return (new File(path)).getName();
00128 }
00129
00130 public String getPath(String path) {
00131 String cp = "";
00132 try {
00133 cp = (new File(path)).getCanonicalPath().replace('\\','/');
00134 } catch (IOException io) {}
00135 return cp;
00136 }
00137
00138 public String getColor(String x, String y, String z) {
00139 int red = 0, green = 0, blue = 0;
00140 try {
00141 red = (int)(Double.parseDouble(x) * 255);
00142 green = (int)(Double.parseDouble(y) * 255);
00143 blue = (int)(Double.parseDouble(z) * 255);
00144 } catch (NumberFormatException nfe) {};
00145 String hexRed = Integer.toHexString(red);
00146 if (hexRed.length() == 1) hexRed = "0" + hexRed;
00147 String hexGreen = Integer.toHexString(green);
00148 if (hexGreen.length() == 1) hexGreen = "0" + hexGreen;
00149 String hexBlue = Integer.toHexString(blue);
00150 if (hexBlue.length() == 1) hexBlue = "0" + hexBlue;
00151 return "#" + hexRed + hexGreen + hexBlue;
00152 }
00153
00154 public String getX(String x, String y, String z) {
00155 double dx = 0.0, dy = 0.0, dz = 0.0;
00156 try {
00157 dx = Double.parseDouble(x);
00158 dy = Double.parseDouble(y);
00159 dz = 1.0 - Double.parseDouble(z);
00160 } catch (NumberFormatException nfe) {};
00161 return String.valueOf(50 + Math.round(300 * (dx + 0.5 * dz * Math.cos(0.5235988))));
00162 }
00163
00164 public String getY(String x, String y, String z) {
00165 double dx = 0.0, dy = 0.0, dz = 0.0;
00166 try {
00167 dx = Double.parseDouble(x);
00168 dy = Double.parseDouble(y);
00169 dz = 1.0 - Double.parseDouble(z);
00170 } catch (NumberFormatException nfe) {};
00171 return String.valueOf(425 - Math.round(300 * (dy + 0.5 * dz * Math.sin(0.5235988))));
00172 }
00173
00174 public String getColor(String value) {
00175 double v = 0.0;
00176 try {
00177 v = Double.parseDouble(value);
00178 } catch (NumberFormatException nfe) {};
00179 return String.valueOf(Math.round(v * 255));
00180 }
00181 }
00182
00183
00184
00186 public Stats2File() {
00187 }
00188
00189 public void init(ParameterSet params, Logger logger) {
00190
00191 super.init(params, logger);
00192
00193 statfile = (String)params.getParameter("statistics");
00194 modelfile = (String)params.getParameter("model");
00195 centroidfile = (String)params.getParameter("centroids");
00196 templatefile = (String)params.getParameter("template");
00197 resultfile = (String)params.getParameter("out");
00198 context_set = (ParameterSet)params.getParameter("context");
00199 }
00200
00201 public void start() throws TaskException {
00202
00203 try {
00204 String resourcePath = (new File(templatefile)).getParentFile().getCanonicalPath();
00205 String resourceFile = (new File(templatefile)).getName();
00206
00207 Properties p = new Properties();
00208 p.setProperty("file.resource.loader.path", resourcePath);
00209
00210 VelocityEngine ve = new VelocityEngine();
00211 ve.init(p);
00212
00213 VelocityContext context = new VelocityContext();
00214
00215
00216
00217 if (statfile != null) {
00218
00219 BufferedReader reader = new BufferedReader(new FileReader(new File(statfile)));
00220
00221 String line;
00222 while ((line = reader.readLine()) != null) {
00223 String key = line.substring(0, line.indexOf(":"));
00224 String value = line.substring(line.indexOf(":")+1, line.length());
00225 context.put(key, value);
00226 }
00227 }
00228
00229
00230
00231
00232 if (modelfile != null) {
00233
00234 model = new VectorFreqList();
00235
00236 BufferedReader in = new BufferedReader(new FileReader(new File(modelfile)));
00237 String line = in.readLine();
00238 StringTokenizer tok = new StringTokenizer(line, ",");
00239 while (tok.hasMoreTokens()) {
00240 model.addAttribute((String)tok.nextToken());
00241 }
00242
00243 while((line = in.readLine()) != null) {
00244 tok = new StringTokenizer(line, ",");
00245 MLVector vec = new MLVector(model.numAttributes());
00246 int j=0;
00247 while (tok.hasMoreTokens()) {
00248 double d = 0.0;
00249 String token = tok.nextToken();
00250 try {
00251 d = Double.parseDouble(token);
00252 } catch (NumberFormatException nfe) {}
00253 vec.value[j] = d;
00254 j++;
00255 }
00256 vec.freq = 1;
00257 model.addVector(vec);
00258 }
00259
00260 in.close();
00261
00262 context.put("model", model);
00263 }
00264
00265
00266
00267 if (centroidfile != null) {
00268
00269 centroids = new VectorFreqList();
00270
00271 BufferedReader in = new BufferedReader(new FileReader(new File(centroidfile)));
00272 String line = in.readLine();
00273 line = in.readLine();
00274 String vector = line.substring(0, line.indexOf(":"));
00275 StringTokenizer tok = new StringTokenizer(vector, ",");
00276 while (tok.hasMoreTokens()) {
00277 centroids.addAttribute((String)tok.nextToken());
00278 }
00279
00280 while((line = in.readLine()) != null) {
00281 vector = line.substring(0, line.indexOf(":"));
00282 String f = line.substring(line.indexOf(":")+1, line.length());
00283 int freq = 0;
00284 try {
00285 freq = Integer.parseInt(f);
00286 } catch (NumberFormatException nfe) {}
00287
00288 tok = new StringTokenizer(vector, ",");
00289 MLVector vec = new MLVector(centroids.numAttributes());
00290 int j=0;
00291 while (tok.hasMoreTokens()) {
00292 double d = 0.0;
00293 String token = tok.nextToken();
00294 try {
00295 d = Double.parseDouble(token);
00296 } catch (NumberFormatException nfe) {}
00297 vec.value[j] = d;
00298 j++;
00299 }
00300 vec.freq = freq;
00301 centroids.addVector(vec);
00302 }
00303
00304 in.close();
00305
00306 context.put("centroids", centroids);
00307 }
00308
00309
00310
00311 if (context_set != null) {
00312
00313 Enumeration e = context_set.getParameterNames();
00314 while (e.hasMoreElements()) {
00315 String key = (String)e.nextElement();
00316 context.put(key, (String)context_set.getParameter(key));
00317 }
00318 }
00319
00320 context.put("func", new Functions());
00321
00322 Template template = ve.getTemplate(resourceFile);
00323
00324 PrintWriter pw = new PrintWriter(new FileOutputStream(new File(resultfile)));
00325
00326 template.merge( context, pw );
00327
00328 pw.close();
00329
00330 } catch (Exception ex) {
00331 throw new TaskException(ex.toString());
00332 }
00333 }
00334
00335 public void stop() {
00336
00337 }
00338
00339 public void pause() {
00340
00341 }
00342
00343 public void resume() {
00344
00345 }
00346 }