Commit 5e9f723a authored by Bruno López Trigo's avatar Bruno López Trigo

Engadida explicación de confusión entre clases e manexador de arquivos

parent 6c8d7d6f
Pipeline #1952 failed with stages
in 27 seconds
......@@ -2,10 +2,11 @@ package brunolopez.expliclas.builder;
import brunolopez.expliclas.exceptions.ConflictEx;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
import brunolopez.expliclas.models.DatasetConfig;
public interface BuilderManager {
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx;
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx, NotFoundEx;
}
package brunolopez.expliclas.builder;
import brunolopez.expliclas.json.MapperJSON;
import brunolopez.expliclas.utils.MapperJSON;
import brunolopez.expliclas.exceptions.ConflictEx;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
import brunolopez.expliclas.models.CategoricAttribute;
import brunolopez.expliclas.models.CategoricProperty;
import brunolopez.expliclas.models.NumericAttribute;
......@@ -10,6 +11,7 @@ import brunolopez.expliclas.models.Consequent;
import brunolopez.expliclas.models.DatasetConfig;
import brunolopez.expliclas.models.Interval;
import brunolopez.expliclas.models.NumericProperty;
import brunolopez.expliclas.utils.FileManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
......@@ -26,25 +28,22 @@ import weka.estimators.EstimatorUtils;
public class BuilderManagerImpl implements BuilderManager {
private final String BASE = getClass().getClassLoader().getResource("").getFile();
private final MapperJSON mapper;
private final FileManager fmanager;
public BuilderManagerImpl() {
this.mapper = new MapperJSON();
this.fmanager = new FileManager();
}
@Override
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx {
public DatasetConfig buildModels(String token, String datasetName, String model, String[] options) throws ConflictEx, FormatEx, NotFoundEx {
FileReader readerLRN;
DatasetConfig config = new DatasetConfig(datasetName);
File dataset = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + ".arff");
String logDirectory = this.BASE + "tmp/" + token + "/" + datasetName + "/" + model;
String logLocation = logDirectory + "/" + datasetName + ".arff." + model + ".log.txt";
File configLocation = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + ".json");
File configLocationEs = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + "-es.json");
File configLocationGl = new File(this.BASE + "tmp/" + token + "/" + datasetName + "/" + datasetName + "-gl.json");
File dataset = this.fmanager.getDataset(token, datasetName);
String logLocation = this.fmanager.getLogLocation(token, datasetName, model);
if(new File(logLocation).exists()){
throw new ConflictEx("Classifier already exists");
}
......@@ -74,7 +73,7 @@ public class BuilderManagerImpl implements BuilderManager {
reptree.buildClassifier(instancesLRN);
}
new File(logDirectory).mkdir();
new File(this.fmanager.getLogDirectory(token, datasetName, model)).mkdir();
log = new PrintStream(new FileOutputStream(logLocation, false));
log.println("=== Run information ===\n");
......@@ -171,13 +170,16 @@ public class BuilderManagerImpl implements BuilderManager {
log.close();
readerLRN.close();
if(!configLocation.exists()){
this.mapper.writeJSON(config, configLocation);
this.mapper.writeJSON(config, configLocationEs);
this.mapper.writeJSON(config, configLocationGl);
}
else
File configLocation;
try{
configLocation = this.fmanager.getConfig(token, datasetName, "en");
config = this.mapper.readConfigJSON(configLocation);
} catch (NotFoundEx ex){
this.mapper.writeJSON(config, new File(this.fmanager.getConfigLocation(token, datasetName, "en")));
this.mapper.writeJSON(config, new File(this.fmanager.getConfigLocation(token, datasetName, "es")));
this.mapper.writeJSON(config, new File(this.fmanager.getConfigLocation(token, datasetName, "gl")));
}
} catch (Exception ex) {
throw new FormatEx("Error building log, check format");
......
package brunolopez.expliclas.classifiers;
import brunolopez.expliclas.json.MapperJSON;
import brunolopez.expliclas.utils.MapperJSON;
import brunolopez.expliclas.exceptions.ConflictEx;
import brunolopez.expliclas.exceptions.FormatEx;
import brunolopez.expliclas.exceptions.NotFoundEx;
......@@ -17,83 +17,75 @@ import brunolopez.expliclas.models.Node;
import brunolopez.expliclas.models.NumericProperty;
import brunolopez.expliclas.models.Property;
import brunolopez.expliclas.models.VisualNode;
import brunolopez.expliclas.utils.FileManager;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
public class ClassifierManagerImpl implements ClassifierManager {
private final String BASE = getClass().getClassLoader().getResource("").getFile();
private TreeBuilder treeBuilder;
private TreeInterpreter treeInterpreter;
private MatrixBuilder matrixBuilder;
private final MapperJSON mapper;
private final FileManager fmanager;
public ClassifierManagerImpl() {
this.mapper = new MapperJSON();
this.fmanager = new FileManager();
}
@Override
public DatasetConfig getConfig(String token, String dataset, String lang) throws NotFoundEx, IOException {
File configFile;
DatasetConfig config = null;
DatasetConfig config;
if (!lang.equals("en")) {
configFile = new File(this.BASE + dataset + "/" + dataset + "-" + lang + ".json");
} else {
configFile = new File(this.BASE + dataset + "/" + dataset + ".json");
try{
configFile = this.fmanager.getConfig(dataset, lang);
} catch (NotFoundEx ex) {
configFile = this.fmanager.getConfig(token, dataset, lang);
}
if (configFile.exists()) {
config = this.mapper.readConfigJSON(configFile);
} else {
if (!lang.equals("en")) {
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-" + lang + ".json");
} else {
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
}
if (configFile.exists()) {
config = this.mapper.readConfigJSON(configFile);
} else {
throw new NotFoundEx("Config not found");
}
}
config = this.mapper.readConfigJSON(configFile);
return config;
}
@Override
public DatasetConfig updateConfig(String token, String dataset, DatasetConfig config, String lang) throws NotFoundEx, FormatEx, IOException {
File configFileEn = null, configFileEs = null, configFileGl = null;
DatasetConfig oldConfig = null, newConfig = null;
File configFileEn, configFileEs, configFileGl, treeFile, logFile;
DatasetConfig oldConfig, newConfig;
boolean rewrite = false;
switch (lang) {
case "en":
configFileEn = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
if (!configFileEn.exists()) {
throw new NotFoundEx("Config not found");
}
configFileEn = this.fmanager.getConfig(token, dataset, lang);
oldConfig = this.mapper.readConfigJSON(configFileEn);
if (checkAttributesFormat(oldConfig, config)) {
rewrite = true;
}
checkConsequentsFormat(oldConfig, config);
configFileGl = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-gl.json");
newConfig = this.mapper.readConfigJSON(configFileGl);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileGl);
configFileEs = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-es.json");
newConfig = this.mapper.readConfigJSON(configFileEs);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEs);
this.mapper.writeJSON(config, configFileEn);
if(rewrite){
configFileGl = this.fmanager.getConfig(token, dataset, "gl");
newConfig = this.mapper.readConfigJSON(configFileGl);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileGl);
configFileEs = this.fmanager.getConfig(token, dataset, "es");
newConfig = this.mapper.readConfigJSON(configFileEs);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEs);
}
break;
case "gl":
configFileGl = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-" + lang + ".json");
configFileGl = this.fmanager.getConfig(token, dataset, lang);
if (!configFileGl.exists()) {
throw new NotFoundEx("Config not found");
}
......@@ -102,18 +94,20 @@ public class ClassifierManagerImpl implements ClassifierManager {
rewrite = true;
}
checkConsequentsFormat(oldConfig, config);
configFileEn = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
newConfig = this.mapper.readConfigJSON(configFileEn);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEn);
configFileEs = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-es.json");
newConfig = this.mapper.readConfigJSON(configFileEs);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEs);
this.mapper.writeJSON(config, configFileGl);
if(rewrite){
configFileEn = this.fmanager.getConfig(token, dataset, "en");
newConfig = this.mapper.readConfigJSON(configFileEn);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEn);
configFileEs = this.fmanager.getConfig(token, dataset, "es");
newConfig = this.mapper.readConfigJSON(configFileEs);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEs);
}
break;
case "es":
configFileEs = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-" + lang + ".json");
configFileEs = this.fmanager.getConfig(token, dataset, lang);
if (!configFileEs.exists()) {
throw new NotFoundEx("Config not found");
}
......@@ -122,18 +116,40 @@ public class ClassifierManagerImpl implements ClassifierManager {
rewrite = true;
}
checkConsequentsFormat(oldConfig, config);
configFileEn = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
newConfig = this.mapper.readConfigJSON(configFileEn);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEn);
configFileGl = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-gl.json");
newConfig = this.mapper.readConfigJSON(configFileGl);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileGl);
this.mapper.writeJSON(config, configFileEs);
if(rewrite){
configFileEn = this.fmanager.getConfig(token, dataset, "en");
newConfig = this.mapper.readConfigJSON(configFileEn);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileEn);
configFileGl = this.fmanager.getConfig(token, dataset, "gl");
newConfig = this.mapper.readConfigJSON(configFileGl);
newConfig.setAttributes(config.getAttributes());
this.mapper.writeJSON(newConfig, configFileGl);
}
break;
}
if(rewrite || lang.equals("en")){
try {
treeFile = this.fmanager.getTree(token, dataset, "J48");
configFileEn = this.fmanager.getConfig(token, dataset, "en");
logFile = this.fmanager.getLog(token, dataset, "J48");
this.treeBuilder = new TreeBuilder(logFile, configFileEn, treeFile);
this.treeBuilder.buildTree(true);
treeFile = this.fmanager.getTree(token, dataset, "REPTree");
logFile = this.fmanager.getLog(token, dataset, "J48");
this.treeBuilder = new TreeBuilder(logFile, configFileEn, treeFile);
this.treeBuilder.buildTree(true);
treeFile = this.fmanager.getTree(token, dataset, "RandomTree");
logFile = this.fmanager.getLog(token, dataset, "RandomTree");
this.treeBuilder = new TreeBuilder(logFile, configFileEn, treeFile);
this.treeBuilder.buildTree(true);
} catch (NotFoundEx | ConflictEx ex) {
throw new IIOException("Error updating tree");
}
}
return config;
}
......@@ -218,7 +234,7 @@ public class ClassifierManagerImpl implements ClassifierManager {
correct = true;
found = false;
}
return newInterval;
}
......@@ -245,59 +261,30 @@ public class ClassifierManagerImpl implements ClassifierManager {
@Override
public VisualNode buildTree(String token, String dataset, String algorithm) throws NotFoundEx, FormatEx, IOException, ConflictEx {
File log = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + algorithm + "/" + dataset + ".arff." + algorithm + ".log.txt");
File configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
File outputTree = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + algorithm + "/tree.json");
/*File log = new File(this.BASE + dataset + "/" + algorithm + "/" + dataset + ".arff." + algorithm + ".log.txt");
File configFile = new File(this.BASE + dataset + "/" + dataset + ".json");
File outputTree = new File(this.BASE + dataset + "/" + algorithm + "/tree.json");
*/
File log = this.fmanager.getLog(token, dataset, algorithm);
File configFile = this.fmanager.getConfig(token, dataset, "en");
File outputTree = new File(this.fmanager.getTreeLocation(token, dataset, algorithm));
/*File log = this.fmanager.getLog(dataset, algorithm);
File configFile = this.fmanager.getConfig(dataset, "en");
File outputTree = new File(this.fmanager.getTreeLocation(dataset, algorithm));*/
this.treeBuilder = new TreeBuilder(log, configFile, outputTree);
return this.treeBuilder.buildTree();
return this.treeBuilder.buildTree(false);
}
@Override
public VisualNode getTree(String token, String dataset, String algorithm, String lang) throws NotFoundEx, IOException {
String location;
File treeFile, configFile = null;
location = this.BASE + dataset + "/" + algorithm + "/tree.json";
switch (lang) {
case "en":
configFile = new File(this.BASE + dataset + "/" + dataset + ".json");
break;
case "es":
configFile = new File(this.BASE + dataset + "/" + dataset + "-" + lang + ".json");
break;
case "gl":
configFile = new File(this.BASE + dataset + "/" + dataset + "-" + lang + ".json");
break;
}
treeFile = new File(location);
if (!treeFile.exists()) {
location = this.BASE + "tmp/" + token + "/" + dataset + "/" + algorithm + "/tree.json";
switch (lang) {
case "en":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
break;
case "es":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-" + lang + ".json");
break;
case "gl":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-" + lang + ".json");
break;
}
}
treeFile = new File(location);
if (!treeFile.exists()) {
throw new NotFoundEx("Tree not found");
File treeFile, configFile;
try{
treeFile = this.fmanager.getTree(dataset, algorithm);
configFile = this.fmanager.getConfig(dataset, lang);
} catch (NotFoundEx ex) {
treeFile = this.fmanager.getTree(token, dataset, algorithm);
configFile = this.fmanager.getConfig(token, dataset, lang);
}
DatasetConfig config = this.mapper.readConfigJSON(configFile);
......@@ -314,46 +301,21 @@ public class ClassifierManagerImpl implements ClassifierManager {
@Override
public Classification classify(String token, String dataset, String algorithm, String lang, Instance instance) throws NotFoundEx, FormatEx, IOException {
File configFile = null;
File configFile;
DatasetConfig config = null;
Node root = null;
DatasetConfig config;
Node root;
switch (lang) {
case "en":
configFile = new File(this.BASE + dataset + "/" + dataset + ".json");
break;
case "es":
configFile = new File(this.BASE + dataset + "/" + dataset + "-es.json");
break;
case "gl":
configFile = new File(this.BASE + dataset + "/" + dataset + "-gl.json");
break;
}
if (configFile.exists()) {
try{
configFile = this.fmanager.getConfig(dataset, lang);
config = this.mapper.readConfigJSON(configFile);
root = this.mapper.readTreeJSON(new File(this.BASE + dataset + "/" + algorithm + "/tree.json"));
} else {
switch (lang) {
case "en":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
break;
case "es":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-es.json");
break;
case "gl":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-gl.json");
break;
}
if(!configFile.exists()){
throw new NotFoundEx("Classifier not found");
}
root = this.mapper.readTreeJSON(this.fmanager.getTree(dataset, algorithm));
} catch (NotFoundEx ex) {
configFile = this.fmanager.getConfig(token, dataset, lang);
config = this.mapper.readConfigJSON(configFile);
root = this.mapper.readTreeJSON(new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + algorithm + "/tree.json"));
root = this.mapper.readTreeJSON(this.fmanager.getTree(token, dataset, algorithm));
}
for (Map.Entry<String, Object> entry
: instance.getValues()
.entrySet()) {
......@@ -362,7 +324,6 @@ public class ClassifierManagerImpl implements ClassifierManager {
}
}
this.treeInterpreter = new TreeInterpreter(root);
return this.treeInterpreter.classify(instance, config);
......@@ -371,44 +332,19 @@ public class ClassifierManagerImpl implements ClassifierManager {
@Override
public ArrayList<Classification> classify(String token, String dataset, String algorithm, String lang, Instance instance, double percentage) throws NotFoundEx, FormatEx, IOException {
DatasetConfig config = null;
File configFile = null;
DatasetConfig config;
File configFile;
Node root = null;
Node root;
switch (lang) {
case "en":
configFile = new File(this.BASE + dataset + "/" + dataset + ".json");
break;
case "es":
configFile = new File(this.BASE + dataset + "/" + dataset + "-es.json");
break;
case "gl":
configFile = new File(this.BASE + dataset + "/" + dataset + "-gl.json");
break;
}
if (configFile.exists()) {
try{
configFile = this.fmanager.getConfig(dataset, lang);
config = this.mapper.readConfigJSON(configFile);
root = this.mapper.readTreeJSON(new File(this.BASE + dataset + "/" + algorithm + "/tree.json"));
} else {
switch (lang) {
case "en":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + ".json");
break;
case "es":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-es.json");
break;
case "gl":
configFile = new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + dataset + "-gl.json");
break;
}
if(!configFile.exists()){
throw new NotFoundEx("Classifier not found");
}
root = this.mapper.readTreeJSON(this.fmanager.getTree(dataset, algorithm));
} catch (NotFoundEx ex) {
configFile = this.fmanager.getConfig(token, dataset, lang);
config = this.mapper.readConfigJSON(configFile);
root = this.mapper.readTreeJSON(new File(this.BASE + "tmp/" + token + "/" + dataset + "/" + algorithm + "/tree.json"));
root = this.mapper.readTreeJSON(this.fmanager.getTree(token, dataset, algorithm));
}
for (Map.Entry<String, Object> entry
......@@ -429,13 +365,10 @@ public class ClassifierManagerImpl implements ClassifierManager {
public Matrix getMatrix(String token, String dataset, String algorithm) throws NotFoundEx, IOException {
File input;