def test_construct_query(self): q = """ CONSTRUCT { v:tmp rdfs:label ?label } WHERE { <http://dbpedia.org/resource/Baseball> rdfs:label ?label ; <http://dbpedia.org/ontology/wikiPageID> ?wikiPageID } """ with self.assertRaises(sparql_dataframe.QueryException): sparql_dataframe.get(self.endpoint, q)
def test_get_integer(self): q = """ SELECT DISTINCT ?wikiPageID WHERE { <http://dbpedia.org/resource/Baseball> rdfs:label ?label ; <http://dbpedia.org/ontology/wikiPageID> ?wikiPageID } """ df = sparql_dataframe.get(endpoint, q) self.assertEqual(df['wikiPageID'].iloc[0], 3850)
def test_get_integer(self): q = self.prefixes + """ SELECT ?astronaut ?astronautLabel ?birthdate (YEAR(?birthdate) as ?year) ?birthplace WHERE { ?astronaut ?x1 wd:Q11631; wdt:P569 ?birthdate; wdt:P19 ?birthplace. SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } } ORDER BY DESC (?birthdate) LIMIT 10 """ df = sparql_dataframe.get(self.endpoint, q, post=True) self.assertEqual(df['year'].iloc[0], 1988)
def test_get_multiple(self): q = """ SELECT ?label ?wikiPageID WHERE { <http://dbpedia.org/resource/Baseball> rdfs:label ?label ; <http://dbpedia.org/ontology/wikiPageID> ?wikiPageID } """ df = sparql_dataframe.get(endpoint, q) # Make sure we get 5 or more labels. self.assertTrue(len(df) > 5) # Both variables should be of equal length. self.assertEqual(len(df['label']), len(df['wikiPageID']))
def test_get_multiple(self): q = self.prefixes + """ SELECT ?human ?humanLabel WHERE { ?human wdt:P31 wd:Q5 . #find humans ?human rdf:type wdno:P40 . #with at least one P40 (child) statement defined to be "no value" SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" } } LIMIT 10 """ df = sparql_dataframe.get(self.endpoint, q, post=True) # Make sure we get 5 or more labels. self.assertTrue(len(df) == 10) # Both variables should be of equal length. self.assertEqual(len(df['human']), len(df['humanLabel']))
def aqcheckindoor(): endpoint = ("http://<ip address>/repositories/UNIAQ") if request.method == "POST": post_code = request.form["postcodeID"] pollutant = request.form["pollutantID"] fromdate = request.form["fromdateID"] todate = request.form["todateID"] reading_limit = request.form["limitID"] q = (""" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX UNIAQ: <http://score-bradford.com/ontologies/UNIAQ.owl#> PREFIX : <http://score-bradford.com/ontologies/UNIAQ.owl#> select ?obs ?val ?uni ?time where { ?s UNIAQ:postcode \"""" + post_code + """\". ?s UNIAQ:observes ?obs. ?obs rdf:type UNIAQ:Observation. ?obs UNIAQ:pollutant UNIAQ:""" + pollutant + """. ?obs UNIAQ:value ?val. ?obs UNIAQ:unit ?uni. ?obs UNIAQ:timestamp ?time FILTER ((?time> \"""" + fromdate + """:00\"^^xsd:dateTime)&&(?time< \"""" + todate + """:00\"^^xsd:dateTime)) } limit """ + reading_limit + """ """) # Converting Results into Dataframes result = sparql_dataframe.get(endpoint, q, post=True) # Standard Deviation sdv = str(result.loc[:, "val"].std()) # Average avg = str(result.loc[:, "val"].mean()) # Count cnt = str(result.loc[:, "val"].count()) # Passing Value to HTML page for Display return render_template('result.html', value1=sdv, value2=avg, value3=cnt, value4=pollutant) # Front End form to get the variable entries from user return render_template('post_code.html')
def plot(): endpoint = ("http://<ip address>/repositories/INDOOR_TEST") if request.method == "POST": post_code = request.form["postcodeID"] pollutant = request.form["pollutantID"] fromdate = request.form["fromdateID"] todate = request.form["todateID"] reading_limit = request.form["limitID"] q = (""" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX UNIAQ: <http://score-bradford.com/ontologies/UNIAQ.owl#> PREFIX : <http://score-bradford.com/ontologies/UNIAQ.owl#> select ?val ?time where { ?s UNIAQ:postcode \"""" + post_code + """\". ?s UNIAQ:observes ?obs. ?obs rdf:type UNIAQ:Observation. ?obs UNIAQ:pollutant UNIAQ:""" + pollutant + """. ?obs UNIAQ:value ?val. ?obs UNIAQ:unit ?uni. ?obs UNIAQ:timestamp ?time FILTER ((?time> \"""" + fromdate + """:00\"^^xsd:dateTime)&&(?time< \"""" + todate + """:00\"^^xsd:dateTime)) } limit """ + reading_limit + """ """) result = sparql_dataframe.get(endpoint, q, post=True) img = BytesIO() y = result['val'] x = result['time'] plt.plot(y) plt.savefig(img, format='png') plt.close() img.seek(0) plot_url = base64.b64encode(img.getvalue()).decode('utf8') print(y) return render_template('aqplot.html', plot_url=plot_url) # Front End form to get the variable entries from user return render_template('post_code.html')
def test_bad_query(self): with self.assertRaises(SPARQLWrapper.SPARQLExceptions.QueryBadFormed): sparql_dataframe.get(self.endpoint, "SELECT ?label FROM {}")
}} """.format(language, abbre)) sparql.setReturnFormat(JSON) results = sparql.query().convert() results_df = pd.io.json.json_normalize(results['results']['bindings']) results_df[['doid.value', 'item.value', 'label.value', 'wpLang.value']].head() import sparql_dataframe endpoint = "https://query.wikidata.org/sparql" q = """ SELECT DISTINCT ?doid ?item ?label ?wpLang WHERE { ?item wdt:P699 ?doid ; rdfs:label ?label ; rdfs:label ?english . ?article schema:about ?item ; schema:inLanguage ?wpLang . FILTER (lang(?label) = ?wpLang) FILTER (lang(?english) = "en") } """ df = sparql_dataframe.get(endpoint, q) df
def retrieveData(self, cube, dims, meas, hierdims=[]): __doc__ = """ retrieveData - SPARQL query builder to retrieve data from SPARQL endpoint. ... Attributes -------------- cube: str Cube to retrieve data dims: list[str] list of Strings (dimension names) to retrieve meas: list[str] list of measures to retrieve hierdims: dict{hierdim:{"selected_level":[value]}} hierarchical dimension (if provided) to retrieve data from specific hierarchical level ... Output ------------ Pandas Dataframe """ skosInSchemeString = "<http://www.w3.org/2004/02/skos/core#inScheme>" queryString = "" queryTemplate = """%s""" selectString = "SELECT " groupByString = "GROUP BY " whereString = "WHERE \n {\n ?s ?p ?o. \n" hierarchyLevelString = "" i = 1 for dimension in dims: selectString += ("(str(?dimLabel" + str(i) + ") as ?" + self.metaDataDict[cube]["dimensions"][dimension] ["dimension_title"] + ") ") groupByString += ("?dimLabel" + str(i) + " ") whereString += "?s <" + self.metaDataDict[cube]["dimensions"][ dimension]["dimension_url"] + "> ?dim" + str( i) + " .\n ?dim" + str(i) + " rdfs:label ?dimLabel" + str( i) + ". \n" i = i + 1 for hierdimension in hierdims: selectString += ("(str(?dimLabel" + str(i) + ") as ?" + self.metaDataDict[cube]["hierarchical_dimensions"] [hierdimension]["dimension_title"] + ") ") groupByString += ("?dimLabel" + str(i) + " ") if (hierdims[hierdimension]["selected_level"]): hierarchyLevelString += "?dim" + str( i) + " " + skosInSchemeString + " <" + self.metaDataDict[ cube]["hierarchical_dimensions"][hierdimension][ "dimension_prefix"] + hierdims[hierdimension][ "selected_level"] + "> .\n" whereString += "?s <" + self.metaDataDict[cube][ "hierarchical_dimensions"][hierdimension][ "dimension_url"] + "> ?dim" + str(i) + " .\n ?dim" + str( i) + " rdfs:label ?dimLabel" + str(i) + ". \n" i = i + 1 i = 1 for measure in meas: selectString += ( " (SUM(?measure" + str(i) + ") as ?" + self.metaDataDict[cube]["measures"][measure]["measure_title"] + ") ") whereString += ( "?s <" + self.metaDataDict[cube]["measures"][measure]["measure_url"] + "> ?measure" + str(i) + " . \n") whereString += hierarchyLevelString + "} \n" queryString = selectString + whereString + groupByString queryTemplate = '''%s ''' sparqldata = sparql_dataframe.get(self.sparqlEndpoint, queryTemplate % (queryString)) return sparqldata
def queryMe(query): # print(query) endpoint = "http://<ip address>/repositories/INDOOR_TEST" df = sparql_dataframe.get(endpoint, query, post=True) meanvalue = mean(df) return meanvalue
def sq2df(qry_str): "sparql to df" import sparql_dataframe endpoint = "https://graph.geodex.org/blazegraph/namespace/cdf/sparql" q=q1+qs+q2 df = sparql_dataframe.get(endpoint, q)