import re


def read_merged_file(given_file):
	"""
	This function is to read prediction and label values from one file.
	
	Assumptions
	-----------
	1. The dimension of each vectro should have same dimension.
	2. 'given_file' must be sorted in increasing order by problem index.
	  ex) given_file
	      1_00001,0.239,0
	      1_00002,0.931,1
	      1_00003,...
	
	Parameters
	----------
	given_file : merged (prediction and label) file name (string)
		
	Returns
	-------
	pred    : Prediction vector (single precision)
	target  : Label vector      (single precision or boolean)
	
	Raises
	------
	Exception : The dimension of two files should be same
	"""

	splitre = re.compile('[,\s]')
	pred = []
	target = []

	with open(given_file, "r") as pl_fid:
		for plline in pl_fid:
			triples = splitre.split(plline.strip())
			if len(triples) != 3:
				continue
			pred.append(float(triples[1]))
			target.append(int(triples[2]))
		pl_fid.close()

	if len(pred) != len(target) or len(pred) == 0 or len(target) == 0:
		raise Exception("ERROR: the number of lines in the prediction file and label file is not the same: len(pred) - "\
                        + str(len(pred)) + ", len(target) - " + str(len(target)))

	return pred, target