#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar  6 08:00:33 2019

@author: Kazuki
"""


import numpy as np
import pandas as pd
from glob import glob
from tqdm import tqdm
import utils


SUBMIT_FILE_PATH = '../output/0305-1.csv.gz'

COMMENT = 'onodera + nyanp'

EXE_SUBMIT = True

# =============================================================================
# 
# =============================================================================
def blend(files):
    
    sub = pd.read_csv('../input/sample_submission.csv.zip')
    
    for file in tqdm(files):
        sub['HasDetections'] = pd.read_pickle(file)['HasDetections'].rank()
    
    return sub

# =============================================================================
# 
# =============================================================================

files1 = glob('../output/0305-1*pkl.gz')
sub1 = blend(files1)


files2 = glob('../output/0305-2*pkl.gz')
sub2 = blend(files2)



print( sub1['HasDetections'].corr( sub2['HasDetections'], method='spearman') )



y_pred = sub1['HasDetections'].rank() + sub2['HasDetections'].rank()
y_pred /= y_pred.max()

sub = pd.read_csv('../input/sample_submission.csv.zip')
sub['HasDetections'] = y_pred.values


print(f'corr with {utils.SUB_BEST}')
sub_best = pd.read_csv(utils.SUB_BEST)
print('with mybest:', sub['HasDetections'].corr( sub_best['HasDetections'], method='spearman') )


sub['HasDetections'] = 1 - sub['HasDetections']


# save
sub.to_csv(SUBMIT_FILE_PATH, index=False, compression='gzip')

# =============================================================================
# submission
# =============================================================================
if EXE_SUBMIT:
    print('submit')
    utils.submit(SUBMIT_FILE_PATH, COMMENT)

