Skip to content
Snippets Groups Projects
Commit 897afc26 authored by a.croix's avatar a.croix
Browse files

Refactoring

parent f4954bea
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,6 @@ package be.cylab.java.wowa.training; ...@@ -2,7 +2,6 @@ package be.cylab.java.wowa.training;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.logging.SimpleFormatter; import java.util.logging.SimpleFormatter;
...@@ -63,21 +62,19 @@ public final class Example { ...@@ -63,21 +62,19 @@ public final class Example {
selection_method, selection_method,
generation_population_method); generation_population_method);
Trainer trainer = new Trainer(parameters, new SolutionDistance(5)); Trainer trainer = new Trainer(parameters, new SolutionDistance(5));
List<double[]> data = Utils.convertJsonToDataForTrainer(
"./ressources/webshell_data.json");
double[] expected = Utils.convertJsonToExpectedForTrainer(
"./ressources/webshell_expected.json");
long start_time = System.currentTimeMillis(); long start_time = System.currentTimeMillis();
AbstractSolution solution = trainer.run(data, expected); AbstractSolution solution = trainer.run(
"./ressources/webshell_data.json",
"./ressources/webshell_expected.json");
System.out.println(solution); System.out.println(solution);
long end_time = System.currentTimeMillis(); long end_time = System.currentTimeMillis();
logger.log(Level.INFO, "Execution time : " logger.log(Level.INFO, "Execution time : "
+ (end_time - start_time) / 1000 + " seconds"); + (end_time - start_time) / 1000 + " seconds");
Utils.computeRocPoints(solution, data, expected, true); //Utils.computeRocPoints(solution, data, expected, true);
System.out.println("AUC value : " //System.out.println("AUC value : "
+ Utils.computeAUC(solution, data, expected)); // + Utils.computeAUC(solution, data, expected));
} }
} }
...@@ -28,6 +28,21 @@ public class Trainer { ...@@ -28,6 +28,21 @@ public class Trainer {
this.factory = new Factory(solution_type); this.factory = new Factory(solution_type);
} }
/**
* Run method with file as arguments.
* @param data_file_name
* @param expected_file_name
* @return
*/
public final AbstractSolution run(
final String data_file_name,
final String expected_file_name) {
List<double[]> data = Utils.convertJsonToDataForTrainer(data_file_name);
double[] expected
= Utils.convertJsonToExpectedForTrainer(expected_file_name);
return this.run(data, expected);
}
/** /**
* @param data * @param data
* @param expected * @param expected
......
...@@ -21,7 +21,7 @@ import java.util.Random; ...@@ -21,7 +21,7 @@ import java.util.Random;
/** /**
* Class Utils. Contain tools for Trainer algorithm * Class Utils. Contain tools for Trainer algorithm
*/ */
public final class Utils { final class Utils {
private Utils() { private Utils() {
...@@ -125,15 +125,11 @@ public final class Utils { ...@@ -125,15 +125,11 @@ public final class Utils {
* *
* @param filename * @param filename
* @return * @return
* @throws IOException if we cannot read file
*/ */
public static List<double[]> convertJsonToDataForTrainer( public static List<double[]> convertJsonToDataForTrainer(
final String filename) final String filename) {
throws IOException {
Genson genson = new Genson(); Genson genson = new Genson();
String data_json = new String(Files.readAllBytes(Paths.get(filename)), String data_json = Utils.readFileToString(filename);
StandardCharsets.UTF_8);
List<double[]> data = genson.deserialize( List<double[]> data = genson.deserialize(
data_json, data_json,
new GenericType<List<double[]>>() { new GenericType<List<double[]>>() {
...@@ -146,19 +142,33 @@ public final class Utils { ...@@ -146,19 +142,33 @@ public final class Utils {
* *
* @param filename * @param filename
* @return * @return
* @throws IOException if we cannot read file
*/ */
public static double[] convertJsonToExpectedForTrainer( public static double[] convertJsonToExpectedForTrainer(
final String filename) throws IOException { final String filename) {
Genson genson = new Genson(); Genson genson = new Genson();
String expected_json = new String( String expected_json = Utils.readFileToString(filename);
Files.readAllBytes(Paths.get(filename)),
StandardCharsets.UTF_8);
double[] expected = genson.deserialize(expected_json, double[].class); double[] expected = genson.deserialize(expected_json, double[].class);
return expected; return expected;
} }
/**
* Method to read file and convert into a String.
* @param filename
* @return
*/
private static String readFileToString(final String filename) {
String json = null;
try {
json = new String(Files.readAllBytes(Paths.get(filename)),
StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
/** /**
* @param solution * @param solution
* @param data * @param data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment