de.jstacs.classifiers.differentiableSequenceScoreBased
Class ScoreClassifier

java.lang.Object
  extended by de.jstacs.classifiers.AbstractClassifier
      extended by de.jstacs.classifiers.AbstractScoreBasedClassifier
          extended by de.jstacs.classifiers.differentiableSequenceScoreBased.ScoreClassifier
All Implemented Interfaces:
Storable, Cloneable
Direct Known Subclasses:
GenDisMixClassifier

public abstract class ScoreClassifier
extends AbstractScoreBasedClassifier

This abstract class implements the main functionality of a DifferentiableSequenceScore based classifier.

Author:
Jens Keilwagen, Jan Grau

Nested Class Summary
 
Nested classes/interfaces inherited from class de.jstacs.classifiers.AbstractScoreBasedClassifier
AbstractScoreBasedClassifier.DoubleTableResult
 
Field Summary
protected  boolean hasBeenOptimized
          This boolean indicates whether the classifier has been optimized with the method AbstractClassifier.train(DataSet[]) or the weighted version.
static double NOT_TRAINED_VALUE
          This value should be used in getLastScore() if the classifier is not trained.
protected  ScoreClassifierParameterSet params
          The parameter set for the classifier.
protected  DifferentiableSequenceScore[] score
          The internally used scoring functions.
protected  SafeOutputStream sostream
          This stream is used for comments, e.g. during the training, ... .
protected  History template
          The default history
 
Constructor Summary
ScoreClassifier(ScoreClassifierParameterSet params, double lastScore, DifferentiableSequenceScore... score)
          Creates a new ScoreClassifier from a given ScoreClassifierParameterSet and DifferentiableSequenceScores .
ScoreClassifier(StringBuffer xml)
          The standard constructor for the interface Storable.
 
Method Summary
 ScoreClassifier clone()
           
protected  void createStructure(DataSet[] data, double[][] weights)
          Creates the structure that will be used in the optimization.
protected  void createStructure(DataSet[] data, double[][] weights, boolean initRandomly)
          Creates the structure that will be used in the optimization.
protected  double doOptimization(DataSet[] reduced, double[][] newWeights)
          This method does the optimization of the train-method
protected  void extractFurtherClassifierInfosFromXML(StringBuffer xml)
          Extracts further information of a classifier from an XML representation.
 CategoricalResult[] getClassifierAnnotation()
          Returns an array of Results of dimension AbstractClassifier.getNumberOfClasses() that contains information about the classifier and for each class.
 ScoreClassifierParameterSet getCurrentParameterSet()
          This method returns the current ParameterSet of the classifier.
 DifferentiableSequenceScore getDifferentiableSequenceScore(int i)
          Returns the internally used DifferentiableSequenceScore with index i.
 DifferentiableSequenceScore[] getDifferentiableSequenceScores()
          Returns all internally used DifferentiableSequenceScores in the internal order.
protected abstract  DiffSSBasedOptimizableFunction getFunction(DataSet[] data, double[][] weights)
          Returns the function that should be optimized.
protected  StringBuffer getFurtherClassifierInfos()
          This method returns further information of a classifier as a StringBuffer.
 String getInstanceName()
          Returns a short description of the classifier.
 double getLastScore()
          Returns the score that was computed in the last optimization of the parameters.
 NumericalResultSet getNumericalCharacteristics()
          Returns the subset of numerical values that are also returned by AbstractClassifier.getCharacteristics().
protected  double getScore(Sequence seq, int i, boolean check)
          This method returns the score for a given Sequence and a given class.
protected abstract  String getXMLTag()
          Returns the String that is used as tag for the XML representation of the classifier.
 boolean hasBeenOptimized()
          This method indicates if the classifier has been optimized by a train-method.
 void initUsingParameters(double[] parameters)
          Sets the parameters of this classifier and the contained scoring functions to the supplied parameters.
 boolean isInitialized()
          This method gives information about the state of the classifier.
protected  OptimizableFunction.KindOfParameter preoptimize(OptimizableFunction f)
          This method allows to pre-optimize the parameter before the real optimization.
 void setOutputStream(OutputStream o)
          Sets the OutputStream that is used e.g. for writing information during training.
 void train(DataSet[] data, double[][] weights)
          This method trains a classifier over an array of weighted DataSet s.
 
Methods inherited from class de.jstacs.classifiers.AbstractScoreBasedClassifier
check, check, classify, classify, createDefaultClassWeights, getClassWeight, getClassWeights, getMultiClassScores, getNumberOfClasses, getPValue, getPValue, getResults, getScore, getScores, setClassWeights, setClassWeights, setThresholdClassWeights
 
Methods inherited from class de.jstacs.classifiers.AbstractClassifier
classify, evaluate, getAlphabetContainer, getCharacteristics, getLength, toXML, train
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

score

protected DifferentiableSequenceScore[] score
The internally used scoring functions.


params

protected ScoreClassifierParameterSet params
The parameter set for the classifier.


hasBeenOptimized

protected boolean hasBeenOptimized
This boolean indicates whether the classifier has been optimized with the method AbstractClassifier.train(DataSet[]) or the weighted version.


sostream

protected SafeOutputStream sostream
This stream is used for comments, e.g. during the training, ... .


NOT_TRAINED_VALUE

public static final double NOT_TRAINED_VALUE
This value should be used in getLastScore() if the classifier is not trained.

See Also:
Constant Field Values

template

protected History template
The default history

Constructor Detail

ScoreClassifier

public ScoreClassifier(ScoreClassifierParameterSet params,
                       double lastScore,
                       DifferentiableSequenceScore... score)
                throws CloneNotSupportedException
Creates a new ScoreClassifier from a given ScoreClassifierParameterSet and DifferentiableSequenceScores .

Parameters:
params - the parameter set for the classifier
lastScore - the score of the last optimization, if no such value exists the programmer should use NOT_TRAINED_VALUE
score - the DifferentiableSequenceScores for the classes
Throws:
CloneNotSupportedException - if at least one DifferentiableSequenceScore could not be cloned
See Also:
AbstractScoreBasedClassifier.AbstractScoreBasedClassifier(AlphabetContainer, int, int)

ScoreClassifier

public ScoreClassifier(StringBuffer xml)
                throws NonParsableException
The standard constructor for the interface Storable. Creates a new ScoreClassifier out of its XML representation.

Parameters:
xml - the XML representation as StringBuffer
Throws:
NonParsableException - if the ScoreClassifier could not be reconstructed out of the XML representation (the StringBuffer could not be parsed)
See Also:
AbstractScoreBasedClassifier.AbstractScoreBasedClassifier(StringBuffer), Storable
Method Detail

clone

public ScoreClassifier clone()
                      throws CloneNotSupportedException
Overrides:
clone in class AbstractScoreBasedClassifier
Throws:
CloneNotSupportedException

getInstanceName

public String getInstanceName()
Description copied from class: AbstractClassifier
Returns a short description of the classifier.

Specified by:
getInstanceName in class AbstractClassifier
Returns:
a short description of the classifier

getClassifierAnnotation

public CategoricalResult[] getClassifierAnnotation()
Description copied from class: AbstractClassifier
Returns an array of Results of dimension AbstractClassifier.getNumberOfClasses() that contains information about the classifier and for each class.

res[0] = new CategoricalResult( "classifier", "the kind of classifier", getInstanceName() );
res[1] = new CategoricalResult( "class info 0", "some information about the class", "info0" );
res[2] = new CategoricalResult( "class info 1", "some information about the class", "info1" );
...

Specified by:
getClassifierAnnotation in class AbstractClassifier
Returns:
an array of Results that contains information about the classifier

getNumericalCharacteristics

public NumericalResultSet getNumericalCharacteristics()
                                               throws Exception
Description copied from class: AbstractClassifier
Returns the subset of numerical values that are also returned by AbstractClassifier.getCharacteristics().

Specified by:
getNumericalCharacteristics in class AbstractClassifier
Returns:
the numerical characteristics
Throws:
Exception - if some of the characteristics could not be defined

isInitialized

public boolean isInitialized()
Description copied from class: AbstractClassifier
This method gives information about the state of the classifier.

Specified by:
isInitialized in class AbstractClassifier
Returns:
true if the classifier is initialized and therefore able to classify sequences, otherwise false

hasBeenOptimized

public boolean hasBeenOptimized()
This method indicates if the classifier has been optimized by a train-method.

Returns:
true if the classifier has been optimized by a train-method, false otherwise

setOutputStream

public void setOutputStream(OutputStream o)
Sets the OutputStream that is used e.g. for writing information during training. It is possible to set o=null, then nothing will be written.

Parameters:
o - the OutputStream

train

public void train(DataSet[] data,
                  double[][] weights)
           throws Exception
Description copied from class: AbstractClassifier
This method trains a classifier over an array of weighted DataSet s. That is why the following has to be fulfilled: This method should work non-incrementally as the method AbstractClassifier.train(DataSet...).

This method should check that the DataSets are defined over the underlying alphabet and length.

Specified by:
train in class AbstractClassifier
Parameters:
data - an array of DataSets
weights - the weights for the DataSets
Throws:
Exception - if the weights are incorrect or the training did not succeed
See Also:
AbstractClassifier.train(DataSet...)

doOptimization

protected double doOptimization(DataSet[] reduced,
                                double[][] newWeights)
                         throws Exception
This method does the optimization of the train-method

Parameters:
reduced - the samples
newWeights - the weights
Returns:
the value of the optimization
Throws:
Exception - if something went wrong during the optimization

preoptimize

protected OptimizableFunction.KindOfParameter preoptimize(OptimizableFunction f)
                                                   throws Exception
This method allows to pre-optimize the parameter before the real optimization. It might be used useful, for instance, to find initial parameters on a part of the complete data.

Parameters:
f - the function to be optimized
Returns:
enumeration value that indicates how to retrieve the parameters from the function
Throws:
Exception - if the pre-optimization fails

createStructure

protected void createStructure(DataSet[] data,
                               double[][] weights,
                               boolean initRandomly)
                        throws Exception
Creates the structure that will be used in the optimization.

Parameters:
data - the data
weights - the weights of the data
initRandomly - initialize the functions randomly
Throws:
Exception - if something went wrong

initUsingParameters

public void initUsingParameters(double[] parameters)
                         throws Exception
Sets the parameters of this classifier and the contained scoring functions to the supplied parameters.

Parameters:
parameters - the new parameters
Throws:
Exception - if the parameters could not be set

createStructure

protected void createStructure(DataSet[] data,
                               double[][] weights)
                        throws Exception
Creates the structure that will be used in the optimization.

Parameters:
data - the data
weights - the weights of the data *
Throws:
Exception - if something went wrong

extractFurtherClassifierInfosFromXML

protected void extractFurtherClassifierInfosFromXML(StringBuffer xml)
                                             throws NonParsableException
Description copied from class: AbstractClassifier
Extracts further information of a classifier from an XML representation. This method is used by the method AbstractClassifier.fromXML(StringBuffer) and should not be made public.

Overrides:
extractFurtherClassifierInfosFromXML in class AbstractScoreBasedClassifier
Parameters:
xml - the XML representation as StringBuffer
Throws:
NonParsableException - if the information could not be parsed out of the XML representation (the StringBuffer could not be parsed)
See Also:
AbstractClassifier.fromXML(StringBuffer)

getFunction

protected abstract DiffSSBasedOptimizableFunction getFunction(DataSet[] data,
                                                              double[][] weights)
                                                       throws Exception
Returns the function that should be optimized.

Parameters:
data - the samples
weights - the weights of the sequences of the samples
Returns:
the function that should be optimized
Throws:
Exception - if something went wrong

getFurtherClassifierInfos

protected StringBuffer getFurtherClassifierInfos()
Description copied from class: AbstractClassifier
This method returns further information of a classifier as a StringBuffer. This method is used by the method AbstractClassifier.toXML() and should not be made public.

Overrides:
getFurtherClassifierInfos in class AbstractScoreBasedClassifier
Returns:
further information of a classifier as a StringBuffer
See Also:
AbstractClassifier.toXML()

getScore

protected double getScore(Sequence seq,
                          int i,
                          boolean check)
                   throws IllegalArgumentException,
                          NotTrainedException,
                          Exception
Description copied from class: AbstractScoreBasedClassifier
This method returns the score for a given Sequence and a given class.

Specified by:
getScore in class AbstractScoreBasedClassifier
Parameters:
seq - the Sequence
i - the index of the class
check - the switch to decide whether to check AlphabetContainer and the length of the Sequence or not
Returns:
the score for a given Sequence and a given class
Throws:
IllegalArgumentException - if something is wrong with the Sequence seq
NotTrainedException - if the classifier is not trained
Exception - if something went wrong

getLastScore

public double getLastScore()
Returns the score that was computed in the last optimization of the parameters.

Returns:
score from the last parameter optimization

getDifferentiableSequenceScore

public DifferentiableSequenceScore getDifferentiableSequenceScore(int i)
                                                           throws CloneNotSupportedException
Returns the internally used DifferentiableSequenceScore with index i.

Parameters:
i - the internal index of the DifferentiableSequenceScore
Returns:
the internally used DifferentiableSequenceScore with index i
Throws:
CloneNotSupportedException - if the DifferentiableSequenceScore could not be cloned

getDifferentiableSequenceScores

public DifferentiableSequenceScore[] getDifferentiableSequenceScores()
                                                              throws CloneNotSupportedException
Returns all internally used DifferentiableSequenceScores in the internal order.

Returns:
the internally used DifferentiableSequenceScores in the internal order
Throws:
CloneNotSupportedException - if a DifferentiableSequenceScore could not be cloned

getXMLTag

protected abstract String getXMLTag()
Description copied from class: AbstractClassifier
Returns the String that is used as tag for the XML representation of the classifier. This method is used by the methods AbstractClassifier.fromXML(StringBuffer) and AbstractClassifier.toXML().

Specified by:
getXMLTag in class AbstractClassifier
Returns:
the String that is used as tag for the XML representation of the classifier

getCurrentParameterSet

public ScoreClassifierParameterSet getCurrentParameterSet()
                                                   throws CloneNotSupportedException
This method returns the current ParameterSet of the classifier.

Returns:
the current ParameterSet of the classifier.
Throws:
CloneNotSupportedException - if the ParameterSet could not be cloned