Exemplo n.º 1
0
def unionPart(*partsList):
    """Finds the union of several partitions"""

    mat = part2graph(util.concat(*partsList))
    parts = graph.connectedComponents(mat.keys(), lambda x: mat[x].keys())

    # remove parition ids from partitioning
    parts = map(lambda part: filter(lambda x: type(x) != int, part), parts)

    return parts
def unionPart(* partsList):
    """Finds the union of several partitions"""

    mat = part2graph(util.concat(* partsList))
    parts = graph.connectedComponents(mat.keys(), lambda x: mat[x].keys())

    # remove parition ids from partitioning
    parts = map(lambda part: filter(lambda x: type(x) != int, part), parts)

    return parts
Exemplo n.º 3
0
def join_tables(* args, **kwargs):
    """Join together tables into one table.
       Each argument is a tuple (table_i, key_i, cols_i)
       
       key_i is either a column name or a function that maps a 
       table row to a unique key
    """
    
    if len(args) == 0:
        return Table()
    
    # determine common keys
    tab, key, cols = args[0]
    if isinstance(key, str):
        keys = tab.cget(key)
        lookups = [tab.lookup(key)]        
    else:
        keys = map(key, tab)
        lookup = {}
        for row in tab:
            lookup[key(row)] = row
        lookups = [lookup]
        
    keyset = set(keys)
    

    for tab, key, cols in args[1:]:
        if isinstance(key, str):
            keyset = keyset & set(tab.cget(key))
            lookups.append(tab.lookup(key))            
        else:
            keyset = keyset & set(map(key, tab))
            lookup = {}
            for row in tab:
                lookup[key(row)] = row
            
            lookups.append(lookup)
    
    keys = filter(lambda x: x in keyset, keys)
    
    
    # build new table
    if "headers" not in kwargs:
        headers = util.concat(*util.cget(args, 2))
    else:
        headers = kwargs["headers"]
    tab = Table(headers=headers)
    
    for key in keys:
        row = {}
        for (tab2, key2, cols), lookup in zip(args, lookups):
            row.update(util.subdict(lookup[key], cols))
        tab.append(row)
    
    return tab
Exemplo n.º 4
0
def join_tables(* args, **kwargs):
    """Join together tables into one table.
       Each argument is a tuple (table_i, key_i, cols_i)
       
       key_i is either a column name or a function that maps a 
       table row to a unique key
    """
    
    if len(args) == 0:
        return Table()
    
    # determine common keys
    tab, key, cols = args[0]
    if isinstance(key, str):
        keys = tab.cget(key)
        lookups = [tab.lookup(key)]        
    else:
        keys = map(key, tab)
        lookup = {}
        for row in tab:
            lookup[key(row)] = row
        lookups = [lookup]
        
    keyset = set(keys)
    

    for tab, key, cols in args[1:]:
        if isinstance(key, str):
            keyset = keyset & set(tab.cget(key))
            lookups.append(tab.lookup(key))            
        else:
            keyset = keyset & set(map(key, tab))
            lookup = {}
            for row in tab:
                lookup[key(row)] = row
            
            lookups.append(lookup)
    
    keys = filter(lambda x: x in keyset, keys)
    
    
    # build new table
    if "headers" not in kwargs:
        headers = util.concat(*util.cget(args, 2))
    else:
        headers = kwargs["headers"]
    tab = Table(headers=headers)
    
    for key in keys:
        row = {}
        for (tab2, key2, cols), lookup in zip(args, lookups):
            row.update(util.subdict(lookup[key], cols))
        tab.append(row)
    
    return tab