|
33 | 33 | }, |
34 | 34 | { |
35 | 35 | "cell_type": "code", |
36 | | - "execution_count": null, |
| 36 | + "execution_count": 2, |
37 | 37 | "metadata": {}, |
38 | 38 | "outputs": [], |
39 | 39 | "source": [ |
|
66 | 66 | }, |
67 | 67 | { |
68 | 68 | "cell_type": "code", |
69 | | - "execution_count": null, |
| 69 | + "execution_count": 3, |
70 | 70 | "metadata": {}, |
71 | | - "outputs": [], |
| 71 | + "outputs": [ |
| 72 | + { |
| 73 | + "data": { |
| 74 | + "text/markdown": [ |
| 75 | + "🚀 Connecting to server at `http://128.178.219.51:7502`..." |
| 76 | + ], |
| 77 | + "text/plain": [ |
| 78 | + "<IPython.core.display.Markdown object>" |
| 79 | + ] |
| 80 | + }, |
| 81 | + "metadata": {}, |
| 82 | + "output_type": "display_data" |
| 83 | + }, |
| 84 | + { |
| 85 | + "data": { |
| 86 | + "text/markdown": [ |
| 87 | + "✅ **Connection successful!** The database is ready to be queried." |
| 88 | + ], |
| 89 | + "text/plain": [ |
| 90 | + "<IPython.core.display.Markdown object>" |
| 91 | + ] |
| 92 | + }, |
| 93 | + "metadata": {}, |
| 94 | + "output_type": "display_data" |
| 95 | + } |
| 96 | + ], |
72 | 97 | "source": [ |
73 | 98 | "display(Markdown(f\"🚀 Connecting to server at `{ENDPOINT_URL}`...\"))\n", |
74 | 99 | "\n", |
|
104 | 129 | }, |
105 | 130 | { |
106 | 131 | "cell_type": "code", |
107 | | - "execution_count": null, |
| 132 | + "execution_count": 4, |
108 | 133 | "metadata": {}, |
109 | | - "outputs": [], |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "data": { |
| 137 | + "text/markdown": [ |
| 138 | + "**Running query:** Counting all triples..." |
| 139 | + ], |
| 140 | + "text/plain": [ |
| 141 | + "<IPython.core.display.Markdown object>" |
| 142 | + ] |
| 143 | + }, |
| 144 | + "metadata": {}, |
| 145 | + "output_type": "display_data" |
| 146 | + }, |
| 147 | + { |
| 148 | + "data": { |
| 149 | + "text/markdown": [ |
| 150 | + "Total Triples in Database: **2000002**" |
| 151 | + ], |
| 152 | + "text/plain": [ |
| 153 | + "<IPython.core.display.Markdown object>" |
| 154 | + ] |
| 155 | + }, |
| 156 | + "metadata": {}, |
| 157 | + "output_type": "display_data" |
| 158 | + } |
| 159 | + ], |
110 | 160 | "source": [ |
111 | 161 | "# Example 1: Count all triples in the database\n", |
112 | 162 | "# This is a good way to see how much data you're working with.\n", |
|
162 | 212 | "\n", |
163 | 213 | "**Tip:** Don't forget to add `LIMIT 10` to your queries while you are exploring, so you don't accidentally try to print a million rows!" |
164 | 214 | ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": null, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [ |
| 221 | + { |
| 222 | + "data": { |
| 223 | + "text/markdown": [ |
| 224 | + "**Running query:** Getting 10 triples..." |
| 225 | + ], |
| 226 | + "text/plain": [ |
| 227 | + "<IPython.core.display.Markdown object>" |
| 228 | + ] |
| 229 | + }, |
| 230 | + "metadata": {}, |
| 231 | + "output_type": "display_data" |
| 232 | + }, |
| 233 | + { |
| 234 | + "data": { |
| 235 | + "text/html": [ |
| 236 | + "<div>\n", |
| 237 | + "<style scoped>\n", |
| 238 | + " .dataframe tbody tr th:only-of-type {\n", |
| 239 | + " vertical-align: middle;\n", |
| 240 | + " }\n", |
| 241 | + "\n", |
| 242 | + " .dataframe tbody tr th {\n", |
| 243 | + " vertical-align: top;\n", |
| 244 | + " }\n", |
| 245 | + "\n", |
| 246 | + " .dataframe thead th {\n", |
| 247 | + " text-align: right;\n", |
| 248 | + " }\n", |
| 249 | + "</style>\n", |
| 250 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 251 | + " <thead>\n", |
| 252 | + " <tr style=\"text-align: right;\">\n", |
| 253 | + " <th></th>\n", |
| 254 | + " <th>repoName</th>\n", |
| 255 | + " <th>disciplineIRI</th>\n", |
| 256 | + " <th>disciplineDescription</th>\n", |
| 257 | + " </tr>\n", |
| 258 | + " </thead>\n", |
| 259 | + " <tbody>\n", |
| 260 | + " <tr>\n", |
| 261 | + " <th>0</th>\n", |
| 262 | + " <td>Example Software 64899</td>\n", |
| 263 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 264 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 265 | + " </tr>\n", |
| 266 | + " <tr>\n", |
| 267 | + " <th>1</th>\n", |
| 268 | + " <td>Example Software 30239</td>\n", |
| 269 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 270 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 271 | + " </tr>\n", |
| 272 | + " <tr>\n", |
| 273 | + " <th>2</th>\n", |
| 274 | + " <td>Example Software 54959</td>\n", |
| 275 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 276 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 277 | + " </tr>\n", |
| 278 | + " <tr>\n", |
| 279 | + " <th>3</th>\n", |
| 280 | + " <td>Example Software 27819</td>\n", |
| 281 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 282 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 283 | + " </tr>\n", |
| 284 | + " <tr>\n", |
| 285 | + " <th>4</th>\n", |
| 286 | + " <td>Example Software 95309</td>\n", |
| 287 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 288 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 289 | + " </tr>\n", |
| 290 | + " <tr>\n", |
| 291 | + " <th>...</th>\n", |
| 292 | + " <td>...</td>\n", |
| 293 | + " <td>...</td>\n", |
| 294 | + " <td>...</td>\n", |
| 295 | + " </tr>\n", |
| 296 | + " <tr>\n", |
| 297 | + " <th>95</th>\n", |
| 298 | + " <td>Example Software 87999</td>\n", |
| 299 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 300 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 301 | + " </tr>\n", |
| 302 | + " <tr>\n", |
| 303 | + " <th>96</th>\n", |
| 304 | + " <td>Example Software 32009</td>\n", |
| 305 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 306 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 307 | + " </tr>\n", |
| 308 | + " <tr>\n", |
| 309 | + " <th>97</th>\n", |
| 310 | + " <td>Example Software 42979</td>\n", |
| 311 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 312 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 313 | + " </tr>\n", |
| 314 | + " <tr>\n", |
| 315 | + " <th>98</th>\n", |
| 316 | + " <td>Example Software 31979</td>\n", |
| 317 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 318 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 319 | + " </tr>\n", |
| 320 | + " <tr>\n", |
| 321 | + " <th>99</th>\n", |
| 322 | + " <td>Example Software 3439</td>\n", |
| 323 | + " <td>http://www.wikidata.org/entity/Q83404</td>\n", |
| 324 | + " <td>city and county seat in Timiș County, Romania</td>\n", |
| 325 | + " </tr>\n", |
| 326 | + " </tbody>\n", |
| 327 | + "</table>\n", |
| 328 | + "<p>100 rows × 3 columns</p>\n", |
| 329 | + "</div>" |
| 330 | + ], |
| 331 | + "text/plain": [ |
| 332 | + " repoName disciplineIRI \\\n", |
| 333 | + "0 Example Software 64899 http://www.wikidata.org/entity/Q83404 \n", |
| 334 | + "1 Example Software 30239 http://www.wikidata.org/entity/Q83404 \n", |
| 335 | + "2 Example Software 54959 http://www.wikidata.org/entity/Q83404 \n", |
| 336 | + "3 Example Software 27819 http://www.wikidata.org/entity/Q83404 \n", |
| 337 | + "4 Example Software 95309 http://www.wikidata.org/entity/Q83404 \n", |
| 338 | + ".. ... ... \n", |
| 339 | + "95 Example Software 87999 http://www.wikidata.org/entity/Q83404 \n", |
| 340 | + "96 Example Software 32009 http://www.wikidata.org/entity/Q83404 \n", |
| 341 | + "97 Example Software 42979 http://www.wikidata.org/entity/Q83404 \n", |
| 342 | + "98 Example Software 31979 http://www.wikidata.org/entity/Q83404 \n", |
| 343 | + "99 Example Software 3439 http://www.wikidata.org/entity/Q83404 \n", |
| 344 | + "\n", |
| 345 | + " disciplineDescription \n", |
| 346 | + "0 city and county seat in Timiș County, Romania \n", |
| 347 | + "1 city and county seat in Timiș County, Romania \n", |
| 348 | + "2 city and county seat in Timiș County, Romania \n", |
| 349 | + "3 city and county seat in Timiș County, Romania \n", |
| 350 | + "4 city and county seat in Timiș County, Romania \n", |
| 351 | + ".. ... \n", |
| 352 | + "95 city and county seat in Timiș County, Romania \n", |
| 353 | + "96 city and county seat in Timiș County, Romania \n", |
| 354 | + "97 city and county seat in Timiș County, Romania \n", |
| 355 | + "98 city and county seat in Timiș County, Romania \n", |
| 356 | + "99 city and county seat in Timiș County, Romania \n", |
| 357 | + "\n", |
| 358 | + "[100 rows x 3 columns]" |
| 359 | + ] |
| 360 | + }, |
| 361 | + "metadata": {}, |
| 362 | + "output_type": "display_data" |
| 363 | + } |
| 364 | + ], |
| 365 | + "source": [ |
| 366 | + "## Example queries:\n", |
| 367 | + "# Example 3: Get software repositories and their discipline descriptions from Wikidata (Federated Query)\n", |
| 368 | + "\n", |
| 369 | + "if graph:\n", |
| 370 | + " query_str_limit = \"\"\"\n", |
| 371 | + "PREFIX schema: <http://schema.org/> \n", |
| 372 | + "PREFIX pulse: <https://open-pulse.epfl.ch/ontology#> \n", |
| 373 | + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n", |
| 374 | + " \n", |
| 375 | + "SELECT ?repoName ?disciplineIRI ?disciplineDescription\n", |
| 376 | + "WHERE {\n", |
| 377 | + " # 1. Get data from our LOCAL graph\n", |
| 378 | + " ?repo a schema:SoftwareSourceCode ;\n", |
| 379 | + " schema:name ?repoName ;\n", |
| 380 | + " pulse:discipline ?disciplineIRI .\n", |
| 381 | + "\n", |
| 382 | + " # 2. Connect to an EXTERNAL graph (Wikidata)\n", |
| 383 | + " SERVICE <https://query.wikidata.org/sparql> {\n", |
| 384 | + " # 3. Ask Wikidata for the description of our ?disciplineIRI\n", |
| 385 | + " ?disciplineIRI schema:description ?disciplineDescription .\n", |
| 386 | + " \n", |
| 387 | + " # Ensure we get the English description\n", |
| 388 | + " FILTER(LANG(?disciplineDescription) = \"en\")\n", |
| 389 | + " }\n", |
| 390 | + "}\n", |
| 391 | + "LIMIT 100\n", |
| 392 | + "\"\"\"\n", |
| 393 | + " \n", |
| 394 | + " display(Markdown(\"**Running query:** Getting 10 triples...\"))\n", |
| 395 | + " \n", |
| 396 | + " # Run the query\n", |
| 397 | + " results = graph.query(query_str_limit)\n", |
| 398 | + " \n", |
| 399 | + " # Convert results to a list of dictionaries\n", |
| 400 | + " results_list = [row.asdict() for row in results]\n", |
| 401 | + " \n", |
| 402 | + " # Load into a pandas DataFrame and display\n", |
| 403 | + " df = pd.DataFrame(results_list)\n", |
| 404 | + " display(df)\n", |
| 405 | + " \n", |
| 406 | + "else:\n", |
| 407 | + " display(Markdown(\"⚠️ Skipping query: Database not connected.\"))\n" |
| 408 | + ] |
| 409 | + }, |
| 410 | + { |
| 411 | + "cell_type": "code", |
| 412 | + "execution_count": null, |
| 413 | + "metadata": {}, |
| 414 | + "outputs": [ |
| 415 | + { |
| 416 | + "data": { |
| 417 | + "text/markdown": [ |
| 418 | + "**Running query:** Finding top 10 authors for Python repos in Chemistry..." |
| 419 | + ], |
| 420 | + "text/plain": [ |
| 421 | + "<IPython.core.display.Markdown object>" |
| 422 | + ] |
| 423 | + }, |
| 424 | + "metadata": {}, |
| 425 | + "output_type": "display_data" |
| 426 | + }, |
| 427 | + { |
| 428 | + "data": { |
| 429 | + "text/html": [ |
| 430 | + "<div>\n", |
| 431 | + "<style scoped>\n", |
| 432 | + " .dataframe tbody tr th:only-of-type {\n", |
| 433 | + " vertical-align: middle;\n", |
| 434 | + " }\n", |
| 435 | + "\n", |
| 436 | + " .dataframe tbody tr th {\n", |
| 437 | + " vertical-align: top;\n", |
| 438 | + " }\n", |
| 439 | + "\n", |
| 440 | + " .dataframe thead th {\n", |
| 441 | + " text-align: right;\n", |
| 442 | + " }\n", |
| 443 | + "</style>\n", |
| 444 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 445 | + " <thead>\n", |
| 446 | + " <tr style=\"text-align: right;\">\n", |
| 447 | + " <th></th>\n", |
| 448 | + " </tr>\n", |
| 449 | + " </thead>\n", |
| 450 | + " <tbody>\n", |
| 451 | + " </tbody>\n", |
| 452 | + "</table>\n", |
| 453 | + "</div>" |
| 454 | + ], |
| 455 | + "text/plain": [ |
| 456 | + "Empty DataFrame\n", |
| 457 | + "Columns: []\n", |
| 458 | + "Index: []" |
| 459 | + ] |
| 460 | + }, |
| 461 | + "metadata": {}, |
| 462 | + "output_type": "display_data" |
| 463 | + } |
| 464 | + ], |
| 465 | + "source": [ |
| 466 | + "if graph: \n", |
| 467 | + " query_str = \"\"\" PREFIX schema: <http://schema.org/> \n", |
| 468 | + " PREFIX pulse: <https://open-pulse.epfl.ch/ontology#> \n", |
| 469 | + " PREFIX wd: <http://www.wikidata.org/entity/>\n", |
| 470 | + "\n", |
| 471 | + "SELECT ?personName (COUNT(DISTINCT ?repo) AS ?repoCount)\n", |
| 472 | + "WHERE {\n", |
| 473 | + " # 1. Find repositories matching the criteria\n", |
| 474 | + " ?repo a schema:SoftwareSourceCode ;\n", |
| 475 | + " schema:programmingLanguage ?language ;\n", |
| 476 | + " pulse:discipline wd:Q2329 . # wd:Q2329 is the IRI for Chemistry\n", |
| 477 | + " \n", |
| 478 | + " # 2. Filter for Python (case-insensitive)\n", |
| 479 | + "# FILTER(CONTAINS(LCASE(STR(?language)), \"python\"))\n", |
| 480 | + " \n", |
| 481 | + " # 3. Find the author of these repos\n", |
| 482 | + " ?repo schema:author ?person .\n", |
| 483 | + " \n", |
| 484 | + " # 4. Ensure the author is a Person and get their name\n", |
| 485 | + " ?person a schema:Person ;\n", |
| 486 | + " schema:name ?personName .\n", |
| 487 | + "}\n", |
| 488 | + "GROUP BY ?person ?personName\n", |
| 489 | + "ORDER BY DESC(?repoCount)\n", |
| 490 | + "LIMIT 10\n", |
| 491 | + "\"\"\"\n", |
| 492 | + "\n", |
| 493 | + "display(Markdown(\"**Running query:** Finding top 10 authors for Python repos in Chemistry...\"))\n", |
| 494 | + "\n", |
| 495 | + "# Run the query and display in pandas\n", |
| 496 | + "results = graph.query(query_str)\n", |
| 497 | + "results_list = [row.asdict() for row in results]\n", |
| 498 | + "df = pd.DataFrame(results_list)\n", |
| 499 | + "display(df)" |
| 500 | + ] |
165 | 501 | } |
166 | 502 | ], |
167 | 503 | "metadata": { |
168 | 504 | "kernelspec": { |
169 | | - "display_name": ".venv", |
| 505 | + "display_name": "open-pulse-quickstart", |
170 | 506 | "language": "python", |
171 | 507 | "name": "python3" |
172 | 508 | }, |
|
0 commit comments