{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Who’s responsible?\n", "\n", "The National Archives of Australia's RecordSearch database divides government activities up into a [series of functions](harvesting_functions_from_recordsearch.ipynb). Over time, different agencies have been made responsible for these functions, and it can be interesting to track how these responsibilities have shifted.\n", "\n", "This notebook uses [data about functions](get_all_agencies_by_function.ipynb) harvested from RecordSearch to create a a simple visualisation of the agencies responsible for a selected function." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "import ipywidgets as widgets\n", "from IPython.display import display, HTML\n", "import json\n", "import altair as alt\n", "from altair import datum\n", "import pandas as pd\n", "\n", "alt.renderers.enable('notebook')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "IPython.OutputArea.prototype._should_scroll = function(lines) {return false}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "IPython.OutputArea.prototype._should_scroll = function(lines) {return false}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Load the harvested functions data from a JSON file\n", "with open('data/agencies_by_function.json', 'r') as json_file:\n", " data = json.load(json_file)\n", " \n", "def get_children(function, level):\n", " '''\n", " Gets the children of the supplied term.\n", " Formats/indents the terms for the dropdown.\n", " '''\n", " f_list = []\n", " if 'narrower' in function:\n", " level += 1\n", " for subf in function['narrower']:\n", " f_list.append(('{}{} {}'.format(level * ' ', level * '-', subf['term']), subf))\n", " f_list += get_children(subf, level=level)\n", " return f_list\n", "\n", "def get_functions():\n", " # Load the JSON file of functions we've previously harvested\n", " with open('data/functions.json', 'r') as json_file:\n", " functions = json.load(json_file)\n", "\n", " # Make the list of options for the dropdown\n", " functions_list = []\n", " for function in functions:\n", " functions_list.append((function['term'], function))\n", " functions_list += get_children(function, level=0)\n", " return functions_list\n", "\n", "def get_function_agencies(term):\n", " for f in data:\n", " if f['term'] == term:\n", " return f['agencies']" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f5ed105781c84eb1b8edc3a2722feb24", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(Label(value='Select a function:'), Dropdown(options=(('administrative services', {'term': 'admiā€¦" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "46f46593db454c489df827b555c8b498", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output(layout=Layout(width='100%'))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def make_chart(change):\n", " # Clear current output\n", " out.clear_output()\n", " # Get the currently selected term from the dropdown\n", " # term = change['new']\n", " function = term.value\n", " atype = agency_type.value\n", " # Get the agencies responsible for the selected function\n", " agencies = get_function_agencies(function['term'])\n", " if agencies:\n", " # Convert to a dataframe\n", " df = pd.DataFrame(agencies)\n", " # Set some defualts for missing dates\n", " missing = {'agency_status': 'Not recorded', 'function_start_date': '1901-01-01', 'function_end_date': '2018-12-31'}\n", " df = df.fillna(value=missing)\n", " df['url'] = df.apply(lambda x: 'http://www.naa.gov.au/cgi-bin/Search?O=S&Number={}'.format(x['agency_id']), axis=1)\n", " if change['owner'].description == 'Agency type:' and atype != 'All':\n", " df = df.loc[df['agency_status'] == atype]\n", " else:\n", " agency_type.value = 'All'\n", " # Create a Gannt style chart\n", " chart = alt.Chart(df).mark_bar(size=20).encode(\n", " x=alt.X('function_start_date:T', axis=alt.Axis(format='%Y', title='Dates agency was responsible for function'), scale=alt.Scale(nice=True)),\n", " x2='function_end_date:T',\n", " y=alt.Y('title', scale=alt.Scale(rangeStep=30), title='Agency'),\n", " color=alt.Color('agency_status', legend=alt.Legend(title='Agency type')),\n", " tooltip=[\n", " alt.Tooltip('agency_id', title='Identifier'), \n", " alt.Tooltip('title', title='Agency'),\n", " alt.Tooltip('agency_status', title='Type'),\n", " alt.Tooltip('location', title='Location'),\n", " alt.Tooltip('function_start_date', title='From', timeUnit='year'), \n", " alt.Tooltip('function_end_date', title='To', timeUnit='year')],\n", " href='url:N'\n", " ).properties(\n", " width=600\n", " )\n", " with out:\n", " display(HTML('

Agencies responsible for ‘{}’

'.format(function['term'])))\n", " display(chart)\n", " else:\n", " with out:\n", " display(HTML('

No agencies responsible for ‘{}’

'.format(function['term'])))\n", "\n", "# This is where the chart will be displayed\n", "out = widgets.Output(layout=widgets.Layout(width='100%'))\n", "\n", "# Create the dropdown\n", "term = widgets.Dropdown(\n", " options=get_functions(),\n", " value=None,\n", " disabled=False,\n", ")\n", "\n", "agency_type = widgets.Dropdown(\n", " options=['All', 'Department of State', 'Head Office', 'Regional or State Office', 'Local Office', 'Intergovernmental agency'],\n", " value='All',\n", " disabled=False,\n", " description='Agency type:'\n", ")\n", "\n", "# Making a selection from the dropdown will automatically run 'make_chart'\n", "term.observe(make_chart, names='value')\n", "agency_type.observe(make_chart, names='value')\n", "\n", "display(widgets.HBox([widgets.Label('Select a function:'), term, agency_type]))\n", "display(out)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }