257 lines
1.5 MiB
Plaintext
257 lines
1.5 MiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAACyIAAAsQCAYAAABS037aAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAC4jAAAuIwF4pT92AAEAAElEQVR4nOzdebidZWEu7metPe+dZO+MJIQwCciMiKIgoDhrHavVWmvLqcdTT61V69j253Dac1pFW6dWW6sWR9SCKCBQFChzmEkIkDCGzHP2PK7h98faWckmCbAUAeW+rytX1v6G932/bw25rs2zHgrVarUaAAAAAAAAAAAAAIAGFJ/sBQAAAAAAAAAAAAAAv3kEkQEAAAAAAAAAAACAhgkiAwAAAAAAAAAAAAANE0QGAAAAAAAAAAAAABomiAwAAAAAAAAAAAAANEwQGQAAAAAAAAAAAABomCAyAAAAAAAAAAAAANAwQWQAAAAAAAAAAAAAoGGCyAAAAAAAAAAAAABAwwSRAQAAAAAAAAAAAICGCSIDAAAAAAAAAAAAAA0TRAYAAAAAAAAAAAAAGiaIDAAAAAAAAAAAAAA0TBAZAAAAAAAAAAAAAGiYIDIAAAAAAAAAAAAA0DBBZAAAAAAAAAAAAACgYYLIAAAAAAAAAAAAAEDDBJEBAAAAAAAAAAAAgIYJIgMAAAAAAAAAAAAADRNEBgAAAAAAAAAAAAAaJogMAAAAAAAAAAAAADRMEBkAAAAAAAAAAAAAaJggMgAAAAAAAAAAAADQMEFkAAAAAAAAAAAAAKBhgsgAAAAAAAAAAAAAQMMEkQEAAAAAAAAAAACAhgkiAwAAAAAAAAAAAAANa36iJ+zt7c2VV15Z/3nRokVpa2t7opcBAAAAAAAAAAAAAL/RxsbGsnr16vrPL3zhC9PT0/O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with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import pandas as pd\n",
|
||
|
"import networkx as nx\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"\n",
|
||
|
"plt.rcParams['font.sans-serif'] = 'SimHei'\n",
|
||
|
"\n",
|
||
|
"BomNodes = pd.read_csv('BomNodes.csv', index_col=0)\n",
|
||
|
"BomNodes.set_index('Code', inplace=True)\n",
|
||
|
"BomCateNet = pd.read_csv('BomCateNet.csv', index_col=0)\n",
|
||
|
"BomCateNet.fillna(0, inplace=True)\n",
|
||
|
"\n",
|
||
|
"G = nx.from_pandas_adjacency(BomCateNet, create_using=nx.MultiDiGraph())\n",
|
||
|
"\n",
|
||
|
"labels_dict = {}\n",
|
||
|
"for code in G.nodes:\n",
|
||
|
" labels_dict[code] = BomNodes.loc[code].to_dict()\n",
|
||
|
"nx.set_node_attributes(G, labels_dict)\n",
|
||
|
"\n",
|
||
|
"pos = nx.nx_agraph.graphviz_layout(G, prog=\"twopi\", args=\"\")\n",
|
||
|
"temp = {}\n",
|
||
|
"for key, value in nx.get_node_attributes(G, 'Name').items():\n",
|
||
|
" temp[key] = key + \" \"+ value\n",
|
||
|
"node_labels = temp\n",
|
||
|
"plt.figure(figsize=(12, 12), dpi=300)\n",
|
||
|
"nx.draw_networkx_nodes(G, pos)\n",
|
||
|
"nx.draw_networkx_edges(G, pos)\n",
|
||
|
"nx.draw_networkx_labels(G, pos, labels = node_labels, font_size=6)\n",
|
||
|
"plt.show()\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 74,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"1.1\n",
|
||
|
"['1.1.1', '1.1.2', '1.1.3']\n",
|
||
|
"[]\n",
|
||
|
"1.2\n",
|
||
|
"['1.2.1', '1.2.2', '1.2.3']\n",
|
||
|
"[]\n",
|
||
|
"1.3.1\n",
|
||
|
"['1.3.1.1', '1.3.1.2', '1.3.1.3', '1.3.1.4', '1.3.1.5', '1.3.1.6', '1.3.1.7']\n",
|
||
|
"[]\n",
|
||
|
"1.3.2\n",
|
||
|
"['1.3.2.1']\n",
|
||
|
"[58, 130, 135]\n",
|
||
|
"1.3.3\n",
|
||
|
"['1.3.3.1', '1.3.3.2', '1.3.3.3', '1.3.3.4', '1.3.3.5', '1.3.3.6', '1.3.3.7']\n",
|
||
|
"[]\n",
|
||
|
"1.3.4\n",
|
||
|
"['1.3.4.1', '1.3.4.2', '1.3.4.3']\n",
|
||
|
"[58, 77, 80, 81, 130]\n",
|
||
|
"1.3.5\n",
|
||
|
"['1.3.5.1']\n",
|
||
|
"[114, 130, 151]\n",
|
||
|
"1.4.1\n",
|
||
|
"['1.4.1.1', '1.4.1.2', '1.4.1.3', '1.4.1.4', '1.4.1.5']\n",
|
||
|
"[]\n",
|
||
|
"1.4.2\n",
|
||
|
"['1.4.2.1', '1.4.2.2', '1.4.2.3', '1.4.2.4', '1.4.2.5', '1.4.2.6', '1.4.2.7']\n",
|
||
|
"[]\n",
|
||
|
"1.4.3\n",
|
||
|
"['1.4.3.1', '1.4.3.2', '1.4.3.3', '1.4.3.4', '1.4.3.5', '1.4.3.6']\n",
|
||
|
"[]\n",
|
||
|
"1.4.4\n",
|
||
|
"['1.4.4.1', '1.4.4.2', '1.4.4.3', '1.4.4.4', '1.4.4.5']\n",
|
||
|
"[]\n",
|
||
|
"1.4.5\n",
|
||
|
"['1.4.5.1', '1.4.5.2', '1.4.5.3', '1.4.5.4', '1.4.5.5', '1.4.5.6', '1.4.5.7', '1.4.5.8', '1.4.5.9']\n",
|
||
|
"[]\n",
|
||
|
"2\n",
|
||
|
"['2.1', '2.2', '2.3']\n",
|
||
|
"[]\n",
|
||
|
"2.1.1\n",
|
||
|
"['2.1.1.1', '2.1.1.2', '2.1.1.3', '2.1.1.4', '2.1.1.5']\n",
|
||
|
"[22, 38, 60, 78, 80, 85, 99, 106, 117, 131, 143, 148]\n",
|
||
|
"2.1.2\n",
|
||
|
"['2.1.2.1', '2.1.2.2', '2.1.2.3', '2.1.2.4']\n",
|
||
|
"[6, 33, 47, 49, 58, 62, 79, 81, 82, 84, 99, 144, 149, 159, 165]\n",
|
||
|
"2.1.3\n",
|
||
|
"['2.1.3.1', '2.1.3.2', '2.1.3.3', '2.1.3.4', '2.1.3.5', '2.1.3.6', '2.1.3.7']\n",
|
||
|
"[13, 22, 26, 73, 74, 79, 97, 99, 106, 108, 126, 135, 148]\n",
|
||
|
"2.1.4.1\n",
|
||
|
"['2.1.4.1.1', '2.1.4.1.2', '2.1.4.1.3', '2.1.4.1.4']\n",
|
||
|
"[13, 22, 31, 45, 79, 81, 84, 99, 102, 115, 117, 137, 154, 163]\n",
|
||
|
"2.1.4.2\n",
|
||
|
"['2.1.4.2.1', '2.1.4.2.2']\n",
|
||
|
"[13, 22, 31, 45, 79, 81, 84, 99, 102, 115, 117, 137, 154, 163]\n",
|
||
|
"2.3\n",
|
||
|
"['2.3.1', '2.3.2', '2.3.3']\n",
|
||
|
"[13, 16, 22, 23, 57, 79, 84, 95, 99, 124, 126, 155, 161, 168]\n",
|
||
|
"1\n",
|
||
|
"['1.1', '1.2', '1.3', '1.4', '2']\n",
|
||
|
"[]\n",
|
||
|
"1.3\n",
|
||
|
"['1.3.1', '1.3.2', '1.3.3', '1.3.4', '1.3.5']\n",
|
||
|
"[]\n",
|
||
|
"1.4\n",
|
||
|
"['1.4.1', '1.4.2', '1.4.3', '1.4.4', '1.4.5']\n",
|
||
|
"[]\n",
|
||
|
"2\n",
|
||
|
"['2.1', '2.2', '2.3']\n",
|
||
|
"[]\n",
|
||
|
"2.1\n",
|
||
|
"['2.1.1', '2.1.2', '2.1.3', '2.1.4']\n",
|
||
|
"[]\n",
|
||
|
"2.1.4\n",
|
||
|
"['2.1.4.1', '2.1.4.2']\n",
|
||
|
"[13, 22, 31, 45, 79, 81, 84, 99, 102, 115, 117, 137, 154, 163]\n",
|
||
|
"1\n",
|
||
|
"['1.1', '1.2', '1.3', '1.4', '2']\n",
|
||
|
"[]\n",
|
||
|
"2\n",
|
||
|
"['2.1', '2.2', '2.3']\n",
|
||
|
"[]\n",
|
||
|
"2.1\n",
|
||
|
"['2.1.1', '2.1.2', '2.1.3', '2.1.4']\n",
|
||
|
"[99]\n",
|
||
|
"1\n",
|
||
|
"['1.1', '1.2', '1.3', '1.4', '2']\n",
|
||
|
"[]\n",
|
||
|
"2\n",
|
||
|
"['2.1', '2.2', '2.3']\n",
|
||
|
"[]\n",
|
||
|
"1\n",
|
||
|
"['1.1', '1.2', '1.3', '1.4', '2']\n",
|
||
|
"[]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dict_nodes = {0: sorted([node for node in G.nodes() if G.out_degree(node)==0])}\n",
|
||
|
"level = 1\n",
|
||
|
"while True:\n",
|
||
|
" nodes = [list(G.predecessors(node)) for node in dict_nodes[level-1]]\n",
|
||
|
" nodes = sorted(list(set([i for j in nodes for i in j])))\n",
|
||
|
" if nodes:\n",
|
||
|
" dict_nodes[level] = nodes\n",
|
||
|
" level += 1\n",
|
||
|
" else:\n",
|
||
|
" break\n",
|
||
|
"\n",
|
||
|
"Firm = pd.read_csv(\"Firm.csv\")\n",
|
||
|
"Firm.fillna(0, inplace=True)\n",
|
||
|
"Firm_copy = Firm.copy()\n",
|
||
|
"\n",
|
||
|
"for tier in list(dict_nodes.keys())[1:]:\n",
|
||
|
" for node in dict_nodes[tier]:\n",
|
||
|
" list_neighbors = list(G.neighbors(node))\n",
|
||
|
" firm_list = Firm_copy.index[(Firm_copy[list_neighbors]==1).all(axis=1)].to_list()\n",
|
||
|
" if firm_list:\n",
|
||
|
" Firm_copy.loc[firm_list, node] = 1\n",
|
||
|
" Firm_copy.loc[firm_list, list_neighbors] = 0\n",
|
||
|
"Firm_copy.to_csv('Firm_amended.csv', index=False, encoding='utf-8-sig')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"['2.1.1.1', '2.1.1.2', '2.1.1.3', '2.1.1.4', '2.1.1.5', '2.1.1']"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"list(nx.dfs_postorder_nodes(G, '2.1.1'))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 71,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[1, 2, 3, 4, 5]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 71,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"list(dict_nodes.keys())[1:]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "base",
|
||
|
"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.8.8"
|
||
|
},
|
||
|
"orig_nbformat": 4,
|
||
|
"vscode": {
|
||
|
"interpreter": {
|
||
|
"hash": "bcdafc093860683ffb58d6956591562b7f8ed5d58147d17d71a5d4d6605a08df"
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|