mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-06-22 19:03:54 +02:00
Executing black over all jupyter notebook (#190)
Reverting black for the jupyter notebooks gets old. Can we just run black over all of them?
This commit is contained in:
@ -169,19 +169,25 @@
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"# create dataframe based on the sample data\n",
|
||||
"df_nodes = pd.read_csv('companies.csv', sep = ';')\n",
|
||||
"df_nodes = pd.read_csv(\"companies.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 = {'Automobilhersteller': ' #729b79ff', 'Automobilzulieferer': '#475b63ff', 'Branche 3': '#f3e8eeff', 'Branche 4': '#bacdb0ff', 'Branche 5': '#2e2c2fff'}\n",
|
||||
"df_nodes['color'] = df_nodes['branche'].map(node_color)\n",
|
||||
"node_color = {\n",
|
||||
" \"Automobilhersteller\": \" #729b79ff\",\n",
|
||||
" \"Automobilzulieferer\": \"#475b63ff\",\n",
|
||||
" \"Branche 3\": \"#f3e8eeff\",\n",
|
||||
" \"Branche 4\": \"#bacdb0ff\",\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())"
|
||||
@ -215,7 +221,7 @@
|
||||
],
|
||||
"source": [
|
||||
"# create dataframe based on the sample data\n",
|
||||
"df_edges = pd.read_csv('relations.csv', sep = ';')\n",
|
||||
"df_edges = pd.read_csv(\"relations.csv\", sep=\";\")\n",
|
||||
"\n",
|
||||
"# show first five entries of the dataframe\n",
|
||||
"print(df_edges.head())"
|
||||
@ -263,23 +269,25 @@
|
||||
"import networkx as nx\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"fig = plt.figure(figsize=(12,12))\n",
|
||||
"fig = plt.figure(figsize=(12, 12))\n",
|
||||
"ax = plt.subplot(111)\n",
|
||||
"ax.set_title('Graph - Shapes', fontsize=10)\n",
|
||||
"ax.set_title(\"Graph - Shapes\", fontsize=10)\n",
|
||||
"\n",
|
||||
"# initiate graph\n",
|
||||
"graph = nx.MultiGraph()\n",
|
||||
"\n",
|
||||
"# create edges from dataframe\n",
|
||||
"graph = nx.from_pandas_edgelist(df_edges, source = 'from', target = 'to', edge_attr= 'label')\n",
|
||||
"graph = nx.from_pandas_edgelist(df_edges, source=\"from\", target=\"to\", edge_attr=\"label\")\n",
|
||||
"\n",
|
||||
"# update node attributes from dataframe\n",
|
||||
"nodes_attr = df_nodes.set_index('id').to_dict(orient = 'index')\n",
|
||||
"nodes_attr = df_nodes.set_index(\"id\").to_dict(orient=\"index\")\n",
|
||||
"nx.set_node_attributes(graph, nodes_attr)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pos = nx.spring_layout(graph)\n",
|
||||
"nx.draw(graph, pos, node_size=1500, node_color='yellow', font_size=8, font_weight='bold')\n",
|
||||
"nx.draw(\n",
|
||||
" graph, pos, node_size=1500, node_color=\"yellow\", font_size=8, font_weight=\"bold\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"plt.tight_layout()\n",
|
||||
"plt.show()\n",
|
||||
@ -307,16 +315,19 @@
|
||||
"# visualize using pyvis\n",
|
||||
"from pyvis.network import Network\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_centrality_graph(df, measure_type, save_path):\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",
|
||||
" adj_list = net.get_adj_list()\n",
|
||||
"\n",
|
||||
@ -341,28 +352,33 @@
|
||||
" measure_vector = nx.average_degree_connectivity(graph)\n",
|
||||
" # df[\"average_degree\"] = measure_vector.values()\n",
|
||||
" print(measure_vector.values())\n",
|
||||
" \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",
|
||||
" \n",
|
||||
" # df[\"edges\"] = measure_vector.values()\n",
|
||||
" \n",
|
||||
" # df[\"edges\"] = measure_vector.values()\n",
|
||||
"\n",
|
||||
" if measure_type == \"edges\":\n",
|
||||
" size = 10 #len(neighbors)*5 \n",
|
||||
" size = 10 # len(neighbors)*5\n",
|
||||
" else:\n",
|
||||
" size = measure_vector[node_id]*50 \n",
|
||||
" next((node.update({'size': size}) for node in net.nodes if node['id'] == node_id), None)\n",
|
||||
" size = measure_vector[node_id] * 50\n",
|
||||
" next(\n",
|
||||
" (\n",
|
||||
" node.update({\"size\": size})\n",
|
||||
" for node in net.nodes\n",
|
||||
" if node[\"id\"] == node_id\n",
|
||||
" ),\n",
|
||||
" None,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # set the node distance and spring lenght using repulsion\n",
|
||||
" net.repulsion(node_distance=150, spring_length=50)\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(save_path)\n"
|
||||
" net.save_graph(save_path)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -593,33 +609,36 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"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(s)\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",
|
||||
"adj_list = net.get_adj_list()\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",
|
||||
" \n",
|
||||
" # df[\"edges\"] = measure_vector.values()\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" size = 10 #len(neighbors)*5 \n",
|
||||
" \n",
|
||||
" next((node.update({'size': size}) for node in net.nodes if node['id'] == node_id), None)\n",
|
||||
" # df[\"edges\"] = measure_vector.values()\n",
|
||||
"\n",
|
||||
" size = 10 # len(neighbors)*5\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=150, spring_length=50)\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(\"./metrics/connected_components_networkx.html\")"
|
||||
|
Reference in New Issue
Block a user