draw g_bom g_firm

This commit is contained in:
unknown 2023-07-13 11:44:13 +08:00
commit b382b686f7
4 changed files with 111 additions and 0 deletions

105
LoadNet.py Normal file
View File

@ -0,0 +1,105 @@
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
df = pd.read_csv('data\\temp.csv')
df['succ_equipment_code'] = df['equipment_code'].str.rsplit('.', n=1)
df['succ_equipment_code'] = df['succ_equipment_code'].apply(lambda lst: lst[0] if len(lst)>1 else None)
assert len(df['equipment_code']) == len((df['equipment_code'].unique())),\
"column equipment_code contains duplicate value"
df.set_index('equipment_code', inplace=True)
# graph bom
g_bom = nx.DiGraph()
g_bom.add_nodes_from(df.index)
# edge
for index, row in df.iterrows():
if row['succ_equipment_code']:
g_bom.add_edge(index, row['succ_equipment_code'])
# attr
df_attr = df.loc[:, ['equipment_name']]
dct_attr = {}
for code in df_attr.index:
dct_attr[code] = df_attr.loc[code].to_dict()
nx.set_node_attributes(g_bom, dct_attr)
# show
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.figure(figsize=(12, 12), dpi=300)
pos = nx.nx_agraph.graphviz_layout(g_bom,
prog='twopi',
args='')
node_label = nx.get_node_attributes(g_bom, 'equipment_name')
nx.draw(g_bom,
pos,
labels=node_label,
font_size=6)
plt.savefig('g_bom_20230712.png')
plt.close()
# graph firm
s_firm = pd.Series(df['firm_name'].unique())
g_firm = nx.MultiDiGraph()
g_firm.add_nodes_from(s_firm.index)
# attr
dct_attr = {}
for firm_code in s_firm.index:
firm_name = s_firm[firm_code]
lst_prod_code = df.index[df['firm_name'] == firm_name].to_list()
dct_attr[firm_code] = {'firm_name': firm_name,
'lst_prod_code': lst_prod_code}
nx.set_node_attributes(g_firm, dct_attr)
print(g_firm.nodes)
print(g_firm.nodes[0])
# edge
for prod_code, row in df.iterrows():
firm_name = row['firm_name']
firm_code = s_firm.index[s_firm == firm_name].to_list()[0]
for succ_prod_code in g_bom.successors(prod_code):
# 目前df中一个prod code只对应一个firm
succ_firm_name = df.loc[df.index == succ_prod_code, 'firm_name'].to_list()[0]
succ_firm_code = s_firm.index[s_firm == succ_firm_name].to_list()[0]
g_firm.add_edges_from([(firm_code, succ_firm_code, {'product': prod_code})])
print((firm_name, succ_firm_name, prod_code))
# show
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.figure(figsize=(12, 12), dpi=300)
pos = nx.nx_agraph.graphviz_layout(g_firm,
prog='twopi',
args='')
node_label = nx.get_node_attributes(g_firm, 'firm_name')
nx.draw(g_firm,
pos,
labels=node_label,
font_size=6)
edge_label = nx.get_edge_attributes(g_firm, 'product')
edge_label = {(n1, n2): label for (n1, n2, _), label in edge_label.items()}
nx.draw_networkx_edge_labels(g_firm,
pos,
edge_label,
font_size=4)
plt.savefig('g_firm_20230712.png')
plt.close()
# pos = nx.random_layout(g_firm)
# nx.draw_networkx_nodes(g_firm, pos, node_color = 'r', node_size = 100, alpha = 1)
# ax = plt.gca()
# for e in g_firm.edges:
# ax.annotate("",
# xy=pos[e[0]], xycoords='data',
# xytext=pos[e[1]], textcoords='data',
# arrowprops=dict(arrowstyle="->", color="0.5",
# shrinkA=5, shrinkB=5,
# patchA=None, patchB=None,
# connectionstyle="arc3,rad=rrr".replace('rrr',str(0.3*e[2])
# ),
# ),
# )
# plt.axis('off')
# plt.show()

6
data/temp.csv Normal file
View File

@ -0,0 +1,6 @@
equipment_code,equipment_model,equipment_name,is_set,set_level,set_type,set_time,document,product_level,product_type,product_category,firm_name,firm_code,is_supervise,supervise_name,supervise_expertise,supervise_expertise_other,software_set_version,software_lastest_version,supervise_contract_type,is_pred_firm,contact_name,contact_phone,comment,status,is_issued,task_source
1,XXX1,XXX1,,,,,,,,,firm1,,,,,,,,,,,,,,,
1.1,XXX1,XXX1-1,,,,,,,,,firm2,,,,,,,,,,,,,,,
1.2,XXX1,XXX1-2,,,,,,,,,firm2,,,,,,,,,,,,,,,
1.3,XXX1,XXX1-3,,,,,,,,,firm3,,,,,,,,,,,,,,,
1.1.1,XXX1,XXX1-1-1,,,,,,,,,firm4,,,,,,,,,,,,,,,
1 equipment_code equipment_model equipment_name is_set set_level set_type set_time document product_level product_type product_category firm_name firm_code is_supervise supervise_name supervise_expertise supervise_expertise_other software_set_version software_lastest_version supervise_contract_type is_pred_firm contact_name contact_phone comment status is_issued task_source
2 1 XXX1 XXX1 firm1
3 1.1 XXX1 XXX1-1 firm2
4 1.2 XXX1 XXX1-2 firm2
5 1.3 XXX1 XXX1-3 firm3
6 1.1.1 XXX1 XXX1-1-1 firm4

BIN
g_bom_20230712.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

BIN
g_firm_20230712.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB