Commit 5580d427 authored by Anze Vavpetic's avatar Anze Vavpetic

selected/unselected example tables and centroids are now output by the clustering widget

parent 351c79d5
......@@ -6,14 +6,10 @@ def cforange_filter_integers(request,input_dict,output_dict,widget):
def cforange_hierarchical_clustering(request,input_dict,output_dict,widget):
import orange
from library import Clustering
matrix = input_dict['dm']
linkage = int(input_dict['linkage'])
linkages = [("Single linkage", orange.HierarchicalClustering.Single),
("Average linkage", orange.HierarchicalClustering.Average),
("Ward's linkage", orange.HierarchicalClustering.Ward),
("Complete linkage", orange.HierarchicalClustering.Complete),
]
root = orange.HierarchicalClustering(matrix, linkage=linkages[linkage][1])
root = Clustering.hierarchical_clustering(linkage, matrix)
attributes = [x.name for x in matrix.items.domain]
def build_hierarchy(node, root=False):
values_dict = dict([(x,matrix.items[node.first][x].value) for x in attributes]) if not node.branches else {}
......@@ -29,4 +25,4 @@ def cforange_hierarchical_clustering(request,input_dict,output_dict,widget):
'leaf' : True if not node.branches else False
}
hierarchy = json.dumps(build_hierarchy(root, root=True))
return render(request, 'interactions/cforange_hierarchical_clustering.html', {'widget' : widget, 'hierarchy' : hierarchy, 'attributes':attributes})
\ No newline at end of file
return render(request, 'interactions/cforange_hierarchical_clustering.html', {'widget' : widget, 'hierarchy' : hierarchy, 'attributes' : attributes})
\ No newline at end of file
......@@ -385,5 +385,52 @@ def cforange_attribute_distance(input_dict):
def cforange_hierarchical_clustering(input_dict):
return {'centroids' : None, 'selected_examples' : None, 'unselected_examples' : None}
def cforange_hierarchical_clustering_finished(postdata,input_dict,output_dict):
return {'centroids' : None, 'selected_examples' : None, 'unselected_examples' : None}
class Clustering:
@staticmethod
def hierarchical_clustering(linkage, distance_matrix):
import orange
linkages = [("Single linkage", orange.HierarchicalClustering.Single),
("Average linkage", orange.HierarchicalClustering.Average),
("Ward's linkage", orange.HierarchicalClustering.Ward),
("Complete linkage", orange.HierarchicalClustering.Complete)]
return orange.HierarchicalClustering(distance_matrix, linkage=linkages[linkage][1])
def cforange_hierarchical_clustering_finished(postdata, input_dict, output_dict):
import json
import orange
matrix = input_dict['dm']
linkage = int(input_dict['linkage'])
widget_pk = postdata['widget_id'][0]
selected_nodes = json.loads(postdata['selected_nodes'][0])
root = Clustering.hierarchical_clustering(linkage, matrix)
cluster_ids = set([cluster for _,_,cluster in selected_nodes])
selected_clusters = set([cluster for _,selected,cluster in selected_nodes if selected])
clustVar = orange.EnumVariable(str('Cluster'), values=["Cluster %d" % i for i in cluster_ids] + ["Other"])
origDomain = matrix.items.domain
domain = orange.Domain(origDomain.attributes, origDomain.classVar)
domain.addmeta(orange.newmetaid(), clustVar)
domain.addmetas(origDomain.getmetas())
# Build table with selected clusters
selected_table, unselected_table = orange.ExampleTable(domain), orange.ExampleTable(domain)
for id, selected, cluster in selected_nodes:
new_ex = orange.Example(domain, matrix.items[id])
if selected:
new_ex[clustVar] = clustVar("Cluster %d" % cluster)
selected_table.append(new_ex)
else:
new_ex[clustVar] = clustVar("Other")
unselected_table.append(new_ex)
# Build table of centroids
centroids = orange.ExampleTable(selected_table.domain)
if len(selected_table) > 0:
for cluster in sorted(selected_clusters):
print [ex for ex in selected_table if ex[clustVar] == "Cluster %d" % cluster]
clusterEx = orange.ExampleTable([ex for ex in selected_table if ex[clustVar] == "Cluster %d" % cluster])
# Attribute statistics
contstat = orange.DomainBasicAttrStat(clusterEx)
discstat = orange.DomainDistributions(clusterEx, 0, 0, 1)
ex = [cs.avg if cs else (ds.modus() if ds else "?") for cs, ds in zip(contstat, discstat)]
example = orange.Example(centroids.domain, ex)
example[clustVar] = clustVar("Cluster %d" % cluster)
centroids.append(example)
return {'centroids' : centroids, 'selected_examples' : selected_table, 'unselected_examples' : unselected_table}
......@@ -134,7 +134,6 @@ function draw(w, h, hierarchy, target) {
$(".attributeselect").change(function () {
$(".attributes").hide();
$(".attribute"+$(".attributeselect").val()).show();
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment