import vtk
import numpy as N
from vtk.util import numpy_support as VN
import time

currGraph = vtk.vtkDirectedGraph()
prevGraph = vtk.vtkDirectedGraph()

reader = vtk.vtkDelimitedTextReader()
reader.SetDetectNumericColumns(True)
reader.SetHaveHeaders(True)
reader.SetFileName('/Volumes/SciVis_LargeData/ArtMarkets/judicial/judicial.csv')
reader.GetFieldDelimiterCharacters()
reader.Update()

table = reader.GetOutput()
year = VN.vtk_to_numpy(table.GetColumnByName('year'))
caseid = VN.vtk_to_numpy(table.GetColumnByName('caseid'))

edges = vtk.vtkDelimitedTextReader()
edges.DetectNumericColumnsOn()
edges.SetFileName('/Volumes/SciVis_LargeData/ArtMarkets/judicial/allcites.txt')
edges.SetFieldDelimiterCharacters(' ')
edges.Update()

edges_table = edges.GetOutput()
start_node = VN.vtk_to_numpy(edges_table.GetColumn(0))
end_node = VN.vtk_to_numpy(edges_table.GetColumn(1))

# This array is the same length as the number of edges
# and denotes the year in which the start node was decided
# 	col3 = VN.numpy_to_vtk(year[year < yr], deep=True)
# 	col3.SetName('year')
# 	node_table.AddColumn(col2)
# 	node_table.AddColumn(col3)
	
	# print '\tConverting table to graph'
	tgraph = vtk.vtkTableToGraph()
	tgraph.SetDirected(True)
	tgraph.SetInput(table)
	tgraph.AddLinkVertex('start_node', 'case_id', False)
	tgraph.AddLinkVertex('end_node', 'case_id', False)
	tgraph.AddLinkEdge('start_node', 'end_node')
	tgraph.SetVertexTableConnection(node_table.GetProducerPort())
	tgraph.Update()
	
	rawGraph = vtk.vtkDirectedGraph()
	rawGraph.DeepCopy(tgraph.GetOutput())

	# Put in positions for already layed-out vertices
	# print '\tAssigning previous coordinates and constraints'
	if yr != years_list[0]:
		currPoints = rawGraph.GetPoints().GetData()
		currIDs = rawGraph.GetVertexData().GetArray('case_id')
		
		for ii in range(currPoints.GetNumberOfTuples()):
			id = currIDs.GetTuple1(ii)
			if id in point_pos:
				pp = point_pos[id]
				currPoints.SetTuple3(ii, pp[0], pp[1], pp[2])
			else:
				pp = 0.1*N.random.randn(3)