# LICENSE
# Copyright (c) 2013-2016, <NAME> (<EMAIL>)
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import argparse
import random

from csv import CsvDataset

cl_parser = argparse.ArgumentParser( description="Creates random subsets of faults" )
cl_parser.add_argument( "input_file", type=str, nargs=1,
						help="The input file, generated by aires-summarize-episode.py" )
cl_parser.add_argument( "--name", type=str, default=None,
					  help="The root name of the dataset. Individual file names are derived from this." )
cl_parser.add_argument( "-N", type=int, default=0, help="Size of each subset" )
cl_parser.add_argument( "-k", type=int, default=1, help="Number of subsets for cross-validation" )
cl_parser.add_argument( "--seed", type=int, default=0, help="RNG seed (default: no particular seed)" )
args = cl_parser.parse_args()

if args.N == 0 and args.k != 1:
	raise RuntimeError( "If args.N == 0, then args.k must be 1" )

with open( args.input_file[0] ) as input_file:
	data = CsvDataset( input_file )
	t_blackout = data.attribute_index( "t_blackout" )
	faults = data.attribute_index( "faults" )
	recoverable_faults = []
	for fv in data.feature_vectors:
		t = int(fv[t_blackout])
		if t > 10 and t < 311:
			recoverable_faults.append( fv[faults] )
	if args.N * args.k > len(recoverable_faults):
		raise AssertionError( "Not enough faults for requested dataset size" )
	rng = random.Random()
	if args.seed != 0:
		rng.seed( args.seed )
	rng.shuffle( recoverable_faults )
	next = 0
	for i in range(0, args.k):
		filename = args.name + str(i) + ".txt"
		with open( filename, "w" ) as fold_i:
			N = len(recoverable_faults) if args.N == 0 else args.N
			for j in range(0, N):
				fold_i.write( recoverable_faults[next] + "\n" )
				next += 1

# LICENSE
# Copyright (c) 2013-2016, <NAME> (<EMAIL>)
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import argparse
import random

from csv import CsvDataset

cl_parser = argparse.ArgumentParser( description="Creates random subsets of faults" )
cl_parser.add_argument( "input_file", type=str, nargs=1,
						help="The input file, generated by aires-summarize-episode.py" )
cl_parser.add_argument( "--name", type=str, default=None,
					  help="The root name of the dataset. Individual file names are derived from this." )
cl_parser.add_argument( "-N", type=int, default=0, help="Size of each subset" )
cl_parser.add_argument( "-k", type=int, default=1, help="Number of subsets for cross-validation" )
cl_parser.add_argument( "--seed", type=int, default=0, help="RNG seed (default: no particular seed)" )
args = cl_parser.parse_args()

if args.N == 0 and args.k != 1:
	raise RuntimeError( "If args.N == 0, then args.k must be 1" )

with open( args.input_file[0] ) as input_file:
	data = CsvDataset( input_file )
	t_blackout = data.attribute_index( "t_blackout" )
	faults = data.attribute_index( "faults" )
	recoverable_faults = []
	for fv in data.feature_vectors:
		t = int(fv[t_blackout])
		if t > 10 and t < 311:
			recoverable_faults.append( fv[faults] )
	if args.N * args.k > len(recoverable_faults):
		raise AssertionError( "Not enough faults for requested dataset size" )
	rng = random.Random()
	if args.seed != 0:
		rng.seed( args.seed )
	rng.shuffle( recoverable_faults )
	next = 0
	for i in range(0, args.k):
		filename = args.name + str(i) + ".txt"
		with open( filename, "w" ) as fold_i:
			N = len(recoverable_faults) if args.N == 0 else args.N
			for j in range(0, N):
				fold_i.write( recoverable_faults[next] + "\n" )
				next += 1

# LICENSE # Copyright (c) 2013-2016, <NAME> (<EMAIL>) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.