{ "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']),\n", " layout=Layout(width='50%', height='30px'))" ] }, { "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", " layout=Layout(width='50%', height='30px'))\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', layout=Layout(width='50%', height='30px'))\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": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Layout for the input 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 QWERTY keyboard.
\n", "If the name shows in the box below, tap on Create the graph.
\n", "You can also select directly a name from the dropdown menu on the right and tap Create the graph.
\n", "
After loading is completed, swipe for zooming inside the network till you see the artists' names
and tap 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", "
\n", " [Tap here](files/dp_last_graph.html) to switch the graph in full screen mode after creating it.
\n", "
" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "🔄 Rotate your phone.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fd775e7ad72c4bf2886eafaceb4f92d2", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Text(value='', description='Artist:', layout=Layout(height='30px', width='50%'), placeholder='(e.g. Jon Lord)'…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "359a43099f374506928a9e87026e92e8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(layout=Layout(height='30px', width='50%'), options=('Select an artist', 'Abe Laboriel', 'Ace Evans', …" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "797d1d38c29244c3b75a05b4f0686a59", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Button(description='Create the graph', layout=Layout(height='30px', width='50%'), style=ButtonStyle())" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "66573012a855487da676deb3162e7e60", "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": [ "print(\"\\U0001F504 Rotate your phone.\")\n", "display(output_text)\n", "display(dropdown_source)\n", "display(graph_button)\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 }