Exemple #1
0
    def generate_basic_graph_with_mapping(
            VISJS=False) -> (GraphDataStruct, dict):
        mapping = {}

        # Create a graphe structure
        if VISJS:
            tmp_meta = Metadata(Source.VISJS)
        else:
            tmp_meta = Metadata(Source.DBDUMP)
        tmp_graph = GraphDataStruct(tmp_meta)

        # For each cluster, fetch all pictures and store it
        for cluster_id in range(0, 2):
            tmp_graph.add_cluster(
                Cluster(label="", tmp_id=cluster_id, image=""))

            for id in range(0, 3):
                pic_id = str(cluster_id) + "_" + str(id) + "OLD"
                pic_image = str(cluster_id) + "_" + str(id) + "IMAGE"

                # Prepare mapping
                mapping[pic_image] = str(cluster_id) + "_" + str(id) + "NEW"

                # Label = picture score, here
                tmp_graph.add_node(
                    Node(label="picture name +" + pic_id,
                         tmp_id=pic_id,
                         image=pic_image))
                tmp_graph.add_edge(Edge(_from=cluster_id, _to=pic_id))

        return tmp_graph, mapping
    def get_storage_graph(self) -> GraphDataStruct:
        """
        # TODO : Move to API ?
        Export the current state of the database as a graph datastructure. This represents the storage graph of the server.
        :return: The storage graph of the server, as is in the database
        """

        # Create a graphe structure
        tmp_meta = Metadata(Source.DBDUMP)
        tmp_graph = GraphDataStruct(tmp_meta)

        # Get all clusters
        cluster_list = self.get_cluster_list()

        # For each cluster, fetch all pictures and store it
        for cluster_id in cluster_list:
            tmp_graph.add_cluster(Cluster(label="", tmp_id=cluster_id, image=""))

            picture_list = self.get_pictures_of_cluster(cluster_id, with_score=True)
            self.logger.info(f"Picture list : {picture_list}")

            for picture in picture_list:
                # Label = picture score, here
                tmp_graph.add_node(Node(label=picture[1], tmp_id=picture[0], image=""))
                tmp_graph.add_edge(Edge(_from=cluster_id, _to=picture[0]))

        return tmp_graph
Exemple #3
0
    def generate_basic_graph() -> GraphDataStruct:
        # Create a graphe structure
        tmp_meta = Metadata(Source.DBDUMP)
        tmp_graph = GraphDataStruct(tmp_meta)

        # For each cluster, fetch all pictures and store it
        for cluster_id in range(0, 2):
            tmp_graph.add_cluster(
                Cluster(label="", tmp_id=cluster_id, image=""))

            for id in range(0, 3):
                pic_id = str(cluster_id) + "_" + str(id)
                # Label = picture score, here
                tmp_graph.add_node(
                    Node(label="picture name +" + pic_id,
                         tmp_id=pic_id,
                         image=""))
                tmp_graph.add_edge(Edge(_from=cluster_id, _to=pic_id))

        return tmp_graph
Exemple #4
0
    def test_compute_score_for_one_threshold(self):
        # Graph example. Please check documentation for more information
        cal_conf = Default_calibrator_conf()
        quality_evaluator = similarity_graph_quality_evaluator.similarity_graph_quality_evaluator(
            cal_conf)

        requests_results = [
            # 1 to 2 and 3
            {
                "list_pictures": [{
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.1,
                    "image_id": "2"
                }, {
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.6,
                    "image_id": "3"
                }],
                "request_id":
                "1",
                "status":
                "matches_found",
                "request_time":
                0
            },
            {
                "list_pictures": [{
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.3,
                    "image_id": "6"
                }],
                "request_id":
                "2",
                "status":
                "matches_found",
                "request_time":
                0
            },
            {
                "list_pictures": [{
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.2,
                    "image_id": "1"
                }],
                "request_id":
                "3",
                "status":
                "matches_found",
                "request_time":
                0
            },
            {
                "list_pictures": [{
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.4,
                    "image_id": "2"
                }],
                "request_id":
                "4",
                "status":
                "matches_found",
                "request_time":
                0
            },
            {
                "list_pictures": [{
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.6,
                    "image_id": "4"
                }],
                "request_id":
                "5",
                "status":
                "matches_found",
                "request_time":
                0
            },
            {
                "list_pictures": [{
                    "cluster_id": "XXX",
                    "decision": "YES",
                    "distance": 0.7,
                    "image_id": "2"
                }],
                "request_id":
                "6",
                "status":
                "matches_found",
                "request_time":
                0
            }
        ]

        # Create the reference graph
        graph_data_struct = GraphDataStruct(Metadata(Source.DBDUMP))
        graph_data_struct.add_cluster(Cluster(label="A", tmp_id="A",
                                              image="A"))
        graph_data_struct.add_cluster(Cluster(label="B", tmp_id="B",
                                              image="B"))
        graph_data_struct.add_cluster(Cluster(label="C", tmp_id="C",
                                              image="C"))
        graph_data_struct.add_node(Node(label="1", tmp_id="1", image="1"))
        graph_data_struct.add_node(Node(label="2", tmp_id="2", image="2"))
        graph_data_struct.add_node(Node(label="3", tmp_id="3", image="3"))
        graph_data_struct.add_node(Node(label="4", tmp_id="4", image="4"))
        graph_data_struct.add_node(Node(label="5", tmp_id="5", image="5"))
        graph_data_struct.add_node(Node(label="6", tmp_id="6", image="6"))
        graph_data_struct.add_edge(Edge(_from="1", _to="A"))
        graph_data_struct.add_edge(Edge(_from="2", _to="A"))
        graph_data_struct.add_edge(Edge(_from="3", _to="A"))
        graph_data_struct.add_edge(Edge(_from="4", _to="B"))
        graph_data_struct.add_edge(Edge(_from="5", _to="B"))
        graph_data_struct.add_edge(Edge(_from="6", _to="C"))

        pprint.pprint(requests_results)
        pprint.pprint(graph_data_struct.export_as_dict())

        quality_evaluator.cal_conf.NB_TO_CHECK = 1

        dist_threshold = 0
        stats_datastruct = quality_evaluator.compute_score_for_one_threshold(
            requests_results, graph_data_struct, dist_threshold)
        print(stats_datastruct)
        self.assertEqual(stats_datastruct.P, 3)
        self.assertEqual(stats_datastruct.N, 3)
        self.assertAlmostEqual(stats_datastruct.TPR, 0.0, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.TNR, 1.0, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FPR, 0.0, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FNR, 1.0, delta=0.05)

        dist_threshold = 0.5
        stats_datastruct = quality_evaluator.compute_score_for_one_threshold(
            requests_results, graph_data_struct, dist_threshold)
        print(stats_datastruct)
        self.assertEqual(stats_datastruct.P, 3)
        self.assertEqual(stats_datastruct.N, 3)
        self.assertAlmostEqual(stats_datastruct.TPR, 0.66, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.TNR, 0.33, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FPR, 0.66, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FNR, 0.33, delta=0.05)

        dist_threshold = 1
        stats_datastruct = quality_evaluator.compute_score_for_one_threshold(
            requests_results, graph_data_struct, dist_threshold)
        print(stats_datastruct)
        self.assertEqual(stats_datastruct.P, 3)
        self.assertEqual(stats_datastruct.N, 3)
        self.assertAlmostEqual(stats_datastruct.TPR, 1.0, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.TNR, 0.0, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FPR, 1.0, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FNR, 0.0, delta=0.05)

        quality_evaluator.cal_conf.NB_TO_CHECK = 3

        dist_threshold = 0.5
        stats_datastruct = quality_evaluator.compute_score_for_one_threshold(
            requests_results, graph_data_struct, dist_threshold)
        print(stats_datastruct)
        self.assertEqual(stats_datastruct.P, 4)
        self.assertEqual(stats_datastruct.N, 3)
        self.assertAlmostEqual(stats_datastruct.TPR, 0.5, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.TNR, 0.33, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FPR, 0.66, delta=0.05)
        self.assertAlmostEqual(stats_datastruct.FNR, 0.5, delta=0.05)