Changeset 7943:26c841639ce0 in orange


Ignore:
Timestamp:
05/25/11 18:22:14 (3 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
0d1075992c94716734bdd3d2f963646ad5fe89f2
Message:

Bug fixes in hierarchical clustering widget.
Added 'clone' function in Orange.clustering.hierarchical module.
Added some docstrings.

Location:
orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/clustering/hierarchical.py

    r7849 r7943  
    1313An example. 
    1414 
    15 .. automethod:: Orange.clustering.hierarchical.clustering 
     15Utility Functions 
     16================= 
     17 
     18.. autofunction:: clustering 
     19.. autofunction:: clustering_features 
     20.. autofunction:: cluster_to_list 
     21.. autofunction:: top_clusters 
     22.. autofunction:: top_cluster_membership 
     23.. autofunction:: order_leaves 
     24 
     25.. autofunction:: postorder 
     26.. autofunction:: preorder 
     27.. autofunction:: dendrogram_layout 
     28.. autofunction:: dendrogram_draw 
     29.. autofunction:: prune 
     30.. autofunction:: pruned 
     31.. autofunction:: cluster_depths 
     32.. autofunction:: instance_distance_matrix 
     33.. autofunction:: feature_distance_matrix 
     34.. autofunction:: joining_cluster 
     35.. autofunction:: cophenetic_distances 
     36.. autofunction:: cophenetic_correlation 
    1637 
    1738""" 
     39 
    1840import orange 
    1941import Orange 
     
    2648import sys 
    2749 
     50SINGLE = HierarchicalClustering.Single 
     51AVERAGE = HierarchicalClustering.Average 
     52COMPLETE = HierarchicalClustering.Complete 
     53WARD = HierarchicalClustering.Ward 
    2854 
    2955def clustering(data, 
    3056               distanceConstructor=orange.ExamplesDistanceConstructor_Euclidean, 
    31                linkage=orange.HierarchicalClustering.Average, 
     57               linkage=AVERAGE, 
    3258               order=False, 
    3359               progressCallback=None): 
    34     """Return a hierarhical clustering of the data set.""" 
     60    """ Return a hierarchical clustering of the instances in a data set. 
     61     
     62    :param data: Input data table for clustering. 
     63    :type data: :class:`Orange.data.Table` 
     64    :param distance_constructor: Instance distance constructor 
     65    :type distance_constructor: :class:`Orange.distances.ExamplesDistanceConstructor` 
     66    :param linkage: Linkage flag. Must be one of global module level flags: 
     67     
     68        - SINGLE 
     69        - AVERAGE 
     70        - COMPLETE 
     71        - WARD 
     72         
     73    :type linkage: int 
     74    :param order: If `True` run `order_leaves` on the resulting clustering. 
     75    :type order: bool 
     76    :param progress_callback: A function (taking one argument) to use for 
     77        reporting the on the progress. 
     78    :type progress_callback: function 
     79     
     80    """ 
    3581    distance = distanceConstructor(data) 
    3682    matrix = orange.SymMatrix(len(data)) 
     
    3884        for j in range(i+1): 
    3985            matrix[i, j] = distance(data[i], data[j]) 
    40     root = orange.HierarchicalClustering(matrix, linkage=linkage, progressCallback=(lambda value, obj=None: progressCallback(value*100.0/(2 if order else 1))) if progressCallback else None) 
     86    root = HierarchicalClustering(matrix, linkage=linkage, progressCallback=(lambda value, obj=None: progressCallback(value*100.0/(2 if order else 1))) if progressCallback else None) 
    4187    if order: 
    4288        order_leaves(root, matrix, progressCallback=(lambda value: progressCallback(50.0 + value/2)) if progressCallback else None) 
     
    4490 
    4591def clustering_features(data, distance=None, linkage=orange.HierarchicalClustering.Average, order=False, progressCallback=None): 
    46     """Return hierarhical clustering of attributes in the data set.""" 
     92    """ Return hierarchical clustering of attributes in a data set. 
     93     
     94    :param data: Input data table for clustering. 
     95    :type data: :class:`Orange.data.Table` 
     96    :param distance: Attribute distance constructor  
     97        .. note:: currently not used. 
     98    :param linkage: Linkage flag. Must be one of global module level flags: 
     99     
     100        - SINGLE 
     101        - AVERAGE 
     102        - COMPLETE 
     103        - WARD 
     104         
     105    :type linkage: int 
     106    :param order: If `True` run `order_leaves` on the resulting clustering. 
     107    :type order: bool 
     108    :param progress_callback: A function (taking one argument) to use for 
     109        reporting the on the progress. 
     110    :type progress_callback: function 
     111     
     112    """ 
    47113    matrix = orange.SymMatrix(len(data.domain.attributes)) 
    48114    for a1 in range(len(data.domain.attributes)): 
     
    55121 
    56122def cluster_to_list(node, prune=None): 
    57     """Return a list of clusters down from the node of hierarchical clustering.""" 
     123    """ Return a list of clusters down from the node of hierarchical clustering. 
     124     
     125    :param node: Cluster node. 
     126    :type node: :class:`HierarchicalCluster` 
     127    :param prune: If not `None` it must be a positive integer. Any cluster 
     128        with less then `prune` items will be left out of the list. 
     129    :type node: int or `NoneType` 
     130     
     131    """ 
    58132    if prune: 
    59133        if len(node) <= prune: 
     
    64138 
    65139def top_clusters(root, k): 
    66     """Return k topmost clusters from hierarchical clustering.""" 
     140    """ Return k topmost clusters from hierarchical clustering. 
     141     
     142    :param root: Root cluster. 
     143    :type root: :class:`HierarchicalCluster` 
     144    :param k: Number of top clusters. 
     145    :type k: int 
     146     
     147    """ 
    67148    candidates = set([root]) 
    68149    while len(candidates) < k: 
     
    74155 
    75156def top_cluster_membership(root, k): 
    76     """Return data instances' cluster membership (list of indices) to k topmost clusters.""" 
     157    """ Return data instances' cluster membership (list of indices) to k topmost clusters. 
     158     
     159    :param root: Root cluster. 
     160    :type root: :class:`HierarchicalCluster` 
     161    :param k: Number of top clusters. 
     162    :type k: int 
     163     
     164    """ 
    77165    clist = top_clusters(root, k) 
    78166    cmap = [None] * len(root) 
     
    84172def order_leaves(tree, matrix, progressCallback=None): 
    85173    """Order the leaves in the clustering tree. 
    86  
     174     
    87175    (based on Ziv Bar-Joseph et al. (Fast optimal leaf ordering for hierarchical clustering') 
    88     Arguments: 
    89         tree   --binary hierarchical clustering tree of type orange.HierarchicalCluster 
    90         matrix --orange.SymMatrix that was used to compute the clustering 
    91         progressCallback --function used to report progress 
    92     """ 
    93 #    from Orange.misc import recursion_limit 
     176     
     177    :param tree: Binary hierarchical clustering tree. 
     178    :type tree: :class:`HierarchicalCluster` 
     179    :param matrix: SymMatrix that was used to compute the clustering. 
     180    :type matrix: :class:`Orange.core.SymMatrix` 
     181    :param progress_callback: Function used to report on progress. 
     182    :type progress_callback: function 
     183     
     184    .. note:: The ordering is done inplace.  
     185     
     186    """ 
    94187     
    95188    objects = getattr(tree.mapping, "objects", None) 
     
    270363        tree.mapping.setattr("objects", objects) 
    271364 
     365""" Matplotlib dendrogram ploting. 
     366""" 
    272367try: 
    273368    import numpy 
     
    505600         
    506601         
     602""" Dendrogram ploting using Orange.misc.reander 
     603""" 
     604 
    507605from orngMisc import ColorPalette, EPSRenderer 
    508606class DendrogramPlot(object): 
     
    643741         
    644742def dendrogram_draw(filename, *args, **kwargs): 
     743    """ Plot the dendrogram to `filename`. 
     744     
     745    .. todo:: Finish documentation. 
     746    """ 
    645747    import os 
    646748    from orngMisc import PILRenderer, EPSRenderer, SVGRenderer 
     
    650752    d = DendrogramPlot(*args, **kwargs) 
    651753    d.plot(filename) 
    652      
    653      
    654 """ 
    655 Utility functions 
    656 ================= 
    657  
    658 """ 
    659754     
    660755def postorder(cluster): 
     
    739834    return result 
    740835     
     836def clone(cluster): 
     837    """ Clone a cluster, including it's subclusters. 
     838     
     839    :param cluster: Cluster to clone 
     840    :type cluster: :class:`HierarchialCluster` 
     841    """ 
     842    import copy 
     843    clones = {} 
     844    mapping = copy.copy(cluster.mapping) 
     845    for node in postorder(cluster): 
     846        node_clone = copy.copy(node) 
     847        if node.branches: 
     848            node_clone.branches = [clones[b] for b in node.branches] 
     849        node_clone.mapping = mapping 
     850        clones[node] = node_clone 
     851         
     852    return clones[cluster] 
    741853     
    742854def pruned(root_cluster, level=None, height=None, condition=None): 
    743855    """ Return a new pruned clustering instance. 
    744856     
    745     .. note:: This uses `copy.deepcopy` to create a copy of the root_cluster 
     857    .. note:: This uses `clone` to create a copy of the root_cluster 
    746858        instance. 
    747859     
     
    762874     
    763875    """ 
    764     import copy 
    765      
    766     # XXX This is unsafe HierarchicalCluster should take care of copying 
    767     if hasattr(root_cluster.mapping, "objects"): 
    768         objects = root_cluster.mapping.objects 
    769         root_cluster.mapping.objects = None 
    770         has_objects = True 
    771     else: 
    772         has_objects = False 
    773          
    774     root_cluster = copy.deepcopy(root_cluster) 
    775      
     876    root_cluster = clone(root_cluster) 
    776877    prune(root_cluster, level, height, condition) 
    777      
    778     if has_objects: 
    779         root_cluster.mapping.objects = objects 
    780          
    781878    return root_cluster 
    782879     
  • orange/OrangeWidgets/Unsupervised/OWHierarchicalClustering.py

    r7942 r7943  
    426426    def on_selection_change(self): 
    427427        if self.matrix: 
    428             items = self.dendrogram.selectedItems() 
    429             self.selected_clusters = [item.cluster for item in items] 
    430             self.commit_data_if() 
     428            try: 
     429                items = self.dendrogram.selectedItems() 
     430                self.selected_clusters = [item.cluster for item in items] 
     431                self.commit_data_if() 
     432            except RuntimeError: # underlying C/C++ object has been deleted 
     433                pass 
    431434         
    432435    def commit_data_if(self): 
Note: See TracChangeset for help on using the changeset viewer.