import matplotlib.pyplot as plt

import networkx as nx

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from subprocess import check_output
comments = pd.read_csv('../input/comment.csv')
likes = pd.read_csv('../input/like.csv')
members = pd.read_csv('../input/member.csv')
posts = pd.read_csv('../input/post.csv')

likeResponse = pd.merge(likes.loc[likes['gid'] == 117291968282998], posts.loc[posts['gid'] == 117291968282998, ['pid', 'name']], left_on='pid', right_on='pid')
result = likeResponse.groupby(['name_y', 'name_x'])['response'].count()
finalResult = pd.DataFrame(result.index.values, columns=['NameCombo'])
finalResult['Weight'] = result.values
finalResult['From'] = finalResult['NameCombo'].map(lambda x: x[0])
finalResult['To'] = finalResult['NameCombo'].map(lambda x: x[1])
del finalResult['NameCombo']
g = nx.Graph()
g.add_edges_from([(row['From'], row['To']) for (index, row) in finalResult.iterrows()])
d = nx.degree(g)
spring_pos = nx.spring_layout(g)
plt.axis('off')
plt.clf()

f = open('g.json', 'w')
f.write('{"nodes":[')
str1 = ''
for i in finalResult['From'].unique():
    str1 += '{"name":"' + str(i) + '","group":' + str(1) + '},'
f.write(str1[:-1])
f.write('],"links":[')
str1 = ''
for i in range(len(finalResult)):
    str1 += '{"source":' + str(finalResult['From'][i]) + ',"target":' + str(finalResult['To'][i]) + ',"value":' + str(finalResult['Weight'][i]) + '},'
f.write(str1[:-1])
f.write(']}')
f.close
h1 = '\n<!DOCTYPE html>\n<meta charset="utf-8">\n<style>\n.link {stroke: #ccc;}\n.node text {pointer-events: none;  font: 10px sans-serif;}\n</style>\n<body>\n<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>\n<script>\nvar width = 800, height = 800;\nvar color = d3.scale.category20();\nvar force = d3.layout.force()\n    .charge(-120)\n    .linkDistance(80)\n    .size([width, height]);\nvar svg = d3.select("body").append("svg")\n    .attr("width", width)\n    .attr("height", height);\nd3.json("g.json", function(error, graph) {\n  if (error) throw error;\n\tforce.nodes(graph.nodes)\n\t    .links(graph.links)\n\t    .start();\n\tvar link = svg.selectAll(".link")\n\t    .data(graph.links)\n\t    .enter().append("line")\n\t    .attr("class", "link")\n\t    .style("stroke-width", function (d) {return Math.sqrt(d.value);});\n\tvar node = svg.selectAll(".node")\n\t    .data(graph.nodes)\n\t    .enter().append("g")\n\t    .attr("class", "node")\n\t    .call(force.drag);\n\tnode.append("circle")\n\t    .attr("r", 8)\n\t    .style("fill", function (d) {return color(d.group);})\n\tnode.append("text")\n\t      .attr("dx", 10)\n\t      .attr("dy", ".35em")\n\t      .text(function(d) { return d.name });\n\tforce.on("tick", function () {\n\t    link.attr("x1", function (d) {return d.source.x;})\n\t\t.attr("y1", function (d) {return d.source.y;})\n\t\t.attr("x2", function (d) {return d.target.x;})\n\t\t.attr("y2", function (d) {return d.target.y;});\n\t    d3.selectAll("circle").attr("cx", function (d) {return d.x;})\n\t\t.attr("cy", function (d) {return d.y;});\n\t    d3.selectAll("text").attr("x", function (d) {return d.x;})\n\t\t.attr("y", function (d) {return d.y;});\n  });\n});\n</script>\n'
f = open('output.html', 'w')
f.write(h1)
f.close