# Import networkx and also matplotlib.pyplot for visualization
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline

"""
Explanation: How does networkx work?
End of explanation
"""


# Create an empty undirected graph
G = nx.Graph()

# Add some nodes and edges.  Adding edges aslo adds nodes if they don't already exist.
G.add_node('Janos')
G.add_nodes_from(['Sophie', 'Otto'])
G.add_edge('Janos', 'Sophie')
G.add_edges_from([('Janos', 'Otto'), ('Sophie', 'Otto')])

nx.draw_spectral(G, with_labels=True, node_size=3000)
plt.show()

"""
Explanation: Undirected Graphs
End of explanation
"""


# Create an empty directed graph
G = nx.DiGraph()

# Add some nodes and edges.  Adding edges aslo adds nodes if they don't already exist.
G.add_node('Janos')
G.add_nodes_from(['Sophie', 'Otto'])
G.add_edge('Janos', 'Sophie')
G.add_edges_from([('Janos', 'Otto'), ('Sophie', 'Otto')])

nx.draw_spectral(G, with_labels=True, node_size=3000)
plt.show()

"""
Explanation: Directed Graphs
End of explanation
"""


import numpy as np
G.add_node(np.mean)
file = open('abc.txt', 'w')
G.add_node(file)
print(G.nodes())

"""
Explanation: What can nodes be?
Nodes can be any hashable object: strings, integers, floats, functions, files, etc.
Both edges and nodes can store attributes in a dictionary.
End of explanation
"""


!head addHealth81.txt

D = nx.read_weighted_edgelist('addHealth81.txt', create_using=nx.DiGraph())

len(D.nodes()), len(D.edges())

"""
Explanation: Reading in Data
We don't usually add data by hand.
NetworkX handles the following (and more) very well:
* Standard files: txt, csv, json
* Graph formats: GML, gexf
* Python objects: dicts, Pandas DataFrames, lists of lists
End of explanation
"""


