Esempio n. 1
0
def nf1(first_partition, second_partition):
    """
    Compute the Normalized F1 score of the optimal algorithms matches among the partitions in input.
    Works on overlapping/non-overlapping complete/partial coverage partitions.

    :param first_partition: NodeClustering object
    :param second_partition: NodeClustering object
    :return: MatchingResult instance

    :Example:

    >>> from cdlib import evaluation, algorithms
    >>> g = nx.karate_club_graph()
    >>> louvain_communities = algorithms.louvain(g)
    >>> leiden_communities = algorithms.leiden(g)
    >>> evaluation.nf1(louvain_communities,leiden_communities)

    :Reference:

    1. Rossetti, G., Pappalardo, L., & Rinzivillo, S. (2016). `A novel approach to evaluate algorithms detection internal on ground truth. <https://www.researchgate.net/publication/287204505_A_novel_approach_to_evaluate_community_detection_algorithms_on_ground_truth/>`_

    2. Rossetti, G. (2017). : `RDyn: graph benchmark handling algorithms dynamics. Journal of Complex Networks. <https://academic.oup.com/comnet/article-abstract/5/6/893/3925036?redirectedFrom=PDF/>`_ 5(6), 893-912.
    """

    nf = NF1(first_partition.communities, second_partition.communities)
    results = nf.summary()
    return results['scores'].loc["NF1"][0]
Esempio n. 2
0
def f1(first_partition, second_partition):
    """
    Compute the average F1 score of the optimal algorithms matches among the partitions in input.
    Works on overlapping/non-overlapping complete/partial coverage partitions.

    :param first_partition: NodeClustering object
    :param second_partition: NodeClustering object
    :return: F1 score (harmonic mean of precision and recall)

    :Example:

    >>> from cdlib import evaluation, algorithms
    >>> g = nx.karate_club_graph()
    >>> louvain_communities = algorithms.louvain(g)
    >>> leiden_communities = algorithms.leiden(g)
    >>> evaluation.f1(louvain_communities,leiden_communities)

    :Reference:

    1. Rossetti, G., Pappalardo, L., & Rinzivillo, S. (2016). `A novel approach to evaluate algorithms detection internal on ground truth. <https://www.researchgate.net/publication/287204505_A_novel_approach_to_evaluate_community_detection_algorithms_on_ground_truth/>`_ In Complex Networks VII (pp. 133-144). Springer, Cham.
    """

    nf = NF1(first_partition.communities, second_partition.communities)
    results = nf.summary()
    return MatchingResult(results['details']['F1 mean'][0],
                          results['details']['F1 std'][0])
Esempio n. 3
0
def NF1scores(filenames):
    communities = []
    for filename in filenames:
        comm = readCommunities(filename)
        comms = [tuple(x) for x in comm]
        communities.append(comms)
    nf = NF1(communities[0], communities[1])
    results = nf.summary()
    print(results['scores'])
    print(results['details'])

    # Visualising the Precision-Recall density scatter-plot
    nf.plot()
Esempio n. 4
0
def run_nf1_evaluation(partition_list):
    for partition in partition_list:
        _partition_list_copy = partition_list.copy()
        _partition_list_copy.remove(partition)
        index = partition_list.index([partition][0])

        for comparable_partition in _partition_list_copy[index:]:
            nf = NF1(partition[0], comparable_partition[0])
            res = nf.summary()

            print(
                f"Comparison between {partition[1]} and {comparable_partition[1]}:"
            )
            print(res["scores"])
            print("____________________________________________________")
Esempio n. 5
0
 def test_nf1(self):
     coms = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
     gt = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
     nf = NF1(coms, gt)
     res = nf.summary()
     self.assertEqual(len(res), 2)
Esempio n. 6
0
 def test_plot(self):
     coms = [(1, 2, 67), (4, 6, 11), (7, 8, 9)]
     gt = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
     nf = NF1(coms, gt)
     nf.plot()
Esempio n. 7
0
    # timing = (stop-start).seconds/60
    # print "ELAPSED TIME:" + str(timing) + " minutes"

    p_demon = apply_demon(g)

    # stop = datetime.datetime.now()
    # timing = (stop-start).seconds/60
    # print "ELAPSED TIME:" + str(timing) + " minutes"

    ps = [p_labelprop, p_louvain, p_gn, p_demon]

    for i in range(len(ps)):
        for j in range(i + 1, len(ps)):
            if ps[i] == p_labelprop:
                if ps[j] == p_louvain:
                    comp = NF1(ps[i], ps[j]).summary()
                    comp['scores'].to_csv(path_or_buf='./comparisons_ita/' +
                                          'label_propagation/label_' +
                                          'propagation_louvain_scores.csv')
                    comp['details'].to_csv(path_or_buf='./comparisons_ita/' +
                                           'label_propagation/label_' +
                                           'propagation_louvain_details.csv')
                elif ps[j] == p_gn:
                    for iteration in ps[j]:
                        comp = NF1(ps[i], ps[j][iteration]).summary()
                        comp['scores'].to_csv(
                            path_or_buf='./comparisons_ita/' +
                            'label_propagation/label_' + 'propagation_gn_it_' +
                            str(iteration) + '_scores.csv')
                        comp['details'].to_csv(
                            path_or_buf='./comparisons_ita/' +
Esempio n. 8
0
 def nf1go(x, y):
     a = NF1(y, x)
     score = a.get_f1()[0]
     return score