Changeset 1917:fc231a027d1b in orange-bioinformatics


Ignore:
Timestamp:
11/25/13 14:52:44 (5 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Prettier expanded variable names.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orangecontrib/bio/widgets/OWVennDiagram.py

    r1916 r1917  
    66import math 
    77 
    8 from collections import namedtuple, defaultdict, OrderedDict 
    9  
     8from collections import namedtuple, defaultdict, OrderedDict, Counter 
     9from itertools import count 
    1010from xml.sax.saxutils import escape 
    1111 
     
    448448        item_id_var = Orange.feature.String("item_id") 
    449449 
     450        names = [itemset.title.strip() for itemset in self.itemsets.values()] 
     451        names = uniquify(names) 
     452 
    450453        for i, (key, input) in enumerate(self.data.items()): 
    451454            attr = self.itemsetAttr(key) 
     
    462465            # add a column with source table id to the data 
    463466            subset.domain.add_meta(source_mid, source_var) 
    464             subset.add_meta_attribute(source_var, 
    465                                       "%s %i" % (str(input.name), i)) 
     467            subset.add_meta_attribute(source_var, str(names[i])) 
    466468            # add a column with instance set id 
    467469            subset.domain.add_meta(item_mid, item_id_var) 
     
    714716 
    715717    return sorted(varying, key=all_possible.index) 
     718 
     719 
     720def uniquify(strings): 
     721    """ 
     722    Return a list of unique strings. 
     723 
     724    The string at i'th position will have the same prefix as strings[i] 
     725    with an appended suffix to make the item unique (if necessary). 
     726 
     727    >>> uniquify(["cat", "dog", "cat"]) 
     728    ["cat 1", "dog", "cat 2"] 
     729 
     730    """ 
     731    counter = Counter(strings) 
     732    counts = defaultdict(count) 
     733    newstrings = [] 
     734    for string in strings: 
     735        if counter[string] > 1: 
     736            newstrings.append(string + (" %i" % (next(counts[string]) + 1))) 
     737        else: 
     738            newstrings.append(string) 
     739 
     740    return newstrings 
    716741 
    717742 
Note: See TracChangeset for help on using the changeset viewer.