Exemplo n.º 1
0
 def _get_context(self):
     currdir = os.path.dirname(os.path.realpath(__file__))
     context = kahypar.Context()
     context.loadINIconfiguration(currdir + "/kahypar_config.ini")
     context.setK(k=2)
     context.setEpsilon(self.epsilon)
     context.suppressOutput(True)
     return context
Exemplo n.º 2
0
    def __init__(self, tilesize=8, mnc=100, addMsgNets=True, config=None):

        self._tilesize = tilesize
        self._mnc = mnc
        self._addMsgNets = addMsgNets
        self._context = kahypar.Context()
        with to_ini(config) as ini:
            self._context.loadINIconfiguration(ini)
Exemplo n.º 3
0
def hgpa(labels, nclass, random_state):
    """HyperGraph Partitioning Algorithm (HGPA).

    Parameters
    ----------
    labels: Labels generated by multiple clustering algorithms such as K-Means.
    nclass: Number of classes in a consensus clustering label.
    random_state: Used for reproducible results.

    Return
    -------
    label_ce: Consensus clustering label obtained from HGPA.
    """
    # Create hypergraph for kahypar
    H = create_hypergraph(labels)
    n_nodes, n_nets = H.shape

    node_weights = [1] * n_nodes
    edge_weights = [1] * n_nets

    hyperedge_indices = [0]
    hyperedges = []
    HT = H.T
    for i in range(n_nets):
        h = HT.getrow(i)
        idx_row, idx_col = h.nonzero()
        hyperedges += list(idx_col)
        hyperedge_indices.append(len(hyperedges))

    hypergraph = kahypar.Hypergraph(n_nodes, n_nets, hyperedge_indices,
                                    hyperedges, nclass, edge_weights,
                                    node_weights)

    # Settings for kahypar
    context = kahypar.Context()
    config_path = os.path.dirname(
        __file__) + "/kahypar_config/km1_kKaHyPar_sea20.ini"
    context.loadINIconfiguration(config_path)
    if random_state is not None:
        context.setSeed(random_state)
    context.setK(nclass)
    context.setEpsilon(0.03)
    context.suppressOutput(True)

    # Hypergraph partitioning
    kahypar.partition(hypergraph, context)

    label_ce = np.empty(n_nodes, dtype=int)
    for i in range(n_nodes):
        label_ce[i] = hypergraph.blockID(i)

    return label_ce
Exemplo n.º 4
0
 def _set_context(self, **kwargs):
     mode = modes[int(kwargs.get('mode'))]
     objective = objectives[int(kwargs.get('objective'))]
     K = int(kwargs.get('K'))
     eps = kwargs.get('eps')
     seed = kwargs.get('seed')
     profile_mode = {'direct': 'k', 'recursive': 'r'}[mode]
     profile = f"{objective}_{profile_mode}KaHyPar_sea20.ini"
     context = kahypar.Context()
     context.loadINIconfiguration(join(KAHYPAR_PROFILE_DIR, profile))
     context.setK(K)
     context.setSeed(seed)
     context.setEpsilon(kwargs.get('epsilon', eps * (K - 1)))
     context.suppressOutput(kwargs.get('quiet', True))
     return context
Exemplo n.º 5
0
    def test_partition_hypergraph(self):
        context = kahypar.Context()
        context.loadINIconfiguration(
            mydir + "/../..//config/km1_kKaHyPar_dissertation.ini")

        ibm01 = kahypar.createHypergraphFromFile(mydir + "/ISPD98_ibm01.hgr",
                                                 2)

        context.setK(2)
        context.setEpsilon(0.03)
        kahypar.partition(ibm01, context)

        self.assertEqual(kahypar.cut(ibm01), 202)
        self.assertEqual(kahypar.soed(ibm01), 404)
        self.assertEqual(kahypar.connectivityMinusOne(ibm01), 202)
        self.assertEqual(kahypar.imbalance(ibm01, context),
                         0.027603513174403904)
Exemplo n.º 6
0
def kahypar_subgraph_find_membership(
    inputs,
    output,
    size_dict,
    weight_nodes='const',
    weight_edges='log',
    fix_output_nodes=False,
    parts=2,
    imbalance=0.01,
    compress=0,
    seed=None,
    profile=None,
    mode='direct',
    objective='cut',
    quiet=True,
):
    import kahypar as kahypar

    if seed is None:
        seed = random.randint(0, 2**31 - 1)

    nv = len(inputs)
    if parts >= nv:
        return list(range(nv))

    hg = get_hypergraph(inputs, output, size_dict, accel=False)

    if compress:
        hg.compress(compress)

    winfo = to_sparse(hg, weight_nodes=weight_nodes, weight_edges=weight_edges)

    hypergraph_kwargs = {
        'num_nodes': hg.get_num_nodes(),
        'num_edges': hg.get_num_edges(),
        'index_vector': winfo['hyperedge_indices'],
        'edge_vector': winfo['hyperedges'],
        'k': parts,
    }

    edge_weights, node_weights = {
        (False, False): (None, None),
        (False, True): ([], winfo['node_weights']),
        (True, False): (winfo['edge_weights'], []),
        (True, True): (winfo['edge_weights'], winfo['node_weights']),
    }[winfo['has_edge_weights'], winfo['has_node_weights']]

    if edge_weights or node_weights:
        hypergraph_kwargs['edge_weights'] = edge_weights
        hypergraph_kwargs['node_weights'] = node_weights

    hypergraph = kahypar.Hypergraph(**hypergraph_kwargs)

    if fix_output_nodes:
        # make sure all the output nodes (those with output indices) are in
        # the same partition
        onodes = tuple(hg.output_nodes())

        if parts >= nv - len(onodes) + 1:
            # too many partitions, simply group all outputs and return
            groups = itertools.count(1)
            return [0 if i in onodes else next(groups) for i in range(nv)]

        for i in onodes:
            hypergraph.fixNodeToBlock(i, 0)

        # silences various warnings
        mode = 'recursive'

    if profile is None:
        profile_mode = {'direct': 'k', 'recursive': 'r'}[mode]
        profile = f"{objective}_{profile_mode}KaHyPar_sea20.ini"

    context = kahypar.Context()
    context.loadINIconfiguration(join(KAHYPAR_PROFILE_DIR, profile))
    context.setK(parts)
    context.setSeed(seed)
    context.suppressOutput(quiet)
    context.setEpsilon(imbalance * parts)

    kahypar.partition(hypergraph, context)

    return [hypergraph.blockID(i) for i in hypergraph.nodes()]
Exemplo n.º 7
0
import os
import kahypar as kahypar

num_nodes = 7
num_nets = 4

hyperedge_indices = [0,2,6,9,12]
hyperedges = [0,2,0,1,3,4,3,4,6,2,5,6]

node_weights = [1,2,3,4,5,6,7]
edge_weights = [11,22,33,44]

k=2

hypergraph = kahypar.Hypergraph(num_nodes, num_nets, hyperedge_indices, hyperedges, k, edge_weights, node_weights)

context = kahypar.Context()
context.loadINIconfiguration("/home/ags/code/kahypar/config/km1_kKaHyPar_sea20.ini")

context.setK(k)
context.setEpsilon(0.03)

kahypar.partition(hypergraph, context)
Exemplo n.º 8
0
def kahypar_subgraph_find_membership(
    inputs,
    output,
    size_dict,
    weight_nodes='constant',
    weight_edges='log',
    fuse_output_inds=False,
    parts=2,
    imbalance=0.01,
    seed=None,
    profile=None,
    mode='direct',
    objective='cut',
    quiet=True,
):
    import kahypar as kahypar

    if seed is None:
        seed = random.randint(0, 2**31 - 1)

    nv = len(inputs)
    if parts >= nv:
        return list(range(nv))

    HG = HyperGraph(inputs, output, size_dict,
                    weight_edges=weight_edges,
                    weight_nodes=weight_nodes,
                    fuse_output_inds=fuse_output_inds)
    hyperedge_indices, hyperedges = HG.to_sparse()

    hypergraph_kwargs = {
        'num_nodes': HG.num_vertices,
        'num_edges': HG.num_edges,
        'index_vector': hyperedge_indices,
        'edge_vector': hyperedges,
        'k': parts,
    }

    edge_weights, node_weights = {
        (False, False): (None, None),
        (False, True): ([], HG.node_weights),
        (True, False): (HG.edge_weights, []),
        (True, True): (HG.edge_weights, HG.node_weights),
    }[HG.has_edge_weights, HG.has_node_weights]

    if edge_weights or node_weights:
        hypergraph_kwargs['edge_weights'] = edge_weights
        hypergraph_kwargs['node_weights'] = node_weights

    hypergraph = kahypar.Hypergraph(**hypergraph_kwargs)

    if profile is None:
        profile_mode = {'direct': 'k', 'recursive': 'r'}[mode]
        profile = f"{objective}_{profile_mode}KaHyPar_dissertation.ini"

    context = kahypar.Context()
    context.loadINIconfiguration(join(KAHYPAR_PROFILE_DIR, profile))
    context.setK(parts)
    context.setSeed(seed)
    context.suppressOutput(quiet)
    context.setEpsilon(imbalance * parts)

    kahypar.partition(hypergraph, context)

    return [hypergraph.blockID(i) for i in hypergraph.nodes()]