database store graph
This commit is contained in:
parent
9435484e3f
commit
082864814b
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -8,7 +8,8 @@ import random
|
|||
import pandas as pd
|
||||
import numpy as np
|
||||
import platform
|
||||
|
||||
import networkx as nx
|
||||
import json
|
||||
|
||||
class ControllerDB:
|
||||
dct_parameter = None
|
||||
|
@ -41,6 +42,8 @@ class ControllerDB:
|
|||
Firm = pd.read_csv("Firm_amended.csv")
|
||||
Firm['Code'] = Firm['Code'].astype('string')
|
||||
Firm.fillna(0, inplace=True)
|
||||
|
||||
# fill dct_lst_init_remove_firm_prod
|
||||
list_dct = []
|
||||
for _, row in Firm.iterrows():
|
||||
code = row['Code']
|
||||
|
@ -52,19 +55,35 @@ class ControllerDB:
|
|||
list_dct = [{'133': ['1.4.4.1']}]
|
||||
# list_dct = [{'2': ['1.1.3']}]
|
||||
# list_dct = [{'135': ['1.3.2.1']}]
|
||||
|
||||
# fill g_bom
|
||||
BomNodes = pd.read_csv('BomNodes.csv', index_col=0)
|
||||
BomNodes.set_index('Code', inplace=True)
|
||||
BomCateNet = pd.read_csv('BomCateNet.csv', index_col=0)
|
||||
BomCateNet.fillna(0, inplace=True)
|
||||
g_bom = nx.from_pandas_adjacency(BomCateNet.T,
|
||||
create_using=nx.MultiDiGraph())
|
||||
bom_labels_dict = {}
|
||||
for code in g_bom.nodes:
|
||||
bom_labels_dict[code] = BomNodes.loc[code].to_dict()
|
||||
nx.set_node_attributes(g_bom, bom_labels_dict)
|
||||
g_product_js = json.dumps(nx.adjacency_data(g_bom))
|
||||
|
||||
# insert exp
|
||||
for idx_exp, dct in enumerate(list_dct):
|
||||
self.add_experiment_1(idx_exp, self.dct_parameter['n_max_trial'],
|
||||
dct)
|
||||
dct, g_product_js) # same g_bom for all exp
|
||||
print(f'Inserted experiment for exp {idx_exp}!')
|
||||
|
||||
def add_experiment_1(self, idx_exp, n_max_trial,
|
||||
dct_lst_init_remove_firm_prod):
|
||||
dct_lst_init_remove_firm_prod, g_bom):
|
||||
e = Experiment(
|
||||
idx_exp=idx_exp,
|
||||
n_sample=int(self.dct_parameter['n_sample']),
|
||||
n_iter=int(self.dct_parameter['n_iter']),
|
||||
n_max_trial=n_max_trial,
|
||||
dct_lst_init_remove_firm_prod=dct_lst_init_remove_firm_prod)
|
||||
dct_lst_init_remove_firm_prod=dct_lst_init_remove_firm_prod,
|
||||
g_bom=g_bom)
|
||||
db_session.add(e)
|
||||
db_session.commit()
|
||||
|
||||
|
|
26
model.py
26
model.py
|
@ -7,6 +7,7 @@ from firm import FirmAgent
|
|||
from product import ProductAgent
|
||||
from orm import db_session, Result
|
||||
import platform
|
||||
import json
|
||||
|
||||
|
||||
class Model(ap.Model):
|
||||
|
@ -20,18 +21,8 @@ class Model(ap.Model):
|
|||
self.dct_lst_remove_firm_prod = self.p.dct_lst_init_remove_firm_prod
|
||||
|
||||
# init graph bom
|
||||
BomNodes = pd.read_csv('BomNodes.csv', index_col=0)
|
||||
BomNodes.set_index('Code', inplace=True)
|
||||
BomCateNet = pd.read_csv('BomCateNet.csv', index_col=0)
|
||||
BomCateNet.fillna(0, inplace=True)
|
||||
|
||||
G_bom = nx.from_pandas_adjacency(BomCateNet.T,
|
||||
create_using=nx.MultiDiGraph())
|
||||
|
||||
bom_labels_dict = {}
|
||||
for code in G_bom.nodes:
|
||||
bom_labels_dict[code] = BomNodes.loc[code].to_dict()
|
||||
nx.set_node_attributes(G_bom, bom_labels_dict)
|
||||
G_bom = nx.adjacency_graph(json.loads(self.p.g_bom))
|
||||
self.product_network = ap.Network(self, G_bom)
|
||||
|
||||
# init graph firm
|
||||
Firm = pd.read_csv("Firm_amended.csv")
|
||||
|
@ -90,13 +81,8 @@ class Model(ap.Model):
|
|||
}) for succ_firm in lst_choose_firm]
|
||||
G_Firm.add_edges_from(lst_add_edge)
|
||||
|
||||
self.sample.g_firm = json.dumps(nx.adjacency_data(G_Firm))
|
||||
self.firm_network = ap.Network(self, G_Firm)
|
||||
self.product_network = ap.Network(self, G_bom)
|
||||
|
||||
out_file = open("myfile.json", "w")
|
||||
import json
|
||||
json.dump(nx.adjacency_data(G_Firm), out_file)
|
||||
out_file.close()
|
||||
|
||||
# init product
|
||||
for ag_node, attr in self.product_network.graph.nodes(data=True):
|
||||
|
@ -151,6 +137,10 @@ class Model(ap.Model):
|
|||
# draw network
|
||||
# self.draw_network()
|
||||
|
||||
out_file = open("myfile.json", "w")
|
||||
json.dump(nx.adjacency_data(G_Firm), out_file)
|
||||
out_file.close()
|
||||
|
||||
def update(self):
|
||||
self.a_lst_total_firms.clean_before_time_step()
|
||||
# output
|
||||
|
|
File diff suppressed because one or more lines are too long
BIN
network.png
BIN
network.png
Binary file not shown.
Before Width: | Height: | Size: 709 KiB After Width: | Height: | Size: 3.0 MiB |
Binary file not shown.
After Width: | Height: | Size: 3.0 MiB |
3
orm.py
3
orm.py
|
@ -46,6 +46,7 @@ class Experiment(Base):
|
|||
# variables
|
||||
n_max_trial = Column(Integer, nullable=False)
|
||||
dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False)
|
||||
g_bom = Column(Text(4294000000), nullable=False)
|
||||
|
||||
sample = relationship('Sample', back_populates='experiment', lazy='dynamic')
|
||||
|
||||
|
@ -65,7 +66,7 @@ class Sample(Base):
|
|||
ts_done = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
stop_t = Column(Integer, nullable=True)
|
||||
|
||||
firm_network = Column(Text(4294000000), nullable=True)
|
||||
g_firm = Column(Text(4294000000), nullable=True)
|
||||
|
||||
experiment = relationship('Experiment', back_populates='sample', uselist=False)
|
||||
result = relationship('Result', back_populates='sample', lazy='dynamic')
|
||||
|
|
|
@ -65,13 +65,6 @@
|
|||
"list_succ_firms = [1, 1]\n",
|
||||
"round(share * len(list_succ_firms)) if round(share * len(list_succ_firms)) > 0 else 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import networkx as nx
|
||||
import json
|
||||
|
||||
in_file = open("myfile.json", "r")
|
||||
js_file = json.load(in_file)
|
||||
in_file.close()
|
||||
firm_network = nx.adjacency_graph(js_file)
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.rcParams['font.sans-serif'] = 'SimHei'
|
||||
pos = nx.nx_agraph.graphviz_layout(firm_network, prog="twopi", args="")
|
||||
node_label = nx.get_node_attributes(firm_network, 'Name')
|
||||
# print(node_label)
|
||||
node_degree = dict(firm_network.out_degree())
|
||||
node_label = {
|
||||
key: f"{node_label[key]} {node_degree[key]}"
|
||||
for key in node_label.keys()
|
||||
}
|
||||
node_size = list(nx.get_node_attributes(firm_network, 'Revenue_Log').values())
|
||||
node_size = list(map(lambda x: x**2, node_size))
|
||||
edge_label = nx.get_edge_attributes(firm_network, "Product")
|
||||
# multi(di)graphs, the keys are 3-tuples
|
||||
edge_label = {(n1, n2): label for (n1, n2, _), label in edge_label.items()}
|
||||
plt.figure(figsize=(12, 12), dpi=300)
|
||||
nx.draw(firm_network, pos, node_size=node_size, labels=node_label, font_size=6)
|
||||
nx.draw_networkx_edge_labels(firm_network, pos, edge_label, font_size=4)
|
||||
plt.savefig("network1.png")
|
Loading…
Reference in New Issue