<NME> AdaBoostMHInspector.java
<BEF> ADDFILE
<MSG> add AdaBoostMHInspector

<DFF> @@ -0,0 +1,118 @@
+package edu.neu.ccs.pyramid.multilabel_classification.adaboostmh;
+
+import edu.neu.ccs.pyramid.classification.PlattScaling;
+import edu.neu.ccs.pyramid.dataset.IdTranslator;
+import edu.neu.ccs.pyramid.dataset.LabelTranslator;
+import edu.neu.ccs.pyramid.dataset.MultiLabel;
+import edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet;
+import edu.neu.ccs.pyramid.feature.Feature;
+import edu.neu.ccs.pyramid.feature.TopFeatures;
+import edu.neu.ccs.pyramid.multilabel_classification.MLPlattScaling;
+import edu.neu.ccs.pyramid.multilabel_classification.MultiLabelPredictionAnalysis;
+import edu.neu.ccs.pyramid.multilabel_classification.imlgb.IMLGradientBoosting;
+import edu.neu.ccs.pyramid.regression.*;
+import edu.neu.ccs.pyramid.regression.regression_tree.RegTreeInspector;
+import edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree;
+import edu.neu.ccs.pyramid.regression.regression_tree.TreeRule;
+import org.apache.mahout.math.Vector;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * Created by chengli on 4/4/15.
+ */
+public class AdaBoostMHInspector {
+
+
+    public static TopFeatures topFeatures(AdaBoostMH boosting, int classIndex, int limit){
+        Map<Feature,Double> totalContributions = new HashMap<>();
+        List<Regressor> regressors = boosting.getRegressors(classIndex);
+        List<RegressionTree> trees = regressors.stream().filter(regressor ->
+                regressor instanceof RegressionTree)
+                .map(regressor -> (RegressionTree) regressor)
+                .collect(Collectors.toList());
+        for (RegressionTree tree: trees){
+            Map<Feature,Double> contributions = RegTreeInspector.featureImportance(tree);
+            for (Map.Entry<Feature,Double> entry: contributions.entrySet()){
+                Feature feature = entry.getKey();
+                Double contribution = entry.getValue();
+                double oldValue = totalContributions.getOrDefault(feature,0.0);
+                double newValue = oldValue+contribution;
+                totalContributions.put(feature,newValue);
+            }
+        }
+        Comparator<Map.Entry<Feature,Double>> comparator = Comparator.comparing(Map.Entry::getValue);
+        List<Feature> list = totalContributions.entrySet().stream().sorted(comparator.reversed()).limit(limit)
+                .map(Map.Entry::getKey).collect(Collectors.toList());
+        TopFeatures topFeatures = new TopFeatures();
+        topFeatures.setTopFeatures(list);
+        topFeatures.setClassIndex(classIndex);
+        LabelTranslator labelTranslator = boosting.getLabelTranslator();
+        topFeatures.setClassName(labelTranslator.toExtLabel(classIndex));
+        return topFeatures;
+    }
+
+    public static ClassScoreCalculation decisionProcess(AdaBoostMH boosting, MLPlattScaling plattScaling,
+                                                        LabelTranslator labelTranslator,
+                                                        Vector vector, int classIndex, int limit){
+        ClassScoreCalculation classScoreCalculation = new ClassScoreCalculation(classIndex,labelTranslator.toExtLabel(classIndex),
+                boosting.predictClassScore(vector,classIndex));
+        double prob = plattScaling.predictClassProb(vector,classIndex);
+        classScoreCalculation.setClassProbability(prob);
+        List<Regressor> regressors = boosting.getRegressors(classIndex);
+        List<TreeRule> treeRules = new ArrayList<>();
+        for (Regressor regressor : regressors) {
+            if (regressor instanceof ConstantRegressor) {
+                Rule rule = new ConstantRule(((ConstantRegressor) regressor).getScore());
+                classScoreCalculation.addRule(rule);
+            }
+
+            if (regressor instanceof RegressionTree) {
+                RegressionTree tree = (RegressionTree) regressor;
+                TreeRule treeRule = new TreeRule(tree, vector);
+                treeRules.add(treeRule);
+            }
+        }
+        Comparator<TreeRule> comparator = Comparator.comparing(decision -> Math.abs(decision.getScore()));
+        List<TreeRule> merged = TreeRule.merge(treeRules).stream().sorted(comparator.reversed())
+                .limit(limit).collect(Collectors.toList());
+        for (TreeRule treeRule : merged){
+            classScoreCalculation.addRule(treeRule);
+        }
+
+        return classScoreCalculation;
+    }
+
+    public static MultiLabelPredictionAnalysis analyzePrediction(AdaBoostMH boosting, MLPlattScaling plattScaling,
+                                                                 MultiLabelClfDataSet dataSet,
+                                                                 int dataPointIndex, List<Integer> classes, int limit){
+        MultiLabelPredictionAnalysis predictionAnalysis = new MultiLabelPredictionAnalysis();
+        LabelTranslator labelTranslator = dataSet.getLabelTranslator();
+        IdTranslator idTranslator = dataSet.getIdTranslator();
+        predictionAnalysis.setInternalId(dataPointIndex);
+        predictionAnalysis.setId(idTranslator.toExtId(dataPointIndex));
+        predictionAnalysis.setInternalLabels(dataSet.getMultiLabels()[dataPointIndex].getMatchedLabelsOrdered());
+        List<String> labels = dataSet.getMultiLabels()[dataPointIndex].getMatchedLabelsOrdered().stream()
+                .map(labelTranslator::toExtLabel).collect(Collectors.toList());
+        predictionAnalysis.setLabels(labels);
+        predictionAnalysis.setProbForTrueLabels(Double.NaN);
+
+        MultiLabel predictedLabels = boosting.predict(dataSet.getRow(dataPointIndex));
+        List<Integer> internalPrediction = predictedLabels.getMatchedLabelsOrdered();
+        predictionAnalysis.setInternalPrediction(internalPrediction);
+        List<String> prediction = internalPrediction.stream().map(labelTranslator::toExtLabel).collect(Collectors.toList());
+        predictionAnalysis.setPrediction(prediction);
+        predictionAnalysis.setProbForPredictedLabels(Double.NaN);
+
+        List<ClassScoreCalculation> classScoreCalculations = new ArrayList<>();
+        for (int k: classes){
+            ClassScoreCalculation classScoreCalculation = decisionProcess(boosting,plattScaling,labelTranslator,
+                    dataSet.getRow(dataPointIndex),k,limit);
+            classScoreCalculations.add(classScoreCalculation);
+        }
+        predictionAnalysis.setClassScoreCalculations(classScoreCalculations);
+        return predictionAnalysis;
+    }
+
+}

add AdaBoostMHInspector