Skip to content

Commit

Permalink
KnowledgeBase is now an interface, while the implementation moved to …
Browse files Browse the repository at this point in the history
…StandardKnowledgeBase. The interface contains a static factory method to produce any KB. This enable us to define the knowledgeBase field of BaseTrainable private and final.
  • Loading branch information
datumbox committed Jan 8, 2016
1 parent a378241 commit ce5d2f1
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 166 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,7 @@
CHANGELOG
=========

Version 0.7.0-SNAPSHOT - Build 20160107
Version 0.7.0-SNAPSHOT - Build 20160108
---------------------------------------

- Rename the erase() method to delete() in all interfaces.
Expand Down Expand Up @@ -50,6 +50,7 @@ Version 0.7.0-SNAPSHOT - Build 20160107
- Changed the behaviour and the public methods of the DatabaseConnector interface. The dropDatabase() is replaced with a clear() method that deletes all the data but does not close the connection with the database.
- KnowledgeBase is no longer serializable. Its serializable fields are stored individually into the Database.
- Restructuring the framework to remove all FindBug warnings.
- KnowledgeBase is now an interface, while the implementation moved to StandardKnowledgeBase. The interface contains a static factory method to produce any KB. This enable us to define the knowledgeBase field of BaseTrainable private and final.

Version 0.6.1 - Build 20160102
------------------------------
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -15,7 +15,7 @@ The code is licensed under the [Apache License, Version 2.0](https://github.com/
Version
-------

The latest version is 0.7.0-SNAPSHOT (Build 20160107).
The latest version is 0.7.0-SNAPSHOT (Build 20160108).

The [master branch](https://github.com/datumbox/datumbox-framework/tree/master) is the latest stable version of the framework. The [devel branch](https://github.com/datumbox/datumbox-framework/tree/devel) is the development branch. All the previous stable versions are marked with [tags](https://github.com/datumbox/datumbox-framework/releases).

Expand Down
1 change: 1 addition & 0 deletions TODO.txt
Expand Up @@ -4,6 +4,7 @@ CODE IMPROVEMENTS
- Checkout some of the "Load of known null value" perhaps we can completely remove this parameter if it is always null. MatrixDataframe.newInstance param recordIdsReference.
- Checkout that the example project passes as is. Check that the hybridized is on by default.
- Update the example project to the latest version build and the config file.

- Add a predictRecord() method BaseMLmodel and refactor the code to be implemented by every algorithm.
- Add multithreading support.
- Update all maven plugins and dependencies to their latest versions.
Expand Down
Expand Up @@ -18,21 +18,23 @@
import com.datumbox.common.dataobjects.Dataframe;
import com.datumbox.common.objecttypes.Trainable;
import com.datumbox.common.persistentstorage.interfaces.DatabaseConfiguration;
import com.datumbox.framework.machinelearning.common.dataobjects.StandardKnowledgeBase;
import com.datumbox.framework.machinelearning.common.dataobjects.KnowledgeBase;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Base class for every Model of the Framework. This includes Machine Learning
* Base class for every Trainable Algorithm of the Framework. This includes Machine Learning
* Models, Data Transformers, Feature Selectors etc.
*
* @author Vasilis Vryniotis <bbriniotis@datumbox.com>
* @param <MP>
* @param <TP>
* @param <KB>
*/
public abstract class BaseTrainable<MP extends BaseModelParameters, TP extends BaseTrainingParameters, KB extends KnowledgeBase<MP, TP>> implements Trainable<MP, TP> {
public abstract class BaseTrainable<MP extends BaseModelParameters, TP extends BaseTrainingParameters, KB extends StandardKnowledgeBase<MP, TP>> implements Trainable<MP, TP> {

/**
* The Logger of all algorithms.
Expand All @@ -43,13 +45,12 @@ public abstract class BaseTrainable<MP extends BaseModelParameters, TP extends B
/**
* The name of the Database where we persist our data.
*/
protected String dbName;
protected final String dbName;

/**
* The KnowledgeBase instance of the algorithm. Do NOT access it directly,
* use the kb() getter instead.
* The KnowledgeBase instance of the algorithm.
*/
protected KB knowledgeBase;
private final KB knowledgeBase;

/**
* Generates a new instance of a BaseTrainable by providing the Class of the
Expand All @@ -58,13 +59,13 @@ public abstract class BaseTrainable<MP extends BaseModelParameters, TP extends B
* @param <BT>
* @param aClass
* @param dbName
* @param dbConfig
* @param dbConf
* @return
*/
public static <BT extends BaseTrainable> BT newInstance(Class<BT> aClass, String dbName, DatabaseConfiguration dbConfig) {
public static <BT extends BaseTrainable> BT newInstance(Class<BT> aClass, String dbName, DatabaseConfiguration dbConf) {
BT algorithm = null;
try {
algorithm = aClass.getConstructor(String.class, DatabaseConfiguration.class).newInstance(dbName, dbConfig);
algorithm = aClass.getConstructor(String.class, DatabaseConfiguration.class).newInstance(dbName, dbConf);
}
catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
throw new RuntimeException(ex);
Expand All @@ -74,34 +75,23 @@ public static <BT extends BaseTrainable> BT newInstance(Class<BT> aClass, String
}

/**
* Protected Constructor which does not include the initialization of the
* KnowledgeBase.
* The basic Constructor of all BaseTrainable classes.
*
* @param dbName
* @param baseDBname
* @param dbConf
* @param kbClass
* @param kbSubtypeClasses
*/
protected BaseTrainable(String dbName, DatabaseConfiguration dbConf) {
protected BaseTrainable(String baseDBname, DatabaseConfiguration dbConf, Class<? extends StandardKnowledgeBase> kbClass, Class<? extends Serializable>... kbSubtypeClasses) {
String methodName = this.getClass().getSimpleName();
String dbNameSeparator = dbConf.getDBnameSeparator();
if(!dbName.contains(methodName+dbNameSeparator)) { //patch for the K-fold cross validation which already contains the name of the algorithm in the dbname
dbName += dbNameSeparator + methodName;
if(!baseDBname.contains(methodName+dbNameSeparator)) { //patch for the K-fold cross validation which already contains the name of the algorithm in the dbname
baseDBname += dbNameSeparator + methodName;
}

this.dbName = dbName;
}

/**
* Protected Constructor which includes the initialization of the
* KnowledgeBase.
*
* @param dbName
* @param dbConf
* @param mpClass
* @param tpClass
*/
protected BaseTrainable(String dbName, DatabaseConfiguration dbConf, Class<MP> mpClass, Class<TP> tpClass) {
this(dbName, dbConf);
this.knowledgeBase = (KB) new KnowledgeBase(this.dbName, dbConf, mpClass, tpClass);
dbName = baseDBname;

knowledgeBase = (KB) KnowledgeBase.newInstance(kbClass, dbName, dbConf, kbSubtypeClasses);
}

/** {@inheritDoc} */
Expand Down
Expand Up @@ -22,7 +22,7 @@

import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseModelParameters;
import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainingParameters;
import com.datumbox.framework.machinelearning.common.dataobjects.KnowledgeBase;
import com.datumbox.framework.machinelearning.common.dataobjects.StandardKnowledgeBase;

/**
* Base class for all the Data Transformers of the framework.
Expand All @@ -31,7 +31,7 @@
* @param <MP>
* @param <TP>
*/
public abstract class DataTransformer<MP extends DataTransformer.ModelParameters, TP extends DataTransformer.TrainingParameters> extends BaseTrainable<MP, TP, KnowledgeBase<MP, TP>> {
public abstract class DataTransformer<MP extends DataTransformer.ModelParameters, TP extends DataTransformer.TrainingParameters> extends BaseTrainable<MP, TP, StandardKnowledgeBase<MP, TP>> {

/**
* The ModelParameters class stores the coefficients that were learned during
Expand Down Expand Up @@ -65,7 +65,7 @@ public static abstract class TrainingParameters extends BaseTrainingParameters {
* @see com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainable#BaseTrainable(java.lang.String, com.datumbox.common.persistentstorage.interfaces.DatabaseConfiguration, java.lang.Class, java.lang.Class)
*/
protected DataTransformer(String dbName, DatabaseConfiguration dbConf, Class<MP> mpClass, Class<TP> tpClass) {
super(dbName, dbConf, mpClass, tpClass);
super(dbName, dbConf, StandardKnowledgeBase.class, mpClass, tpClass);
}

/**
Expand Down
Expand Up @@ -22,7 +22,7 @@
import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainable;
import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseModelParameters;
import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainingParameters;
import com.datumbox.framework.machinelearning.common.dataobjects.KnowledgeBase;
import com.datumbox.framework.machinelearning.common.dataobjects.StandardKnowledgeBase;

/**
* Base class for all the Feature Selectors of the framework.
Expand All @@ -31,7 +31,7 @@
* @param <MP>
* @param <TP>
*/
public abstract class FeatureSelection<MP extends FeatureSelection.ModelParameters, TP extends FeatureSelection.TrainingParameters> extends BaseTrainable<MP, TP, KnowledgeBase<MP, TP>> {
public abstract class FeatureSelection<MP extends FeatureSelection.ModelParameters, TP extends FeatureSelection.TrainingParameters> extends BaseTrainable<MP, TP, StandardKnowledgeBase<MP, TP>> {

/**
* The ModelParameters class stores the coefficients that were learned during
Expand Down Expand Up @@ -65,7 +65,7 @@ public static abstract class TrainingParameters extends BaseTrainingParameters {
* @see com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainable#BaseTrainable(java.lang.String, com.datumbox.common.persistentstorage.interfaces.DatabaseConfiguration, java.lang.Class, java.lang.Class)
*/
protected FeatureSelection(String dbName, DatabaseConfiguration dbConf, Class<MP> mpClass, Class<TP> tpClass) {
super(dbName, dbConf, mpClass, tpClass);
super(dbName, dbConf, StandardKnowledgeBase.class, mpClass, tpClass);
}


Expand Down
Expand Up @@ -72,17 +72,16 @@ public static abstract class ValidationMetrics extends BaseValidationMetrics {
}

/**
* @param dbName
* @param baseDBname
* @param dbConf
* @param mpClass
* @param tpClass
* @param vmClass
* @param modelValidator
* @see com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainable#BaseTrainable(java.lang.String, com.datumbox.common.persistentstorage.interfaces.DatabaseConfiguration, java.lang.Class, java.lang.Class)
*/
protected BaseMLmodel(String dbName, DatabaseConfiguration dbConf, Class<MP> mpClass, Class<TP> tpClass, Class<VM> vmClass, ModelValidation<MP, TP, VM> modelValidator) {
super(dbName, dbConf);
this.knowledgeBase = new MLmodelKnowledgeBase<>(this.dbName, dbConf, mpClass, tpClass, vmClass);
protected BaseMLmodel(String baseDBname, DatabaseConfiguration dbConf, Class<MP> mpClass, Class<TP> tpClass, Class<VM> vmClass, ModelValidation<MP, TP, VM> modelValidator) {
super(baseDBname, dbConf, MLmodelKnowledgeBase.class, mpClass, tpClass, vmClass);
this.modelValidator = modelValidator;
}

Expand Down
Expand Up @@ -21,9 +21,9 @@
import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseModelParameters;
import com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainingParameters;
import com.datumbox.framework.machinelearning.common.bases.mlmodels.BaseMLmodel;
import com.datumbox.framework.machinelearning.common.dataobjects.KnowledgeBase;
import com.datumbox.framework.machinelearning.common.bases.datatransformation.DataTransformer;
import com.datumbox.framework.machinelearning.common.bases.featureselection.FeatureSelection;
import com.datumbox.framework.machinelearning.common.dataobjects.StandardKnowledgeBase;

/**
* The BaseWrapper is a trainable object that uses composition instead of inheritance
Expand All @@ -35,7 +35,7 @@
* @param <MP>
* @param <TP>
*/
public abstract class BaseWrapper<MP extends BaseWrapper.ModelParameters, TP extends BaseWrapper.TrainingParameters> extends BaseTrainable<MP, TP, KnowledgeBase<MP, TP>> {
public abstract class BaseWrapper<MP extends BaseWrapper.ModelParameters, TP extends BaseWrapper.TrainingParameters> extends BaseTrainable<MP, TP, StandardKnowledgeBase<MP, TP>> {

/**
* The DataTransformer instance of the wrapper.
Expand Down Expand Up @@ -214,7 +214,7 @@ public void setMLmodelTrainingParameters(ML.TrainingParameters mlmodelTrainingPa
* @see com.datumbox.framework.machinelearning.common.bases.baseobjects.BaseTrainable#BaseTrainable(java.lang.String, com.datumbox.common.persistentstorage.interfaces.DatabaseConfiguration, java.lang.Class, java.lang.Class)
*/
protected BaseWrapper(String dbName, DatabaseConfiguration dbConf, Class<MP> mpClass, Class<TP> tpClass) {
super(dbName, dbConf, mpClass, tpClass);
super(dbName, dbConf, StandardKnowledgeBase.class, mpClass, tpClass);
}

/** {@inheritDoc} */
Expand Down

0 comments on commit ce5d2f1

Please sign in to comment.