{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![The Deep Purple Network Notebook App](https://raw.githubusercontent.com/greggtdd/DeepPurpleNetwork/master/app_images/dpnetwork_graph_banner.png)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "######################################################################################################\n", "# Project: The Deep Purple Network. #\n", "# (CC) 2020 Made by Gregorio Tedde just for analysis purpose. #\n", "# #\n", "# #\n", "# PLEASE, DO NOT EDIT! #\n", "# #\n", "# #\n", "# Work License: Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License #\n", "# Libraries licenses in environment.yml #\n", "# Git Repo: https://github.com/greggtdd/DeepPurpleNetwork #\n", "# Docs for widgets handling: https://bit.ly/2W2JPGm #\n", "# Info: greggtedde@gmail.com #\n", "######################################################################################################" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "import requests\n", "import pandas as pd\n", "from PIL import Image\n", "from io import BytesIO\n", "import ipywidgets as widgets\n", "from ipywidgets import Layout\n", "from graph_bouncer import DataFramer, Networker\n", "from IPython.display import display, clear_output" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Data frame handling\n", "dp = DataFramer('https://raw.githubusercontent.com/greggtdd/DeepPurpleNetwork/master/dp_union_edges.csv')\n", "dp.upload_df()\n", "dp.density_sources()\n", "dp.artist_research()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Init state for the dropdown menu\n", "artist = \"Select an artist\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "def unique_artist_source(array):\n", " \"\"\"\n", " Returns the unique values for\n", " a specific source written as\n", " a query in the search box.\n", " \n", " Paramters\n", " ---------\n", " array: pandas.core.series.Series\n", " Desired column from the data frame.\n", " \n", " Returns\n", " -------\n", " list\n", " \"\"\"\n", " unique = array.unique().tolist()\n", " unique.sort()\n", " unique.insert(0, artist)\n", " return unique\n", "\n", "dropdown_source = widgets.Dropdown(options = unique_artist_source(dp.data_['Source']))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - search output display\n", "output_source = widgets.Output()\n", "\n", "def dropdown_handler(change):\n", " output_source.clear_output()\n", " with output_source:\n", " output_text.value = dropdown_source.value\n", " display(dp.data_[dp.data_.Source == change.new])\n", " \n", "dropdown_source.observe(dropdown_handler, names='value')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - search box\n", "output_text = widgets.Text(value='',\n", " placeholder='(e.g. Jon Lord)',\n", " description=\"Artist:\",\n", " disabled=False)\n", "\n", "def callback(widget):\n", " if output_text.value in dp.sour_:\n", " dropdown_source.value = output_text.value\n", " return widget.value\n", " \n", "output_text.on_submit(callback)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Request for error message image\n", "response = requests.get('https://raw.githubusercontent.com/greggtdd/DeepPurpleNetwork/master/app_images/ritchie_message.png')\n", "error_img = Image.open(BytesIO(response.content))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - rendered network graph\n", "output_graph = widgets.Output()\n", "\n", "def create_graph(_):\n", " if output_text.value in dp.sour_:\n", " dp.sub_framer(output_text.value)\n", " edges = dp.get_edge_data(sources=dp.sources_,\n", " targets=dp.targets_,\n", " weights=dp.weights_)\n", " nw = Networker(data=edges)\n", " nw.init_graph()\n", " nw.add_elements()\n", " nw.get_neighbors()\n", " nw.get_info()\n", " display(nw.show_graph())\n", " else:\n", " display(error_img)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Widget - 'Create the graph' button\n", "graph_button = widgets.Button(description='Create the graph')\n", "output_start = widgets.Output()\n", "\n", "def on_button_clicked(_):\n", " with output_start:\n", " with output_graph:\n", " output_start.clear_output()\n", " create_graph(_)\n", " \n", "graph_button.on_click(on_button_clicked)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Layout for the input widgets\n", "item_layout = widgets.Layout(margin='20px 0 30px 150px')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Tab for the output widgets\n", "tab = widgets.Tab([output_graph, output_source])\n", "tab.set_title(0, \"Network Graph\")\n", "tab.set_title(1, \"Subset Exploration\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Digit an Artist's name and press Enteron your keyboard.
\n", " If the name shows on the right, click on Create the graph.
\n", "You can also select directly a name from the dropdown menu on the right and click Create the graph.
\n", "
After loading is completed, scroll for zooming inside the network till you see the artists' names
and click on the nodes to see who their neighbors are. Drag and drop for moving across the graph.
\n", " The Subset Exploration tab shows each artist's universe used for building its neighborhood.

\n", " Click here to switch the graph in full screen mode after creating it.
" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "80faa275dd4944c599cf90e8ac0671f3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(Text(value='', description='Artist:', placeholder='(e.g. Jon Lord)'), Dropdown(options=('Select…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1e7cb0d7b80e42269b42134401a7d2c3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Tab(children=(Output(), Output()), _titles={'0': 'Network Graph', '1': 'Subset Exploration'})" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "input_widgets = widgets.HBox([output_text,\n", " dropdown_source,\n", " graph_button],\n", " layout=item_layout)\n", "display(input_widgets)\n", "display(tab)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Return to the website
" ] } ], "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.2" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 4 }