#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 22 21:53:46 2019

@author: kazuki.onodera
"""

import numpy as np
import pandas as pd
import os, gc
from multiprocessing import Pool

import utils


#for d in ['../data', '../feature']:
#    os.system(f'rm -rf {d}')
#    os.system(f'mkdir  {d}')


def main(args):
    
    input_path, output_path = args
    df = pd.read_csv(input_path, dtype=utils.DTYPES)
    
    di = {'on':0, 'audit':1}
    df['PuaMode'] = df['PuaMode'].map(lambda x: di.get(x, np.nan))
    
    # parse OsBuildLab
    # wired record in test -> 17134.1*amd64fre.rs4_release.180410-1804
    tmp = df['OsBuildLab'].map(lambda x: x.replace('*', '.').split('.') if isinstance(x, str) else [np.nan]*5)
    print(tmp.map(len).describe())
    df['OsBuildLab_major'] = tmp.map(lambda x: x[0]).astype(np.float64)
    df['OsBuildLab_minor'] = tmp.map(lambda x: x[1]).astype(np.float64)
    df['OsBuildLab_build'] = tmp.map(lambda x: x[2])
    df['OsBuildLab_architecture'] = tmp.map(lambda x: x[3])
    df['OsBuildLab_date'] = tmp.map(lambda x: x[4].split('-')[0] if isinstance(x[4], str) else np.nan).astype(np.float64)
    df['OsBuildLab_time'] = tmp.map(lambda x: x[4].split('-')[1] if isinstance(x[4], str) else np.nan).astype(np.float64)
    
    # SmartScreen
    di = {
          '00000000': '0', # tekiko~
          'enabled': 'on', # tekiko~
          'requiredadmin': 'requireadmin',
          'deny': 'off', # tekiko~
          'of': 'off',
          'promprt': 'prompt',
          }
    df['SmartScreen'] = df['SmartScreen'].str.lower()
    df['SmartScreen'].replace(di, inplace=True)
    
    
    utils.reduce_mem_usage(df)
    df.to_feather(output_path)
    
    if 'train' in input_path:
        df[['HasDetections']].to_feather('../data/target.f')
    
    return


# =============================================================================
# main
# =============================================================================
if __name__ == "__main__":
    utils.start(__file__)
    
    argss = []
    argss.append(['../input/train.csv.zip', '../data/train.f'])
    argss.append(['../input/test.csv.zip', '../data/test.f'])
    
    pool = Pool( 2 )
    pool.map(main, argss)
    pool.close()
    
    utils.end(__file__)


