Commits

Miha Stajdohar committed 0408b79

Select representative models.

Comments (0)

Files changed (2)

examples/modelmix/mixzoo.py

 
 """
 import argparse
+import collections
 import os
 import sys
 
 import Orange
 import Orange.orng.orngVizRank as vr
 import orangecontrib.modelmaps as mm
-
+import orangecontrib.network as network
 
 parser = argparse.ArgumentParser(description='NGS reads demultiplexer.')
 
 
 
 def select_representatives(models):
+    print models[0].type
+    smx = build_map.build_model_matrix(models, mm.distance_euclidean)
+    nc, knn = 2, 1
+    while nc > 1:
+        net = mm.model_network(smx, knn=knn)
+        nc = len(network.nx.algorithms.components.connected_components(net))
+        print "  knn: {}, components: {}".format(knn, nc)
+        knn += 1
+
+    clusters = collections.defaultdict(list)
+    for node, cluster in network.community.label_propagation(net, seed=42).iteritems():
+        clusters[cluster].append(node)
+
+    print "  representatives: {}".format(len(clusters))
+
+    representatives = []
+    for nodes in clusters.values():
+        cmatrix = smx.getitems(nodes)
+        cdsts  = zip([sum(i) for i in cmatrix], nodes)
+        cmedian = min(cdsts)[1]
+        representatives.append(models[cmedian])
+
     return models
 
 
     final_models.extend(representatives)
 
 
-add(lambda f: build_map.build_projection_model(f, vr.LINEAR_PROJECTION), features)
-add(lambda f: build_map.build_projection_model(f, vr.RADVIZ), features)
+# add(lambda f: build_map.build_projection_model(f, vr.LINEAR_PROJECTION), features)
+# add(lambda f: build_map.build_projection_model(f, vr.RADVIZ), features)
 add(lambda f: build_map.build_projection_model(f, vr.POLYVIZ), features)
-add(lambda f: build_map.build_projection_model(f, vr.SCATTERPLOT), features_scatterplot)
+# add(lambda f: build_map.build_projection_model(f, vr.SCATTERPLOT), features_scatterplot)
 
-learner = Orange.classification.bayes.NaiveLearner()
-add(lambda f: build_map.build_model(f, learner), features)
-
-learner = Orange.classification.knn.kNNLearner()
-add(lambda f: build_map.build_model(f, learner), features)
-
-models = build_map.build_rf_models(trees=args.n, max_depth=4, min_instances=5)
-representatives = select_representatives(models)
-final_models.extend(representatives)
+# learner = Orange.classification.bayes.NaiveLearner()
+# add(lambda f: build_map.build_model(f, learner), features)
+#
+# learner = Orange.classification.knn.kNNLearner()
+# add(lambda f: build_map.build_model(f, learner), features)
+#
+# models = build_map.build_rf_models(trees=args.n, max_depth=4, min_instances=5)
+# representatives = select_representatives(models)
+# final_models.extend(representatives)
 
 table = build_map.build_model_data(final_models)
 smx = build_map.build_model_matrix(final_models, mm.distance_euclidean)
 
 mm.save(os.path.join(args.output_dir, "zoo_{}_{}".format(smx.dim, sys.platform)), smx, table, data)
+
+# graph = graph.subgraph(component)
+# smx = smx.getitems(component)

orangecontrib/modelmaps/modelmap.py

 import numpy as np
 
 import Orange.orng.orngVizRank as vr
-import model
 
 from Orange import data, distance, ensemble, feature, misc, projection
 from Orange.classification.knn import kNNLearner
 from Orange.classification.tree import TreeLearner
 
+from orangecontrib import network
+
+import model
+
 
 MODEL_LIST = ["", "SCATTERPLOT", "RADVIZ", "SPCA", "POLYVIZ", "TREE", "NaiveLearner", "kNNLearner", "SVM", "RF"]
 
 
 def get_models_table():
     """Return an empty data table for model meta data."""
-
     attrs = []
     attrs.append(feature.String("uuid"))
     varAttrs = feature.Continuous("number of attributes")
     return data.Table(data.Domain(attrs, 0))
 
 
+def model_network(smx, table=None, knn=1):
+    """Build network from model distance matrix."""
+    graph = network.Graph()
+    graph.add_nodes_from(range(smx.dim))
+    graph.set_items(table)
+    edge_list = network.GraphLayout().edges_from_distance_matrix(smx, -1., -1., knn)
+    graph.add_edges_from(((u, v, {'weight':1 - d}) for u, v, d in edge_list))
+    return graph
+
+
 def load(file_name):
     """Load model map.
 
                 classList = np.compress(validData, classListFull)
                 selectedData = np.compress(validData, np.take(graph.no_jittering_scaled_data, attrIndices, axis=0), axis=1)
                 sum_i = graph._getSum_i(selectedData)
-                table = graph.create_projection_as_example_table(attrIndices, validData=validData, classList=classList, sum_i=sum_i, XAnchors=XAnchors, YAnchors=YAnchors)
+                table = graph.create_projection_as_example_table(attrIndices, valid_data=validData, class_list=classList, sum_i=sum_i, xanchors=XAnchors, yanchors=YAnchors)
 
         if not table: return None
 
         test = self.data().select(indices, 1)
         models = []
 
-        rf_learner = ensemble.forest.RandomForestLearner(trees=trees*2, base_learner=TreeLearner(max_depth=max_depth, min_instances=min_instances), name="RF: %d trees; max depth: None; min instances: %d" % (trees, min_instances))
+        rf_learner = ensemble.forest.RandomForestLearner(trees=trees, base_learner=TreeLearner(max_depth=max_depth, min_instances=min_instances), name="RF: %d trees; max depth: None; min instances: %d" % (trees, min_instances))
         rf_classifier = rf_learner(train)
 
         def get_features(cls, domain):