Esempio n. 1
0
    def test_rdf_subclassing_definitions(self):
        """
        Test that RDF Subclass definitions are written to the resulting graph.
        """
        # -- Test default subclassing
        doc = odml.Document()
        _ = odml.Section(name="test_subclassing", type="cell", parent=doc)

        rdf_writer = RDFWriter([doc])
        curr_str = " ".join(rdf_writer.get_rdf_str().split())
        self.assertIn("odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section",
                      curr_str)
        self.assertIn("odml:Section a rdfs:Class", curr_str)

        # -- Test multiple entries; a definition should only occur once in an RDF document
        doc = odml.Document()
        sec = odml.Section(name="test_subclassing", type="cell", parent=doc)
        sub_sec = odml.Section(name="test_subclassing",
                               type="cell",
                               parent=sec)
        _ = odml.Section(name="test_subclassing", type="cell", parent=sub_sec)

        rdf_writer = RDFWriter([doc])
        curr_str = " ".join(rdf_writer.get_rdf_str().split())
        self.assertIn("odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section",
                      curr_str)
        self.assertIs(
            curr_str.count(
                "odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section"), 1)
        self.assertIn("odml:Section a rdfs:Class", curr_str)
        self.assertIs(curr_str.count("odml:Section a rdfs:Class"), 1)

        # -- Test custom subclassing
        type_custom_class = "species"
        custom_class_dict = {type_custom_class: "Species"}

        doc = odml.Document()
        _ = odml.Section(name="test_subclassing", type="cell", parent=doc)
        _ = odml.Section(name="test_custom_subclassing",
                         type=type_custom_class,
                         parent=doc)

        rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict)
        curr_str = " ".join(rdf_writer.get_rdf_str().split())
        self.assertIn("odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section",
                      curr_str)
        self.assertIn(
            "odml:Species a rdfs:Class ; rdfs:subClassOf odml:Section",
            curr_str)
        self.assertIn("odml:Section a rdfs:Class", curr_str)

        # -- Test inactive subclassing
        doc = odml.Document()
        _ = odml.Section(name="test_subclassing", type="cell", parent=doc)

        rdf_writer = RDFWriter([doc], rdf_subclassing=False)
        curr_str = " ".join(rdf_writer.get_rdf_str().split())
        self.assertNotIn("odml:Section a rdfs:Class", curr_str)
        self.assertNotIn(
            "odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section", curr_str)
Esempio n. 2
0
    def test_rdf_subclassing_switch(self):
        """
        Test the RDF section subclassing switch.
        """
        # Section type term defined in odml/resources/section_subclasses.yaml that will
        # be converted to an RDF Section Subclass of Class "Cell".
        sub_class_term = "cell"

        # Create minimal document
        doc = odml.Document()
        _ = odml.Section(name="test_subclassing",
                         type=sub_class_term,
                         parent=doc)

        # Test default subclassing
        rdf_writer = RDFWriter([doc])
        result = rdf_writer.get_rdf_str()
        self.assertIn("odml:Cell", result)

        # Test inactivation of subclassing feature
        rdf_writer = RDFWriter([doc], rdf_subclassing=False)
        result = rdf_writer.get_rdf_str()
        self.assertNotIn("odml:Cell", result)
Esempio n. 3
0
 def test_get_rdf_string(self):
     w = RDFWriter([self.doc1])
     w.get_rdf_str()
     with self.assertRaises(ValueError):
         w.get_rdf_str("abc")
Esempio n. 4
0
    def test_rdf_subclassing_queries(self):
        """
        Test the proper implementation of the RDF subclassing feature. Tests ensure, that queries
        relying on RDF Subclasses return appropriate results.
        """
        if version_info > (3, 4):
            namespace_map = {
                "odml": Namespace(ODML_NS),
                "rdf": RDF,
                "rdfs": RDFS
            }

            doc = odml.Document()
            _ = odml.Section(name="test_subclass", type="cell", parent=doc)
            _ = odml.Section(name="test_regular_class",
                             type="regular",
                             parent=doc)

            rdf_writer = RDFWriter([doc])
            _ = rdf_writer.get_rdf_str()

            use_graph = rdf_writer.graph
            DeductiveClosure(RDFS_Semantics).expand(use_graph)

            q_string = "SELECT * WHERE {?s rdf:type odml:Section .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            # Make sure the query finds two sections
            self.assertIs(len(use_graph.query(curr_query)), 2)

            # Make sure the query finds
            result_section = []
            for row in use_graph.query(curr_query):
                result_section.append(row.s)

            q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            self.assertIs(len(use_graph.query(curr_query)), 1)
            for row in use_graph.query(curr_query):
                self.assertIn(row.s, result_section)

            # -- Test custom subclassing queries
            type_custom_class = "species"
            type_overwrite_class = "cell"
            custom_class_dict = {
                type_custom_class: "Species",
                type_overwrite_class: "Neuron"
            }

            doc = odml.Document()
            sec = odml.Section(name="test_subclass",
                               type="species",
                               parent=doc)
            _ = odml.Section(name="test_subclass_overwrite",
                             type="cell",
                             parent=sec)
            _ = odml.Section(name="test_regular_class",
                             type="regular",
                             parent=sec)

            rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict)
            _ = rdf_writer.get_rdf_str()

            use_graph = rdf_writer.graph
            DeductiveClosure(RDFS_Semantics).expand(use_graph)

            q_string = "SELECT * WHERE {?s rdf:type odml:Section .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            # Make sure the query finds three sections
            self.assertIs(len(use_graph.query(curr_query)), 3)

            # Make sure the query finds
            result_section = []
            for row in use_graph.query(curr_query):
                result_section.append(row.s)

            # Custom class 'Species' should be found.
            q_string = "SELECT * WHERE {?s rdf:type odml:Species .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            self.assertIs(len(use_graph.query(curr_query)), 1)
            for row in use_graph.query(curr_query):
                self.assertIn(row.s, result_section)

            # Custom class 'Neuron' should be found.
            q_string = "SELECT * WHERE {?s rdf:type odml:Neuron .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            self.assertIs(len(use_graph.query(curr_query)), 1)
            for row in use_graph.query(curr_query):
                self.assertIn(row.s, result_section)

            # Default class 'Cell' was replaced and should not return any result.
            q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            self.assertIs(len(use_graph.query(curr_query)), 0)

            # -- Test inactivated subclassing
            doc = odml.Document()
            _ = odml.Section(name="test_regular_class",
                             type="regular",
                             parent=doc)
            _ = odml.Section(name="test_subclass", type="cell", parent=doc)

            rdf_writer = RDFWriter([doc], rdf_subclassing=False)
            _ = rdf_writer.get_rdf_str()

            use_graph = rdf_writer.graph
            DeductiveClosure(RDFS_Semantics).expand(use_graph)

            q_string = "SELECT * WHERE {?s rdf:type odml:Section .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            self.assertIs(len(use_graph.query(curr_query)), 2)

            q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}"
            curr_query = prepareQuery(q_string, initNs=namespace_map)

            self.assertIs(len(use_graph.query(curr_query)), 0)
Esempio n. 5
0
    def test_rdf_custom_subclasses(self):
        """
        Test collection of the odml RDF subclassing feature.
        Tests that the resulting output RDF document contains any required
        additional RDF subclasses.
        """
        sub_class_term = "cell"

        # Create minimal document
        doc = odml.Document()
        _ = odml.Section(name="test_subclassing",
                         type=sub_class_term,
                         parent=doc)

        # Test None dict
        rdf_writer = RDFWriter([doc], custom_subclasses=None)
        self.assertIn("odml:Cell", rdf_writer.get_rdf_str())

        # Test invalid dict
        rdf_writer = RDFWriter([doc], custom_subclasses=["invalid"])
        self.assertIn("odml:Cell", rdf_writer.get_rdf_str())

        # Test value whitespace
        inval_a = "This should"
        inval_b = "fail\nin"
        inval_c = "the\tmost"
        inval_d = "complete\rway"
        invalid_dict = {
            "type_1": inval_a,
            "type_2": inval_b,
            "type_3": inval_c,
            "type_4": inval_d
        }
        with self.assertRaises(ValueError):
            _ = RDFWriter([doc], custom_subclasses=invalid_dict)

        # Test custom subclassing
        type_custom_class = "species"
        custom_class_dict = {type_custom_class: "Species"}

        doc = odml.Document()
        _ = odml.Section(name="test_subclassing", type="cell", parent=doc)
        _ = odml.Section(name="test_custom_subclassing",
                         type=type_custom_class,
                         parent=doc)

        rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict)
        self.assertIn("odml:Cell", rdf_writer.get_rdf_str())
        self.assertIn("odml:Species", rdf_writer.get_rdf_str())

        # Test custom subclassing overwrite
        sub_class_type = "cell"
        custom_class_dict = {sub_class_type: "Neuron"}

        doc = odml.Document()
        _ = odml.Section(name="test_subclassing",
                         type=sub_class_type,
                         parent=doc)

        if version_info > (3, 4):
            with self.assertWarns(UserWarning):
                rdf_writer = RDFWriter([doc],
                                       custom_subclasses=custom_class_dict)
                self.assertNotIn("odml:Cell", rdf_writer.get_rdf_str())
                self.assertIn("odml:Neuron", rdf_writer.get_rdf_str())