Index: /trunk/CrossPare/src/de/ugoe/cs/cpdp/training/MetricMatchingTraining.java
===================================================================
--- /trunk/CrossPare/src/de/ugoe/cs/cpdp/training/MetricMatchingTraining.java	(revision 139)
+++ /trunk/CrossPare/src/de/ugoe/cs/cpdp/training/MetricMatchingTraining.java	(revision 140)
@@ -49,9 +49,17 @@
  * This also means we can use any Weka Classifier not just LogisticRegression.
  * 
+ * Config: <setwisetestdataawaretrainer name="MetricMatchingTraining" param="Logistic weka.classifiers.functions.Logistic" threshold="0.05" method="spearman"/>
+ * Instead of spearman metchod it also takes ks, percentile.
+ * Instead of Logistic every other weka classifier can be chosen.
+ * 
+ * Future work:
+ * implement chisquare test in addition to significance for attribute selection
+ * http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/inference/ChiSquareTest.html
+ * use chiSquareTestDataSetsComparison
  */
 public class MetricMatchingTraining extends WekaBaseTraining implements ISetWiseTestdataAwareTrainingStrategy {
 
     private MetricMatch mm = null;
-    private Classifier classifier = new MetricMatchingClassifier();
+    private Classifier classifier = null;
     
     private String method;
@@ -89,5 +97,8 @@
 	@Override
 	public void apply(SetUniqueList<Instances> traindataSet, Instances testdata) {
-
+	    // reset these for each run
+	    this.mm = null;
+	    this.classifier = null;
+	    
 		double score = 0; // matching score to select the best matching training data from the set
 		int num = 0;
@@ -116,5 +127,5 @@
 		}
 		
-		// if we have found a matching instance we use it
+		// if we have found a matching instance we use it, log information about the match for additional eval later
 		Instances ilist = null;
 		if (this.mm != null) {
@@ -131,4 +142,5 @@
 		try {
 			if(this.mm != null) {
+			    this.classifier = new MetricMatchingClassifier();
 			    this.classifier.buildClassifier(ilist);
 			    ((MetricMatchingClassifier) this.classifier).setMetricMatching(this.mm);
@@ -257,28 +269,38 @@
 		    return this.train_values.get(0).length;
 		}
-		
-		/** 
-		 * This creates a new Instance out of the passed Instance and the previously matched attributes.
-		 * We do this because the evaluation phase requires an original Instance with every attribute.
+
+		
+		/**
+		 * The test instance must be of the same dataset as the train data, otherwise WekaEvaluation will die.
+		 * This means we have to force the dataset of this.train (after matching) and only 
+		 * set the values for the attributes we matched but with the index of the traindata attributes we matched.
 		 * 
-		 * @param test instance
-		 * @return new instance
+		 * @param test
+		 * @return
 		 */
 		public Instance getMatchedTestInstance(Instance test) {
-		    //create new instance with our matched number of attributes + 1 (the class attribute)
-			Instances testdata = this.getMatchedTest();
-
-			Instance ni = new DenseInstance(this.attributes.size()+1);
-			ni.setDataset(testdata);
-			 
-	        for(Map.Entry<Integer, Integer> attmatch : this.attributes.entrySet()) {
-	            ni.setValue(testdata.attribute(attmatch.getKey()), test.value(attmatch.getValue()));
-	        }
-	        
-			ni.setClassValue(test.value(test.classAttribute()));
-
-			return ni;
-        }
-
+            Instance ni = new DenseInstance(this.attributes.size()+1);
+            
+            Instances inst = this.getMatchedTrain();
+            
+            ni.setDataset(inst);
+            
+            // assign only the matched attributes to new indexes
+            double val;
+            int k = 0;
+            for(Map.Entry<Integer, Integer> attmatch : this.attributes.entrySet()) {
+                // get value from matched attribute
+                val = test.value(attmatch.getValue());
+                
+                // set it to new index, the order of the attributes is the same
+                ni.setValue(k, val);
+                k++;
+            }
+            ni.setClassValue(test.value(test.classAttribute()));
+
+            return ni;
+		}
+
+		
         /**
          * returns a new instances array with the metric matched training data
@@ -494,5 +516,5 @@
                 // -1 means that it is not in the set of maximal matching
                 if( i != -1 && result[i] != -1) {
-                    //Console.traceln(Level.INFO, "Found maximal bipartite match between: "+ i + " and " + result[i]);
+                    this.p_sum += mwbm.weights[i][result[i]];  // we add the weight of the returned matching for scoring the complete match later
                     this.attributes.put(i, result[i]);
                 }
@@ -544,5 +566,4 @@
                     
                     if( score > cutoff ) {
-                        this.p_sum += score;
                         mwbm.setWeight(i, j, score);
                     }
@@ -587,5 +608,4 @@
 					p = t.correlation(this.train_values.get(i), this.test_values.get(j));
 					if (p > cutoff) {
-					    this.p_sum += p;
                         mwbm.setWeight(i, j, p);
 					}
@@ -667,5 +687,4 @@
 					p = t.approximateP(t.kolmogorovSmirnovStatistic(this.train_values.get(i), this.test_values.get(j)), this.train_values.get(i).length, this.test_values.get(j).length);
 					if (p > cutoff) {
-                        this.p_sum += p;
                         mwbm.setWeight(i, j, p);
 					}
