#!/usr/bin/python

from ChangePointTools import *
from cPickle import Unpickler
import sys

def getfile( filename ) :
	file=open( filename )
	u=Unpickler(file)
	data=u.load()
	file.close()
	return data

try:
	statesfile = sys.argv[1]
	states = getfile( statesfile )
except:
	print "usage: %s <states file>" % sys.argv[0]
	sys.exit()

# go through and assign the segments, tallying the N's and C's into the states. This
# then allows calculation of the average emission for each state (Ctot/Ntot)
trajectory = states.data
trajectory.timesort()
statetrajectory=[]
for segment in trajectory :
	i=0
	currpartition=states.statePartitions[i]
	while segment.lam >= states.statePartitions[i] :
		i = i+1
		try: currpartition=states.statePartitions[i]
		except IndexError: break 
	try: cutoff = states.statePartitions[i]
	except IndexError: cutoff = 100000
	statetrajectory.extend( [i] * segment.N )

for state in statetrajectory: print state
