{ "cells": [ { "cell_type": "markdown", "id": "177cb892", "metadata": {}, "source": [ "*In this tutorial we will visualise connections between 7 entities.*" ] }, { "cell_type": "code", "execution_count": 1, "id": "d4c815ff", "metadata": {}, "outputs": [], "source": [ "import sugartrail\n", "from tqdm import tqdm\n", "sugartrail.api.basic_auth.username = \"\"" ] }, { "cell_type": "markdown", "id": "71030bd7", "metadata": {}, "source": [ "We will explore connections between several persons associated with donations and lobbying efforts within the UK Conservative Party:" ] }, { "cell_type": "markdown", "id": "bff72de9", "metadata": {}, "source": [ "- O. PATERSON ([uhmCAOx6PDrXSxKDXJSD1Vv2prc](https://find-and-update.company-information.service.gov.uk/officers/uhmCAOx6PDrXSxKDXJSD1Vv2prc/appointments))\n", "- M. AMERSI ([3wTyHYmLN5-J6XiTww5SL0iL3fI](https://find-and-update.company-information.service.gov.uk/officers/3wTyHYmLN5-J6XiTww5SL0iL3fI/appointments))\n", "- A. Temerko ([lBdRiCfTDhMcaLwOU6393XUfPDg](https://find-and-update.company-information.service.gov.uk/officers/lBdRiCfTDhMcaLwOU6393XUfPDg/appointments))\n", "- A. BAMFORD ([KwkjxuswE9qwWKLU0ndEaau9cq0](https://find-and-update.company-information.service.gov.uk/officers/KwkjxuswE9qwWKLU0ndEaau9cq0/appointments))\n", "- B. ELLIOT ([g8BmvnpH8blqT87i93sgJeowx7I](https://find-and-update.company-information.service.gov.uk/officers/g8BmvnpH8blqT87i93sgJeowx7I/appointments))\n", "- L. CHERNUKHIN ([D-2pqWTW2QY0ooHbL5O7soMwTRc](https://find-and-update.company-information.service.gov.uk/officers/D-2pqWTW2QY0ooHbL5O7soMwTRc/appointments))\n", "- P. CRUDDAS ([WtiEW0LL-mMmPaRLrQSCjsWBpXY](https://find-and-update.company-information.service.gov.uk/officers/WtiEW0LL-mMmPaRLrQSCjsWBpXY/appointments))" ] }, { "cell_type": "markdown", "id": "b59db34a", "metadata": {}, "source": [ "To do this, lets create a list of dictionaries with the id for each entity. These networks are initialised from officers but we could include addresses or companies with the keys 'address' and 'company_id':" ] }, { "cell_type": "code", "execution_count": 2, "id": "5fec2142", "metadata": {}, "outputs": [], "source": [ "entities = [{\"officer_id\": \"uhmCAOx6PDrXSxKDXJSD1Vv2prc\"},\n", " {\"officer_id\": \"3wTyHYmLN5-J6XiTww5SL0iL3fI\"},\n", " {\"officer_id\": \"lBdRiCfTDhMcaLwOU6393XUfPDg\"},\n", " {\"officer_id\": \"KwkjxuswE9qwWKLU0ndEaau9cq0\"},\n", " {\"officer_id\": \"g8BmvnpH8blqT87i93sgJeowx7I\"},\n", " {\"officer_id\": \"D-2pqWTW2QY0ooHbL5O7soMwTRc\"},\n", " {\"officer_id\": \"WtiEW0LL-mMmPaRLrQSCjsWBpXY\"}]" ] }, { "cell_type": "markdown", "id": "8cbee60b", "metadata": {}, "source": [ "Lets attempt to find connections between entities through building a network for each entity that is 3 degrees deep. You can build the networks from scratch (Option 1) or load the pre-downloaded networks by uncommenting the code below (Option 2):" ] }, { "cell_type": "code", "execution_count": null, "id": "16453850", "metadata": {}, "outputs": [], "source": [ "# Option 1: Build networks from scratch\n", "entity_graphs = []\n", "for entity in entities: \n", " if list(entity.keys())[0] == \"officer_id\":\n", " entity_graphs.append(sugartrail.base.Network(officer_id=entity[\"officer_id\"]))\n", " elif list(entity.keys())[0] == \"address\":\n", " entity_graphs.append(sugartrail.base.Network(address=entity[\"address\"]))\n", " elif list(entity.keys())[0] == \"company_id\":\n", " entity_graphs.append(sugartrail.base.Network(company_id=entity[\"company_id\"]))\n", " \n", "for entity in tqdm(entity_graphs):\n", " entity.hop.officer_appointments_maxsize = 20\n", " entity.hop.officers_at_address_maxsize = 20\n", " entity.hop.companies_at_address_maxsize = 20\n", " entity.perform_hop(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "40439af2", "metadata": {}, "outputs": [], "source": [ "## Option 2: Load networks\n", "# entity_graphs = sugartrail.processing.load_multiple_networks(f'{sugartrail.const.data_path}/networks/multinode/')" ] }, { "cell_type": "code", "execution_count": 5, "id": "7503bc29", "metadata": {}, "outputs": [], "source": [ "s_path_network = sugartrail.processing.find_multi_network_connections(entity_graphs)" ] }, { "cell_type": "code", "execution_count": 6, "id": "c204f503", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "../assets/visualisations/graph.html\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sugartrail.processing.visualise_connections(s_path_network, f'{sugartrail.const.data_path}/visualisations')" ] }, { "cell_type": "markdown", "id": "280758b4", "metadata": {}, "source": [ "Lets now save all our networks:" ] }, { "cell_type": "code", "execution_count": 7, "id": "d49c0ce7", "metadata": {}, "outputs": [], "source": [ "for i, entity in enumerate(entity_graphs):\n", " entity.save(f'multinode/{list(entity_graphs[i].graph.keys())[0]}_network.json')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }