#NEWLINE# def main( ) : #NEWLINE# #INDENT# import os #NEWLINE# import logging #NEWLINE# import subprocess #NEWLINE# from optparse import OptionParser #NEWLINE# from mrec.evaluation.preprocessing import TSVParser , SplitCreator #NEWLINE# from filename_conventions import get_sortedfile , get_splitfile #NEWLINE# logging . basicConfig ( level = logging . INFO , format = '[%(asctime)s] %(levelname)s: %(message)s' ) #NEWLINE# parser = OptionParser ( ) #NEWLINE# parser . add_option ( '--dataset' , dest = 'dataset' , help = 'path to input dataset in tsv format' ) #NEWLINE# parser . add_option ( '--delimiter' , dest = 'delimiter' , default = '\t' , help = 'input delimiter (default: tab)' ) #NEWLINE# parser . add_option ( '--outdir' , dest = 'outdir' , help = 'directory for output files' ) #NEWLINE# parser . add_option ( '--num_splits' , dest = 'num_splits' , type = 'int' , default = 5 , help = 'number of train/test splits to create (default: %default)' ) #NEWLINE# parser . add_option ( '--min_items_per_user' , dest = 'min_items_per_user' , type = 'int' , default = 10 , help = 'skip users with less than this number of ratings (default: %default)' ) #NEWLINE# parser . add_option ( '--binarize' , dest = 'binarize' , action = 'store_true' , default = False , help = 'binarize ratings' ) #NEWLINE# parser . add_option ( '--normalize' , dest = 'normalize' , action = 'store_true' , help = 'scale training ratings to unit norm' ) #NEWLINE# parser . add_option ( '--rating_thresh' , dest = 'rating_thresh' , type = 'float' , default = 0 , help = 'treat ratings below this as zero (default: %default)' ) #NEWLINE# parser . add_option ( '--test_size' , dest = 'test_size' , type = 'float' , default = 0.5 , help = 'target number of test items for each user, if test_size >= 1 treat as an absolute number, otherwise treat as a fraction of the total items (default: %default)' ) #NEWLINE# parser . add_option ( '--discard_zeros' , dest = 'discard_zeros' , action = 'store_true' , help = 'discard zero training ratings after thresholding (not recommended, incompatible with using training items to guarantee that recommendations are novel)' ) #NEWLINE# parser . add_option ( '--sample_before_thresholding' , dest = 'sample_before_thresholding' , action = 'store_true' , help = 'choose test items before thresholding ratings (not recommended, test items below threshold will then be discarded)' ) #NEWLINE# ( opts , args ) = parser . parse_args ( ) #NEWLINE# if ( ( not opts . dataset ) or ( not opts . outdir ) ) : #NEWLINE# #INDENT# parser . print_help ( ) #NEWLINE# raise SystemExit #NEWLINE# #UNINDENT# opts . dataset = os . path . abspath ( opts . dataset ) #NEWLINE# opts . outdir = os . path . abspath ( opts . outdir ) #NEWLINE# logging . info ( 'sorting input data...' ) #NEWLINE# infile = get_sortedfile ( opts . dataset , opts . outdir ) #NEWLINE# subprocess . check_call ( [ 'mkdir' , '-p' , opts . outdir ] ) #NEWLINE# subprocess . check_call ( [ 'sort' , '-k1' , '-n' , opts . dataset ] , stdout = open ( infile , 'w' ) ) #NEWLINE# parser = TSVParser ( thresh = opts . rating_thresh , binarize = opts . binarize , delimiter = opts . delimiter ) #NEWLINE# splitter = SplitCreator ( test_size = opts . test_size , normalize = opts . normalize , discard_zeros = opts . discard_zeros , sample_before_thresholding = opts . sample_before_thresholding ) #NEWLINE# processor = Processor ( splitter , parser , opts . min_items_per_user ) #NEWLINE# for i in xrange ( opts . num_splits ) : #NEWLINE# #INDENT# trainfile = get_splitfile ( opts . dataset , opts . outdir , 'train' , i ) #NEWLINE# testfile = get_splitfile ( opts . dataset , opts . outdir , 'test' , i ) #NEWLINE# logging . info ( 'creating split {0}: {1} {2}' . format ( i , trainfile , testfile ) ) #NEWLINE# processor . create_split ( open ( infile ) , open ( trainfile , 'w' ) , open ( testfile , 'w' ) ) #NEWLINE# too_few_items = processor . get_too_few_items ( ) #NEWLINE# if too_few_items : #NEWLINE# #INDENT# logging . info ( 'skipped {0} users with less than {1} ratings' . format ( too_few_items , opts . min_items_per_user ) ) #NEWLINE# #UNINDENT# #UNINDENT# logging . info ( 'cleaning up...' ) #NEWLINE# subprocess . check_call ( [ 'rm' , infile ] ) #NEWLINE# logging . info ( 'done' )