<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __email__ = '<STR_LIT>' <EOL> __date__ = '<STR_LIT>' <EOL> import utils <EOL> log = utils . start_logging ( __name__ ) <EOL> import json <EOL> import csv <EOL> import gc <EOL> import pandas as pd <EOL> import time <EOL> import os <EOL> from datetime import datetime <EOL> from sklearn . externals import joblib <EOL> def load_json ( file_path ) : <EOL> return [ json . loads ( line ) for line in open ( file_path ) ] <EOL> def load_flatfile ( file_path , delimiter = '<STR_LIT>' ) : <EOL> temp_array = [ ] <EOL> if delimiter == '<STR_LIT>' : <EOL> csv_reader = csv . reader ( open ( file_path ) ) <EOL> else : <EOL> csv_reader = csv . reader ( open ( file_path ) , delimiter ) <EOL> for line in csv_reader : <EOL> temp_array += line <EOL> return temp_array <EOL> def load_flatfile_to_df ( file_path , delimiter = '<STR_LIT>' ) : <EOL> if delimiter == '<STR_LIT>' : <EOL> return pd . read_csv ( file_path ) <EOL> else : <EOL> return pd . read_csv ( file_path , delimiter ) <EOL> def save_predictions ( df , target , model_name = '<STR_LIT>' , directory = '<STR_LIT>' , estimator_class = '<STR_LIT>' , note = '<STR_LIT>' ) : <EOL> timestamp = datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> filename = directory + timestamp + '<STR_LIT>' + model_name + '<STR_LIT:_>' + estimator_class + '<STR_LIT:_>' + note + '<STR_LIT>' <EOL> try : <EOL> df [ target ] = [ x [ <NUM_LIT:0> ] for x in df [ target ] ] <EOL> except IndexError : <EOL> df [ target ] = [ x for x in df [ target ] ] <EOL> df . ix [ : , [ '<STR_LIT:id>' , target ] ] . to_csv ( filename , index = False ) <EOL> log . info ( '<STR_LIT>' % filename ) <EOL> def save_combined_predictions ( df , directory , filename , note = '<STR_LIT>' ) : <EOL> try : <EOL> modified_date = time . strptime ( time . ctime ( os . path . getmtime ( directory + filename ) ) , '<STR_LIT>' ) <EOL> modified_date = datetime . fromtimestamp ( time . mktime ( modified_date ) ) . strftime ( '<STR_LIT>' ) <EOL> archived_file = directory + '<STR_LIT>' + filename [ : len ( filename ) - <NUM_LIT:4> ] + '<STR_LIT>' + modified_date + '<STR_LIT>' <EOL> os . rename ( directory + filename , archived_file ) <EOL> log . info ( '<STR_LIT>' + archived_file ) <EOL> except WindowsError : <EOL> pass <EOL> df . to_csv ( directory + filename , index = False ) <EOL> log . info ( '<STR_LIT>' % filename ) <EOL> def save_cached_object ( object , filename , directory = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if filename [ - <NUM_LIT:4> : ] != '<STR_LIT>' : <EOL> filename = filename + '<STR_LIT>' <EOL> try : <EOL> modified_date = time . strptime ( time . ctime ( os . path . getmtime ( directory + filename ) ) , '<STR_LIT>' ) <EOL> modified_date = datetime . fromtimestamp ( time . mktime ( modified_date ) ) . strftime ( '<STR_LIT>' ) <EOL> archived_file = directory + '<STR_LIT>' + filename [ : len ( filename ) - <NUM_LIT:4> ] + '<STR_LIT>' + modified_date + '<STR_LIT>' <EOL> os . rename ( directory + filename , archived_file ) <EOL> log . info ( '<STR_LIT>' + archived_file ) <EOL> except WindowsError : <EOL> pass <EOL> joblib . dump ( object , directory + filename , compress = <NUM_LIT:9> ) <EOL> log . info ( '<STR_LIT>' + directory + filename ) <EOL> def load_cached_object ( filename , directory = '<STR_LIT>' ) : <EOL> if filename [ - <NUM_LIT:4> : ] != '<STR_LIT>' : <EOL> filename = filename + '<STR_LIT>' <EOL> try : <EOL> object = joblib . load ( directory + filename ) <EOL> log . info ( '<STR_LIT>' + directory + filename ) <EOL> except IOError : <EOL> log . info ( '<STR_LIT>' + directory + filename ) <EOL> return object <EOL> def save_text_features ( output_file , feature_names ) : <EOL> o_f = open ( output_file , '<STR_LIT:wb>' ) <EOL> feature_names = '<STR_LIT:\n>' . join ( feature_names ) <EOL> o_f . write ( feature_names ) </s>
