def fetch(self, entity_name): u = self._get_access_url(entity_name) requests_session = requests.session() requests_session.mount('file://', LocalFileAdapter()) requests_session.mount('file:///', LocalFileAdapter()) r = requests_session.get(u, headers=self.headers, allow_redirects=True, stream=True) npub = nanopub.Nanopublication() if 'content-disposition' in r.headers: d = r.headers['content-disposition'] fname = re.findall("filename=(.+)", d) else: fname = entity_name.split('/')[-1] content_type = r.headers.get('content-type') if self.file_types is not None: for file_type in self.file_types: npub.assertion.add((entity_name, rdflib.RDF.type, file_type)) f = FileStorage(FileLikeFromIter(r.iter_content()), fname, content_type=content_type) old_nanopubs = self.app.add_file(f, entity_name, npub) npub.assertion.add((entity_name, self.app.NS.RDF.type, self.app.NS.pv.File)) # old_np variable unused for _, old_np_assertion in old_nanopubs: npub.pubinfo.add((npub.assertion.identifier, self.app.NS.prov.wasRevisionOf, old_np_assertion)) return npub
def test_positive_classifier(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://test.org/test_data", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite", "http://test.org/PNC" ] }''', format="json-ld") # print(np.serialize(format="trig")) agent = ca.Classifier() # replace any user-defined classifiers with test classifier agent.classifiers = {'test_classifier': testclassifier.TestClassifier()} results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 1) # print(results[0].serialize(format='trig')) labeled_correctly = False confidence = None if results[0].resource(URIRef("http://test.org/test_data"))[RDF.type : URIRef("http://test.org/ItsAPNC")]: labeled_correctly = True for conf in results[0].objects(subject=None, predicate=sio.SIO_000638): confidence = conf.value self.assertEquals(confidence, 1) self.assertTrue(labeled_correctly)
def test_no_conversion(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:253f65a70d014043be04c2b8583c174d", "@type": [ "http://nanomine.org/ns/CurrentDensity" ], "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://www.ontology-of-units-of-measure.org/resource/om-2/amperePerSquareMetre", "http://www.w3.org/2000/01/rdf-schema#label": [ {"@value": "A/m^2"} ] } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 4.713376e-08} ] } ] }''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 0)
def test_conversion_to_incompatible_units(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:1", "@type": "http://nanomine.org/ns/FiberTensileStrength", "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://www.ontology-of-units-of-measure.org/resource/om-2/voltPerMetre", "http://www.w3.org/2000/01/rdf-schema#label": [ {"@value": "Volts per Meter"} ] } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 0.3} ] } ] }''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 0)
def test_lack_of_preferred_unit(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:0", "@type": [ "http://nanomine.org/ns/Width" ], "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://nanomine.org/ns/unit/nm", "http://www.w3.org/2000/01/rdf-schema#label": [ {"@value": "Nanometer"} ] } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 50} ] } ] }''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 0)
def test_bad_unit_type(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:2", "@type": "http://example.com/badUnitType", "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://www.ontology-of-units-of-measure.org/resource/om-2/kelvin", "http://www.w3.org/2000/01/rdf-schema#label": [ {"@value": "Kelvin"} ] } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 250} ] } ] } ''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 0)
def test_dc_terms_import(self): np = nanopub.Nanopublication() np.assertion.parse(data=str('''<http://example.com/testonto> a <http://www.w3.org/2002/07/owl#Ontology>; <http://www.w3.org/2002/07/owl#imports> <http://purl.org/dc/terms/>.'''), format="turtle") #print(np.serialize(format="trig")) agent = autonomic.OntologyImporter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 1) #print(results[0].serialize(format="trig")) self.assertTrue(results[0].resource(URIRef('http://purl.org/dc/terms/created'))[RDF.type:RDF.Property])
def test_foaf_import(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/testonto", "@type" : "http://www.w3.org/2002/07/owl#Ontology", "http://www.w3.org/2002/07/owl#imports":{"@id":"http://xmlns.com/foaf/0.1/"} }''', format="json-ld") #print(np.serialize(format="trig")) agent = autonomic.OntologyImporter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 1) self.assertTrue(results[0].resource(URIRef('http://xmlns.com/foaf/0.1/'))[RDF.type:OWL.Ontology])
def test_basic_setl(self): self.dry_run = False np = nanopub.Nanopublication() np.assertion.parse(data=test_setl_script, format="turtle") #print(np.serialize(format="trig")) agent = autonomic.SETLr() results = self.run_agent(agent, nanopublication=np) persons = list( self.app.db.subjects(RDF.type, URIRef('http://xmlns.com/foaf/0.1/Person'))) self.assertEquals(len(persons), 4)
def test_conversion(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:0", "@type": [ "http://nanomine.org/ns/Width" ], "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://nanomine.org/ns/unit/nm", "http://www.w3.org/2000/01/rdf-schema#label": [ {"@value": "Nanometer"} ] } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 50} ] } ] }''', format="json-ld") np.assertion.parse(data='''{ "@id": "http://nanomine.org/ns/Width", "http://nanomine.org/ns/hasPreferredUnit": "http://www.ontology-of-units-of-measure.org/resource/om-2/micrometre" }''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 1) # print("Printing agent results:\n\n", results[0].serialize(format="trig"), "\n") contains_micrometre = False correct_micrometre_value = False if len(results) > 0: for attr in results[0].resource( URIRef("http://example.com/converter_test"))[ sio.hasAttribute]: if attr[sio.hasUnit:om.micrometre]: contains_micrometre = True if attr[sio.hasValue:Literal(0.05)]: correct_micrometre_value = True self.assertTrue(contains_micrometre) self.assertTrue(correct_micrometre_value)
def process(self, i, o): document_count = float(self.document_count) query = """select distinct ?concept (count(distinct ?othernode) as ?count) ?assertion where { ?node sio:hasPart [ a sio:Term; prov:specializationOf ?concept]. ?othernode sio:hasPart [ a sio:Term; prov:specializationOf ?concept]. optional { graph ?assertion { ?concept sio:InverseDocumentFrequency ?idf. } } } group by ?concept ?assertion""" for concept, count, assertion in self.app.db.query(query, initBindings=dict(node=i.identifier)): idf = log10(old_div(document_count,count.value)) npub = nanopub.Nanopublication(store=o.graph.store) if assertion is not None: npub.pubinfo.add((npub.assertion.identifier, prov.wasRevisionOf, assertion)) npub.assertion.add((concept, sio.InverseDocumentFrequency, Literal(idf)))
def test_sio_import(self): # 20190807 CircleCI is having some difficulty fetching https URLs if os.environ.get("CI") == "true": return SIO_URL = "http://semanticscience.org/ontology/sio.owl" # Use the final URL instead # SIO_URL = "https://raw.githubusercontent.com/micheldumontier/semanticscience/master/ontology/sio/release/sio-release.owl" np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/testonto", "@type" : "http://www.w3.org/2002/07/owl#Ontology", "http://www.w3.org/2002/07/owl#imports":{"@id":"%(SIO_URL)s"} }''' % locals(), format="json-ld") agent = autonomic.OntologyImporter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 1) self.assertTrue(results[0].resource(URIRef(SIO_URL))[RDF.type:OWL.Ontology])
def test_prov_import(self): # 20190807 CircleCI is having some difficulty fetching https URLs if os.environ.get("CI") == "true": return np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/testonto", "@type" : "http://www.w3.org/2002/07/owl#Ontology", "http://www.w3.org/2002/07/owl#imports":{"@id":"http://www.w3.org/ns/prov#"} }''', format="json-ld") #print(np.serialize(format="trig")) agent = autonomic.OntologyImporter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 1) self.assertTrue(len(results[0]) > 0) self.assertTrue(results[0].resource(URIRef('http://www.w3.org/ns/prov#'))[RDF.type:OWL.Ontology])
def test_no_value(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:14626f9a927b4e7a980f802676b42402", "@type": [ "http://nanomine.org/ns/AC_DielectricDispersion" ] } ] }''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 0)
def test_already_converted(self): np = nanopub.Nanopublication() np.assertion.parse(data='''{ "@id": "http://example.com/converter_test", "@type": [ "http://nanomine.org/ns/PolymerNanocomposite" ], "http://semanticscience.org/resource/hasAttribute": [ { "@id": "bnode:0", "@type": [ "http://nanomine.org/ns/Width" ], "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://nanomine.org/ns/unit/nm", "http://www.w3.org/2000/01/rdf-schema#label": [ {"@value": "Nanometer"} ] } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 50} ] }, { "@id": "bnode:1", "@type": [ "http://nanomine.org/ns/Width" ], "http://semanticscience.org/resource/hasUnit": [ { "@id": "http://www.ontology-of-units-of-measure.org/resource/om-2/micrometre" } ], "http://semanticscience.org/resource/hasValue": [ {"@value": 0.05} ], "http://www.w3.org/ns/prov#wasDerivedFrom": [ {"@id": "bnode:0"} ] } ] }''', format="json-ld") np.assertion.parse(data='''{ "@id": "http://nanomine.org/ns/Width", "http://nanomine.org/ns/hasPreferredUnit": "http://www.ontology-of-units-of-measure.org/resource/om-2/micrometre" }''', format="json-ld") # print(np.serialize(format="trig")) agent = converter.UnitConverter() results = self.run_agent(agent, nanopublication=np) self.assertEquals(len(results), 0)
def test_basic_sdd(self): self.dry_run = False np = nanopub.Nanopublication() np.assertion.parse(data=test_sdd_rdf, format="turtle") #print(np.serialize(format="trig")) agent = autonomic.SDDAgent() nanopubs = self.app.nanopub_manager.prepare(np) self.app.nanopub_manager.publish(*nanopubs) results = self.run_agent(agent) nanopubs = list( self.app.db.query( ''' select distinct ?np where {?np a np:Nanopublication}''', initNs=NS.prefixes)) self.assertEqual(len(nanopubs), 4) invocations = list( self.app.db.query( '''select distinct ?sdd_file ?dataset ?data_file where { ?setl_script prov:wasDerivedFrom ?sdd_file; a setl:SemanticETLScript. ?dataset prov:wasGeneratedBy ?setl_script; prov:wasGeneratedBy ?template. ?template a setl:Transform; prov:used/prov:wasGeneratedBy ?extract. ?extract a setl:Extract; prov:used ?data_file. }''', initBindings=dict( sdd_file=rdflib.URIRef( 'https://raw.githubusercontent.com/tetherless-world/SemanticDataDictionary/master/ExampleProject/example_sdd.xlsx' ), data_file=rdflib .URIRef( 'https://raw.githubusercontent.com/tetherless-world/SemanticDataDictionary/master/ExampleProject/input/Data/exampleData.csv' )), initNs=NS.prefixes)) self.assertEqual(len(invocations), 1)