#!/usr/bin/python
#encoding:utf8
'''
python resample.py -i plwn_pwn_sumo-24.10.2013-alg-v3-reduced_step1.csv -n 5 -s 100
'''

import random
import argparse
import ConfigParser
import io
from plumper.tools.evaluation.resample import *

def make_parser():
	desc = 'Sampler do pliku'
	parser = argparse.ArgumentParser(description = desc)
	parser.add_argument('-i', '--in-file', dest = 'in_file', required = True)
	parser.add_argument('-n', '--sample-count', dest = 'sample_count', required = True)
	parser.add_argument('-s', '--sample-size', dest = 'sample_size', required = True)
	parser.add_argument('-c', '--config', help='path to samlpe config file', default =None)
	return parser

def group_rows(lines):
	#grupowanie według typu relacji
	lines_dir = {}

	for line in lines:
		li = line.split(';')
		if li[-1] not in lines_dir:
			lines_dir[li[-1]] = []
		lines_dir[li[-1]].append(line)

	
	return lines_dir

def sample_weighted(lines_dir, s_number, s_size):
	samples = []
	lines_number = 0
	for i in lines_dir.keys():
			lines_number+= len(lines_dir[i])
	#losowanie dla grup tych samych relacji
	for key in lines_dir.keys():	
		ss = ( s_size * len(lines_dir[key]) )//lines_number
		for line in sample(lines_dir[key], s_number, ss):
			samples.append(line)
	#scalanie różnych relacji (tak aby w jednym pliku były wszystkie relacje)
	for n in range(0, s_number):
		for i in range(1, len(samples)/s_number):
			samples[n].extend(samples[n+i*s_number])
	while len(samples) > s_number:
		samples.remove(samples[-1])


	return samples

def sample_weighted_config(lines_dir, s_number, s_size, rel_weight):
	samples = []
	#losowanie dla grup tych samych relacji
	for key in rel_weight.keys():
		if key in lines_dir.keys():
			ss = int((float(rel_weight[key])*s_size))
			for line in sample(lines_dir[key], s_number, ss):
				samples.append(line)
	#scalanie różnych relacji (tak aby w jednym pliku były wszystkie relacje)
	for n in range(0, s_number):
		for i in range(1, len(samples)/s_number):
			samples[n].extend(samples[n+i*s_number])
	while len(samples) > s_number:
		samples.remove(samples[-1])

	return samples

def main(argv = None):
	parser = make_parser()
	args = parser.parse_args()
	
	lines = read_file(args.in_file)
	group_rows(lines)

	
	#samples = sample(lines, int(args.sample_count), int(args.sample_size))
	if args.config == None:
		samples = sample_weighted(group_rows(lines),
		 		int(args.sample_count),
		 		int(args.sample_size))
	else:
		config = ConfigParser.RawConfigParser()
		config.readfp(open(args.config))
		config_params = {}
		try:
			config_params['subsumed'] = config.get('sample', 'subsumed')
		except ConfigParser.NoOptionError:
			pass
		try:
			config_params['instance_of'] = config.get('sample','instance_of')
		except ConfigParser.NoOptionError:
			pass
		try:
			config_params['R'] = config.get('sample', 'R')
		except ConfigParser.NoOptionError:
			pass	
		try:
			config_params['equivalent'] = config.get('sample', 'equivalent')
		except ConfigParser.NoOptionError:
			pass
		
		samples = sample_weighted_config(group_rows(lines),
		 		int(args.sample_count), 
				int(args.sample_size), config_params  )
	
	save_samples(samples)


if __name__ == '__main__':
	main()
