示例#1
0
def similarity_at_each_step(dynamicCommunityReference: tn.DynCommunitiesSN,
                            dynamicCommunityObserved: tn.DynCommunitiesSN,
                            score=None):
    """
    Compute similarity at each step

    It takes into account the fact that the reference might by incomplete. (remove from the observations all nodes/time not present in the reference)

    :param dynamicCommunityReference: the dynamic partition to use as reference
    :param dynamicCommunityObserved: the dynamic partition to evaluate
    :param score: score to use, default adjusted NMI
    :return: pair (list of scores, list of sizes)
    """

    if score == None:
        score = sklearn.metrics.adjusted_mutual_info_score
    scores = []
    sizes = []

    comsToEvaluate = dynamicCommunityObserved.snapshot_affiliations()

    #for each step
    for t, affils in dynamicCommunityReference.snapshot_affiliations().items():
        affilReference = []
        affilToEvaluate = []

        #for each node
        for n, comId in affils.items():
            affilReference.append(list(comId)[0])
            if n in comsToEvaluate[t]:
                affilToEvaluate.append(list(comsToEvaluate[t][n])[0])
            else:
                affilToEvaluate.append("-1")
        scores.append(score(affilReference, affilToEvaluate))
        sizes.append(len(affilReference))

    return scores, sizes
示例#2
0
def longitudinal_similarity(dynamicCommunityReference: tn.DynCommunitiesSN,
                            dynamicCommunityObserved: tn.DynCommunitiesSN,
                            score=None,
                            convert_coms_sklearn_format=True):
    """
    Longitudinal similarity

    The longitudinal similarity between two dynamic clusters is computed by considering each couple (node,time) as an element belong to a cluster, a cluster containing therefore nodes in differnt times
    It takes into account the fact that the reference might by incomplete by removing from the partition to evaluate all (node,time) not present in the reference.

    :param dynamicCommunityReference: the dynamic partition used as reference (ground truth)
    :param dynamicCommunityObserved: the dynamic partition to evaluate (result of an algorithm)
    :param score: community comparison score, by default the adjsted NMI. (sklearn)
    :param convert_coms_sklearn_format: if the score expect in input clusters represented as in sklearn, True. if False, score will receive in input lists of sets of nodes
    :return: score
    """

    if score == None:
        score = lambda x, y: sklearn.metrics.adjusted_mutual_info_score(
            x, y, average_method="arithmetic")

    affilReference = []
    affilToEvaluate = []

    if convert_coms_sklearn_format:

        comsToEvaluate = dynamicCommunityObserved.snapshot_affiliations()

        #for each step
        for t, affils in dynamicCommunityReference.snapshot_affiliations(
        ).items():

            #for each node
            for n, comId in affils.items():
                affilReference.append(str(list(comId)[0]))
                if n in comsToEvaluate[t]:
                    affilToEvaluate.append(str(list(comsToEvaluate[t][n])[0]))
                else:
                    print("node not in partition to evaluate: ", str(n), " ",
                          str(t))
                    affilToEvaluate.append("-1")
    else:

        affilReference = {}
        affilToEvaluate = {}
        for t, coms in dynamicCommunityReference.snapshot_communities().items(
        ):
            all_nodes = set()
            for id, nodes in coms.items():
                node_sn = {(n, t) for n in nodes}
                all_nodes.update(node_sn)
                affilReference.setdefault(id, set()).update(node_sn)

            for id, nodes in dynamicCommunityObserved.snapshot_communities(
                    t).items():
                node_sn = {(n, t) for n in nodes}

                affilToEvaluate.setdefault(id,
                                           set()).update(node_sn & all_nodes)

        affilReference = list(affilReference.values())
        affilToEvaluate = list(affilToEvaluate.values())

    return score(affilReference, affilToEvaluate)