{ "cells": [ { "cell_type": "markdown", "id": "81108783", "metadata": {}, "source": [ "
\n", "\n", "# Animal diseases in Switzerland\n", "\n", "FSVO, Federal Food Safety and Veterinary Office, collects data on the animal diseases in Switzerland. This data is published as [Linked Data](https://en.wikipedia.org/wiki/Linked_data). \n", "\n", "In this tutorial, we will show **how to work with Linked Data.** Mainly, we will see how to work with data on animal disease. \n", "We will look into how to query, process, and visualize it. \n", "\n", "[1. Setup](#Setup) \n", " [1.1 Python](#SPARQL-endpoints) \n", " [1.2 SPARQL endpoints](#SPARQL-endpoints) \n", " [1.3 SPARQL client](#SPARQL-endpoints) \n", "\n", "[2. Data](#Data) \n", " [2.1 Animal species](#Animal-species) \n", " [2.2 Diseases](#Diseases) \n", " [2.3 Can we link disease to animal type?](#Can-we-link-disease-to-animal-type) \n", " [2.4 Reports](#Reports) \n", " [2.5 Example: goats](#example-goats) \n", "\n", "\n", "[3. Analysis](#Analysis) \n", " [3.1 Cattle: Mucosal Disease](#cattle-mucosal-disease) \n", " [3.2 Bees: Sauerbrut](#Sauerbrut) \n", " [3.3 Rabies](#Rabies) \n", " [3.4 Most common diseases](#Most-common-diseases) \n", " [3.5 Reports over time](#reports-over-time) \n", "\n" ] }, { "cell_type": "markdown", "id": "6a3e5743", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "markdown", "id": "e96bd575", "metadata": {}, "source": [ "### Python\n", "This notebook requires Python 3.9 or later to run." ] }, { "cell_type": "markdown", "id": "d4442f36", "metadata": {}, "source": [ "### SPARQL endpoints\n", "\n", "#### For epidemiological data\n", "Reports on animal diseases are published as Linked Data. It can be accessed with [SPARQL queries](https://www.w3.org/TR/rdf-sparql-query/). \n", "You can send queries using HTTP requests. The API endpoint is **[https://lindas.admin.ch/query/](https://int.lindas.admin.ch/query).** \n", "\n", "#### For geodata\n", "Animal diseases are closely linked to places. To understand their location, we will work with \n", "Swiss geodata. It is published as Linked Data. It can be accessed using API endpoint under **[https://geo.ld.admin.ch/query](https://geo.ld.admin.ch/query).** " ] }, { "cell_type": "markdown", "id": "90db373f", "metadata": {}, "source": [ "### SPARQL client\n", "\n", "Let's use `SparqlClient` from [graphly](https://github.com/zazuko/graphly) to communicate with both databases. \n", "Graphly will allow us to:\n", "* send SPARQL queries\n", "* automatically add prefixes to all queries\n", "* format response to `pandas` or `geopandas`" ] }, { "cell_type": "code", "execution_count": null, "id": "dc0815c3", "metadata": {}, "outputs": [], "source": [ "import datetime\n", "import itertools\n", "import json\n", "\n", "import folium\n", "import mapclassify\n", "import matplotlib as mpl\n", "import matplotlib.cm\n", "import networkx as nx\n", "import pandas as pd\n", "import plotly.express as px\n", "import plotly.graph_objects as go\n", "from bokeh.io import output_notebook, show\n", "from bokeh.models import (Circle, CustomJS, HoverTool, LabelSet, MultiLine,\n", " NodesAndLinkedEdges, Plot, TapTool)\n", "from bokeh.palettes import Paired\n", "from bokeh.plotting import ColumnDataSource, from_networkx\n", "from dateutil.relativedelta import relativedelta\n", "from folium.plugins import TimeSliderChoropleth\n", "from graphly.api_client import SparqlClient\n", "from plotly.subplots import make_subplots\n", "from enum import Enum\n", "from collections import namedtuple" ] }, { "cell_type": "code", "execution_count": null, "id": "4bc9e4ef", "metadata": {}, "outputs": [], "source": [ "# Uncomment to install dependencies in Colab environment\n", "#!pip install mapclassify\n", "#!pip install git+https://github.com/zazuko/graphly.git" ] }, { "cell_type": "code", "execution_count": null, "id": "985890ce", "metadata": {}, "outputs": [], "source": [ "sparql = SparqlClient(\"https://int.lindas.admin.ch/query\")\n", "geosparql = SparqlClient(\"https://geo.ld.admin.ch/query\")\n", "\n", "sparql.add_prefixes({\n", " \"schema\": \"