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]:
<module 'BayesianDataStructure' from 'BayesianDataStructure.py'>

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)


DEBUG:root:total edges:21
DEBUG:root:HasEducation<->HasPaid: 0.184299291087
DEBUG:root:HasEducation<->GEO: 0.224299545108
DEBUG:root:HasEducation<->SKU: 1.01082902517
DEBUG:root:HasEducation<->OS: 0.0221935378469
DEBUG:root:HasEducation<->AppType: 0.0461421242789
DEBUG:root:HasEducation<->Agent: 0.125311573842
DEBUG:root:HasPaid<->GEO: 0.187183188029
DEBUG:root:HasPaid<->SKU: 0.188524051342
DEBUG:root:HasPaid<->OS: 0.00306242986807
DEBUG:root:HasPaid<->AppType: 0.00559801943448
DEBUG:root:HasPaid<->Agent: 0.0297473834999
DEBUG:root:GEO<->SKU: 0.271692030531
DEBUG:root:GEO<->OS: 0.0291361795478
DEBUG:root:GEO<->AppType: 0.0634628605984
DEBUG:root:GEO<->Agent: 0.186274211285
DEBUG:root:SKU<->OS: 0.0278315850599
DEBUG:root:SKU<->AppType: 0.0675524603726
DEBUG:root:SKU<->Agent: 0.172502688932
DEBUG:root:OS<->AppType: 0.267021526399
DEBUG:root:OS<->Agent: 0.771003582722
DEBUG:root:AppType<->Agent: 1.29986077215
DEBUG:root:all valid edges:15
DEBUG:root:Graph ends, edges left:6

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)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-63-29cb4e3e151e> in <module>()
      1 bg = bynt.BasicGraph
      2 paths = bg.GetPathBetweenTwoNodes(bg.NodesList[0], bg.NodesList[6])
----> 3 neibors = bg.GetNeighborsInPathBetweenTwoNodes(bg.NodesList[0], bg.NodesList[6])
      4 for line in paths:
      5     print map(lambda x:bg.NodesList[x].NodeName, line)

AttributeError: Graph instance has no attribute 'GetNeighborsInPathBetweenTwoNodes'