def test_serialize_portfolio(self): """ Unit test serialize portfolio """ node1 = eao.assets.Node('node_1') node2 = eao.assets.Node('node_2') timegrid = eao.assets.Timegrid(dt.date(2021, 1, 1), dt.date(2021, 2, 1), freq='d') a1 = eao.assets.SimpleContract(name='SC_1', price='rand_price_1', nodes=node1, min_cap=-20., max_cap=20., start=dt.date(2021, 1, 10), end=dt.date(2021, 1, 20)) #a1.set_timegrid(timegrid) a2 = eao.assets.SimpleContract(name='SC_2', price='rand_price_1', nodes=node1, min_cap=-5., max_cap=10., extra_costs=1.) #a2.set_timegrid(timegrid) a3 = eao.assets.SimpleContract(name='SC_3', price='rand_price_2', nodes=node2, min_cap=-10., max_cap=10., extra_costs=1., start=dt.date(2021, 1, 10), end=dt.date(2021, 1, 25)) a4 = eao.assets.Transport(name='Tr', costs_const=1., nodes=[node1, node2], min_cap=0., max_cap=1.) a5 = eao.assets.Storage('storage', nodes = node1, \ start=dt.date(2021,1,1), end=dt.date(2021,2,1),size=10, \ cap_in=1.0/24.0, cap_out=1.0/24.0, start_level=5, end_level=5) #a3.set_timegrid(timegrid) prices = { 'rand_price_1': np.ones(timegrid.T) * 1., 'rand_price_2': np.ones(timegrid.T) * 10., 'rand_price_3': np.random.randn(timegrid.T) * 10. } portf = eao.portfolio.Portfolio([a1, a2, a3, a4, a5]) portf.set_timegrid(timegrid) mys = json.dumps(portf, indent=4, default=eao.serialization.json_serialize_objects) aa = json.loads(mys, object_hook=eao.serialization.json_deserialize_objects) # file activity myf = 'test_portf.json' to_json(portf, myf) x = load_from_json(file_name=myf) xx = load_from_json(mys) run_from_json(file_name_in=myf, prices=prices, file_name_out='test_results.xlsx') run_from_json(file_name_in = myf, prices = prices, file_name_out = 'test_results.csv',\ format_out='csv', csv_ger= True) res = run_from_json(file_name_in=myf, prices=prices) # array mys = json.dumps(prices, indent=4, default=json_serialize_objects) pp = json.loads(mys, object_hook=json_deserialize_objects) check = True for pr in prices: check = check and all(prices[pr] == pp[pr]) return check
for n in a.nodes: G.add_edge(a.name, n.name, label='') G.add_edge(n.name, a.name, label='') color_map_edges.append('grey') color_map_edges.append('grey') pos = nx.spring_layout(G) nx.draw_networkx_nodes(G,pos,node_color=color_map, node_size=node_sizes, \ alpha = 0.7) nx.draw_networkx_edges(G, pos, edge_color='grey') #color_map_edges) nx.draw_networkx_labels(G, pos, font_size=8) g_labels = nx.get_edge_attributes(G, 'label') nx.draw_networkx_edge_labels(G, pos, edge_labels=g_labels, font_size=8) if title is None: title = 'Network graph for portfolio' plt.title(title) plt.axis("off") if not file_name is None: plt.savefig(file_name) else: plt.show() plt.close() if __name__ == "__main__": myf = 'demo_portf.json' portf = load_from_json(file_name=myf) create_graph(portf=portf, file_name='test_graph.pdf')
def test_create_network(self): """ simple test to create network graph """ myf = 'tests/demo_portf.json' portf = load_from_json(file_name=myf) create_graph(portf=portf, file_name='tests/test_graph.pdf') return True