Changeset 1368:ae15e1830ccc in orange-bioinformatics


Ignore:
Timestamp:
04/11/11 13:03:16 (3 years ago)
Author:
markotoplak
Branch:
default
Convert:
409ac0e4e76195f4483f9bf985b9b7f350eeebbc
Message:

Sorting by field values (converts to integers when possible).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • widgets/prototypes/OWGenotypeDistances.py

    r1367 r1368  
    1010import numpy 
    1111import math 
     12 
     13def data_type(vals): 
     14    try: 
     15        _ = [ int(a) for a in vals ] 
     16        return int 
     17    except: 
     18        try: 
     19            _ = [ float(a) for a in vals ] 
     20            return float 
     21        except: 
     22            return None 
    1223 
    1324def separate_by(data, separate, ignore=[], consider=None, add_empty=True): 
     
    2940    types = {} 
    3041    for k,vals in all_values.iteritems(): 
    31         try: 
    32             _ = [ int(a) for a in vals ] 
    33             types[k] = int 
    34         except: 
    35             try: 
    36                 _ = [ float(a) for a in vals ] 
    37                 types[k] = float 
    38             except: 
    39                 types[k] = None 
     42        types[k] = data_type(vals) 
    4043     
    4144    groups = defaultdict(list) 
     
    336339            self.warning(0, "No separate by attribute selected.") 
    337340        partitions = separate_by(self.data, separate_keys, consider=relevant_keys).items() 
    338 #        print partitions 
    339          
    340         partitions = sorted(partitions) 
     341 
     342        pkeys = [ key for key,_ in partitions ] 
     343        types = [ data_type([a[i] for a in pkeys]) for i in range(len(pkeys[0])) ] 
     344 
     345        partitions = sorted(partitions, key=lambda x: 
     346                    tuple(v if types[i] == None else types[i](v) 
     347                    for i,v in enumerate(x[0]))) 
     348 
    341349        split_data = [] 
    342350         
     
    358366                return self.data.domain[attr_index] 
    359367             
    360         for keys, indices in sorted(partitions): 
     368        for keys, indices in partitions: 
    361369            attrs = [get_attr(attr_index, i) for i, attr_index in enumerate(indices)] 
    362370            domain = Orange.data.Domain(attrs, None) 
Note: See TracChangeset for help on using the changeset viewer.