Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove java.io.Serializable #389

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* A factory for generating events.
*/
public final class AnomalyFactory implements OutputFactory<Event> {
private static final long serialVersionUID = 1L;

/**
* The unknown event. Used at inference time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* The base class for tracking anomalous events.
*/
public abstract class AnomalyInfo implements OutputInfo<Event> {
private static final long serialVersionUID = 1L;

private static final Set<Event> DOMAIN = makeDomain();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* do support training from anomalous and expected data.
*/
public final class Event implements Output<Event> {
private static final long serialVersionUID = 1L;

/**
* The default score of events.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
*/
@ProtoSerializableClass(serializedDataClass=AnomalyInfoProto.class, version=0)
public final class ImmutableAnomalyInfo extends AnomalyInfo implements ImmutableOutputInfo<Event> {
private static final long serialVersionUID = 1L;

private static final Logger logger = Logger.getLogger(ImmutableAnomalyInfo.class.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/
@ProtoSerializableClass(serializedDataClass=AnomalyInfoProto.class, version=0)
public final class MutableAnomalyInfo extends AnomalyInfo implements MutableOutputInfo<Event> {
private static final long serialVersionUID = 1L;

MutableAnomalyInfo() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
* </pre>
*/
public class LibLinearAnomalyModel extends LibLinearModel<Event> {
private static final long serialVersionUID = 3L;

private static final Logger logger = Logger.getLogger(LibLinearAnomalyModel.class.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@
import org.tribuo.common.liblinear.LibLinearType;
import de.bwaldvogel.liblinear.SolverType;

import java.io.Serializable;

/**
* The carrier type for liblinear anomaly detection modes.
* <p>
* Supports: ONECLASS_SVM
*/
public final class LinearAnomalyType implements LibLinearType<Event> {
private static final long serialVersionUID = 1L;

/**
* The different model types available for classification.
*/
public enum LinearType implements Serializable {
public enum LinearType {
/**
* Linear one-class SVM
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void gaussianDataTest() {
logger.finer(output);

// Test serialization
Helpers.testModelSerialization(model,Event.class);
Helpers.testModelProtoSerialization(model,Event.class,testData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
* </pre>
*/
public class LibSVMAnomalyModel extends LibSVMModel<Event> {
private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
* Supports ONE_CLASS. Yes it's a single value enum.
*/
public class SVMAnomalyType implements SVMType<Event> {
private static final long serialVersionUID = 1L;

/**
* Valid SVM modes for anomaly detection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ public void gaussianDataTest() {
logger.finer(confusion);
logger.finer(output);

// Test serialization
Helpers.testModelSerialization(model,Event.class);

// Test protobuf serialization
Helpers.testModelProtoSerialization(model, Event.class, pair.getB());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
public class ImmutableLabelInfo extends LabelInfo implements ImmutableOutputInfo<Label> {
private static final Logger logger = Logger.getLogger(ImmutableLabelInfo.class.getName());

private static final long serialVersionUID = 1L;

private final Map<Integer,String> idLabelMap;

private final Map<String,Integer> labelIDMap;
Expand Down Expand Up @@ -303,10 +301,4 @@ public Pair<Integer, Label> next() {
return new Pair<>(e.getKey(),new Label(e.getValue()));
}
}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();

domain = Collections.unmodifiableSet(new HashSet<>(labels.values()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
*/
@ProtoSerializableClass(serializedDataClass=LabelProto.class, version=0)
public final class Label implements Classifiable<Label> {
private static final long serialVersionUID = 1L;

/**
* The name of the unknown label (i.e., an unlabelled output).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* Label factories have no state, and are all equal to each other.
*/
public final class LabelFactory implements OutputFactory<Label> {
private static final long serialVersionUID = 1L;

/**
* The singleton unknown label, used for unlablled examples.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* The base class for information about multi-class classification Labels.
*/
public abstract class LabelInfo implements OutputInfo<Label> {
private static final long serialVersionUID = 1L;

/**
* The occurrence counts of each label.
Expand Down Expand Up @@ -176,11 +175,4 @@ public MutableOutputInfo<Label> generateMutableOutputInfo() {
@Override
public abstract LabelInfo copy();

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
labels = new HashMap<>();
for (Map.Entry<String,MutableLong> e : labelCounts.entrySet()) {
labels.put(e.getKey(),new Label(e.getKey()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/
@ProtoSerializableClass(serializedDataClass=MutableLabelInfoProto.class, version=0)
public class MutableLabelInfo extends LabelInfo implements MutableOutputInfo<Label> {
private static final long serialVersionUID = 1L;

MutableLabelInfo() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
* A model which performs dummy classifications (e.g., constant output, uniform sampled labels, stratified sampled labels).
*/
public class DummyClassifierModel extends Model<Label> {
private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* which uses the most likely prediction for each ensemble member.
*/
public final class FullyWeightedVotingCombiner implements EnsembleCombiner<Label> {
private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* which uses the full distribution of predictions for each ensemble member.
*/
public final class VotingCombiner implements EnsembleCombiner<Label> {
private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.tribuo.sequence.SequenceExample;
import org.tribuo.sequence.SequenceModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -37,7 +36,6 @@
* in a SequenceExample.
*/
public abstract class ConfidencePredictingSequenceModel extends SequenceModel<Label> {
private static final long serialVersionUID = 1L;

/**
* Constructs a ConfidencePredictingSequenceModel with the supplied parameters.
Expand Down Expand Up @@ -86,8 +84,7 @@ private static <SUB extends Subsequence> Double multiplyWeights(List<Prediction<
/**
* A range class used to define a subsequence of a SequenceExample.
*/
public static class Subsequence implements Serializable {
private static final long serialVersionUID = 1L;
public static class Subsequence {
/**
* The subsequence start index.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,11 +28,7 @@
import org.tribuo.sequence.SequenceTrainer;
import org.tribuo.util.Util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;

Expand Down Expand Up @@ -77,16 +73,6 @@ public String getOptionsDescription() {
*/
@Option(charName = 't', longName = "trainer-name", usage = "Name of the trainer in the configuration file.")
public SequenceTrainer<Label> trainer;
/**
* Load in the data in protobuf format.
*/
@Option(charName = 'p', longName = "protobuf-format-dataset", usage = "Load the model from a protobuf. Optional")
public boolean protobufFormat;
/**
* Write the model out in protobuf format.
*/
@Option(longName = "write-protobuf-model", usage = "Write the model out in protobuf format.")
public boolean writeProtobuf;
}

/**
Expand Down Expand Up @@ -120,32 +106,15 @@ public static void main(String[] args) throws ClassNotFoundException, IOExceptio
break;
default:
if ((o.trainDataset != null) && (o.testDataset != null)) {
if (o.protobufFormat) {
logger.info("Loading protobuf format training data from " + o.trainDataset);
SequenceDataset<?> tmpTrain = SequenceDataset.deserializeFromFile(o.trainDataset);
train = SequenceDataset.castDataset(tmpTrain, Label.class);
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features");
logger.info("Loading protobuf format testing data from " + o.testDataset);
SequenceDataset<?> tmpTest = SequenceDataset.deserializeFromFile(o.testDataset);
test = SequenceDataset.castDataset(tmpTest, Label.class);
logger.info(String.format("Loaded %d testing examples", test.size()));
} else {
logger.info("Loading training data from " + o.trainDataset);
try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(Files.newInputStream(o.trainDataset)));
ObjectInputStream oits = new ObjectInputStream(new BufferedInputStream(Files.newInputStream(o.testDataset)))) {
@SuppressWarnings("unchecked") // deserialising a generic dataset.
SequenceDataset<Label> tmpTrain = (SequenceDataset<Label>) ois.readObject();
train = tmpTrain;
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features");
logger.info("Loading testing data from " + o.testDataset);
@SuppressWarnings("unchecked") // deserialising a generic dataset.
SequenceDataset<Label> tmpTest = (SequenceDataset<Label>) oits.readObject();
test = tmpTest;
logger.info(String.format("Loaded %d testing examples", test.size()));
}
}
logger.info("Loading training data from " + o.trainDataset);
SequenceDataset<?> tmpTrain = SequenceDataset.deserializeFromFile(o.trainDataset);
train = SequenceDataset.castDataset(tmpTrain, Label.class);
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features");
logger.info("Loading testing data from " + o.testDataset);
SequenceDataset<?> tmpTest = SequenceDataset.deserializeFromFile(o.testDataset);
test = SequenceDataset.castDataset(tmpTest, Label.class);
logger.info(String.format("Loaded %d testing examples", test.size()));
} else {
logger.warning("Unknown dataset " + o.datasetName);
logger.info(cm.usage());
Expand All @@ -169,13 +138,7 @@ public static void main(String[] args) throws ClassNotFoundException, IOExceptio
System.out.println(evaluation.getConfusionMatrix().toString());

if (o.outputPath != null) {
if (o.writeProtobuf) {
model.serializeToFile(o.outputPath);
} else {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(o.outputPath))) {
oos.writeObject(model);
}
}
model.serializeToFile(o.outputPath);
logger.info("Serialized model to file: " + o.outputPath);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
@ProtoSerializableClass(serializedDataClass = DefaultFeatureExtractorProto.class, version = DefaultFeatureExtractor.CURRENT_VERSION)
public class DefaultFeatureExtractor implements LabelFeatureExtractor {

private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
import org.tribuo.protos.ProtoSerializable;
import org.tribuo.protos.ProtoUtil;

import java.io.Serializable;
import java.util.List;

/**
* A class for featurising labels from previous steps in Viterbi.
*/
public interface LabelFeatureExtractor extends Configurable, ProtoSerializable<LabelFeatureExtractorProto>, Provenancable<ConfiguredObjectProvenance>, Serializable {
public interface LabelFeatureExtractor extends Configurable, ProtoSerializable<LabelFeatureExtractorProto>, Provenancable<ConfiguredObjectProvenance> {

/**
* Generates features based on the previously produced labels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
@ProtoSerializableClass(version = NoopFeatureExtractor.CURRENT_VERSION)
public class NoopFeatureExtractor implements LabelFeatureExtractor {

private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
*/
public class ViterbiModel extends SequenceModel<Label> {

private static final long serialVersionUID = 1L;

/**
* Protobuf serialization version.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static void testDummyClassifier(Pair<Dataset<Label>, Dataset<Label>> p, b
Evaluator<Label, LabelEvaluation> evaluator = new LabelEvaluator();
LabelEvaluation evaluation = evaluator.evaluate(m,p.getB());
if (testModelSave) {
Helpers.testModelSerialization(m,Label.class);
Helpers.testModelProtoSerialization(m, Label.class, p.getB());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void votingCombinerTest() {
assertEquals(prediction.getOutput(),dishwasher);
modelList.clear();

Helpers.testModelSerialization(ensemble,Label.class);
Helpers.testModelProtoSerialization(ensemble,Label.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
* the current impurity and a bunch of other statistics.
*/
public class ClassifierTrainingNode extends AbstractTrainingNode<Label> {
private static final long serialVersionUID = 1L;

private static final Logger logger = Logger.getLogger(ClassifierTrainingNode.class.getName());

Expand Down Expand Up @@ -432,8 +431,4 @@ private static ArrayList<TreeFeature> invertData(Dataset<Label> examples) {
return data;
}

private void writeObject(java.io.ObjectOutputStream stream)
throws IOException {
throw new NotSerializableException("ClassifierTrainingNode is a runtime class only, and should not be serialized.");
}
}
Loading
Loading