Ejemplo n.º 1
0
    def SPLIT(self, toSplit: Community, newComs: [str], sizes: [int],
              **kwargs):
        """
        Split a single community into several ones. Note that to control exactly which nodes are moved, one should use migrate instead

        :param toSplit: label of the community to split
        :param newComs: labels to give to the new snapshot_affiliations (list). The label of the community before split can be or not among them
        :param sizes: sizes of the new snapshot_affiliations, in number of nodes. In the same order as newComs.
        :return: a list of snapshot_affiliations resulting from the split.
        """
        if sum(sizes) != len(toSplit.nodes()):
            raise Exception(
                "The number of nodes in resulting snapshot_affiliations does not match the number of nodes in the initial one"
            )
            return
        splittingOut = []
        listNodes = toSplit.nodes()

        for i, nbNodes in enumerate(sizes):
            chosenNodes = set(
                np.random.choice(list(listNodes), nbNodes, replace=False))
            splittingOut.append(chosenNodes)
            listNodes = listNodes - chosenNodes

        return self._add_action(
            _Operation.migrate([toSplit], newComs, splittingOut), **kwargs)
Ejemplo n.º 2
0
    def MERGE(self, toMerge: [Community], merged: str, **kwargs):
        """
        Merge the communities in input into a single community with the name (label) provided in output

        :param toMerge: labels of snapshot_affiliations to merge
        :param merged: label of the merged community (can be same as one of the input or not
        :return: the merged community (community object)
        """
        allNodes = set()
        for com in toMerge:
            allNodes.update(com.nodes())
        return self._add_action(
            _Operation.migrate(toMerge, [merged], [allNodes]), **kwargs)[0]
Ejemplo n.º 3
0
    def ASSIGN(self, comsBefore: [Community], comsAfter: [str],
               splittingOut: [{str}], **kwargs):
        """
        Define a custom event

        Migrate nodes from a set of snapshot_affiliations to another set of snapshot_affiliations. Can be used to move a set of nodes from a community to
        another or any other more complex scenario.

        :param comBefore: Ccommunities in input
        :param comsAfter: label(s) to give to the resulting communities
        :param splittingOut: How to distribute nodes in output. It is a list of same lenght than comsAfter, and each element of the list is a set of names of nodes. Note that if some nodes present in input does not appear in output, they are considered "killed"
        :return: the communities resulting from the operation (list)
        """
        return self._add_action(
            _Operation.migrate(comsBefore, comsAfter, splittingOut), **kwargs)