def slices_to_layers(G_coupling,<EOL>slice_attr='<STR_LIT>',<EOL>vertex_id_attr='<STR_LIT:id>',<EOL>edge_type_attr='<STR_LIT:type>',<EOL>weight_attr='<STR_LIT>'):
if not slice_attr in G_coupling.vertex_attributes():<EOL><INDENT>raise ValueError("<STR_LIT>".format(slice_attr))<EOL><DEDENT>if not weight_attr in G_coupling.edge_attributes():<EOL><INDENT>raise ValueError("<STR_LIT>".format(weight_attr))<EOL><DEDENT>for v_slice in G_coupling.vs:<EOL><INDENT>H = v_slice[slice_attr]<EOL>H.vs[slice_attr] = v_slice.index<EOL>if not vertex_id_attr in H.vertex_attributes():<EOL><INDENT>raise ValueError("<STR_LIT>".format(vertex_id_attr ))<EOL><DEDENT>if not weight_attr in H.edge_attributes():<EOL><INDENT>H.es[weight_attr] = <NUM_LIT:1><EOL><DEDENT><DEDENT>G = disjoint_union_attrs(G_coupling.vs[slice_attr])<EOL>G.es[edge_type_attr] = '<STR_LIT>'<EOL>for v_slice in G_coupling.vs:<EOL><INDENT>for u_slice in v_slice.neighbors(mode=_ig.OUT):<EOL><INDENT>if v_slice.index < u_slice.index or G_coupling.is_directed():<EOL><INDENT>nodes_v = G.vs.select(lambda v: v[slice_attr]==v_slice.index)[vertex_id_attr]<EOL>if len(set(nodes_v)) != len(nodes_v):<EOL><INDENT>err = '<STR_LIT:\n>'.join(<EOL>['<STR_LIT>'.format(item, count) for item, count in Counter(nodes_v).items() if count > <NUM_LIT:1>]<EOL>)<EOL>raise ValueError('<STR_LIT>'.format(v_slice.index, err))<EOL><DEDENT>nodes_u = G.vs.select(lambda v: v[slice_attr]==u_slice.index)[vertex_id_attr]<EOL>if len(set(nodes_u)) != len(nodes_u):<EOL><INDENT>err = '<STR_LIT:\n>'.join(<EOL>['<STR_LIT>'.format(item, count) for item, count in Counter(nodes_u).items() if count > <NUM_LIT:1>]<EOL>)<EOL>raise ValueError('<STR_LIT>'.format(u_slice.index, err))<EOL><DEDENT>common_nodes = set(nodes_v).intersection(set(nodes_u))<EOL>nodes_v = sorted([v for v in G.vs if v[slice_attr] == v_slice.index and v[vertex_id_attr] in common_nodes], key=lambda v: v[vertex_id_attr])<EOL>nodes_u = sorted([v for v in G.vs if v[slice_attr] == u_slice.index and v[vertex_id_attr] in common_nodes], key=lambda v: v[vertex_id_attr])<EOL>edges = zip(nodes_v, nodes_u)<EOL>e_start = G.ecount()<EOL>G.add_edges(edges)<EOL>e_end = G.ecount()<EOL>e_idx = range(e_start, e_end)<EOL>interslice_weight = G_coupling.es[G_coupling.get_eid(v_slice, u_slice)][weight_attr]<EOL>if not interslice_weight is None:<EOL><INDENT>G.es[e_idx][weight_attr] = interslice_weight<EOL><DEDENT>G.es[e_idx][edge_type_attr] = '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT>G_layers = [None]*G_coupling.vcount()<EOL>for v_slice in G_coupling.vs:<EOL><INDENT>H = G.subgraph_edges(G.es.select(_within=[v.index for v in G.vs if v[slice_attr] == v_slice.index]), delete_vertices=False)<EOL>H.vs['<STR_LIT>'] = [<NUM_LIT:1> if v[slice_attr] == v_slice.index else <NUM_LIT:0> for v in H.vs]<EOL>G_layers[v_slice.index] = H<EOL><DEDENT>G_interslice = G.subgraph_edges(G.es.select(type_eq='<STR_LIT>'), delete_vertices=False)<EOL>G_interslice.vs['<STR_LIT>'] = <NUM_LIT:0><EOL>return G_layers, G_interslice, G<EOL>
Convert a coupling graph of slices to layers of graphs.

    This function converts a graph of slices to layers so that they can be used
    with this package. This function assumes that the slices are represented by
    nodes in ``G_coupling``, and stored in the attribute ``slice_attr``. In other
    words, ``G_coupling.vs[slice_attr]`` should contain :class:`ig.Graph` s . The
    slices will be converted to layers, and nodes in different slices will be
    coupled if the two slices are connected in ``G_coupling``. Nodes in two
    connected slices are identified on the basis of the ``vertex_id_attr``, i.e.
    if two nodes in two connected slices have an identical value of the
    ``vertex_id_attr`` they will be coupled. The ``vertex_id_attr`` should hence
    be unique in each slice.  The weight of the coupling is determined by the
    weight of this link in ``G_coupling``, as determined by the ``weight_attr``.

    Parameters
    ----------
    G_coupling : :class:`ig.Graph`
      The graph connecting the different slices.

    slice_attr : string
      The vertex attribute which contains the slices.

    edge_type_attr : string
      The edge attribute to use for indicating the type of link (``interslice``
      or ``intraslice``).

    weight_attr : string
      The edge attribute used to indicate the (coupling) weight.

    Returns
    -------
    G_layers : list of :class:`ig.Graph`
      A list of slices converted to layers.

    G_interslice : :class:`ig.Graph`
      The interslice coupling layer.

    G : :class:`ig.Graph`
      The complete graph containing all layers and interslice couplings.

    Notes
    -----
    The distinction between slices and layers is not easy to grasp. Slices in
    this context refer to graphs that somehow represents different aspects of a
    network. The simplest example is probably slices that represents time: there
    are different snapshots network across time, and each snapshot is considered
    a slice. Some nodes may drop out of the network over time, while others enter
    the network. Edges may change over time, or the weight of the links may
    change over time. This is just the simplest example of a slice, and there may
    be different, more complex possibilities. Below an example with three time
    slices:

    .. image:: figures/slices.png

    Now in order to optimise partitions across these different slices, we
    represent them slightly differently, namely as layers. The idea of layers is
    that all graphs always are defined on the same set of nodes, and that only
    the links differ for different layers. We thus create new nodes as
    combinations of original nodes and slices. For example, if node 1 existed in
    both slice 1 and in slice 2, we will thus create two nodes to build the
    layers: a node 1-1 and a node 1-2. Additionally, if the slices are connected
    in the slice graph, the two nodes would also be connected, so there would be
    a linke between node 1-1 and 1-2. Different slices will then correspond to
    different layers: each layer only contains the link for that particular
    slice. In addition, for methods such as :class:`CPMVertexPartition`,
    so-called ``node_sizes`` are required, and for them to properly function,
    they should be set to 0 (which is handled appropriately in this function, and
    stored in the vertex attribute ``node_size``). We thus obtain equally many
    layers as we have slices, and we need one more layer for representing the
    interslice couplings.  For the example provided above, we thus obtain the
    following:

    .. image:: figures/layers_separate.png

    The idea of doing community detection with slices is further detailed in [1].

    References
    ----------
    .. [1] Mucha, P. J., Richardson, T., Macon, K., Porter, M. A., & Onnela,
           J.-P. (2010).  Community structure in time-dependent, multiscale, and
           multiplex networks. Science, 328(5980), 876-8.
           `10.1126/science.1184819 <http://doi.org/10.1126/science.1184819>`_
    See Also
    --------
    :func:`find_partition_temporal`

    :func:`time_slices_to_layers`