Fix standard classifier JSON logging

This commit is contained in:
Jamie Mac 2022-02-23 14:11:22 +00:00
parent 80b580d30d
commit 2ed51b9c70
3 changed files with 110 additions and 28 deletions

View File

@ -3,6 +3,7 @@ package clickTrainDetector.classification.standardClassifier;
import clickTrainDetector.classification.CTClassification;
import clickTrainDetector.classification.CTClassifierType;
import clickTrainDetector.classification.ClassifierJSONLogging;
import clickTrainDetector.classification.bearingClassifier.BearingClassifierJSON;
/**
* A classification object for a standard classification
@ -23,6 +24,10 @@ public class StandardClassification implements CTClassification {
private CTClassification[] ctClassifications;
public CTClassification[] getCtClassifications() {
return ctClassifications;
}
/**
* Standard classifier JSON logging.
*/
@ -41,9 +46,15 @@ public class StandardClassification implements CTClassification {
* @param jsonstring
*/
public StandardClassification(String jsonstring) {
standardClassifierJSONLogging = new StandardClassificationJSON();
CTClassification classification = standardClassifierJSONLogging.createClassification(jsonstring);
this.ctClassifications = ((StandardClassification) classification).getCtClassifications();
this.speciesID =classification.getSpeciesID();
}
@Override
public CTClassifierType getClassifierType() {

View File

@ -1,10 +1,22 @@
package clickTrainDetector.classification.standardClassifier;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import clickTrainDetector.classification.CTClassification;
import clickTrainDetector.classification.SimpleClassifierJSONLogging;
import clickTrainDetector.classification.bearingClassifier.BearingClassification;
import clickTrainDetector.classification.bearingClassifier.BearingClassifier;
import clickTrainDetector.classification.idiClassifier.IDIClassification;
import clickTrainDetector.classification.idiClassifier.IDIClassifier;
import clickTrainDetector.classification.simplechi2classifier.Chi2CTClassification;
import clickTrainDetector.classification.simplechi2classifier.Chi2ThresholdClassifier;
import clickTrainDetector.classification.templateClassifier.CTTemplateClassifier;
import clickTrainDetector.classification.templateClassifier.TemplateClassification;
/**
@ -25,6 +37,11 @@ public class StandardClassificationJSON extends SimpleClassifierJSONLogging {
this.standardClassifier=ctClassifications;
}
public StandardClassificationJSON() {
}
@Override
public void writeJSONData(JsonGenerator jg, CTClassification ctClassification) {
@ -35,27 +52,59 @@ public class StandardClassificationJSON extends SimpleClassifierJSONLogging {
@Override
public CTClassification createClassification(JsonNode jTree) {
JsonNode na = jTree.findValue("SPECIES");
int speciesID;
if (na != null ) {
speciesID = na.asInt();
}
else {
System.err.println("Cannot load template classifier");
return null;
}
public CTClassification createClassification(String jsonString) {
try {
ObjectMapper om = new ObjectMapper();
JsonNode jTree = om.readTree(new ByteArrayInputStream(jsonString.getBytes()));
// JsonNode nv = jTree.findValue("NAME");
CTClassification[] ctClassification = new CTClassification[standardClassifier.length];
for (int i=0; i<standardClassifier.length; i++) {
ctClassification[i] = ((SimpleClassifierJSONLogging) standardClassifier[i].getJSONLogging()).createClassification(jTree);
JsonNode na = jTree.findValue("SPECIES");
int speciesID;
if (na != null ) {
speciesID = na.asInt();
}
else {
System.err.println("Cannot load standard classifier");
return null;
}
//System.out.println("Hello load classification: " + standardClassifier.length);
CTClassification[] ctClassification = new CTClassification[StandardClassifier.CLASSIFIER_TYPES.length];
for (int i=0; i<ctClassification.length; i++) {
CTClassification classification = null;
switch (StandardClassifier.CLASSIFIER_TYPES[i]) {
case CHI2THRESHOLD:
classification = new Chi2CTClassification(jsonString);
break;
case IDICLASSIFIER:
classification = new IDIClassification(jsonString);
break;
case TEMPLATECLASSIFIER:
classification = new TemplateClassification(jsonString);
break;
case BEARINGCLASSIFIER:
classification = new BearingClassification(jsonString);
break;
default:
System.err.println("StandardClassification: classifier JSON not found");
break;
}
ctClassification[i] = classification;
}
StandardClassification stClassification =
new StandardClassification(ctClassification, speciesID);
return stClassification;
} catch (IOException e) {
System.err.println("Classification interpreting " + jsonString);
e.printStackTrace();
return null;
}
StandardClassification stClassification =
new StandardClassification(ctClassification, speciesID);
return stClassification;
}

View File

@ -7,6 +7,7 @@ import clickTrainDetector.ClickTrainControl;
import clickTrainDetector.classification.CTClassification;
import clickTrainDetector.classification.CTClassifier;
import clickTrainDetector.classification.CTClassifierParams;
import clickTrainDetector.classification.CTClassifierType;
import clickTrainDetector.classification.bearingClassifier.BearingClassifier;
import clickTrainDetector.classification.bearingClassifier.BearingClassifierParams;
import clickTrainDetector.classification.idiClassifier.IDIClassification;
@ -50,6 +51,13 @@ public class StandardClassifier implements CTClassifier {
* Click train control.
*/
private ClickTrainControl clickTrainControl;
/**
* The classifier types used in the standard classifier.
* ****New types MUST BE ADDED HERE****
*/
public static CTClassifierType[] CLASSIFIER_TYPES = {CTClassifierType.CHI2THRESHOLD, CTClassifierType.IDICLASSIFIER, CTClassifierType.TEMPLATECLASSIFIER, CTClassifierType.BEARINGCLASSIFIER};
public StandardClassifier(ClickTrainControl clickTrainControl, int speciesID) {
this.clickTrainControl = clickTrainControl;
@ -65,18 +73,32 @@ public class StandardClassifier implements CTClassifier {
*/
private void createClassifiers() {
classifiers = new ArrayList<CTClassifier>();
classifiers.add(new Chi2ThresholdClassifier(clickTrainControl, SUB_CLASSIFIER_SPECIESID));
classifiers.add(new IDIClassifier(clickTrainControl,SUB_CLASSIFIER_SPECIESID));
classifiers.add(new CTTemplateClassifier(clickTrainControl, SUB_CLASSIFIER_SPECIESID));
classifiers.add(new BearingClassifier(clickTrainControl, SUB_CLASSIFIER_SPECIESID));
//do this so that is CLASSIFIER_TYPES changes order things still work.
for (int i=0; i<CLASSIFIER_TYPES.length; i++) {
switch (CLASSIFIER_TYPES[i]) {
default:
break;
case CHI2THRESHOLD:
classifiers.add(new Chi2ThresholdClassifier(clickTrainControl, SUB_CLASSIFIER_SPECIESID));
break;
case IDICLASSIFIER:
classifiers.add(new IDIClassifier(clickTrainControl,SUB_CLASSIFIER_SPECIESID));
break;
case TEMPLATECLASSIFIER:
classifiers.add(new CTTemplateClassifier(clickTrainControl, SUB_CLASSIFIER_SPECIESID));
break;
case BEARINGCLASSIFIER:
classifiers.add(new BearingClassifier(clickTrainControl, SUB_CLASSIFIER_SPECIESID));
break;
}
}
setClassifierParams();
}
/**
* Set the parameters for the individual classifiers
*/