diff --git a/documentations/research/networkx_pyvis_pandas.ipynb b/documentations/research/networkx_pyvis_pandas.ipynb index 31ce3a5..e235f09 100644 --- a/documentations/research/networkx_pyvis_pandas.ipynb +++ b/documentations/research/networkx_pyvis_pandas.ipynb @@ -122,19 +122,25 @@ "import pandas as pd\n", "\n", "# create dataframe based on the sample data\n", - "df_nodes = pd.read_csv('nodes.csv', sep = ';')\n", + "df_nodes = pd.read_csv(\"nodes.csv\", sep=\";\")\n", "\n", "# define shape based on the type\n", - "node_shape = {'Company': 'dot', 'Person': 'triangle'}\n", - "df_nodes['shape'] = df_nodes['type'].map(node_shape)\n", + "node_shape = {\"Company\": \"dot\", \"Person\": \"triangle\"}\n", + "df_nodes[\"shape\"] = df_nodes[\"type\"].map(node_shape)\n", "\n", "# define color based on branche\n", - "node_color = {'Branche 1': ' #f3e8eeff', 'Branche 2': '#bacdb0ff', 'Branche 3': '#729b79ff', 'Branche 4': '#475b63ff', 'Branche 5': '#2e2c2fff'}\n", - "df_nodes['color'] = df_nodes['branche'].map(node_color)\n", + "node_color = {\n", + " \"Branche 1\": \" #f3e8eeff\",\n", + " \"Branche 2\": \"#bacdb0ff\",\n", + " \"Branche 3\": \"#729b79ff\",\n", + " \"Branche 4\": \"#475b63ff\",\n", + " \"Branche 5\": \"#2e2c2fff\",\n", + "}\n", + "df_nodes[\"color\"] = df_nodes[\"branche\"].map(node_color)\n", "\n", "# add information column that can be used for the mouse over in the graph\n", - "df_nodes = df_nodes.fillna('')\n", - "df_nodes['title'] = df_nodes['label'] + '\\n' + df_nodes['branche']\n", + "df_nodes = df_nodes.fillna(\"\")\n", + "df_nodes[\"title\"] = df_nodes[\"label\"] + \"\\n\" + df_nodes[\"branche\"]\n", "\n", "# show first five entries of the dataframe\n", "print(df_nodes.head())" @@ -167,7 +173,7 @@ ], "source": [ "# create dataframe based on the sample data\n", - "df_edges = pd.read_csv('edges.csv', sep = ';')\n", + "df_edges = pd.read_csv(\"edges.csv\", sep=\";\")\n", "\n", "# show first five entries of the dataframe\n", "print(df_edges.head())" @@ -197,15 +203,17 @@ "graph = nx.MultiGraph()\n", "\n", "# create edges from dataframe\n", - "graph = nx.from_pandas_edgelist(df_edges, source = 'from', target = 'to', edge_attr= ['label']) #, 'weight'])\n", + "graph = nx.from_pandas_edgelist(\n", + " df_edges, source=\"from\", target=\"to\", edge_attr=[\"label\"]\n", + ") # , 'weight'])\n", "\n", - "#pos = nx.spring_layout(graph, weight = 'weight')\n", - "#df_nodes['x'] = df_nodes['id'].map(lambda x: pos[x][0])\n", - "#df_nodes['y'] = df_nodes['id'].map(lambda x: pos[x][1])\n", + "# pos = nx.spring_layout(graph, weight = 'weight')\n", + "# df_nodes['x'] = df_nodes['id'].map(lambda x: pos[x][0])\n", + "# df_nodes['y'] = df_nodes['id'].map(lambda x: pos[x][1])\n", "\n", "# update node attributes from dataframe\n", - "nodes_attr = df_nodes.set_index('id').to_dict(orient = 'index')\n", - "nx.set_node_attributes(graph, nodes_attr)\n" + "nodes_attr = df_nodes.set_index(\"id\").to_dict(orient=\"index\")\n", + "nx.set_node_attributes(graph, nodes_attr)" ] }, { @@ -237,11 +245,11 @@ " # create empty dictionary\n", " dict = {}\n", " # get node id\n", - " dict['id'] = node\n", + " dict[\"id\"] = node\n", " # get k-neighbours for k=1,2,3, subtract -1 since output of single_source_shortest_path_length contains node itself\n", - " dict['k=1'] = len(nx.single_source_shortest_path_length(graph, node, cutoff=1))-1\n", - " dict['k=2'] = len(nx.single_source_shortest_path_length(graph, node, cutoff=2))-1\n", - " dict['k=3'] = len(nx.single_source_shortest_path_length(graph, node, cutoff=3))-1\n", + " dict[\"k=1\"] = len(nx.single_source_shortest_path_length(graph, node, cutoff=1)) - 1\n", + " dict[\"k=2\"] = len(nx.single_source_shortest_path_length(graph, node, cutoff=2)) - 1\n", + " dict[\"k=3\"] = len(nx.single_source_shortest_path_length(graph, node, cutoff=3)) - 1\n", " # append list for each node\n", " k_neighbours.append(dict)\n", "\n", @@ -269,41 +277,49 @@ "from pyvis.network import Network\n", "\n", "# initiate network\n", - "net = Network(directed=False, neighborhood_highlight=True, bgcolor = \"white\", font_color=\"black\")\n", + "net = Network(\n", + " directed=False, neighborhood_highlight=True, bgcolor=\"white\", font_color=\"black\"\n", + ")\n", "\n", "# pass networkx graph to pyvis\n", "net.from_nx(graph)\n", "\n", - "# set edge options \n", + "# set edge options\n", "net.inherit_edge_colors(False)\n", - "net.set_edge_smooth('dynamic')\n", + "net.set_edge_smooth(\"dynamic\")\n", "\n", "# chose size format\n", - "size_type = 'edges' # select 'edges' or 'eigen'\n", + "size_type = \"edges\" # select 'edges' or 'eigen'\n", "\n", "adj_list = net.get_adj_list()\n", "\n", - "if size_type == 'eigen':\n", + "if size_type == \"eigen\":\n", " eigenvector = nx.eigenvector_centrality(graph)\n", "\n", "# calculate and update size of the nodes depending on their number of edges\n", "for node_id, neighbors in adj_list.items():\n", - " if size_type == 'edges':\n", - " size = len(neighbors)*5\n", - " if size_type == 'eigen':\n", - " size = eigenvector[node_id]*900\n", - " next((node.update({'value': size}) for node in net.nodes if node['id'] == node_id), None)\n", - " next((node.update({'size': size}) for node in net.nodes if node['id'] == node_id), None)\n", + " if size_type == \"edges\":\n", + " size = len(neighbors) * 5\n", + " if size_type == \"eigen\":\n", + " size = eigenvector[node_id] * 900\n", + " next(\n", + " (node.update({\"value\": size}) for node in net.nodes if node[\"id\"] == node_id),\n", + " None,\n", + " )\n", + " next(\n", + " (node.update({\"size\": size}) for node in net.nodes if node[\"id\"] == node_id),\n", + " None,\n", + " )\n", "\n", "# set the node distance and spring lenght using repulsion\n", "net.repulsion(node_distance=250, spring_length=150)\n", "\n", "# activate physics buttons to further explore the available solvers:\n", "# barnesHut, forceAtlas2Based, repulsion, hierarchicalRepulsion\n", - "net.show_buttons(filter_=['physics'])\n", + "net.show_buttons(filter_=[\"physics\"])\n", "\n", "# save graph as HTML\n", - "net.save_graph('networkx_pyvis.html')\n" + "net.save_graph(\"networkx_pyvis.html\")" ] }, {