def parseRdf(text: str, contentType: str): g = Graph() g.parse(StringInputSource(text), format={ 'text/n3': 'n3', }[contentType]) return g
def dmrs_to_rdf(d, prefix: str, identifier, iname="dmrsi#dmrs", graph=None, out=None, text=None, format="turtle"): """ Parses a pydelphin DMRS into RDF representation. d - a delphin DMRS instance to be parsed into RDF format. prefix - the IRI to be prefixed to the RDF formated DMRS. identifier - an string or a list of strings identifying the DMRS. It should be unique, possibly using a composite identifier, given in list. For instance one may use it as [textid, dmrs-id] if the same text admits various mrs interpretations. iname - the dmrs instance name (the dmrs as RDF node name) to be used. As default, it is "dmrsi#dmrs". graph - and rdflib graph. If given, uses it to store the dmrs as RDF representation. out - filename to serialize the output into. text - the text that is represented in dmrs as RDF. format - file format to serialize the output into. """ if not graph: graph = Graph() if type(identifier) == list: identifier = "/".join(identifier) namespace = prefix + "/" + identifier + "/" dmrsi = URIRef(namespace + iname) graph.add((dmrsi, RDF.type, DMRS.DMRS)) NODES = Namespace(namespace + "nodes#") LINKS = Namespace(namespace + "links#") __nodes_to_rdf__(d, graph, dmrsi, NODES) #Adding top graph.add((dmrsi, DMRS['hasTop'], NODES["node{}".format(d.top)])) #Adding index graph.add((dmrsi, DMRS['hasTop'], NODES["node{}".format(d.index)])) __links_to_rdf__(d, graph, dmrsi, NODES, LINKS) # add text as one graph node if it's given if text: graph.add((dmrsi, DMRS.text, Literal(text))) # serializes graph if given an output file if out: graph.serialize(destination=out, format=format) return graph
def setUp(self): self.graph = Graph(store="SPARQLStore") self.graph.open(self.path, create=self.create) ns = list(self.graph.namespaces()) assert len(ns) > 0, ns
import hydra.tpf # ensures that the TPFStore plugin is registered from rdflib import Graph g = Graph('TPFStore') g.open('http://data.linkeddatafragments.org/dbpedia2014') results = g.query("SELECT DISTINCT ?cls { [ a ?cls ] } LIMIT 10") print(results)
def comunicacion(): """ Entrypoint de comunicacion del agente Simplemente retorna un objeto fijo que representa una respuesta a una busqueda de hotel Asumimos que se reciben siempre acciones que se refieren a lo que puede hacer el agente (buscar con ciertas restricciones, reservar) Las acciones se mandan siempre con un Request Prodriamos resolver las busquedas usando una performativa de Query-ref """ global dsgraph global mss_cnt logger.info('Peticion de informacion recibida') # Extraemos el mensaje y creamos un grafo con el message = request.args['content'] gm = Graph() gm.parse(data=message) msgdic = get_message_properties(gm) # Comprobamos que sea un mensaje FIPA ACL if msgdic is None: # Si no es, respondemos que no hemos entendido el mensaje gr = build_message(Graph(), ACL['not-understood'], sender=InfoAgent.uri, msgcnt=mss_cnt) else: # Obtenemos la performativa perf = msgdic['performative'] if perf != ACL.request: # Si no es un request, respondemos que no hemos entendido el mensaje gr = build_message(Graph(), ACL['not-understood'], sender=InfoAgent.uri, msgcnt=mss_cnt) else: # Extraemos el objeto del contenido que ha de ser una accion de la ontologia de acciones del agente # de registro # Averiguamos el tipo de la accion if 'content' in msgdic: content = msgdic['content'] accion = gm.value(subject=content, predicate=RDF.type) # Aqui realizariamos lo que pide la accion # Por ahora simplemente retornamos un Inform-done gr = build_message( Graph(), ACL['inform-done'], sender=InfoAgent.uri, msgcnt=mss_cnt, receiver=msgdic['sender'], ) mss_cnt += 1 logger.info('Respondemos a la peticion') return gr.serialize(format='xml')
def makeTestFile(filename, params): global test_person_uuid, test_p2_subject_uuids nidm_project_name = params.get('NIDM_PROJECT_NAME', False) or "Project_name_sample" nidm_project_identifier = params.get('NIDM_PROJECT_IDENTIFIER', False) or 9610 nidm_project2_identifier = params.get('NIDM_PROJECT_IDENTIFIER', False) or 550 nidm_project_description = params.get('NIDM_PROJECT_DESCRIPTION', False) or "1234356 Test investigation" project_uuid = params.get('PROJECT_UUID', False) or "_proj1" project_uuid2 = params.get('PROJECT2_UUID', False) or "_proj2" session_uuid = params.get('SESSION_UUID', False) or "_ses1" session_uuid2 = params.get('SESSION2_UUID', False) or "_ses2" p1kwargs={Constants.NIDM_PROJECT_NAME:nidm_project_name, Constants.NIDM_PROJECT_IDENTIFIER:nidm_project_identifier, Constants.NIDM_PROJECT_DESCRIPTION:nidm_project_description} p2kwargs={Constants.NIDM_PROJECT_NAME:nidm_project_name, Constants.NIDM_PROJECT_IDENTIFIER:nidm_project2_identifier, Constants.NIDM_PROJECT_DESCRIPTION:nidm_project_description} project = Project(uuid=project_uuid,attributes=p1kwargs) session = Session(uuid=session_uuid,project=project) acq = Acquisition(uuid="_acq1",session=session) acq2 = Acquisition(uuid="_acq2",session=session) acq3 = Acquisition(uuid="_acq2",session=session) person=acq.add_person(attributes=({Constants.NIDM_SUBJECTID:"a1_9999"})) test_person_uuid = (str(person.identifier)).replace("niiri:", "") acq.add_qualified_association(person=person,role=Constants.NIDM_PARTICIPANT) person2=acq2.add_person(attributes=({Constants.NIDM_SUBJECTID:"a1_8888"})) acq2.add_qualified_association(person=person2,role=Constants.NIDM_PARTICIPANT) person3=acq3.add_person(attributes=({Constants.NIDM_SUBJECTID:"a2_7777"})) acq2.add_qualified_association(person=person3,role=Constants.NIDM_PARTICIPANT) project2 = Project(uuid=project_uuid2,attributes=p2kwargs) session2 = Session(uuid=session_uuid2,project=project2) acq4 = Acquisition(uuid="_acq3",session=session2) acq5 = Acquisition(uuid="_acq4",session=session2) person4=acq4.add_person(attributes=({Constants.NIDM_SUBJECTID:"a3_6666"})) acq4.add_qualified_association(person=person4,role=Constants.NIDM_PARTICIPANT) person5=acq5.add_person(attributes=({Constants.NIDM_SUBJECTID:"a4_5555"})) acq5.add_qualified_association(person=person5,role=Constants.NIDM_PARTICIPANT) # now add some assessment instrument data addData(acq,{Constants.NIDM_AGE:9, Constants.NIDM_HANDEDNESS: "R", Constants.NIDM_DIAGNOSIS: "Anxiety"}) addData(acq2,{Constants.NIDM_AGE:8, Constants.NIDM_HANDEDNESS: "L", Constants.NIDM_DIAGNOSIS: "ADHD"}) addData(acq4,{Constants.NIDM_AGE:7, Constants.NIDM_HANDEDNESS: "A", Constants.NIDM_DIAGNOSIS: "Depression"}) addData(acq5,{Constants.NIDM_AGE:6, Constants.NIDM_HANDEDNESS: "R", Constants.NIDM_DIAGNOSIS: "Depression"}) test_p2_subject_uuids.append( (str(person4.identifier)).replace("niiri:", "") ) test_p2_subject_uuids.append( (str(person5.identifier)).replace("niiri:", "") ) with open("a.ttl",'w') as f: f.write(project.graph.serialize(None, format='rdf', rdf_format='ttl')) with open("b.ttl",'w') as f: f.write(project2.graph.serialize(None, format='rdf', rdf_format='ttl')) #create empty graph graph=Graph() for nidm_file in ("a.ttl", "b.ttl"): tmp = Graph() graph = graph + tmp.parse(nidm_file,format=util.guess_format(nidm_file)) graph.serialize(filename, format='turtle') os.unlink("a.ttl") os.unlink("b.ttl") with open(filename, "r") as f: x = f.read() with open("./agent.ttl", "w") as f: f.write(x)
def toGraph(tg): rval = Graph() for t in tg: rval.add(t) return rval
from rdflib import Graph, plugin from rdflib.serializer import Serializer testrdf = ''' @prefix dc: <http://purl.org/dc/terms/> . <http://example.org/about> dc:title "Someone's Homepage"@en . ''' g = Graph().parse(data=testrdf, format='json-ld') print(g.serialize(format='json-ld', indent=4))
def _delete_graph(ontology_name): store = _get_store() graph_iri = "%s/%s" % (SPARQL_GRAPH, ontology_name) print("graph:" + graph_iri) graph = Graph(store=store, identifier=graph_iri) store.remove_graph(graph)
def __init__(self, graphid): self._index = search.Index(name=graphid) self._graph = Graph(store=connect(identifier=graphid))
def get_countries(): f = open("playlists.json", "r") playlists = json.loads(f.read()) f.close() f = open("songs.json", "r") songs = json.loads(f.read()) f.close() country_graph = Graph() info_graph = Graph() dbo = Namespace("http://dbpedia.org/ontology/") dbp = Namespace("http://dbpedia.org/property/") mc = Namespace("http://localhost:3030/mc") for playlist in playlists["playlists"]: genres = [] durations = [] tracks = [] popular_tracks = [] popular_genres = [] try: country = pycountry.countries.get(name=playlist["country"]).alpha_2 except KeyError: try: country = pycountry.countries.get( common_name=playlist["country"]).alpha_2 except KeyError: country = playlist["country"] print(playlist["country"]) country_graph.load("http://dbpedia.org/resource/" + playlist["country"].replace(" ", "_")) for song in songs["songs"]: if song["country"] == country: for genre in song["genres"]: already_added = False for gen in genres: if genre == gen: already_added = True gen["count"] += 1 break if not already_added: genres.append({"genre": genre, "count": 1}) durations.append(song["duration"]) if len(genres) > 5: for i in range(5): current_largest = genres[0] current_largest_index = 0 counter = 0 for genre_instance in genres: if current_largest["count"] < genre_instance["count"]: current_largest = genre_instance current_largest_index = counter counter += 1 popular_genres.append(current_largest["genre"]) genres[current_largest_index]["count"] = -1 for s, p, o in country_graph.triples( (None, dbo.populationDensity, None)): info_graph.add([s, p, o]) for s, p, o in country_graph.triples( (None, dbo.populationDensityRank, None)): info_graph.add([s, p, o]) for s, p, o in country_graph.triples((None, dbp.gdpPpp, None)): info_graph.add([s, p, o]) for s, p, o in country_graph.triples( (None, dbp.gdpPppPerCapita, None)): info_graph.add([s, p, o]) for s, p, o in country_graph.triples((None, dbp.gini, None)): info_graph.add([s, p, o]) for s, p, o in country_graph.triples((None, dbp.hdi, None)): info_graph.add([s, p, o]) for s, p, o in country_graph.triples((None, dbp.hdiRank, None)): info_graph.add([s, p, o]) average_duration = sum(durations) / len(durations) country_node = URIRef("http://dbpedia.org/resource/" + playlist["country"].replace(" ", "_")) info_graph.add([country_node, mc.Duration, Literal(average_duration)]) info_graph.add([country_node, mc.Code, Literal(country.lower())]) for genre in popular_genres: info_graph.add([country_node, mc.Genre, Literal(genre)]) for track in tracks: info_graph.add([country_node, mc.Song, Literal(track)]) f = open("db.rdf", "w") f.write(str(info_graph.serialize())) f.close()
from rdflib import Graph g = Graph().parse("../dataset.ttl", format="turtle") print(len(g))
from rdflib.term import bind if __name__ == '__main__': # complex numbers are not registered by default # no custom constructor/serializer needed since # complex('(2+3j)') works fine bind(XSD.complexNumber, complex) ns = Namespace("urn:my:namespace:") c = complex(2, 3) l = Literal(c) g = Graph() g.add((ns.mysubject, ns.myprop, l)) n3 = g.serialize(format='n3') # round-trip through n3 g2 = Graph() g2.parse(data=n3, format='n3') l2 = list(g2)[0][2] print(l2) print(l2.value == c) # back to a python complex object
def as_arche_res(res, res_type='Resource', arche_prop=False): g = Graph() try: sub = URIRef(get_p4d_id(res, arche_prop=arche_prop)) except: sub = URIRef(get_arche_id(res, )) # sub = URIRef(get_arche_id(res,)) g.add((sub, RDF.type, acdh_ns[res_type])) g.add((sub, acdh_ns.hasDescription, Literal(get_arche_desc(res), lang=ARCHE_LANG))) # g.add((sub, acdh_ns.isPartOf, URIRef(directory_to_col_id(res)))) # if res_type == 'Collection': # pass # else: # g.add((sub, acdh_ns.hasCategory, URIRef(get_category(res)))) for x in get_arche_fields(res): cur_val = x['value'] arche_prop = x['extra_fields']['arche_prop'].strip() arche_prop_domain = ARCHE_PROPS_LOOKUP.get(arche_prop, 'No Match') if arche_prop == 'hasAccessRestriction': if cur_val.pref_label.startswith( 'publ') or cur_val.pref_label.startswith('open'): g.add(( sub, acdh_ns[arche_prop], URIRef( "https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/public" ))) else: g.add( (sub, acdh_ns[arche_prop], URIRef(FC_DEFAULT_ACCESS_RES))) elif arche_prop_domain == 'string': arche_prop_str_template = x['extra_fields'].get( 'arche_prop_str_template', None) if arche_prop_str_template: cur_val = arche_prop_str_template.replace( '<value>', f"{cur_val}") g.add((sub, acdh_ns[arche_prop], Literal(cur_val, lang=ARCHE_LANG))) elif arche_prop_domain == 'date': g.add((sub, acdh_ns[arche_prop], Literal(cur_val, datatype=XSD.date))) else: if isinstance(cur_val, QuerySet): for obj in cur_val: if obj is not None: try: object_uri = obj.canonic_arche_uri if object_uri != "": g.add((sub, acdh_ns[arche_prop], URIRef(object_uri))) except AttributeError: g.add((sub, acdh_ns[arche_prop], URIRef(get_arche_id(obj)))) else: if cur_val is not None: try: object_uri = cur_val.canonic_arche_uri print(object_uri) if object_uri != "": g.add( (sub, acdh_ns[arche_prop], URIRef(object_uri))) else: g.add((sub, acdh_ns[arche_prop], URIRef(get_arche_id(cur_val)))) except AttributeError: g.add((sub, acdh_ns[arche_prop], URIRef(get_arche_id(cur_val)))) for const in ARCHE_CONST_MAPPINGS: arche_prop_domain = ARCHE_PROPS_LOOKUP.get(const[0], 'No Match') if arche_prop_domain == 'date': g.add((sub, acdh_ns[const[0]], Literal(const[1], datatype=XSD.date))) else: g.add((sub, acdh_ns[const[0]], URIRef(const[1]))) return g
def setUp(self): """Set up test fixtures.""" self.graph = Graph()
def _get_graph_from_file(_file): graph = Graph() graph.parse(source=_file, format="n3") return graph
def make_default_graph(self): return Graph()
def graph(): """ returns the NFBStore :return: a Graph() instance """ return Graph(store=NDBStore(identifier=_GRAPH_ID))
def __init__(self, file_path_dict): self.file_path_dict = file_path_dict self.g = Graph() # sub: Object(SubBasics) self.sub_dict = {}
def register(): """ Entry point del agente que recibe los mensajes de registro La respuesta es enviada al retornar la funcion, no hay necesidad de enviar el mensaje explicitamente Asumimos una version simplificada del protocolo FIPA-request en la que no enviamos el mesaje Agree cuando vamos a responder :return: """ def process_register(): # Si la hay extraemos el nombre del agente (FOAF.Name), el URI del agente # su direccion y su tipo logger.info('Peticion de registro') agn_add = gm.value(subject=content, predicate=DSO.Address) agn_name = gm.value(subject=content, predicate=FOAF.Name) agn_uri = gm.value(subject=content, predicate=DSO.Uri) agn_type = gm.value(subject=content, predicate=DSO.AgentType) # Añadimos la informacion en el grafo de registro vinculandola a la URI # del agente y registrandola como tipo FOAF.Agent dsgraph.add((agn_uri, RDF.type, FOAF.Agent)) dsgraph.add((agn_uri, FOAF.name, agn_name)) dsgraph.add((agn_uri, DSO.Address, agn_add)) dsgraph.add((agn_uri, DSO.AgentType, agn_type)) logger.info('Registrado agente: ' + agn_name + ' - tipus:' + agn_type) # Generamos un mensaje de respuesta return build_message(Graph(), ACL.confirm, sender=DirectoryAgent.uri, receiver=agn_uri, msgcnt=mss_cnt) def process_search(): # Asumimos que hay una accion de busqueda que puede tener # diferentes parametros en funcion de si se busca un tipo de agente # o un agente concreto por URI o nombre # Podriamos resolver esto tambien con un query-ref y enviar un objeto de # registro con variables y constantes # Solo consideramos cuando Search indica el tipo de agente # Buscamos una coincidencia exacta # Retornamos el primero de la lista de posibilidades logger.info('Peticion de busqueda') agn_type = gm.value(subject=content, predicate=DSO.AgentType) rsearch = dsgraph.triples((None, DSO.AgentType, agn_type)) if rsearch is not None: agn_uri = rsearch.next()[0] agn_add = dsgraph.value(subject=agn_uri, predicate=DSO.Address) agn_name = dsgraph.value(subject=agn_uri, predicate=FOAF.name) gr = Graph() gr.bind('dso', DSO) rsp_obj = agn['Directory-response'] gr.add((rsp_obj, DSO.Address, agn_add)) gr.add((rsp_obj, DSO.Uri, agn_uri)) gr.add((rsp_obj, FOAF.name, agn_name)) logger.info("Agente encontrado: " + agn_name) return build_message(gr, ACL.inform, sender=DirectoryAgent.uri, msgcnt=mss_cnt, receiver=agn_uri, content=rsp_obj) else: # Si no encontramos nada retornamos un inform sin contenido return build_message(Graph(), ACL.inform, sender=DirectoryAgent.uri, msgcnt=mss_cnt) global dsgraph global mss_cnt # Extraemos el mensaje y creamos un grafo con él message = request.args['content'] gm = Graph() gm.parse(data=message) msgdic = get_message_properties(gm) # Comprobamos que sea un mensaje FIPA ACL if not msgdic: # Si no es, respondemos que no hemos entendido el mensaje gr = build_message(Graph(), ACL['not-understood'], sender=DirectoryAgent.uri, msgcnt=mss_cnt) else: # Obtenemos la performativa if msgdic['performative'] != ACL.request: # Si no es un request, respondemos que no hemos entendido el mensaje gr = build_message(Graph(), ACL['not-understood'], sender=DirectoryAgent.uri, msgcnt=mss_cnt) else: # Extraemos el objeto del contenido que ha de ser una accion de la ontologia # de registro content = msgdic['content'] # Averiguamos el tipo de la accion accion = gm.value(subject=content, predicate=RDF.type) # Accion de registro if accion == DSO.Register: gr = process_register() # Accion de busqueda elif accion == DSO.Search: gr = process_search() # No habia ninguna accion en el mensaje else: gr = build_message(Graph(), ACL['not-understood'], sender=DirectoryAgent.uri, msgcnt=mss_cnt) mss_cnt += 1 return gr.serialize(format='xml')
def main(): '''Converts Codelist XMLs to Turtle files and stores these to local folder.''' # Settings xml_folder = "/media/Acer/School/IATI-data/xml/codelists/" turtle_folder = "/media/Acer/School/IATI-data/codelist/" Iati = Namespace("http://purl.org/collections/iati/") if not os.path.isdir(turtle_folder): os.makedirs(turtle_folder) document_count = 1 total_elapsed_time = 0 # Retrieve XML files from the XML folder for document in glob.glob(xml_folder + '*.xml'): doc_id = str(document.rsplit('/', 1)[1])[:-4] doc_folder = turtle_folder + doc_id + '/' if not os.path.isdir(doc_folder): os.makedirs(doc_folder) provenance = Graph() provenance.bind('iati', Iati) xml = ET.parse(document) root = xml.getroot() version = AttributeHelper.attribute_key(root, 'version') try: # Convert each codelist in XML file to RDFLib Graph converter = IatiConverter.ConvertCodelist(root) graph, id, last_updated = converter.convert(Iati) except TypeError as e: print "Error in " + document + ":" + str(e) graph = None if not graph == None: # Write codelist to Turtle and store in local folder graph_turtle = graph.serialize(format='turtle') with open(doc_folder + id.replace('/', '%2F') + '.ttl', 'w') as turtle_file: turtle_file.write(graph_turtle) # Add provenance of last-updated, version and source document provenance.add( (URIRef(Iati + 'graph/codelist/' + str(id)), URIRef(Iati + 'last-updated'), Literal(last_updated))) provenance.add((URIRef(Iati + 'graph/codelist/' + str(id)), URIRef(Iati + 'version'), Literal(version))) provenance.add( (URIRef(Iati + 'graph/codelist/' + str(id)), URIRef(Iati + 'source-document-id'), Literal(str(id)))) provenance.add( (URIRef(Iati + 'graph/codelist/' + str(id)), URIRef(Iati + 'source-document-download-url'), URIRef('http://datadev.aidinfolabs.org/data/codelist/' + str(id) + '.xml'))) print "Progress: Document #" + str(document_count) document_count += 1 # Add prov model start_time = datetime.datetime.now() source_xml = str('http://datadev.aidinfolabs.org/data/codelist/' + str(id) + '.xml') entities = [] entities.append(str(id)) script = "conversion%20scripts/CodelistsToTurtle.py" provenance = AddProvenance.addProv(Iati, provenance, 'codelist', doc_id, start_time, source_xml, entities, script) # Write provenance graph to Turtle and store in local folder provenance_turtle = provenance.serialize(format='turtle') with open(doc_folder + 'provenance-' + str(id) + '.ttl', 'w') as turtle_file: turtle_file.write(provenance_turtle) print "Done!"
def main(): import argparse parser = argparse.ArgumentParser( prog='ants_seg_to_nidm.py', description='''This program will load in the ReproNim-style ANTS brain segmentation outputs, augment the ANTS anatomical region designations with common data element anatomical designations, and save the statistics + region designations out as NIDM serializations (i.e. TURTLE, JSON-LD RDF)''', formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( '-f', '--ants_stats', dest='stats_files', required=True, type=str, help='''A comma separated string of paths to: ANTS \"lablestats\" CSV file (col 1=Label number, col 2=VolumeinVoxels), ANTS \"brainvols\" CSV file (col 2=BVOL, col 3=GVol, col 4=WVol), MRI or Image file to extract voxel sizes OR A comma separated string of path \n OR URLs to a ANTS segmentation files and an image file for voxel sizes: /..../antslabelstats,/..../antsbrainvols,/..../mri_file. \n Note, currently this is tested on ReproNim data''') parser.add_argument( '-subjid', '--subjid', dest='subjid', required=False, help='If a path to a URL or a stats file' 'is supplied via the -f/--seg_file parameters then -subjid parameter must be set with' 'the subject identifier to be used in the NIDM files') parser.add_argument('-o', '--output', dest='output_dir', type=str, help='Output filename with full path', required=True) parser.add_argument( '-j', '--jsonld', dest='jsonld', action='store_true', default=False, help= 'If flag set then NIDM file will be written as JSONLD instead of TURTLE' ) parser.add_argument( '-add_de', '--add_de', dest='add_de', action='store_true', default=None, help= 'If flag set then data element data dictionary will be added to nidm file else it will written to a' 'separate file as ants_cde.ttl in the output directory (or same directory as nidm file if -n paramemter' 'is used.') parser.add_argument('-n', '--nidm', dest='nidm_file', type=str, required=False, help='Optional NIDM file to add segmentation data to.') parser.add_argument( '-forcenidm', '--forcenidm', action='store_true', required=False, help= 'If adding to NIDM file this parameter forces the data to be added even if the participant' 'doesnt currently exist in the NIDM file.') args = parser.parse_args() # test whether user supplied stats file directly and if so they the subject id must also be supplied so we # know which subject the stats file is for if (args.stats_files and (args.subjid is None)): parser.error( "-f/--ants_urls and -d/--ants_stats requires -subjid/--subjid to be set!" ) # if output_dir doesn't exist then create it out_path = os.path.dirname(args.output_dir) if not os.path.exists(out_path): os.makedirs(out_path) # WIP: ANTS URL forms as comma spearated string in args.stats_urls: # 1: https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE/Outputs/mindboggle_swf/mindboggle/ants_subjects/sub-0050002/antslabelstats.csv # 2: https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE/Outputs/mindboggle_swf/mindboggle/ants_subjects/sub-0050002/antsbrainvols.csv # 3: https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE/Outputs/mindboggle_swf/mindboggle/ants_subjects/sub-0050002/antsBrainSegmentation.nii.gz # split input string argument into the 3 URLs above url_list = args.stats_files.split(',') # check url 1, the labelstats.csv url = url_validator(url_list[0]) # if user supplied a url as a segfile if url is not False: #try to open the url and get the pointed to file...for labelstats file try: #open url and get file opener = ur.urlopen(url_list[0]) # write temporary file to disk and use for stats temp = tempfile.NamedTemporaryFile(delete=False) temp.write(opener.read()) temp.close() labelstats = temp.name except: print("ERROR! Can't open url: %s" % url) exit() #try to open the url and get the pointed to file...for brainvols file try: #open url and get file opener = ur.urlopen(url_list[1]) # write temporary file to disk and use for stats temp = tempfile.NamedTemporaryFile(delete=False) temp.write(opener.read()) temp.close() brainvol = temp.name except: print("ERROR! Can't open url: %s" % url) exit() #try to open the url and get the pointed to file...for brainvols file try: #open url and get file opener = ur.urlopen(url_list[2]) # write temporary file to disk and use for stats temp = tempfile.NamedTemporaryFile(delete=False, suffix=".nii.gz") temp.write(opener.read()) temp.close() imagefile = temp.name except: print("ERROR! Can't open url: %s" % url) exit() # else these must be a paths to the stats files else: # split input string argument into the 3 URLs above file_list = args.stats_files.split(',') labelstats = file_list[0] brainvol = file_list[1] imagefile = file_list[2] measures = read_ants_stats(labelstats, brainvol, imagefile) [e, doc] = convert_stats_to_nidm(measures) g = create_cde_graph() # convert nidm stats graph to rdflib g2 = Graph() g2.parse(source=StringIO(doc.serialize(format='rdf', rdf_format='turtle')), format='turtle') # for measures we need to create NIDM structures using anatomy mappings # If user has added an existing NIDM file as a command line parameter then add to existing file for subjects who exist in the NIDM file if args.nidm_file is None: print("Creating NIDM file...") # name output NIDM file by subjid # args.subjid + "_" + [everything after the last / in the first supplied URL] output_filename = args.subjid + "_NIDM" # If user did not choose to add this data to an existing NIDM file then create a new one for the CSV data if args.add_de is not None: nidmdoc = g + g2 else: nidmdoc = g2 # print(nidmdoc.serializeTurtle()) # add seg data to new NIDM file add_seg_data(nidmdoc=nidmdoc, subjid=args.subjid, stats_entity_id=e.identifier) #serialize NIDM file print("Writing NIDM file...") if args.jsonld is not False: # nidmdoc.serialize(destination=join(args.output_dir,output_filename +'.json'),format='jsonld') nidmdoc.serialize(destination=join(args.output_dir), format='jsonld') else: # nidmdoc.serialize(destination=join(args.output_dir,output_filename +'.ttl'),format='turtle') nidmdoc.serialize(destination=join(args.output_dir), format='turtle') # added to support separate cde serialization if args.add_de is None: # serialize cde graph g.serialize(destination=join(dirname(args.output_dir), "ants_cde.ttl"), format='turtle') #nidmdoc.save_DotGraph(join(args.output_dir,output_filename + ".pdf"), format="pdf") # we adding these data to an existing NIDM file else: #read in NIDM file with rdflib print("Reading in NIDM graph....") g1 = Graph() g1.parse(args.nidm_file, format=util.guess_format(args.nidm_file)) if args.add_de is not None: print("Combining graphs...") nidmdoc = g + g1 + g2 else: nidmdoc = g1 + g2 if args.forcenidm is not False: add_seg_data(nidmdoc=nidmdoc, subjid=args.subjid, stats_entity_id=e.identifier, add_to_nidm=True, forceagent=True) else: add_seg_data(nidmdoc=nidmdoc, subjid=args.subjid, stats_entity_id=e.identifier, add_to_nidm=True) #serialize NIDM file print("Writing Augmented NIDM file...") if args.jsonld is not False: nidmdoc.serialize(destination=args.nidm_file + '.json', format='jsonld') else: nidmdoc.serialize(destination=args.nidm_file, format='turtle') if args.add_de is None: # serialize cde graph g.serialize(destination=join(dirname(args.output_dir), "ants_cde.ttl"), format='turtle')
# Contador de mensajes mss_cnt = 0 # Datos del Agente InfoAgent = Agent('AgenteInfo1', agn.AgenteInfo, 'http://%s:%d/comm' % (hostname, port), 'http://%s:%d/Stop' % (hostname, port)) # Directory agent address DirectoryAgent = Agent('DirectoryAgent', agn.Directory, 'http://%s:%d/Register' % (dhostname, dport), 'http://%s:%d/Stop' % (dhostname, dport)) # Global dsgraph triplestore dsgraph = Graph() # Cola de comunicacion entre procesos cola1 = Queue() def register_message(): """ Envia un mensaje de registro al servicio de registro usando una performativa Request y una accion Register del servicio de directorio :param gmess: :return: """
from rdflib.namespace import RDF, FOAF, SKOS, RDFS import os os.chdir('C:\Users\Maral\Desktop') #sparql = SPARQLWrapper2("http://localhost:3030/Datasets/sparql") foaf = Namespace("http://xmlns.com/foaf/0.1/") skos = Namespace("http://www.w3.org/2004/02/skos/core#") gndo = Namespace("http://d-nb.info/standards/elementset/gnd#") jl = Namespace("http://data.judaicalink.org/ontology/") owl = Namespace("http://www.w3.org/2002/07/owl#") edm = Namespace("http://www.europeana.eu/schemas/edm/") dc = Namespace("http://purl.org/dc/elements/1.1/") graph = Graph() graph.parse('C:\\Users\\Maral\\Desktop\\bhr-new-enrich.rdf', format="turtle") dicsame = {} spar = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX gndo: <http://d-nb.info/standards/elementset/gnd#> PREFIX pro: <http://purl.org/hpi/patchr#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX edm: <http://www.europeana.eu/schemas/edm/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
def graph_from_DOM(self, dom, graph=None, pgraph=None): """ Extract the RDF Graph from a DOM tree. This is where the real processing happens. All other methods get down to this one, eventually (e.g., after opening a URI and parsing it into a DOM). @param dom: a DOM Node element, the top level entry node for the whole tree (i.e., the C{dom.documentElement} is used to initiate processing down the node hierarchy) @keyword graph: an RDF Graph (if None, than a new one is created) @type graph: rdflib Graph instance. @keyword pgraph: an RDF Graph to hold (possibly) the processor graph content. If None, and the error/warning triples are to be generated, they will be added to the returned graph. Otherwise they are stored in this graph. @type pgraph: rdflib Graph instance @return: an RDF Graph @rtype: rdflib Graph instance """ def copyGraph(tog, fromg): for t in fromg: tog.add(t) for k, ns in fromg.namespaces(): tog.bind(k, ns) if graph == None: # Create the RDF Graph, that will contain the return triples... graph = Graph() # this will collect the content, the 'default graph', as called in the RDFa spec default_graph = Graph() # get the DOM tree topElement = dom.documentElement # Create the initial state. This takes care of things # like base, top level namespace settings, etc. state = ExecutionContext( topElement, default_graph, base=self.required_base if self.required_base != None else "", options=self.options, rdfa_version=self.rdfa_version) # Perform the built-in and external transformations on the HTML tree. for trans in self.options.transformers + builtInTransformers: trans(topElement, self.options, state) # This may have changed if the state setting detected an explicit version information: self.rdfa_version = state.rdfa_version # The top level subject starts with the current document; this # is used by the recursion # this function is the real workhorse parse_one_node(topElement, default_graph, None, state, []) # Massage the output graph in term of rdfa:Pattern and rdfa:copy handle_prototypes(default_graph) # If the RDFS expansion has to be made, here is the place... if self.options.vocab_expansion: from .rdfs.process import process_rdfa_sem process_rdfa_sem(default_graph, self.options) # Experimental feature: nothing for now, this is kept as a placeholder if self.options.experimental_features: pass # What should be returned depends on the way the options have been set up if self.options.output_default_graph: copyGraph(graph, default_graph) if self.options.output_processor_graph: if pgraph != None: copyGraph(pgraph, self.options.processor_graph.graph) else: copyGraph(graph, self.options.processor_graph.graph) elif self.options.output_processor_graph: if pgraph != None: copyGraph(pgraph, self.options.processor_graph.graph) else: copyGraph(graph, self.options.processor_graph.graph) # this is necessary if several DOM trees are handled in a row... self.options.reset_processor_graph() return graph
def __init__(self): self.base = None self.namespace_manager = NamespaceManager( Graph()) # ns man needs a store
def make_graph(): graph = Graph('IOMemory', BNode()) person = URIRef('http://example.com/#person') graph.add((person, RDF.type, FOAF.Person)) graph.add((person, FOAF.age, Literal(15, datatype=XSD.integer))) return graph
import namespace as ns from api_fx import vivo_api_query, uri_gen, email_lookup from namespace import * import csv import sys import unicodedata # Support Python 2 and 3 input_func = None try: input_func = raw_input except NameError: input_func = input # Instantiate a graph and namespace g = Graph(namespace_manager=ns.ns_manager) count = 0 newfile = open('addExpertise.ttl', 'wb') f = open('EarthCollab Fields of Study and Expertise.csv', 'r') row_count = sum(1 for row in f) - 1 # count the rows to update progress. f.seek(0) csv_f = csv.reader(f) next(csv_f, None) # skip the headers next(csv_f, None) # SurveyMonkey puts weird characters into the result document def remove_control_characters(s): return s.replace("\xa0", " ").encode('utf-8')
'dc': DC }) results = g.query(q, initBindings={'s': URIRef(uri)}) for row in results: prefLabel = row[0].encode('utf-8') date = row[1] global match match = [label, str(prefLabel), uri, date] os.chdir(directory) startTime = time.time() date = datetime.datetime.now().strftime('%Y-%m-%d %H.%M.%S') #import rdf file into graph g = Graph() g.parse(rdfFileName, format='n3') g.bind('skos', SKOS) #create dict of pref and alt labels from rdf file existingLabels = {} q = prepareQuery('SELECT ?s ?o WHERE { ?s skos:prefLabel|skos:altLabel ?o }', initNs={'skos': SKOS}) results = g.query(q) for row in results: existingLabels[str(row[1].encode('utf-8'))] = str(row[0]) #create lists and csv files completeNearMatches = [] completeExactMatches = [] f = csv.writer(
def get_graph_uri_for_vocab(vocab: Path, g: Graph = None) -> URIRef: """We can get the Graph URI for a vocab using assumption that the ConceptScheme is declared in the graph being processed.""" if not g: g = Graph().parse(str(vocab), format="ttl") for s in g.subjects(predicate=RDF.type, object=SKOS.ConceptScheme): yield str(s)