In [1]:
import unittest
import jgraph
import os
In [2]:
import networkx as nx
import matplotlib.pyplot as plt
In [56]:
os.chdir(r"D:\projects\data_compression\BayesianNetworkPython")
import BayesianDataStructure
reload(BayesianDataStructure)
Out[56]:
In [16]:
def draw_graph(graph, labels=None, graph_layout='shell',
node_size=1600, node_color='blue', node_alpha=0.3,
node_text_size=12,
edge_color='blue', edge_alpha=0.3, edge_tickness=1,
edge_text_pos=0.3,
text_font='sans-serif'):
# create networkx graph
G=nx.Graph()
# add edges
for edge in graph:
G.add_edge(edge[0], edge[1])
# these are different layouts for the network you may try
# shell seems to work best
if graph_layout == 'spring':
graph_pos=nx.spring_layout(G)
elif graph_layout == 'spectral':
graph_pos=nx.spectral_layout(G)
elif graph_layout == 'random':
graph_pos=nx.random_layout(G)
else:
graph_pos=nx.shell_layout(G)
# draw graph
nx.draw_networkx_nodes(G,graph_pos,node_size=node_size,
alpha=node_alpha, node_color=node_color)
nx.draw_networkx_edges(G,graph_pos,width=edge_tickness,
alpha=edge_alpha,edge_color=edge_color)
nx.draw_networkx_labels(G, graph_pos,font_size=node_text_size,
font_family=text_font)
if labels is None:
labels = range(len(graph))
edge_labels = dict(zip(graph, labels))
nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels,
label_pos=edge_text_pos)
# show graph
plt.show()
In [64]:
dp = BayesianDataStructure.DataParser()
dp.readCsvFile(r"D:\projects\data_compression\BayesianNetworkPython\DATA\AggUsage_OneDriveForBusiness_Aggregated_selectColumns.txt", True, 300000)
Measurements = map(lambda x: float(x), dp.ColumnVals[-1])
bynt = BayesianDataStructure.BayesianNetwork(dp.ColumnNames[:-1], dp.ColumnVals[:-1], Measurements)
bynt.Draft()
# if edge labels is not specified, numeric labels (0, 1, 2...) will be used
#draw_graph(graph)
In [58]:
bynt.Thickening()
In [62]:
graph, CMIs = bynt.BasicGraph.GetEdgesAndCMIs()
# you may name your edge labels
CMIs4f = map(lambda x:"%.4f"%x, CMIs)
draw_graph(graph, CMIs4f)
In [63]:
bg = bynt.BasicGraph
paths = bg.GetPathBetweenTwoNodes(bg.NodesList[0], bg.NodesList[6])
neibors = bg.GetNeighborsInPathBetweenTwoNodes(bg.NodesList[0], bg.NodesList[6])
for line in paths:
print map(lambda x:bg.NodesList[x].NodeName, line)
print map(lambda x:x.NodeName, neibors)
for line in bg.Connections: print map(lambda x:1 if x else 0, line)