Added `seems-to-work' elastic montaging. Missing parts are
[trakem2.git] / lineage / LineageClassifier.java
blob80dfe3a8332901fc158fd887adcf16a8650de3f1
1 package lineage;
3 import java.io.ObjectInputStream;
4 import java.util.ArrayList;
5 import java.util.Hashtable;
7 import weka.classifiers.Classifier;
8 import weka.core.Attribute;
9 import weka.core.DenseInstance;
10 import weka.core.Instance;
11 import weka.core.Instances;
13 /**
14 * Lineage classifier class
16 * 2009 Ignacio Arganda-Carreras and Sergio Jimenez-Celorrio
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 public class LineageClassifier
36 // Deserialize model
37 static private Classifier getClassifier() {
38 ObjectInputStream ois = null;
39 try {
40 ois = new ObjectInputStream(LineageClassifier.class.getResourceAsStream("random_forest_top8_w1.1.model"));
41 return (Classifier) ois.readObject();
42 } catch (Exception e) {
43 e.printStackTrace();
44 } finally {
45 if (null != ois) try { ois.close(); } catch (Exception e) { e.printStackTrace(); }
47 return null;
50 // Hashtable's methods are all synchronized
51 static private final Hashtable<Thread,Operator> table = new Hashtable<Thread,Operator>();
53 final static protected String[] attrs = new String[]{"APD", "CPD", "STD", "MPD", "PM", "LEV", "SIM", "PRX", "PRM", "LR", "TR", "CLASS"};
55 static private final class Operator {
56 final Classifier c = getClassifier();
57 final Instances data;
58 Operator() {
59 ArrayList<Attribute> a = new ArrayList<Attribute>();
60 for (int i=0; i<attrs.length-1; i++) {
61 a.add(new Attribute(attrs[i])); // numeric
63 ArrayList<String> d = new ArrayList<String>();
64 d.add("false");
65 d.add("true");
66 a.add(new Attribute(attrs[attrs.length-1], d)); // nominal attribute
67 data = new Instances("Buh", a, 0);
68 data.setClassIndex(attrs.length-1); // the CLASS
72 public static final boolean classify(final double[] vector) throws Exception {
74 // Obtain or generate a Thread-local instance
76 Operator op;
77 synchronized (table) { // avoid clashes within weka
78 final Thread t = Thread.currentThread();
79 op = table.get(t);
80 if (null == op) {
81 op = new Operator();
82 table.put(t, op);
86 // Future weka versions will use new DenseInstance(1, vector) instead
87 final Instance ins = new DenseInstance(1, vector);
88 ins.setDataset(op.data);
89 // Was trained to return true or false, represented in weka as 0 or 1
90 return 1 == ((int) Math.round(op.c.classifyInstance(ins)));
93 /** Removes all threads and Instances from the cache tables. */
94 static public final void flush () {
95 table.clear();