Changeset 9034:04d25830c75b in orange


Ignore:
Timestamp:
09/27/11 17:06:08 (3 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
91315a5cc7def9ea010e23061e36bb3326600de0
Message:

Fixed label parameter handling in DendrogramPlot.
DendrogramPlot can now save the image to a file like object.

File:
1 edited

Legend:

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

    r9027 r9034  
    8787 
    8888    Represents a node in the clustering tree, as returned by 
    89     :obj:`HierarchicalClustering`` 
     89    :obj:`HierarchicalClustering` 
    9090 
    9191    .. attribute:: branches 
     
    591591    return cmap 
    592592 
    593 def order_leaves_py(tree, matrix, progressCallback=None): 
     593def order_leaves_py(tree, matrix, progress_callback=None): 
    594594    """Order the leaves in the clustering tree. 
    595595     
     
    724724    for i, subtree in enumerate(subtrees): 
    725725        _optOrderingIterative(subtree) 
    726         if progressCallback and i in milestones: 
    727             progressCallback(100.0 * i / len(subtrees)) 
     726        if progress_callback and i in milestones: 
     727            progress_callback(100.0 * i / len(subtrees)) 
    728728 
    729729#    def _orderRecursive(tree, u, w): 
     
    808808     
    809809    Orange.core.HierarchicalClusterOrdering(tree, matrix, progress_callback=p) 
    810      
    811 ## The cpp code still needs testing 
     810 
     811from Orange.misc import deprecated_keywords 
     812order_leaves_cpp = deprecated_keywords({"progressCallback":"progress_callback"})(order_leaves_cpp) 
     813order_leaves_py = deprecated_keywords({"progressCallback":"progress_callback"})(order_leaves_py) 
     814 
     815## The cpp code still needs testing, so we leave the python version as a default for now 
    812816order_leaves = order_leaves_py 
    813817     
     
    10601064    """ 
    10611065    def __init__(self, tree, attr_tree = None, labels=None, data=None, width=None, height=None, tree_height=None, heatmap_width=None, text_width=None,  
    1062                  spacing=2, cluster_colors={}, color_palette=ColorPalette([(255, 0, 0), (0, 255, 0)]), maxv=None, minv=None, gamma=None, renderer=EPSRenderer): 
     1066                 spacing=2, cluster_colors={}, color_palette=ColorPalette([(255, 0, 0), (0, 255, 0)]), maxv=None, minv=None, gamma=None, renderer=EPSRenderer, **kwargs): 
    10631067        self.tree = tree 
    10641068        self.attr_tree = attr_tree 
    1065         self.labels = [str(ex.getclass()) for ex in data] if not labels and data and data.domain.classVar else (labels or []) 
     1069        if not labels: 
     1070            if data and data.domain.class_var: 
     1071                labels = [str(ex.getclass()) for ex in data] 
     1072            elif hasattr(tree.mapping, "objects"): 
     1073                labels = [str(obj) for obj in tree.mapping.objects] 
     1074            else: 
     1075                labels = [""] * len(tree) 
     1076        self.labels = labels 
     1077         
    10661078#        self.attr_labels = [str(attr.name) for attr in data.domain.attributes] if not attr_labels and data else attr_labels or [] 
    10671079        self.data = data 
     
    11351147        return width, height, tree_height, heatmap_width, heatmap_height, text_width, font_size 
    11361148     
    1137     def plot(self, filename="graph.eps"): 
     1149    def plot(self, filename="graph.eps", **kwargs): 
    11381150        width, height, tree_height, heatmap_width, heatmap_height, text_width, font_size = self.layout() 
    11391151        heatmap_cell_height = heatmap_height / len(self.labels) 
     
    11841196#        print self.renderer.transform() 
    11851197        self.renderer.set_font("Times-Roman", font_size) 
    1186         for index in self.tree: #label in self.labels: 
     1198        for index in self.tree.mapping: #label in self.labels: 
    11871199            self.renderer.draw_text(0.0, 0.0, self.labels[index]) 
    11881200            self.renderer.translate(0.0, heatmap_cell_height) 
    11891201        self.renderer.restore_render_state() 
    1190         self.renderer.save(filename) 
    1191          
    1192 def dendrogram_draw(filename, *args, **kwargs): 
     1202        self.renderer.save(filename, **kwargs) 
     1203         
     1204def dendrogram_draw(file, *args, **kwargs): 
    11931205    """ Plot the dendrogram to `filename`. 
    11941206     
     
    11971209    import os 
    11981210    from Orange.misc.render import PILRenderer, EPSRenderer, SVGRenderer 
    1199     name, ext = os.path.splitext(filename) 
    1200     kwargs["renderer"] = {".eps":EPSRenderer, ".svg":SVGRenderer, ".png":PILRenderer}.get(ext.lower(), PILRenderer) 
    1201 #    print kwargs["renderer"], ext 
     1211    if isinstance(file, basestring): 
     1212        name, ext = os.path.splitext(file) 
     1213        format = ext.lower().lstrip(".") 
     1214    else: 
     1215        format = kwargs.get("format", "png").lower() 
     1216    renderer = {"eps":EPSRenderer, "svg":SVGRenderer, "png":PILRenderer}.get(format, "png") 
     1217         
     1218    kwargs["renderer"] = renderer 
    12021219    d = DendrogramPlot(*args, **kwargs) 
    1203     d.plot(filename) 
     1220    if renderer is PILRenderer: 
     1221        d.plot(file, format=format) 
     1222    else: 
     1223        d.plot(file) 
    12041224     
    12051225def postorder(cluster): 
Note: See TracChangeset for help on using the changeset viewer.