{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "177cb892",
   "metadata": {},
   "source": [
    "*In this tutorial we will visualise connections between 7 entities.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4c815ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sugartrail\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38fbba9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "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": null,
   "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": null,
   "id": "40439af2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Option 2: Load networks\n",
    "# entity_graphs = sugartrail.multinetwork.load_multiple_networks(f'{sugartrail.const.data_path}/networks/multinode/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7503bc29",
   "metadata": {},
   "outputs": [],
   "source": [
    "s_path_network = sugartrail.multinetwork.find_multi_network_connections(entity_graphs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c204f503",
   "metadata": {},
   "outputs": [],
   "source": [
    "sugartrail.graphvis.visualise_connections(s_path_network, f'{sugartrail.const.data_path}visualisations')"
   ]
  }
 ],
 "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.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
