#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  3 18:18:43 2019

@author: Kazuki
"""

import numpy as np
import pandas as pd
from tqdm import tqdm
import os
from itertools import combinations
import utils
utils.start(__file__)
#==============================================================================


PREF = 'f021'

cat = 'AvSigVersion'

categorical_w_version = [
                         'EngineVersion',
                         'AppVersion',
                         'AvSigVersion',
                         'Census_OSVersion',
                        
                         ]

comb = list(combinations(categorical_w_version, 2))
comb = [col for col in comb if cat not in col]

stats = ['min', 'mean', 'max', 'median', 'std', 'skew']

def fe(input_path, output_path):
    """
    input_path  = '../data/train.f'
    output_path = '../data/train_{PREF}.f'
    """
    
    print('loading...', input_path)
    base = pd.read_feather(input_path)[categorical_w_version]
    
    feature = []
    cat_size = base.groupby(cat).size()
    
    for c2 in tqdm(comb):
        c2 = list(c2)
        c3 = [cat] + c2
        df = base.groupby(c3).size() / cat_size
        df = df.reset_index()
        df = df.groupby(cat)[0].agg(stats)
        df.columns = [f'{PREF}_{"-".join(c2)}-in-{cat}_{c}' for c in df.columns]
        
        feature.append( pd.merge(base[cat], df, on=cat, how='left').drop(cat, axis=1) )
    
    feature = pd.concat(feature, axis=1)
    print('writing...', output_path)
    feature.to_feather(output_path)
    
    return



fe('../data/train.f', f'../data/train_{PREF}.f')
fe('../data/test.f',  f'../data/test_{PREF}.f')


#==============================================================================
utils.end(__file__)

