Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added current work from BitBucket repo
- Loading branch information
Showing
1,280 changed files
with
489,537 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#Fri Dec 14 11:26:16 GMT 2018 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
package Bags2DShapelets; | ||
|
||
/** | ||
* | ||
* @author James Large (james.large@uea.ac.uk) | ||
*/ | ||
public class Distance2D { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package Bags2DShapelets; | ||
|
||
import weka.core.Attribute; | ||
import weka.core.DenseInstance; | ||
import weka.core.FastVector; | ||
import weka.core.Instances; | ||
import weka.filters.SimpleBatchFilter; | ||
|
||
/** | ||
* | ||
* @author James Large (james.large@uea.ac.uk) | ||
*/ | ||
public class ST2D extends SimpleBatchFilter { | ||
|
||
ShapeletSearch2D searcher; | ||
Shapelet2D[] shapelets; | ||
|
||
int k = -1; | ||
int numShapeletsToSearch = 1000; | ||
|
||
boolean shapeletsFound = false; | ||
|
||
public ST2D() { | ||
searcher = new ShapeletSearch2D(0); | ||
} | ||
|
||
public ST2D(int seed) { | ||
searcher = new ShapeletSearch2D(seed); | ||
} | ||
|
||
@Override | ||
public String globalInfo() { | ||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | ||
} | ||
|
||
@Override | ||
protected Instances determineOutputFormat(Instances inputFormat) throws Exception { | ||
//copied from shapelettransform | ||
|
||
if (k < 1) { | ||
System.out.println(this.k); | ||
throw new IllegalArgumentException("ShapeletFilter not initialised correctly - please specify a value of k that is greater than or equal to 1"); | ||
} | ||
|
||
|
||
FastVector atts = new FastVector(); | ||
String name; | ||
for (int i = 0; i < k; i++) { | ||
name = "Shapelet_" + i; | ||
atts.addElement(new Attribute(name)); | ||
} | ||
|
||
if (inputFormat.classIndex() >= 0) { | ||
//Classification set, set class | ||
//Get the class values as a fast vector | ||
Attribute target = inputFormat.attribute(inputFormat.classIndex()); | ||
|
||
FastVector vals = new FastVector(target.numValues()); | ||
for (int i = 0; i < target.numValues(); i++) { | ||
vals.addElement(target.value(i)); | ||
} | ||
atts.addElement(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(), vals)); | ||
} | ||
Instances result = new Instances("Shapelets" + inputFormat.relationName(), atts, inputFormat.numInstances()); | ||
if (inputFormat.classIndex() >= 0) { | ||
result.setClassIndex(result.numAttributes() - 1); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public Instances process(Instances imgs) throws Exception { | ||
|
||
//checks if the shapelets haven't been found yet, finds them if it needs too. | ||
if (!shapeletsFound) { | ||
searcher.numShapeletsToSearch = numShapeletsToSearch; | ||
if (k > 0) // has already been set | ||
searcher.k = this.k; | ||
|
||
shapelets = searcher.generateKShapelets(imgs); | ||
if (k < 1) // if wasnt already set before, searcher will have set k to numinstances | ||
k = shapelets.length; | ||
shapeletsFound = true; | ||
} | ||
|
||
//build the transformed dataset with the shapelets we've found either on this data, or the previous training data | ||
return transformDataset(imgs); | ||
|
||
} | ||
|
||
public Instances transformDataset(Instances imgs) throws Exception { | ||
Instances transformed = determineOutputFormat(imgs); | ||
|
||
int numInsts = imgs.numInstances(); | ||
int numAtts = shapelets.length + 1; // + classVal | ||
|
||
//create the (empty) instances | ||
for (int instId = 0; instId < numInsts; instId++) { | ||
transformed.add(new DenseInstance(shapelets.length + 1)); | ||
} | ||
|
||
//populate the distances | ||
for (int instId = 0; instId < numInsts; instId++) { | ||
for (int shapeletID = 0; shapeletID < shapelets.length; shapeletID++) { | ||
double distance = searcher.sDist2D(imgs.get(instId), shapelets[shapeletID]); | ||
transformed.instance(instId).setValue(shapeletID, distance); | ||
} | ||
} | ||
|
||
//do the classValues | ||
for (int instId = 0; instId < numInsts; instId++) | ||
transformed.instance(instId).setValue(numAtts-1, imgs.instance(instId).classValue()); | ||
|
||
return transformed; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
package Bags2DShapelets; | ||
|
||
import timeseriesweka.classifiers.ensembles.elastic_ensemble.ED1NN; | ||
import vector_classifiers.CAWPE; | ||
import weka.classifiers.AbstractClassifier; | ||
import weka.classifiers.Classifier; | ||
import weka.core.Instance; | ||
import weka.core.Instances; | ||
|
||
/** | ||
* | ||
* @author James Large (james.large@uea.ac.uk) | ||
*/ | ||
public class ST2D_Classifier extends AbstractClassifier { | ||
|
||
ST2D transform = null; | ||
Classifier classifier = null; | ||
|
||
int seed = 0; | ||
|
||
Instances trainData = null; | ||
Instances testFormat = null; | ||
|
||
|
||
public ST2D_Classifier() { | ||
transform = new ST2D(); | ||
//classifier = new CAWPE(); | ||
classifier = new ED1NN(); | ||
} | ||
|
||
public void setNumShapeletsToSearch(int numShapeletsToSearch) { | ||
transform.numShapeletsToSearch = numShapeletsToSearch; | ||
} | ||
public int getNumShapeletsToSearch() { | ||
return transform.numShapeletsToSearch; | ||
} | ||
|
||
public void setK(int k) { | ||
transform.k = k; | ||
} | ||
public int getK() { | ||
return transform.k; | ||
} | ||
|
||
@Override | ||
public void buildClassifier(Instances data) throws Exception { | ||
trainData = new Instances(data); | ||
testFormat = new Instances(trainData, 0); | ||
|
||
Instances transformedData = transform.process(trainData); | ||
classifier.buildClassifier(transformedData); | ||
} | ||
|
||
@Override | ||
public double[] distributionForInstance(Instance inst) throws Exception { | ||
testFormat.add(inst); | ||
Instances transformed = transform.process(testFormat); | ||
testFormat.remove(0); | ||
|
||
return classifier.distributionForInstance(transformed.get(0)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
|
||
package Bags2DShapelets; | ||
|
||
import java.util.Objects; | ||
import weka.core.Instance; | ||
import static utilities.multivariate_tools.MultivariateInstanceTools.splitMultivariateInstance; | ||
import weka.core.Instances; | ||
|
||
/** | ||
* | ||
* @author James Large (james.large@uea.ac.uk) | ||
*/ | ||
public class Shapelet2D implements Comparable<Shapelet2D> { | ||
|
||
public double [][] shapelet = null; | ||
ShapeletSummary summary; | ||
public int size = -1; | ||
public boolean normalised = false; | ||
|
||
public double score = -1.0; | ||
|
||
public static class ShapeletSummary { | ||
public Instance originatingImg = null; | ||
public int xStart = -1; | ||
public int yStart = -1; | ||
public int xLen = -1; | ||
public int yLen = -1; | ||
|
||
public ShapeletSummary(Instance originatingImg, int xStart, int yStart, int xLen, int yLen) { | ||
this.originatingImg = originatingImg; | ||
this.xStart = xStart; | ||
this.yStart = yStart; | ||
this.xLen = xLen; | ||
this.yLen = yLen; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
|
||
if (o == null || getClass() != o.getClass()) return false; | ||
ShapeletSummary other = (ShapeletSummary) o; | ||
|
||
if (xStart != other.xStart) return false; | ||
if (yStart != other.yStart) return false; | ||
if (xLen != other.xLen) return false; | ||
if (yLen != other.yLen) return false; | ||
|
||
return !originatingImg.equals(other.originatingImg); | ||
|
||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int hash = 5; | ||
hash = 59 * hash + Objects.hashCode(this.originatingImg); | ||
hash = 59 * hash + this.xStart; | ||
hash = 59 * hash + this.yStart; | ||
hash = 59 * hash + this.xLen; | ||
hash = 59 * hash + this.yLen; | ||
return hash; | ||
} | ||
} | ||
|
||
public Shapelet2D() { | ||
|
||
} | ||
|
||
public Shapelet2D(Instance img, int xStart, int yStart, int xLen, int yLen) { | ||
summary = new ShapeletSummary(img, xStart, yStart, xLen, yLen); | ||
extract(img, xStart, yStart, xLen, yLen); | ||
normalise(); | ||
} | ||
|
||
public Shapelet2D(ShapeletSummary summary) { | ||
extract(summary.originatingImg, summary.xStart, summary.yStart, summary.xLen, summary.yLen); | ||
normalise(); | ||
} | ||
|
||
public void extract(Instance img, int xStart, int yStart, int xLen, int yLen) { | ||
if (yLen < 1) yLen = xLen; //assume square if yStart not specified | ||
|
||
this.shapelet = new double[xLen][yLen]; | ||
|
||
Instance[] instsRows = splitMultivariateInstance(img); | ||
|
||
for (int sx = xStart, tx = 0; sx < xStart+xLen; sx++, tx++) | ||
for (int sy = yStart, ty = 0; sy < yStart+yLen; sy++, ty++) | ||
this.shapelet[tx][ty] = instsRows[sx].value(sy); | ||
|
||
this.size = xLen * yLen; | ||
} | ||
|
||
/** | ||
* @return exact distance | ||
*/ | ||
public double distanceTo(Shapelet2D other) { | ||
double dist = .0; | ||
|
||
for (int x = 0; x < summary.xLen; x++) | ||
for (int y = 0; y < summary.yLen; y++) | ||
dist += (other.shapelet[x][y] - this.shapelet[x][y]) * (other.shapelet[x][y] - this.shapelet[x][y]); | ||
|
||
return dist; | ||
} | ||
|
||
/** | ||
* @param bestDistSoFar test for early abandon | ||
* @return exact distance if less than bestDistSoFar, otherwise _some_ value greater than bestDistSoFar | ||
*/ | ||
public double distanceTo_EarlyAbandon(Shapelet2D other, double bestDistSoFar) { | ||
double dist = .0; | ||
boolean earlyAbandon = false; | ||
|
||
for (int x = 0; x < summary.xLen && !earlyAbandon; x++) { | ||
for (int y = 0; y < summary.yLen; y++) { | ||
dist += (other.shapelet[x][y] - this.shapelet[x][y]) * (other.shapelet[x][y] - this.shapelet[x][y]); | ||
|
||
if (dist > bestDistSoFar) { | ||
earlyAbandon = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dist; | ||
} | ||
|
||
|
||
@Override | ||
public int compareTo(Shapelet2D other) { | ||
int c = Double.compare(this.score, other.score); | ||
if (c == 0) | ||
return (-1) * Integer.compare(this.size, other.size); | ||
else | ||
return c; | ||
} | ||
|
||
public boolean isBetterThan(Shapelet2D other) { | ||
return this.compareTo(other) > 0; | ||
} | ||
|
||
/** | ||
* For now, simply scaling to range 0-1, same as the overall images are themselves | ||
*/ | ||
public void normalise() { | ||
if (normalised) | ||
return; | ||
|
||
double min = Double.MAX_VALUE; | ||
double max = -Double.MAX_VALUE; | ||
|
||
for (int i = 0; i < shapelet.length; i++) { | ||
for (int j = 0; j < shapelet[i].length; j++) { | ||
if (shapelet[i][j] < min) | ||
min = shapelet[i][j]; | ||
if (shapelet[i][j] > max) | ||
max = shapelet[i][j]; | ||
} | ||
} | ||
|
||
for (int i = 0; i < shapelet.length; i++) | ||
for (int j = 0; j < shapelet[i].length; j++) | ||
shapelet[i][j] = (shapelet[i][j] - min) / (max-min); | ||
|
||
normalised = true; | ||
} | ||
} |
Oops, something went wrong.