# Ver 21.6 ANDに対応 main_v2({"Train Station":3 , "Mall" : 3} , "JP")は
# (Train Station || Platform || Train) &&  Mallという解釈
# AND検索する場合は316行目でget_similarity_ANDを使う。従来通りの場合get_similarityを使う
# Ver 21. ①Sum Weight計算を変更。DataBaseのBugで全く同時刻に同じVENUEのcheckinが
# 見つかっため、previous_venue と previous_date を用いて、Sum Weightに加わらないようにした。
# ② 比較的に短いTravel Record(checkin数が例えば5箇所以下)を除外するために、
# main_v2()内に、元々の if len(row[i]) > 0 : の行を
# if len(row[i]) > 0 and int(row[i+1]) > 5: に変更。5は適宜に変更して下さい。
# ③ 入力国がJPとSGの場合、プログラムが自動的にそれらの国のCSVファイルを使うようにした。
# ver 14. VENUE_NAME 出力のDEBUGを行った。SELECT VENUE_ID, LATITUDE, LONGITUDE, COUNTRY, HOME, CATEGORY, DATE FROM
# ver.11 Tiny_categoryのDebug @2018_1218 14:00
# ver.10 Country Name がJPの場合だけ、VENUE_NAMEを出力
# ver.9  重みがマイナスの場合でも対応できるように絶対値(abs)を使用 @2018_1215
# 重み入力対応＋ 結果をoutput_resultへ出力(output_result.txt)
# main_v2 の実行形式, 重みは必ず指定すること。デフォルト値として1を入れること。
# main_v2({'Jazz Club':2,'Museum':1},'US')
# main_v2({'Jazz Club':-2,'Museum':5},'US')

from datetime import datetime
from math import sqrt
import csv
import sys
import json
import numpy as np
from matplotlib.widgets import Button
import matplotlib.image as mpimg

# 以下は BigQueryや地図表示するためのImport。
from bigquery import get_client
from bigquery.errors import BigQueryTimeoutException
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

#以下の Gloval Variable をMain_v2内へ移動」
#dataset = {}
#countryset = {}

# 新しいバージョンの Main
def main_v2(all_interest, country):

    dataset = {}
    countryset = {}
    if len(dataset) == 0:

        input_dict_category = {}

        all_interest_all_category = {}
        original_input = all_interest
        #create the test user and put him in dataset
        for interest_location , weight_score  in all_interest.items():
            #print("input interest " + interest_location)
            subset_category = take_input(interest_location)
            #print(subset_category)
            for item in subset_category:
                # person0 を作成
                input_dict_category.update({item : 1 }) # 該当するカテの重み＝1,Dictに入れる
#                input_dict_category.update({interest_location : 1 })
                # TODO convert from weight score into multiplyier

                #update alternative
                #all_interest_all_category.update({item : weight_score})
                all_interest_all_category[item] = weight_score # 該当するカテに入力重み(1~5を適用)
#                all_interest_all_category[interest_location] = weight_score
                #print(all_interest_all_category)
        dataset["0"] = input_dict_category; # User0 を datasetに入れる
        all_interest = all_interest_all_category # Sub-Category＋重みをall_interestとして代入
        #transform the input all_interest into smaller category
        reader = ""
        print('Loading Traverler-Interest file...')
        if (country == "JP"):
            print ("Using JP Record")
            with open("./Traveler_Interest_JP.csv", "r") as csvfile:
                reader = csv.reader(csvfile)
                for row in reader:
                    if len(row) == 0: #空の行に対応するため
                        break         #空の行に対応するため
                    if(row[0]=='USER_ID'):
                        continue
                    category_frequency = {}
                    for i in range(1, len(row), 2):
                        if len(row[i]) > 0:
                            category_frequency[row[i]] = int(row[i + 1])
                        dataset[row[0]] = category_frequency
                    apply_weight(dataset[row[0]] , all_interest) #Weight（重み）
        elif (country == "SG"):
            print ("Using SG Record")
            with open("./Traveler_Interest_SG.csv", "r") as csvfile:
                reader = csv.reader(csvfile)
                for row in reader:
                    if len(row) == 0: #空の行に対応するため
                        break         #空の行に対応するため
                    if(row[0]=='USER_ID'):
                        continue
                    category_frequency = {}
                    for i in range(1, len(row), 2):
                        if len(row[i]) > 0:
                            category_frequency[row[i]] = int(row[i + 1])
                        dataset[row[0]] = category_frequency
                    apply_weight(dataset[row[0]] , all_interest) #Weight（重み）
        else:
            print('Using All Traverler-Interest Data')
            with open("./Traveler_Interest_all.csv", "r") as csvfile:
                reader = csv.reader(csvfile)
                for row in reader:
                    if len(row) == 0: #空の行に対応するため
                        break         #空の行に対応するため
                    if(row[0]=='USER_ID'):
                        continue
                    category_frequency = {}
                    for i in range(1, len(row), 2):
                        if len(row[i]) > 0:
                            category_frequency[row[i]] = int(row[i + 1])
                        dataset[row[0]] = category_frequency
                    apply_weight(dataset[row[0]] , all_interest) #Weight（重み）

# 評価値の実験用に、以下３つのコマンドを ON-OFF の組み合わせでやる

# 正規化適応 (normalize)
#                normalize(dataset[row[0]] )

# 偏差値以下を除外 (outlier)
#                remove_outlier(dataset[row[0]])

# 現時点では、Weight（重み）は必ず入れるようにする。
                #apply_weight(dataset[row[0]] , all_interest)

                #break;
                #print(dataset[row[0]])

    if len(countryset) == 0:
        print('Loading Traveler-Country file...')
#        with open("./Travelers_Countries_1_89308.csv", "r") as csvfile:
        with open("./Traveler_Countries_all.csv", "r") as csvfile:
            reader = csv.reader(csvfile)
# ver17 CSVの中に空の行があっても対応できるようにした。
            for row in reader:
                if len(row) == 0: #空の行に対応するため
                    break         #空の行に対応するため
# ver21 if len(row[i]) > 0 の行に and int(row[i+1]) > 5: を追加した。
                country_frequency = {}
                for i in range(1, len(row), 2):
                    if len(row[i]) > 0 and int(row[i+1]) > 5:
                        country_frequency[row[i]] = int(row[i + 1])
                    countryset[row[0]] = country_frequency
    rankings = []
    # print(get_recommend(rankings, "0", country, 10))
    # Pattara が 3人だけ表示に修正した（時間短縮のため）
    print('Original Input from User:')
    print(original_input, country, "\n")  # Debug用
# v21.6 get_recommendの引数にoriginal_inputを追加
    rankings = get_recommend(rankings, "0", country, 5 , dataset , countryset, original_input)
    if (rankings == "Error"):
        print("No record match ! Terminate Program !")
        return
    print('Top Rankings:', rankings, "\n")
    visualize("0", country, rankings , all_interest, original_input)

# show_category() はFourSquareの全カテゴリーを試し出力（jsonファイルから）
def show_category():
    all_interest = []
    with open('categories.json', encoding='utf-8') as data_file:
        data = json.loads(data_file.read())
    AllBigName = data["response"]["categories"]
    for bigName in AllBigName:
        big_category = "Current Big : " + bigName["name"]
        all_interest.append(big_category)
        for midName in bigName["categories"]:
            mid_category = "  Current Middle : " + midName["name"]
            all_interest.append(mid_category)
            for smallName in midName["categories"]:
                small_category = "    Current Small : " + smallName["name"]
                all_interest.append(small_category)
                for tinyName in smallName["categories"]:
                    tiny_category = "       Current Tiny : " + tinyName["name"]
                    all_interest.append(tiny_category)

#    timestamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
#    f = open("output_gaterory" +timestamp+ ".txt", "w" ,encoding='utf-8')
    f = open("output_gaterory.txt", "w" ,encoding='utf-8')
    for item in all_interest:
        f.write(item+"\n")

# take_input() はUserが指定するカテゴリー名及びその下階層のサブカテゴリーを全て出力
# tiny_category(Categoryの4階層目)に適用できるようにDebugした
# Stringを入力と出来るようにしました。(2018_1212更新)
# 実行例① take_input(['Music Venue'])
# 実行例② take_input(['Music Venue','Travel & Transport'])
import json

def take_input(all_interest):
    all_input_interest = [];
    if type(all_interest) == str:
        receive = all_interest
        all_interest = []
        all_interest.append(receive)
    with open('categories.json', encoding='utf-8') as data_file:
        data = json.loads(data_file.read())
    clean_data = data["response"]["categories"]
    for interest in all_interest:
        counter =0
        #print(interest)
        for big_name in clean_data:
            if(interest == big_name["name"]):
                counter = 1
                #find and add the rest subcategory
                all_input_interest.append(big_name["name"])
                for mid_name in big_name["categories"]:
                    all_input_interest.append(mid_name["name"])
                    for small_name in mid_name["categories"]:
                        all_input_interest.append(small_name["name"])
                        for tiny_name in small_name["categories"]:
                            all_input_interest.append(tiny_name["name"])

            for mid_name in big_name["categories"]:
                if(interest == mid_name["name"]):
                    counter = 1
                    all_input_interest.append(mid_name["name"])
                    for small_name in mid_name["categories"]:
                        all_input_interest.append(small_name["name"])
                        for tiny_name in small_name["categories"]:
                            all_input_interest.append(tiny_name["name"])

                for small_name in mid_name["categories"]:
                    if(interest == small_name["name"]):
                        counter = 1
                        all_input_interest.append(small_name["name"])
                        for tiny_name in small_name["categories"]:
                            all_input_interest.append(tiny_name["name"])

                    for tiny_name in small_name["categories"]:
                        if(interest == tiny_name["name"]):
                            counter = 1
                            all_input_interest.append(tiny_name["name"])
        if (counter == 0):
            all_input_interest.append(interest)
#     #WRITE FILE
#     f = open("take_input_output.txt", "w" )
#     for item in all_input_interest:
#         f.write(item)\

    return all_input_interest

# 2018_1225 person1のチェックインしたカテゴリ集合⊆person2のチェックインしたカテゴリ集合でなければ
# (person2がperson1のチェックインしたカテゴリを全て訪問していなければ)類似度0とする
#
# 2018_1215 重みがマイナスの場合でも対応できるように
#    union_num += absdataset[person1][union] を
#    union_num += abs(dataset[person1][union]) に変更
# 旅行者person1とperson2の類似度[0, 1]を計算する
# ここでのperson1とperson2の類似度の定義
# person1とperson2の両方がチェックしたカテゴリをCとする
# 類似度　=
# person1がCにチェックインした回数 + person2がCにチェックインした回数 /
# person1のチェックイン総数 + person2のチェックイン総数
# ただしCが空集合の場合、類似度0とする

def get_similarity(person1, person2, dataset):
    set_person1 = set(dataset[person1].keys())
    set_person2 = set(dataset[person2].keys())
    #person1とperson2の両方がチェックしたカテゴリがない
    if len(set_person1.intersection(set_person2)) == 0:
        return 0
    #if not set_person1 <= set_person2:
     #   return 0
    # person1とperson2の両方がチェックしたカテゴリがある
    inter_num = 0
    union_num = 0
    for inter in set_person1.intersection(set_person2):
        inter_num += dataset[person1][inter]
        inter_num += dataset[person2][inter]
    for union in set_person1.union(set_person2):
        if union in dataset[person1]:
            union_num += abs(dataset[person1][union])
        if union in dataset[person2]:
            union_num += abs(dataset[person2][union])
    return inter_num / union_num

def get_similarity_AND(person1, person2, dataset, original_input):
    set_person1 = set(dataset[person1].keys())
    set_person2 = set(dataset[person2].keys())
    #person1とperson2の両方がチェックしたカテゴリがない
    if len(set_person1.intersection(set_person2)) == 0:
        return 0
# AND対応
    original_input_list = list(original_input)
    expanded_input = take_input(original_input)
    for i in range(len(original_input_list)):
        j = expanded_input.index(original_input_list[i])
        if i < len(original_input_list)-1:
            k = expanded_input.index(original_input_list[i+1])
        else:
            k = len(expanded_input)
        if len(set(expanded_input[j:k]).intersection(set_person2)) == 0:
#            print("not found:" + str(set(expanded_input[j:k])))# + "###" + str(set_person2))
            return 0
    #if not set_person1 <= set_person2:
     #   return 0
    # person1とperson2の両方がチェックしたカテゴリがある
#    print("OK:" + str(expanded_input))
    inter_num = 0
    union_num = 0
    for inter in set_person1.intersection(set_person2):
        inter_num += dataset[person1][inter]
        inter_num += dataset[person2][inter]
    for union in set_person1.union(set_person2):
        if union in dataset[person1]:
            union_num += abs(dataset[person1][union])
        if union in dataset[person2]:
            union_num += abs(dataset[person2][union])
    return inter_num / union_num

# 旅行者personに類似する他の旅行者を上位top_Nまでランキングする
def get_recommend(rankings, person, country, top_N , dataset , countryset, original_input):
    counter = 0
    for other in list(dataset.keys()):
        if person != other: # 自分を除く
            if country in countryset[other].keys(): # 他の旅行者がcountryを訪問してるか？
# v21_6 AND対応
#                sim = get_similarity(person, other, dataset)
                sim = get_similarity_AND(person, other, dataset, original_input)
                rankings.append((sim, other))
                if (sim != 0):
                    #print(counter)
                    counter = counter + 1
    rankings.sort()
    rankings.reverse()
    if (counter == 0 ):
        return "Error"
    return [i for i in rankings][:top_N]

def visualize(person, country, rankings,all_interest,original_input):

#Outputをファイル1,2へ書き込み用の初期化
    timestamp = datetime.now().strftime("%Y%m%d_%H_%M_%S")
    f = open("output_result " + timestamp + ".txt", "w" ,encoding='utf-8')
    f2 = open("output_result2 " + timestamp + ".txt", "w" ,encoding='utf-8')
    f3 = open("output_result3 " + timestamp + ".txt", "w" ,encoding='utf-8')
#    f = open("output_result.txt", "w" ,encoding='utf-8')
#    f2 = open("output_result2.txt", "w" ,encoding='utf-8')
    # fig, axs = plt.subplots(1, 2, constrained_layout=True)

    # plt.figure(figsize=(14, 8))
    fig, axs = plt.subplots(1, 4, figsize=(13, 7))
    axs[0] = plt.subplot(1, 2, 1 )
    # plt.subplot(1, 2, 1 )

#地図を表示させたくない場合は、earth = Basemap()とearth.shadedrelief()をコメントアウトする
    if (country == "JP") :
        earth = Basemap(llcrnrlon=125.,llcrnrlat=24.,urcrnrlon=150.,urcrnrlat=48.)
        # earth = Basemap(llcrnrlon=.,llcrnrlat=24.,urcrnrlon=150.,urcrnrlat=48.)
    else :
        earth = Basemap()

    # earth.bluemarble()
    earth.shadedrelief()

    #PROJECT_ID = 'tour-miner-project'
#    SERVICE_ACCOUNT = 'bigquery-admin@tour-miner-project-2019.iam.gserviceaccount.com'
    JSON_KEY_PATH = 'tour-miner-project-2019-511090dd4bd9.json'
    # BigQueryClientの取得
    client = get_client(json_key_file=JSON_KEY_PATH, readonly=True)

    #Interest Keyとその重みを表示
    print("Matched Interested Category: ")
    for key , value in all_interest.items():
        sys.stdout.write(key + ":" +str(value) +",")
#    print("\n")

# Output2への書き込み用； Interest Keyとその重みをHeaderとして書き込む
    f2.write("Input Country from User: " + country + "\n")
    f2.write("Input Interested Category from User: ")
    for key , value in original_input.items():
        f2.write(key + ":" +str(value) +",")
    f2.write("\nMatched Interested Category: \n")
    for key , value in all_interest.items():
        f2.write(key + ":" +str(value) +",")
    f2.write("\n \n")
    f2.write("Top Rankings (Similarity,User Name): " + str(rankings)+ "\n \n")

#Output 3
    f3.write("Input Country from User: " + country + "\n")
    f3.write("Input Interested Category from User: ")
    for key , value in original_input.items():
        f3.write(key + ":" +str(value) +",")
    f3.write("\nMatched Interested Category: \n")
    for key , value in all_interest.items():
        f3.write(key + ":" +str(value) +",")
    f3.write("\n \n")
    f3.write("Top Rankings (Similarity,User Name): " + str(rankings)+ "\n \n")

    print(len(rankings))
    for u in rankings:
        # queryの実行
        print("\nUser:" + u[1])
        query = 'SELECT VENUE_ID, LATITUDE, LONGITUDE, COUNTRY, HOME, CATEGORY, DATE FROM dataset_TIST2015.Checkins_POIs_Travel_marked WHERE USER_ID = '          + u[1] + ' and TRAVEL = 1 and COUNTRY = \'' + country + '\''
        #print(query)

        try:
            job_id, results = client.query(query, timeout=60)
        except BigQueryTimeoutException as e:
            print('Exception')

        # 日付のフォーマットを変換
        for q in results:
            q['DATE'] = q['DATE'][4:]
            Mon = q['DATE'][:3]
            q['DATE'] = q['DATE'][4:]
            if Mon == "Jan":
                q['DATE'] = "01" + q['DATE']
            if Mon == "Feb":
                q['DATE'] = "02" + q['DATE']
            if Mon == "Mar":
                q['DATE'] = "03" + q['DATE']
            if Mon == "Apr":
                q['DATE'] = "04" + q['DATE']
            if Mon == "May":
                q['DATE'] = "05" + q['DATE']
            if Mon == "Jun":
                q['DATE'] = "06" + q['DATE']
            if Mon == "Jul":
                q['DATE'] = "07" + q['DATE']
            if Mon == "Aug":
                q['DATE'] = "08" + q['DATE']
            if Mon == "Sep":
                q['DATE'] = "09" + q['DATE']
            if Mon == "Oct":
                q['DATE'] = "10" + q['DATE']
            if Mon == "Nov":
                q['DATE'] = "11" + q['DATE']
            if Mon == "Dec":
                q['DATE'] = "12" + q['DATE']
            YEAR = q['DATE'][len(q['DATE'])-4:len(q['DATE'])]
            q['DATE'] = YEAR + q['DATE'][:len(q['DATE'])-4]

        # 結果の表示
        results.sort(key=lambda x: x['DATE'])

        total_weight = 0
        time_counter = 0

        previous_venue = "" # Version21で追加
        previous_date = ""  # Version21で追加

# 入力される Country Nameが JPの時だけ VENUE_NAMEで出力するように対応
        location_name_dictionary = {}
        if (country == "JP"):
            location_name_dictionary = location_name = get_venue_names_from_venue_ids([q['VENUE_ID'] for q in results])
        location_name = ""
        previous_venue = "0"
        for q in results:
          if q['VENUE_ID'] in location_name_dictionary:
              location_name = location_name_dictionary[q['VENUE_ID']]
          else:
              location_name = q['VENUE_ID']

          counter = 0
          for key_category , weight in all_interest.items():
              if (key_category == q['CATEGORY']):
                  counter = weight

          print(q['DATE'] + ', ' + q['CATEGORY'] + ', ' + location_name + ", Weight:" + str(counter))
#          print(q['DATE'] + ',' + q['CATEGORY'] + ',' + str(q['LONGITUDE']) + ',' + str(q['LATITUDE']) + ',' + q['VENUE_ID'])

# Output1へ書き込み:主観的評価用（岡部さん用）:ここから
          f = open("output_result " + timestamp + ".txt", "a" ,encoding='utf-8')
#          f = open("output_result.txt", "a" ,encoding='utf-8')

          writing_information =(q['DATE'] + ', ' + q['CATEGORY'] + ', ' + location_name + ", Weight:" + str(counter) + ", Similarity:" + str("{0:.4f}".format(u[0])))
#          writing_information =(q['DATE'] + ',' + q['CATEGORY'] + ',' + str(q['LONGITUDE']) + ',' + str(q['LATITUDE']) + ',' + get_venue_name_from_venue_id(q['VENUE_ID']) + ",weight:" + str(counter))
          f.write(("user:" + u[1]) + ', ')
          f.write(writing_information+"\n")
# Output1へ書き込み：ここまで

# Output2へ書き込み:客観的評価用（パッタラ用）:ここから
          counter = 0
#          print(q['VENUE_ID'])
          if (previous_venue != q['VENUE_ID'] and previous_date != q['DATE']):
              for key_category , weight in all_interest.items():
                  if (key_category == q['CATEGORY']):
                      time_counter += 1
                      counter = weight
                      total_weight += weight
          previous_venue = q['VENUE_ID']
          previous_date = q['DATE']

          writing_information =(q['DATE'] + ', ' + q['CATEGORY'] + ', ' + location_name + ", Weight:" + str(counter))
          f2.write(("user:" + u[1]) + ',')
          f2.write(writing_information+"\n")

          if (counter>0):
              writing_information =(q['DATE'] + ', ' + q['CATEGORY'] + ', ' + location_name + ", Weight:" + str(counter))
              f3.write(("user:" + u[1]) + ',')
              f3.write(writing_information+"\n")

        f2.write("Sum Weight = " + str(total_weight) + ", Similarity = " + str("{0:.4f}".format(u[0])) + ", Number of correspond place = "+ str(time_counter) + "\n \n")
        f3.write("Sum Weight = " + str(total_weight) + ", Similarity = " + str("{0:.4f}".format(u[0])) + "\n \n")

        print("Sum Weight = " + str(total_weight))
# Output2へ書き込み：ここまで

        lngs = [float(q['LONGITUDE']) for q in results]
        lats = [float(q['LATITUDE']) for q in results]

        # get_venue_names_from_venue_ids([q['VENUE_ID'] for q in results])
        #earth.drawcoastlines(color='#555566', linewidth=1)
        # set japanese font
        font = {'family' : 'IPAexGothic'}
        # set Thai font
        # font = {'family' : 'Tahoma'}

    # 地図へ表示させる部分

        # plt.plot(lngs, lats, '-o', label=u[1] , markersize=5)
        axs[0].plot(lngs, lats, '-o', label=u[1] , markersize=5)
        #place name in the map.
        for q in results :
            if q['VENUE_ID'] in location_name_dictionary:
              location_name2 = location_name_dictionary[q['VENUE_ID']]
            else:
              location_name2 = q['VENUE_ID']
            # axs[0].text(float(q['LONGITUDE']), float(q['LATITUDE']), location_name2, dict(size=7),**font)
            # plt.text(float(q['LONGITUDE']), float(q['LATITUDE']), location_name2, dict(size=7),**font)
    axs[0].set_title('Travel Records of Top Rankings Persons',**font)
    # plt.title('Travel Records of Top Rankings Persons',**font)

#    plt.title('Travel Records of people who prefer checkin-spots similar to Traveler ' + person)
    plt.legend(loc=4)

        #Travel Records by User.
        # p2 = plt.subplot(1, 2, 2)

        # to check that array is which in rankings.
    ranking_number = -1
    next_button = None
    # axs[1] = plt.subplot(2, 2, 2 )
    # axs[2] = plt.subplot(2, 2, 4 )
    axs[2] = plt.subplot(1, 2, 2 )
    axs[2].axis([0, 10, 0, 100])

    #fuction for command button Next
    def next_user(event) :
        print("\nWating .....", end="")
        nonlocal ranking_number
        # axs[0].cla()
        axs[2].cla()
        axs[2].axis([0, 10, 0, 100])

        ranking_number += 1

        #check if list index out of range to set start rankings[0]
        if(ranking_number == len(rankings)) :
            ranking_number = 0

        for u in rankings:
            #check user, Which that
            user = np.array(rankings[ranking_number])
            if(u[1] == user[1]) :
                # queryの実行
                print("\nTravel record of User:" + u[1])

                query = 'SELECT VENUE_ID, LATITUDE, LONGITUDE, COUNTRY, HOME, CATEGORY, DATE FROM dataset_TIST2015.Checkins_POIs_Travel_marked WHERE USER_ID = '          + u[1] + ' and TRAVEL = 1 and COUNTRY = \'' + country + '\''
                #print(query)

                try:
                    job_id, results = client.query(query, timeout=60)
                except BigQueryTimeoutException as e:
                    print('Exception')

                # 日付のフォーマットを変換
                for q in results:
                    q['DATE'] = q['DATE'][4:]
                    Mon = q['DATE'][:3]
                    q['DATE'] = q['DATE'][4:]
                    if Mon == "Jan":
                        q['DATE'] = "01" + q['DATE']
                    if Mon == "Feb":
                        q['DATE'] = "02" + q['DATE']
                    if Mon == "Mar":
                        q['DATE'] = "03" + q['DATE']
                    if Mon == "Apr":
                        q['DATE'] = "04" + q['DATE']
                    if Mon == "May":
                        q['DATE'] = "05" + q['DATE']
                    if Mon == "Jun":
                        q['DATE'] = "06" + q['DATE']
                    if Mon == "Jul":
                        q['DATE'] = "07" + q['DATE']
                    if Mon == "Aug":
                        q['DATE'] = "08" + q['DATE']
                    if Mon == "Sep":
                        q['DATE'] = "09" + q['DATE']
                    if Mon == "Oct":
                        q['DATE'] = "10" + q['DATE']
                    if Mon == "Nov":
                        q['DATE'] = "11" + q['DATE']
                    if Mon == "Dec":
                        q['DATE'] = "12" + q['DATE']
                    YEAR = q['DATE'][len(q['DATE'])-4:len(q['DATE'])]
                    q['DATE'] = YEAR + q['DATE'][:len(q['DATE'])-4]

                # 結果の表示
                results.sort(key=lambda x: x['DATE'])

                total_weight = 0
                time_counter = 0

                previous_venue = "" # Version21で追加
                previous_date = ""  # Version21で追加

        # Output2へ書き込み：ここまで

                lngs = [float(q['LONGITUDE']) for q in results]
                lats = [float(q['LATITUDE']) for q in results]

                # set japanese font
                font = {'family' : 'IPAexGothic'}
                # set Thai font
                # font = {'family' : 'Tahoma'}

            # 入力される Country Nameが JPの時だけ VENUE_NAMEで出力するように対応
                location_name_dictionary = {}
                if (country == "JP"):
                    location_name_dictionary = location_name = get_venue_names_from_venue_ids([q['VENUE_ID'] for q in results])
                location_name = ""
                previous_venue = "0"
                for q in results:
                    if q['VENUE_ID'] in location_name_dictionary:
                        location_name = location_name_dictionary[q['VENUE_ID']]
                    else:
                        location_name = q['VENUE_ID']
            # 地図へ表示させる部分

                axs[1].set_title('Travel Records of user '+ u[1],**font)

                #C fllow number is mean Color in Tableau Colors

                axs[1].plot(lngs, lats, '-o' 'C'+str(ranking_number) , markersize=6)
                #place name in the map.
                check_date = ""
                check_category = ""
                text_date = ""
                text_time = ""
                text_category = ""
                text_plcename = ""
                array_date =[]
                array_time = []
                array_category = []
                array_location = []

                set_y = 89
                set_y_icon_h1 = 84.4
                set_y_icon_h2 = 88.1

                long_text = ""
                for q in results :

                    if q['VENUE_ID'] in location_name_dictionary:
                        location_name2 = location_name_dictionary[q['VENUE_ID']]
                    else:
                        location_name2 = q['VENUE_ID']
                    axs[1].text(float(q['LONGITUDE']), float(q['LATITUDE']), location_name2, dict(size=10),**font)

                    check_date = q['DATE']

                    array_date.append(check_date.split(" ")[0])
                    array_time.append(check_date.split(" ")[1])
                    array_category.append(q['CATEGORY'])
                    array_location.append(location_name2)

                    text_date = check_date.split(" ")[0]
                    text_time = check_date.split(" ")[1]
                    check_category = q['CATEGORY']
                    text_category = q['CATEGORY']
                    text_plcename = location_name2

                    url = "https://matplotlib.org/"

                    #text of travel record by set y axis of graph
                    axs[2].text(1,set_y, "Date : " + text_date , size =8 , **font) #set_y = 89 , 81.95 , 74.9 , ...
                    set_y -= 2.25
                    axs[2].text(1,set_y, "       " + text_plcename , size =8 , **font ) #set_y = 86.75 , 79.7 , 72.65 , ...
                    set_y -= 2.35
                    axs[2].text(1,set_y, "       " + text_time , size =8 , **font) #set_y = 84.4 , 77.35 , 70.3 , ...
                    set_y -= 2.45  # sum -7.05

                    #img icon by set y axis
                    #Change name categoty with '/' to ':' for get image name
                    try :
                        if '/' in check_category :
                            check_category = check_category.replace('/' , ':')
                            img=mpimg.imread('./icon pictures/' + check_category+'.png')
                        else :
                            img=mpimg.imread('./icon pictures/' + q['CATEGORY']+'.png')
                    except FileNotFoundError:
                        img=mpimg.imread('./icon pictures/question.png')
                    # extent = (0 , 3.15, 3.3 ,6.67 )
                    # extent = (0.15 , 4.15, 84.4 ,88 )
                    extent = (0.15 , 4.15, set_y_icon_h1 ,set_y_icon_h2 )  #startx , endX , startY , endY
                    #set y icon in graph.
                    set_y_icon_h1 -= 7.05
                    set_y_icon_h2 -= 7.05
                    axs[2].imshow(img, extent=extent)

                    # long_text += text_date +  "\n     " + text_plcename + "\n     "  + text_time + "\n"
                    # long_text += text_date.split(" ")[0] + "\n" + text_date.split(" ")[1] = "\n" + q['CATEGORY'] + "\n" + location_name2

                axs[2].set_title('Travel Records of user '+ u[1],**font ,color ='C'+str(ranking_number) )
                # axs[2].text(2, 50,long_text,va='center', size=8,**font)
                axs[2].axis('off')
                plt.show()

                break

    def previous_user(event) :
        print("\nWating .....", end="")
        nonlocal ranking_number
        # axs[0].cla()
        axs[2].cla()
        axs[2].axis([0, 10, 0, 100])

        ranking_number -= 1
        #check if list index out of range to set start last index of rankings
        if(ranking_number < 0) :
            ranking_number = len(rankings)-1
        for u in rankings:
            #check user, Which that
            user = np.array(rankings[ranking_number])
            if(u[1] == user[1]) :
                # queryの実行
                print("\nTravel record of User:" + u[1])

                query = 'SELECT VENUE_ID, LATITUDE, LONGITUDE, COUNTRY, HOME, CATEGORY, DATE FROM dataset_TIST2015.Checkins_POIs_Travel_marked WHERE USER_ID = '          + u[1] + ' and TRAVEL = 1 and COUNTRY = \'' + country + '\''
                #print(query)

                try:
                    job_id, results = client.query(query, timeout=60)
                except BigQueryTimeoutException as e:
                    print('Exception')

                # 日付のフォーマットを変換
                for q in results:
                    q['DATE'] = q['DATE'][4:]
                    Mon = q['DATE'][:3]
                    q['DATE'] = q['DATE'][4:]
                    if Mon == "Jan":
                        q['DATE'] = "01" + q['DATE']
                    if Mon == "Feb":
                        q['DATE'] = "02" + q['DATE']
                    if Mon == "Mar":
                        q['DATE'] = "03" + q['DATE']
                    if Mon == "Apr":
                        q['DATE'] = "04" + q['DATE']
                    if Mon == "May":
                        q['DATE'] = "05" + q['DATE']
                    if Mon == "Jun":
                        q['DATE'] = "06" + q['DATE']
                    if Mon == "Jul":
                        q['DATE'] = "07" + q['DATE']
                    if Mon == "Aug":
                        q['DATE'] = "08" + q['DATE']
                    if Mon == "Sep":
                        q['DATE'] = "09" + q['DATE']
                    if Mon == "Oct":
                        q['DATE'] = "10" + q['DATE']
                    if Mon == "Nov":
                        q['DATE'] = "11" + q['DATE']
                    if Mon == "Dec":
                        q['DATE'] = "12" + q['DATE']
                    YEAR = q['DATE'][len(q['DATE'])-4:len(q['DATE'])]
                    q['DATE'] = YEAR + q['DATE'][:len(q['DATE'])-4]

                # 結果の表示
                results.sort(key=lambda x: x['DATE'])

                total_weight = 0
                time_counter = 0

                previous_venue = "" # Version21で追加
                previous_date = ""  # Version21で追加

        # Output2へ書き込み：ここまで

                lngs = [float(q['LONGITUDE']) for q in results]
                lats = [float(q['LATITUDE']) for q in results]

                # set japanese font
                font = {'family' : 'IPAexGothic'}
                # set Thai font
                # font = {'family' : 'Tahoma'}

            # 入力される Country Nameが JPの時だけ VENUE_NAMEで出力するように対応
                location_name_dictionary = {}
                if (country == "JP"):
                    location_name_dictionary = location_name = get_venue_names_from_venue_ids([q['VENUE_ID'] for q in results])
                location_name = ""
                previous_venue = "0"
                for q in results:
                    if q['VENUE_ID'] in location_name_dictionary:
                        location_name = location_name_dictionary[q['VENUE_ID']]
                    else:
                        location_name = q['VENUE_ID']
            # 地図へ表示させる部分

                axs[1].set_title('Travel Records of user '+ u[1],**font)

                #C fllow number is mean Color in Tableau Colors
                axs[1].plot(lngs, lats, '-o' 'C'+str(ranking_number) , markersize=6)
                #place name in the map.
                check_date = ""
                check_category = ""
                text_date = ""
                text_time = ""
                text_category = ""
                text_plcename = ""
                array_date =[]
                array_time = []
                array_category = []
                array_location = []

                set_y = 89
                set_y_icon_h1 = 84.4
                set_y_icon_h2 = 88.1

                long_text = ""
                for q in results :

                    if q['VENUE_ID'] in location_name_dictionary:
                        location_name2 = location_name_dictionary[q['VENUE_ID']]
                    else:
                        location_name2 = q['VENUE_ID']
                    axs[1].text(float(q['LONGITUDE']), float(q['LATITUDE']), location_name2, dict(size=10),**font)

                    check_date = q['DATE']

                    array_date.append(check_date.split(" ")[0])
                    array_time.append(check_date.split(" ")[1])
                    array_category.append(q['CATEGORY'])
                    array_location.append(location_name2)

                    text_date = check_date.split(" ")[0]
                    text_time = check_date.split(" ")[1]
                    check_category = q['CATEGORY']
                    text_category = q['CATEGORY']
                    text_plcename = location_name2

                    #text of travel record by set y axis of graph
                    axs[2].text(1,set_y, "Date : " + text_date , size =8 , **font) #set_y = 89 , 81.95 , 74.9 , ...
                    set_y -= 2.25
                    axs[2].text(1,set_y, "       " + text_plcename , size =8 , **font) #set_y = 86.75 , 79.7 , 72.65 , ...
                    set_y -= 2.35
                    axs[2].text(1,set_y, "       " + text_time , size =8 , **font) #set_y = 84.4 , 77.35 , 70.3 , ...
                    set_y -= 2.45  # sum -7.05

                    #img icon by set y axis
                    #Change name categoty with '/' to ':' for get image name
                    try :
                        if '/' in check_category :
                            check_category = check_category.replace('/' , ':')
                            img=mpimg.imread('./icon pictures/' + check_category+'.png')
                        else :
                            img=mpimg.imread('./icon pictures/' + q['CATEGORY']+'.png')
                    except FileNotFoundError:
                        img=mpimg.imread('./icon pictures/question.png')

                    # extent = (0 , 3.15, 3.3 ,6.67 )
                    # extent = (0.15 , 4.15, 84.4 ,88 )
                    extent = (0.15 , 4.15, set_y_icon_h1 ,set_y_icon_h2 )  #startx , endX , startY , endY
                    #set y icon in graph.
                    set_y_icon_h1 -= 7.05
                    set_y_icon_h2 -= 7.05
                    axs[2].imshow(img, extent=extent)

                    # long_text += text_date +  "\n     " + text_plcename + "\n     "  + text_time + "\n"
                    # long_text += text_date.split(" ")[0] + "\n" + text_date.split(" ")[1] = "\n" + q['CATEGORY'] + "\n" + location_name2

                axs[2].set_title('Travel Records of user '+ u[1],**font ,color ='C'+str(ranking_number) )
                # axs[2].text(2, 50,long_text,va='center', size=8,**font)
                axs[2].axis('off')
                plt.show()

                break

    def clicked_refresh(event):
        print("Refresh")

    # Button Previous
    axButton1 = plt.axes([0.125, 0.07 , 0.1 , 0.1]) #left , bottom , width , height
    btn1 = Button (ax = axButton1 , label ="Previous")

    # Button Next
    axButton2 = plt.axes([0.25, 0.07 , 0.1 , 0.1]) #left , bottom , width , height
    btn2 = Button (ax = axButton2 , label ="Next" )

    # Button Refresh
    # axButton3 = plt.axes([0.375, 0.07 , 0.05 , 0.05]) #left , bottom , width , height
    # btn3 = Button (ax = axButton3 , label ="refresh" )

    btn1.on_clicked(previous_user)
    btn2.on_clicked(next_user)
    # btn3.on_clicked(clicked_refresh)

    plt.show()

#    plt.savefig("graph.png")
#   上記Debug用

# get_venue_name_from_venue_id() の実行例
# get_venue_name_from_venue_id('4b569977f964a520551628e3')
# 東京スカイツリー(TokyoSkyTree)
def get_venue_name_from_venue_id(venue_id):
    #PROJECT_ID = 'tour-miner-project'
#    SERVICE_ACCOUNT = 'bigquery-admin@tour-miner-project.iam.gserviceaccount.com'
#    JSON_KEY_PATH = 'tour-miner-project-8873e737b27c.json'
    SERVICE_ACCOUNT ='bigquery-admin@tour-miner-project-2019.iam.gserviceaccount.com'
    JSON_KEY_PATH = 'tour-miner-project-2019-511090dd4bd9.json'

    # BigQueryClientの取得
    client = get_client(json_key_file=JSON_KEY_PATH, readonly=True)

    query = '#standardSQL\nSELECT VENUE_NAME FROM `tour-miner-project.dataset_TIST2015.JP_VENUE_DICTIONARY` WHERE VENUE_ID = \'' + venue_id + '\' LIMIT 1'
#    print(query)

    try:
        job_id, results = client.query(query, timeout=60)
    except BigQueryTimeoutException as e:
        print(e)

    #venue_idと  VENUE_NAMEが該当しない時にエラーが出るため改良した
    #print(results[0]['VENUE_NAME'])
    if len(results)==0:
        return '該当なし'
    else:
        return (results[0]['VENUE_NAME'])

# Venue idのリストを名前のリストに一括変換する関数
# 2018年12月時点では、JPに限定されることに注意
# Input: List of venue id, ex. ['4d96a21fc19fb60c41908365', '4e478efcd164155c0df4bafc']
# Output Dictionary of venue name (key: venue id, value venue name)
# {'VENUE_NAME': '上野動物園パンダ舎(GiantPandasCage-', 'VENUE_NAME': '上野動物園カピバラ舎'}
def get_venue_names_from_venue_ids(venue_id_list):
    #PROJECT_ID = 'tour-miner-project'
#    SERVICE_ACCOUNT = 'bigquery-admin@tour-miner-project.iam.gserviceaccount.com'
#    JSON_KEY_PATH = 'tour-miner-project-8873e737b27c.json'
#    SERVICE_ACCOUNT ='bigquery-admin@tour-miner-project-2019.iam.gserviceaccount.com'
    JSON_KEY_PATH = 'tour-miner-project-2019-511090dd4bd9.json'

    # BigQueryClientの取得
    client = get_client(json_key_file=JSON_KEY_PATH, readonly=True)

    where_phrase = ''
    for i in range(len(venue_id_list)):
        where_phrase += "VENUE_ID = \'" + venue_id_list[i] + "\'"
        if i < len(venue_id_list) - 1: # 最後でなければORをつける
            where_phrase += ' or '
#    query = '#standardSQL\nSELECT VENUE_ID, VENUE_NAME FROM `tour-miner-project.dataset_TIST2015.JP_VENUE_DICTIONARY` WHERE ' + where_phrase
    query = '#standardSQL\nSELECT VENUE_ID, VENUE_NAME FROM `tour-miner-project-2019.dataset_TIST2015.JP_VENUE_DICTIONARY` WHERE ' + where_phrase
 #   print(query)

    try:
        job_id, results = client.query(query, timeout=60)
    except BigQueryTimeoutException as e:
        print(e)
 #   print(results)
    location_name_dictionary = {}
    for q in results:
        location_name_dictionary[q['VENUE_ID']] = q['VENUE_NAME']
    return location_name_dictionary

# 重みを与えるためのサブルーチン
def apply_weight(current_dataset , all_interest):
    #print(all_interest)
    for dataset_key , dataset_value in current_dataset.items():
        for interest_key , interest_value in all_interest.items():
            if(dataset_key == interest_key):
                current_dataset[dataset_key] *= interest_value
                #print("KEY IS " + dataset_key)

# 正規化計算 (ver10から追加)
def normalize(current_dataset):

    minimum_value = 0
    maximum_value = 0

    #Find Max and Min
    for dataset_value in current_dataset.values():
        #print(dataset_value)
        if(maximum_value < dataset_value):
            maximum_value = dataset_value

        if(minimum_value > dataset_value ):
            minimum_value = dataset_value

    #normalize
    for dataset_key , dataset_value in current_dataset.items():
        new_value = ( dataset_value - minimum_value ) / (maximum_value - minimum_value )
        current_dataset[dataset_key] = new_value

# 偏差値以下のデータを取り除く (ver10から追加)
import statistics
def remove_outlier(current_dataset):
    #if length is only 1 then do nothing
    if (len(current_dataset) == 1):
        return

    #find stdev
    current_stdev = statistics.stdev(current_dataset.values())
    category_to_remove = []
    #check  outlier
    for dataset_key , dataset_value in current_dataset.items():
        if (current_stdev > dataset_value):
            category_to_remove.append(dataset_key)

    #remove outlier
    for item_to_remove in category_to_remove:
        current_dataset.pop(item_to_remove)

# main_v2({'Jazz Club':2,'Museum':1},'US')
main_v2({'Temple':1},'JP')
# main_v2({'Spiritual Center':1},'JP')


# Authors: Jean-Remi King <jeanremi.king@gmail.com>
#          Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr>
#          Denis Engemann <denis.engemann@gmail.com>
#          Clement Moutard <clement.moutard@gmail.com>
#
# License: BSD (3-clause)

import numpy as np
import copy

from ..io.pick import pick_types
from ..viz.decoding import plot_gat_matrix, plot_gat_times
from ..parallel import parallel_func, check_n_jobs

class _DecodingTime(dict):
    """A dictionary to configure the training times that has the following keys:

    'slices' : ndarray, shape (n_clfs,)
        Array of time slices (in indices) used for each classifier.
        If not given, computed from 'start', 'stop', 'length', 'step'.
    'start' : float
        Time at which to start decoding (in seconds).
        Defaults to min(epochs.times).
    'stop' : float
        Maximal time at which to stop decoding (in seconds).
        Defaults to max(times).
    'step' : float
        Duration separating the start of subsequent classifiers (in
        seconds). Defaults to one time sample.
    'length' : float
        Duration of each classifier (in seconds). Defaults to one time sample.
    If None, empty dict. """

    def __repr__(self):
        s = ""
        if "start" in self:
            s += "start: %0.3f (s)" % (self["start"])
        if "stop" in self:
            s += ", stop: %0.3f (s)" % (self["stop"])
        if "step" in self:
            s += ", step: %0.3f (s)" % (self["step"])
        if "length" in self:
            s += ", length: %0.3f (s)" % (self["length"])
        if "slices" in self:
            # identify depth: training times only contains n_time but
            # testing_times can contain n_times or n_times * m_times
            depth = [len(ii) for ii in self["slices"]]
            if len(np.unique(depth)) == 1:  # if all slices have same depth
                if depth[0] == 1:  # if depth is one
                    s += ", n_time_windows: %s" % (len(depth))
                else:
                    s += ", n_time_windows: %s x %s" % (len(depth), depth[0])
            else:
                s += (", n_time_windows: %s x [%s, %s]" %
                      (len(depth),
                       min([len(ii) for ii in depth]),
                       max(([len(ii) for ii in depth]))))
        return "<DecodingTime | %s>" % s

class _GeneralizationAcrossTime(object):
    """ see GeneralizationAcrossTime
    """  # noqa
    def __init__(self, picks=None, cv=5, clf=None, train_times=None,
                 test_times=None, predict_mode='cross-validation', scorer=None,
                 n_jobs=1):

        from sklearn.preprocessing import StandardScaler
        from sklearn.linear_model import LogisticRegression
        from sklearn.pipeline import Pipeline

        # Store parameters in object
        self.cv = cv
        # Define training sliding window
        self.train_times = (_DecodingTime() if train_times is None
                            else _DecodingTime(train_times))
        # Define testing sliding window. If None, will be set in predict()
        if test_times is None:
            self.test_times = _DecodingTime()
        elif test_times == 'diagonal':
            self.test_times = 'diagonal'
        else:
            self.test_times = _DecodingTime(test_times)

        # Default classification pipeline
        if clf is None:
            scaler = StandardScaler()
            estimator = LogisticRegression()
            clf = Pipeline([('scaler', scaler), ('estimator', estimator)])
        self.clf = clf
        self.predict_mode = predict_mode
        self.scorer = scorer
        self.picks = picks
        self.n_jobs = n_jobs

    def fit(self, epochs, y=None):
        """ Train a classifier on each specified time slice.

        Note. This function sets the ``picks_``, ``ch_names``, ``cv_``,
        ``y_train``, ``train_times_`` and ``estimators_`` attributes.

        Parameters
        ----------
        epochs : instance of Epochs
            The epochs.
        y : list or ndarray of int, shape (n_samples,) or None, optional
            To-be-fitted model values. If None, y = epochs.events[:, 2].

        Returns
        -------
        self : GeneralizationAcrossTime
            Returns fitted GeneralizationAcrossTime object.

        Notes
        ------
        If X and y are not C-ordered and contiguous arrays of np.float64 and
        X is not a scipy.sparse.csr_matrix, X and/or y may be copied.

        If X is a dense array, then the other methods will not support sparse
        matrices as input.
        """
        from sklearn.base import clone
        from sklearn.cross_validation import check_cv, StratifiedKFold

        # clean attributes
        for att in ['picks_', 'ch_names', 'y_train_', 'cv_', 'train_times_',
                    'estimators_', 'test_times_', 'y_pred_', 'y_true_',
                    'scores_', 'scorer_']:
            if hasattr(self, att):
                delattr(self, att)

        n_jobs = self.n_jobs
        # Extract data from MNE structure
        X, y, self.picks_ = _check_epochs_input(epochs, y, self.picks)
        self.ch_names = [epochs.ch_names[p] for p in self.picks_]

        cv = self.cv
        if isinstance(cv, (int, np.int)):
            cv = StratifiedKFold(y, cv)
        cv = check_cv(cv, X, y, classifier=True)
        self.cv_ = cv  # update CV

        self.y_train_ = y

        # Cross validation scheme
        # XXX Cross validation should later be transformed into a make_cv, and
        # defined in __init__
        self.train_times_ = copy.deepcopy(self.train_times)
        if 'slices' not in self.train_times_:
            self.train_times_ = _sliding_window(epochs.times, self.train_times)

        # Parallel across training time
        # TODO: JRK: Chunking times points needs to be simplified
        parallel, p_time_gen, n_jobs = parallel_func(_fit_slices, n_jobs)
        n_chunks = min(len(self.train_times_['slices']), n_jobs)
        splits = np.array_split(self.train_times_['slices'], n_chunks)

        def f(x):
            return np.unique(np.concatenate(x))

        out = parallel(p_time_gen(clone(self.clf),
                                  X[..., f(train_slices_chunk)],
                                  y, train_slices_chunk, cv)
                       for train_slices_chunk in splits)
        # Unpack estimators into time slices X folds list of lists.
        self.estimators_ = sum(out, list())
        return self

    def predict(self, epochs):
        """ Test each classifier on each specified testing time slice.

        .. note:: This function sets the ``y_pred_`` and ``test_times_``
                  attributes.

        Parameters
        ----------
        epochs : instance of Epochs
            The epochs. Can be similar to fitted epochs or not. See
            predict_mode parameter.

        Returns
        -------
        y_pred : list of lists of arrays of floats, shape (n_train_t, n_test_t, n_epochs, n_prediction_dims)
            The single-trial predictions at each training time and each testing
            time. Note that the number of testing times per training time need
            not be regular; else
            ``np.shape(y_pred_) = (n_train_time, n_test_time, n_epochs)``.
        """  # noqa

        # Check that at least one classifier has been trained
        if not hasattr(self, 'estimators_'):
            raise RuntimeError('Please fit models before trying to predict')

        # clean attributes
        for att in ['y_pred_', 'test_times_', 'scores_', 'scorer_', 'y_true_']:
            if hasattr(self, att):
                delattr(self, att)

        n_jobs = self.n_jobs

        X, y, _ = _check_epochs_input(epochs, None, self.picks_)

        # Define testing sliding window
        if self.test_times == 'diagonal':
            test_times = _DecodingTime()
            test_times['slices'] = [[s] for s in self.train_times_['slices']]
            test_times['times'] = [[s] for s in self.train_times_['times']]
        elif isinstance(self.test_times, dict):
            test_times = copy.deepcopy(self.test_times)
        else:
            raise ValueError('`test_times` must be a dict or "diagonal"')

        if 'slices' not in test_times:
            # Check that same number of time sample in testing than in training
            # (otherwise it won 't be the same number of features')
            if 'length' not in test_times:
                test_times['length'] = self.train_times_['length']
            if test_times['length'] != self.train_times_['length']:
                raise ValueError('`train_times` and `test_times` must have '
                                 'identical `length` keys')
            # Make a sliding window for each training time.
            slices_list = list()
            times_list = list()
            for t in range(0, len(self.train_times_['slices'])):
                test_times_ = _sliding_window(epochs.times, test_times)
                times_list += [test_times_['times']]
                slices_list += [test_times_['slices']]
            test_times = test_times_
            test_times['slices'] = slices_list
            test_times['times'] = times_list

        # Store all testing times parameters
        self.test_times_ = test_times

        # Prepare parallel predictions across time points
        # FIXME Note that this means that TimeDecoding.predict isn't parallel
        parallel, p_time_gen, n_jobs = parallel_func(_predict_slices, n_jobs)
        n_test_slice = max([len(sl) for sl in self.train_times_['slices']])
        # Loop across estimators (i.e. training times)
        n_chunks = min(n_test_slice, n_jobs)
        splits = [np.array_split(slices, n_chunks)
                  for slices in self.test_times_['slices']]
        splits = map(list, zip(*splits))

        def chunk_X(X, slices):
            """Smart chunking to avoid memory overload"""
            # from object array to list
            slices = [sl for sl in slices if len(sl)]
            start = np.min(slices)
            stop = np.max(slices) + 1
            slices_ = np.array(slices) - start
            X_ = X[:, :, start:stop]
            return (X_, self.estimators_, self.cv_, slices_.tolist(),
                    self.predict_mode)

        y_pred = parallel(p_time_gen(*chunk_X(X, slices))
                          for slices in splits)

        # concatenate chunks across test time dimension. Don't use
        # np.concatenate as this would need new memory allocations
        self.y_pred_ = [[test for chunk in train for test in chunk]
                        for train in map(list, zip(*y_pred))]
        return self.y_pred_

    def score(self, epochs=None, y=None):
        """Score Epochs

        Estimate scores across trials by comparing the prediction estimated for
        each trial to its true value.

        Calls ``predict()`` if it has not been already.

        Note. The function updates the ``scorer_``, ``scores_``, and
        ``y_true_`` attributes.

        Parameters
        ----------
        epochs : instance of Epochs | None, optional
            The epochs. Can be similar to fitted epochs or not.
            If None, it needs to rely on the predictions ``y_pred_``
            generated with ``predict()``.
        y : list | ndarray, shape (n_epochs,) | None, optional
            True values to be compared with the predictions ``y_pred_``
            generated with ``predict()`` via ``scorer_``.
            If None and ``predict_mode``=='cross-validation' y = ``y_train_``.

        Returns
        -------
        scores : list of lists of float
            The scores estimated by ``scorer_`` at each training time and each
            testing time (e.g. mean accuracy of ``predict(X)``). Note that the
            number of testing times per training time need not be regular;
            else, np.shape(scores) = (n_train_time, n_test_time).
        """
        from sklearn.metrics import accuracy_score

        # Run predictions if not already done
        if epochs is not None:
            self.predict(epochs)
        else:
            if not hasattr(self, 'y_pred_'):
                raise RuntimeError('Please predict() epochs first or pass '
                                   'epochs to score()')

        # clean gat.score() attributes
        for att in ['scores_', 'scorer_', 'y_true_']:
            if hasattr(self, att):
                delattr(self, att)

        # Check scorer
        # XXX Need API to identify proper scorer from the clf
        self.scorer_ = accuracy_score if self.scorer is None else self.scorer

        # If no regressor is passed, use default epochs events
        if y is None:
            if self.predict_mode == 'cross-validation':
                y = self.y_train_
            else:
                if epochs is not None:
                    y = epochs.events[:, 2]
                else:
                    raise RuntimeError('y is undefined because '
                                       'predict_mode="mean-prediction" and '
                                       'epochs are missing. You need to '
                                       'explicitly specify y.')
            if not np.all(np.unique(y) == np.unique(self.y_train_)):
                raise ValueError('Classes (y) passed differ from classes used '
                                 'for training. Please explicitly pass your y '
                                 'for scoring.')
        elif isinstance(y, list):
            y = np.array(y)
        self.y_true_ = y  # to be compared with y_pred for scoring

        # Preprocessing for parallelization
        n_jobs = min(len(self.y_pred_[0][0]), check_n_jobs(self.n_jobs))
        parallel, p_time_gen, n_jobs = parallel_func(_score_slices, n_jobs)
        n_estimators = len(self.train_times_['slices'])
        n_chunks = min(n_estimators, n_jobs)
        splits = np.array_split(range(len(self.train_times_['slices'])),
                                n_chunks)
        scores = parallel(
            p_time_gen(self.y_true_,
                       [self.y_pred_[train] for train in split],
                       self.scorer_)
            for split in splits)

        self.scores_ = [score for chunk in scores for score in chunk]
        return self.scores_

def _predict_slices(X, estimators, cv, slices, predict_mode):
    """Aux function of GeneralizationAcrossTime that loops across chunks of
    testing slices.
    """
    out = list()
    for this_estimator, this_slice in zip(estimators, slices):
        out.append(_predict_time_loop(X, this_estimator, cv, this_slice,
                                      predict_mode))
    return out

def _predict_time_loop(X, estimators, cv, slices, predict_mode):
    """Aux function of GeneralizationAcrossTime

    Run classifiers predictions loop across time samples.

    Parameters
    ----------
    X : ndarray, shape (n_epochs, n_features, n_times)
        To-be-fitted data.
    estimators : array-like, shape (n_times, n_folds)
        Array of scikit-learn classifiers fitted in cross-validation.
    slices : list
        List of slices selecting data from X from which is prediction is
        generated.
    predict_mode : {'cross-validation', 'mean-prediction'}
        Indicates how predictions are achieved with regards to the cross-
        validation procedure:
            'cross-validation' : estimates a single prediction per sample based
                on the unique independent classifier fitted in the cross-
                validation.
            'mean-prediction' : estimates k predictions per sample, based on
                each of the k-fold cross-validation classifiers, and average
                these predictions into a single estimate per sample.
        Default: 'cross-validation'
    """
    n_epochs = len(X)
    # Loop across testing slices
    y_pred = [list() for _ in range(len(slices))]

    # XXX EHN: This loop should be parallelized in a similar way to fit()
    for t, indices in enumerate(slices):
        # Flatten features in case of multiple time samples
        Xtrain = X[:, :, indices].reshape(
            n_epochs, np.prod(X[:, :, indices].shape[1:]))

        # Single trial predictions
        if predict_mode == 'cross-validation':
            # If predict within cross validation, only predict with
            # corresponding classifier, else predict with each fold's
            # classifier and average prediction.

            # Check that training cv and predicting cv match
            if (len(estimators) != len(cv)) or (cv.n != Xtrain.shape[0]):
                raise ValueError(
                    'When `predict_mode = "cross-validation"`, the training '
                    'and predicting cv schemes must be identical.')
            for k, (train, test) in enumerate(cv):
                # XXX I didn't manage to initialize correctly this array, as
                # its size depends on the the type of predictor and the
                # number of class.
                if k == 0:
                    y_pred_ = _predict(Xtrain[test, :], estimators[k:k + 1])
                    y_pred[t] = np.empty((n_epochs, y_pred_.shape[1]))
                    y_pred[t][test, :] = y_pred_
                y_pred[t][test, :] = _predict(Xtrain[test, :],
                                              estimators[k:k + 1])
        elif predict_mode == 'mean-prediction':
            y_pred[t] = _predict(Xtrain, estimators)
        else:
            raise ValueError('`predict_mode` must be a str, "mean-prediction"'
                             ' or "cross-validation"')
    return y_pred

def _score_slices(y_true, list_y_pred, scorer):
    """Aux function of GeneralizationAcrossTime that loops across chunks of
    testing slices.
    """
    scores_list = list()
    for y_pred in list_y_pred:
        scores = list()
        for t, this_y_pred in enumerate(y_pred):
            # Scores across trials
            scores.append(scorer(y_true, np.array(this_y_pred)))
        scores_list.append(scores)
    return scores_list

def _check_epochs_input(epochs, y, picks=None):
    """Aux function of GeneralizationAcrossTime

    Format MNE data into scikit-learn X and y

    Parameters
    ----------
    epochs : instance of Epochs
            The epochs.
    y : ndarray shape (n_epochs) | list shape (n_epochs) | None
        To-be-fitted model. If y is None, y == epochs.events.
    picks : array-like of int | None
        The channels indices to include. If None the data
        channels in info, except bad channels, are used.

    Returns
    -------
    X : ndarray, shape (n_epochs, n_selected_chans, n_times)
        To-be-fitted data.
    y : ndarray, shape (n_epochs,)
        To-be-fitted model.
    picks : array-like of int | None
        The channels indices to include. If None the data
        channels in info, except bad channels, are used.
    """
    if y is None:
        y = epochs.events[:, 2]
    elif isinstance(y, list):
        y = np.array(y)

    # Convert MNE data into trials x features x time matrix
    X = epochs.get_data()

    # Pick channels
    if picks is None:  # just use good data channels
        picks = pick_types(epochs.info, meg=True, eeg=True, seeg=True,
                           eog=False, ecg=False, misc=False, stim=False,
                           ref_meg=False, exclude='bads')
    if isinstance(picks, (list, np.ndarray)):
        picks = np.array(picks, dtype=np.int)
    else:
        raise ValueError('picks must be a list or a numpy.ndarray of int')
    X = X[:, picks, :]

    # Check data sets
    assert X.shape[0] == y.shape[0]
    return X, y, picks

def _fit_slices(clf, x_chunk, y, slices, cv):
    """Aux function of GeneralizationAcrossTime

    Fit each classifier.

    Parameters
    ----------
    clf : scikit-learn classifier
        The classifier object.
    x_chunk : ndarray, shape (n_epochs, n_features, n_times)
        To-be-fitted data.
    y : list | array, shape (n_epochs,)
        To-be-fitted model.
    slices : list | array, shape (n_training_slice,)
        List of training slices, indicating time sample relative to X
    cv : scikit-learn cross-validation generator
        A cross-validation generator to use.

    Returns
    -------
    estimators : list of lists of estimators
        List of fitted scikit-learn classifiers corresponding to each training
        slice.
    """
    from sklearn.base import clone
    # Initialize
    n_epochs = len(x_chunk)
    estimators = list()
    # Identify the time samples of X_chunck corresponding to X
    values = np.unique(np.concatenate(slices))
    indices = range(len(values))
    # Loop across time slices
    for t_slice in slices:
        # Translate absolute time samples into time sample relative to x_chunk
        for ii in indices:
            t_slice[t_slice == values[ii]] = indices[ii]
        # Select slice
        X = x_chunk[..., t_slice]
        # Reshape data matrix to flatten features in case of multiple time
        # samples.
        X = X.reshape(n_epochs, np.prod(X.shape[1:]))
        # Loop across folds
        estimators_ = list()
        for fold, (train, test) in enumerate(cv):
            # Fit classifier
            clf_ = clone(clf)
            clf_.fit(X[train, :], y[train])
            estimators_.append(clf_)
        # Store classifier
        estimators.append(estimators_)
    return estimators

def _sliding_window(times, window_params):
    """Aux function of GeneralizationAcrossTime

    Define the slices on which to train each classifier.

    Parameters
    ----------
    times : ndarray, shape (n_times,)
        Array of times from MNE epochs.
    window_params : dict keys: ('start', 'stop', 'step', 'length')
        Either train or test times. See GAT documentation.

    Returns
    -------
    time_pick : list
        List of training slices, indicating for each classifier the time
        sample (in indices of times) to be fitted on.
    """

    window_params = _DecodingTime(window_params)

    # Sampling frequency as int
    freq = (times[-1] - times[0]) / len(times)

    # Default values
    if ('slices' in window_params and
            all(k in window_params for k in
                ('start', 'stop', 'step', 'length'))):
        time_pick = window_params['slices']
    else:
        if 'start' not in window_params:
            window_params['start'] = times[0]
        if 'stop' not in window_params:
            window_params['stop'] = times[-1]
        if 'step' not in window_params:
            window_params['step'] = freq
        if 'length' not in window_params:
            window_params['length'] = freq

        if (window_params['start'] < times[0] or
                window_params['start'] > times[-1]):
            raise ValueError(
                '`start` (%.2f s) outside time range [%.2f, %.2f].' % (
                    window_params['start'], times[0], times[-1]))
        if (window_params['stop'] < times[0] or
                window_params['stop'] > times[-1]):
            raise ValueError(
                '`stop` (%.2f s) outside time range [%.2f, %.2f].' % (
                    window_params['stop'], times[0], times[-1]))
        if window_params['step'] < freq:
            raise ValueError('`step` must be >= 1 / sampling_frequency')
        if window_params['length'] < freq:
            raise ValueError('`length` must be >= 1 / sampling_frequency')
        if window_params['length'] > np.ptp(times):
            raise ValueError('`length` must be <= time range')

        # Convert seconds to index

        def find_time_idx(t):  # find closest time point
            return np.argmin(np.abs(np.asarray(times) - t))

        start = find_time_idx(window_params['start'])
        stop = find_time_idx(window_params['stop'])
        step = int(round(window_params['step'] / freq))
        length = int(round(window_params['length'] / freq))

        # For each training slice, give time samples to be included
        time_pick = [range(start, start + length)]
        while (time_pick[-1][0] + step) <= (stop - length + 1):
            start = time_pick[-1][0] + step
            time_pick.append(range(start, start + length))
        window_params['slices'] = time_pick

    # Keep last training times in milliseconds
    t_inds_ = [t[-1] for t in window_params['slices']]
    window_params['times'] = times[t_inds_]

    return window_params

def _predict(X, estimators):
    """Aux function of GeneralizationAcrossTime

    Predict each classifier. If multiple classifiers are passed, average
    prediction across all classifiers to result in a single prediction per
    classifier.

    Parameters
    ----------
    estimators : ndarray, shape (n_folds,) | shape (1,)
        Array of scikit-learn classifiers to predict data.
    X : ndarray, shape (n_epochs, n_features, n_times)
        To-be-predicted data
    Returns
    -------
    y_pred : ndarray, shape (n_epochs, m_prediction_dimensions)
        Classifier's prediction for each trial.
    """
    from scipy import stats
    from sklearn.base import is_classifier
    # Initialize results:
    n_epochs = X.shape[0]
    n_clf = len(estimators)

    # Compute prediction for each sub-estimator (i.e. per fold)
    # if independent, estimators = all folds
    for fold, clf in enumerate(estimators):
        _y_pred = clf.predict(X)
        # See inconsistency in dimensionality: scikit-learn/scikit-learn#5058
        if _y_pred.ndim == 1:
            _y_pred = _y_pred[:, None]
        # initialize predict_results array
        if fold == 0:
            predict_size = _y_pred.shape[1]
            y_pred = np.ones((n_epochs, predict_size, n_clf))
        y_pred[:, :, fold] = _y_pred

    # Collapse y_pred across folds if necessary (i.e. if independent)
    if fold > 0:
        # XXX need API to identify how multiple predictions can be combined?
        if is_classifier(clf):
            y_pred, _ = stats.mode(y_pred, axis=2)
        else:
            y_pred = np.mean(y_pred, axis=2)

    # Format shape
    y_pred = y_pred.reshape((n_epochs, predict_size))
    return y_pred

class GeneralizationAcrossTime(_GeneralizationAcrossTime):
    """Generalize across time and conditions

    Creates and estimator object used to 1) fit a series of classifiers on
    multidimensional time-resolved data, and 2) test the ability of each
    classifier to generalize across other time samples.

    Parameters
    ----------
    picks : array-like of int | None
        The channels indices to include. If None the data
        channels in info, except bad channels, are used.
    cv : int | object
        If an integer is passed, it is the number of folds.
        Specific cross-validation objects can be passed, see
        scikit-learn.cross_validation module for the list of possible objects.
        Defaults to 5.
    clf : object | None
        An estimator compliant with the scikit-learn API (fit & predict).
        If None the classifier will be a standard pipeline including
        StandardScaler and LogisticRegression with default parameters.
    train_times : dict | None
        A dictionary to configure the training times:

            ``slices`` : ndarray, shape (n_clfs,)
                Array of time slices (in indices) used for each classifier.
                If not given, computed from 'start', 'stop', 'length', 'step'.
            ``start`` : float
                Time at which to start decoding (in seconds).
                Defaults to min(epochs.times).
            ``stop`` : float
                Maximal time at which to stop decoding (in seconds).
                Defaults to max(times).
            ``step`` : float
                Duration separating the start of subsequent classifiers (in
                seconds). Defaults to one time sample.
            ``length`` : float
                Duration of each classifier (in seconds).
                Defaults to one time sample.

        If None, empty dict.
    test_times : 'diagonal' | dict | None, optional
        Configures the testing times.
        If set to 'diagonal', predictions are made at the time at which
        each classifier is trained.
        If set to None, predictions are made at all time points.
        If set to dict, the dict should contain ``slices`` or be contructed in
        a similar way to train_times::

            ``slices`` : ndarray, shape (n_clfs,)
                Array of time slices (in indices) used for each classifier.
                If not given, computed from 'start', 'stop', 'length', 'step'.

        If None, empty dict.
    predict_mode : {'cross-validation', 'mean-prediction'}
        Indicates how predictions are achieved with regards to the cross-
        validation procedure:

            ``cross-validation`` : estimates a single prediction per sample
                based on the unique independent classifier fitted in the
                cross-validation.
            ``mean-prediction`` : estimates k predictions per sample, based on
                each of the k-fold cross-validation classifiers, and average
                these predictions into a single estimate per sample.

        Default: 'cross-validation'
    scorer : object | None
        scikit-learn Scorer instance. If None, set to accuracy_score.
    n_jobs : int
        Number of jobs to run in parallel. Defaults to 1.

    Attributes
    ----------
    picks_ : array-like of int | None
        The channels indices to include.
    ch_names : list, array-like, shape (n_channels,)
        Names of the channels used for training.
    y_train_ : list | ndarray, shape (n_samples,)
        The categories used for training.
    train_times_ : dict
        A dictionary that configures the training times:

            ``slices`` : ndarray, shape (n_clfs,)
                Array of time slices (in indices) used for each classifier.
                If not given, computed from 'start', 'stop', 'length', 'step'.
            ``times`` : ndarray, shape (n_clfs,)
                The training times (in seconds).

    test_times_ : dict
        A dictionary that configures the testing times for each training time:

            ``slices`` : ndarray, shape (n_clfs, n_testing_times)
                Array of time slices (in indices) used for each classifier.
            ``times`` : ndarray, shape (n_clfs, n_testing_times)
                The testing times (in seconds) for each training time.

    cv_ : CrossValidation object
        The actual CrossValidation input depending on y.
    estimators_ : list of list of scikit-learn.base.BaseEstimator subclasses.
        The estimators for each time point and each fold.
    y_pred_ : list of lists of arrays of floats, shape (n_train_times, n_test_times, n_epochs, n_prediction_dims)
        The single-trial predictions estimated by self.predict() at each
        training time and each testing time. Note that the number of testing
        times per training time need not be regular, else
        ``np.shape(y_pred_) = (n_train_time, n_test_time, n_epochs).``
    y_true_ : list | ndarray, shape (n_samples,)
        The categories used for scoring ``y_pred_``.
    scorer_ : object
        scikit-learn Scorer instance.
    scores_ : list of lists of float
        The scores estimated by ``self.scorer_`` at each training time and each
        testing time (e.g. mean accuracy of self.predict(X)). Note that the
        number of testing times per training time need not be regular;
        else, ``np.shape(scores) = (n_train_time, n_test_time)``.

    See Also
    --------
    TimeDecoding

    Notes
    -----
    The function implements the method used in:

        Jean-Remi King, Alexandre Gramfort, Aaron Schurger, Lionel Naccache
        and Stanislas Dehaene, "Two distinct dynamic modes subtend the
        detection of unexpected sounds", PLoS ONE, 2014
        DOI: 10.1371/journal.pone.0085791

    .. versionadded:: 0.9.0
    """  # noqa
    def __init__(self, picks=None, cv=5, clf=None, train_times=None,
                 test_times=None, predict_mode='cross-validation', scorer=None,
                 n_jobs=1):
        super(GeneralizationAcrossTime, self).__init__(
            picks=picks, cv=cv, clf=clf, train_times=train_times,
            test_times=test_times, predict_mode=predict_mode, scorer=scorer,
            n_jobs=n_jobs)

    def __repr__(self):
        s = ''
        if hasattr(self, "estimators_"):
            s += "fitted, start : %0.3f (s), stop : %0.3f (s)" % (
                self.train_times_['start'], self.train_times_['stop'])
        else:
            s += 'no fit'
        if hasattr(self, 'y_pred_'):
            s += (", predicted %d epochs" % len(self.y_pred_[0][0]))
        else:
            s += ", no prediction"
        if hasattr(self, "estimators_") and hasattr(self, 'scores_'):
            s += ',\n '
        else:
            s += ', '
        if hasattr(self, 'scores_'):
            s += "scored"
            if callable(self.scorer_):
                s += " (%s)" % (self.scorer_.__name__)
        else:
            s += "no score"

        return "<GAT | %s>" % s

    def plot(self, title=None, vmin=None, vmax=None, tlim=None, ax=None,
             cmap='RdBu_r', show=True, colorbar=True,
             xlabel=True, ylabel=True):
        """Plotting function of GeneralizationAcrossTime object

        Plot the score of each classifier at each tested time window.

        Parameters
        ----------
        title : str | None
            Figure title.
        vmin : float | None
            Min color value for scores. If None, sets to min(``gat.scores_``).
        vmax : float | None
            Max color value for scores. If None, sets to max(``gat.scores_``).
        tlim : ndarray, (train_min, test_max) | None
            The time limits used for plotting.
        ax : object | None
            Plot pointer. If None, generate new figure.
        cmap : str | cmap object
            The color map to be used. Defaults to ``'RdBu_r'``.
        show : bool
            If True, the figure will be shown. Defaults to True.
        colorbar : bool
            If True, the colorbar of the figure is displayed. Defaults to True.
        xlabel : bool
            If True, the xlabel is displayed. Defaults to True.
        ylabel : bool
            If True, the ylabel is displayed. Defaults to True.

        Returns
        -------
        fig : instance of matplotlib.figure.Figure
            The figure.
        """
        return plot_gat_matrix(self, title=title, vmin=vmin, vmax=vmax,
                               tlim=tlim, ax=ax, cmap=cmap, show=show,
                               colorbar=colorbar, xlabel=xlabel, ylabel=ylabel)

    def plot_diagonal(self, title=None, xmin=None, xmax=None, ymin=None,
                      ymax=None, ax=None, show=True, color=None,
                      xlabel=True, ylabel=True, legend=True, chance=True,
                      label='Classif. score'):
        """Plotting function of GeneralizationAcrossTime object

        Plot each classifier score trained and tested at identical time
        windows.

        Parameters
        ----------
        title : str | None
            Figure title.
        xmin : float | None, optional
            Min time value.
        xmax : float | None, optional
            Max time value.
        ymin : float | None, optional
            Min score value. If None, sets to min(scores).
        ymax : float | None, optional
            Max score value. If None, sets to max(scores).
        ax : object | None
            Instance of mataplotlib.axes.Axis. If None, generate new figure.
        show : bool
            If True, the figure will be shown. Defaults to True.
        color : str
            Score line color.
        xlabel : bool
            If True, the xlabel is displayed. Defaults to True.
        ylabel : bool
            If True, the ylabel is displayed. Defaults to True.
        legend : bool
            If True, a legend is displayed. Defaults to True.
        chance : bool | float. Defaults to None
            Plot chance level. If True, chance level is estimated from the type
            of scorer.
        label : str
            Score label used in the legend. Defaults to 'Classif. score'.

        Returns
        -------
        fig : instance of matplotlib.figure.Figure
            The figure.
        """
        return plot_gat_times(self, train_time='diagonal', title=title,
                              xmin=xmin, xmax=xmax,
                              ymin=ymin, ymax=ymax, ax=ax, show=show,
                              color=color, xlabel=xlabel, ylabel=ylabel,
                              legend=legend, chance=chance, label=label)

    def plot_times(self, train_time, title=None, xmin=None, xmax=None,
                   ymin=None, ymax=None, ax=None, show=True, color=None,
                   xlabel=True, ylabel=True, legend=True, chance=True,
                   label='Classif. score'):
        """Plotting function of GeneralizationAcrossTime object

        Plot the scores of the classifier trained at specific training time(s).

        Parameters
        ----------
        train_time : float | list or array of float
            Plots scores of the classifier trained at train_time.
        title : str | None
            Figure title.
        xmin : float | None, optional
            Min time value.
        xmax : float | None, optional
            Max time value.
        ymin : float | None, optional
            Min score value. If None, sets to min(scores).
        ymax : float | None, optional
            Max score value. If None, sets to max(scores).
        ax : object | None
            Instance of mataplotlib.axes.Axis. If None, generate new figure.
        show : bool
            If True, the figure will be shown. Defaults to True.
        color : str or list of str
            Score line color(s).
        xlabel : bool
            If True, the xlabel is displayed. Defaults to True.
        ylabel : bool
            If True, the ylabel is displayed. Defaults to True.
        legend : bool
            If True, a legend is displayed. Defaults to True.
        chance : bool | float.
            Plot chance level. If True, chance level is estimated from the type
            of scorer.
        label : str
            Score label used in the legend. Defaults to 'Classif. score'.

        Returns
        -------
        fig : instance of matplotlib.figure.Figure
            The figure.
        """
        if (not isinstance(train_time, float) and
            not (isinstance(train_time, (list, np.ndarray)) and
                 np.all([isinstance(time, float) for time in train_time]))):
            raise ValueError('train_time must be float | list or array of '
                             'floats. Got %s.' % type(train_time))

        return plot_gat_times(self, train_time=train_time, title=title,
                              xmin=xmin, xmax=xmax,
                              ymin=ymin, ymax=ymax, ax=ax, show=show,
                              color=color, xlabel=xlabel, ylabel=ylabel,
                              legend=legend, chance=chance, label=label)

class TimeDecoding(_GeneralizationAcrossTime):
    """Train and test a series of classifiers at each time point to obtain a
    score across time.

    Parameters
    ----------
    picks : array-like of int | None
        The channels indices to include. If None the data
        channels in info, except bad channels, are used.
    cv : int | object
        If an integer is passed, it is the number of folds.
        Specific cross-validation objects can be passed, see
        scikit-learn.cross_validation module for the list of possible objects.
        Defaults to 5.
    clf : object | None
        An estimator compliant with the scikit-learn API (fit & predict).
        If None the classifier will be a standard pipeline including
        StandardScaler and a Logistic Regression with default parameters.
    times : dict | None
        A dictionary to configure the training times:

            ``slices`` : ndarray, shape (n_clfs,)
                Array of time slices (in indices) used for each classifier.
                If not given, computed from 'start', 'stop', 'length', 'step'.
            ``start`` : float
                Time at which to start decoding (in seconds). By default,
                min(epochs.times).
            ``stop`` : float
                Maximal time at which to stop decoding (in seconds). By
                default, max(times).
            ``step`` : float
                Duration separating the start of subsequent classifiers (in
                seconds). By default, equals one time sample.
            ``length`` : float
                Duration of each classifier (in seconds). By default, equals
                one time sample.

        If None, empty dict.
    predict_mode : {'cross-validation', 'mean-prediction'}
        Indicates how predictions are achieved with regards to the cross-
        validation procedure:

            ``cross-validation`` : estimates a single prediction per sample
                based on the unique independent classifier fitted in the
                cross-validation.
            ``mean-prediction`` : estimates k predictions per sample, based on
                each of the k-fold cross-validation classifiers, and average
                these predictions into a single estimate per sample.

        Default: 'cross-validation'
    scorer : object | None
        scikit-learn Scorer instance. If None, set to accuracy_score.
    n_jobs : int
        Number of jobs to run in parallel. Defaults to 1.

    Attributes
    ----------
    picks_ : array-like of int | None
        The channels indices to include.
    ch_names : list, array-like, shape (n_channels,)
        Names of the channels used for training.
    y_train_ : ndarray, shape (n_samples,)
        The categories used for training.
    times_ : dict
        A dictionary that configures the training times:

            ``slices`` : ndarray, shape (n_clfs,)
                Array of time slices (in indices) used for each classifier.
                If not given, computed from 'start', 'stop', 'length', 'step'.
            ``times`` : ndarray, shape (n_clfs,)
                The training times (in seconds).

    cv_ : CrossValidation object
        The actual CrossValidation input depending on y.
    estimators_ : list of list of scikit-learn.base.BaseEstimator subclasses.
        The estimators for each time point and each fold.
    y_pred_ : ndarray, shape (n_times, n_epochs, n_prediction_dims)
        Class labels for samples in X.
    y_true_ : list | ndarray, shape (n_samples,)
        The categories used for scoring ``y_pred_``.
    scorer_ : object
        scikit-learn Scorer instance.
    scores_ : list of float, shape (n_times,)
        The scores (mean accuracy of self.predict(X) wrt. y.).

    See Also
    --------
    GeneralizationAcrossTime

    Notes
    -----
    The function is equivalent to the diagonal of GeneralizationAcrossTime()

    .. versionadded:: 0.10
    """

    def __init__(self, picks=None, cv=5, clf=None, times=None,
                 predict_mode='cross-validation', scorer=None, n_jobs=1):
        super(TimeDecoding, self).__init__(picks=picks, cv=cv, clf=None,
                                           train_times=times,
                                           test_times='diagonal',
                                           predict_mode=predict_mode,
                                           scorer=scorer, n_jobs=n_jobs)
        self._clean_times()

    def __repr__(self):
        s = ''
        if hasattr(self, "estimators_"):
            s += "fitted, start : %0.3f (s), stop : %0.3f (s)" % (
                self.times_['start'], self.times_['stop'])
        else:
            s += 'no fit'
        if hasattr(self, 'y_pred_'):
            s += (", predicted %d epochs" % len(self.y_pred_[0]))
        else:
            s += ", no prediction"
        if hasattr(self, "estimators_") and hasattr(self, 'scores_'):
            s += ',\n '
        else:
            s += ', '
        if hasattr(self, 'scores_'):
            s += "scored"
            if callable(self.scorer_):
                s += " (%s)" % (self.scorer_.__name__)
        else:
            s += "no score"

        return "<TimeDecoding | %s>" % s

    def fit(self, epochs, y=None):
        """ Train a classifier on each specified time slice.

        Note. This function sets the ``picks_``, ``ch_names``, ``cv_``,
        ``y_train``, ``train_times_`` and ``estimators_`` attributes.

        Parameters
        ----------
        epochs : instance of Epochs
            The epochs.
        y : list or ndarray of int, shape (n_samples,) or None, optional
            To-be-fitted model values. If None, y = epochs.events[:, 2].

        Returns
        -------
        self : TimeDecoding
            Returns fitted TimeDecoding object.

        Notes
        ------
        If X and y are not C-ordered and contiguous arrays of np.float64 and
        X is not a scipy.sparse.csr_matrix, X and/or y may be copied.

        If X is a dense array, then the other methods will not support sparse
        matrices as input.
        """
        self._prep_times()
        super(TimeDecoding, self).fit(epochs, y=y)
        self._clean_times()
        return self

    def predict(self, epochs):
        """ Test each classifier on each specified testing time slice.

        .. note:: This function sets the ``y_pred_`` and ``test_times_``
                  attributes.

        Parameters
        ----------
        epochs : instance of Epochs
            The epochs. Can be similar to fitted epochs or not. See
            predict_mode parameter.

        Returns
        -------
        y_pred : list of lists of arrays of floats, shape (n_times, n_epochs, n_prediction_dims)
            The single-trial predictions at each time sample.
        """  # noqa
        self._prep_times()
        super(TimeDecoding, self).predict(epochs)
        self._clean_times()
        return self.y_pred_

    def score(self, epochs=None, y=None):
        """Score Epochs

        Estimate scores across trials by comparing the prediction estimated for
        each trial to its true value.

        Calls ``predict()`` if it has not been already.

        Note. The function updates the ``scorer_``, ``scores_``, and
        ``y_true_`` attributes.

        Parameters
        ----------
        epochs : instance of Epochs | None, optional
            The epochs. Can be similar to fitted epochs or not.
            If None, it needs to rely on the predictions ``y_pred_``
            generated with ``predict()``.
        y : list | ndarray, shape (n_epochs,) | None, optional
            True values to be compared with the predictions ``y_pred_``
            generated with ``predict()`` via ``scorer_``.
            If None and ``predict_mode``=='cross-validation' y = ``y_train_``.

        Returns
        -------
        scores : list of float, shape (n_times,)
            The scores estimated by ``scorer_`` at each time sample (e.g. mean
            accuracy of ``predict(X)``).
        """
        if epochs is not None:
            self.predict(epochs)
        else:
            if not hasattr(self, 'y_pred_'):
                raise RuntimeError('Please predict() epochs first or pass '
                                   'epochs to score()')
        self._prep_times()
        super(TimeDecoding, self).score(epochs=None, y=y)
        self._clean_times()
        return self.scores_

    def plot(self, title=None, xmin=None, xmax=None, ymin=None, ymax=None,
             ax=None, show=True, color=None, xlabel=True, ylabel=True,
             legend=True, chance=True, label='Classif. score'):
        """Plotting function

        Predict each classifier. If multiple classifiers are passed, average
        prediction across all classifiers to result in a single prediction per
        classifier.

        Parameters
        ----------
        title : str | None
            Figure title.
        xmin : float | None, optional,
            Min time value.
        xmax : float | None, optional,
            Max time value.
        ymin : float
            Min score value. Defaults to 0.
        ymax : float
            Max score value. Defaults to 1.
        ax : object | None
            Instance of mataplotlib.axes.Axis. If None, generate new figure.
        show : bool
            If True, the figure will be shown. Defaults to True.
        color : str
            Score line color. Defaults to 'steelblue'.
        xlabel : bool
            If True, the xlabel is displayed. Defaults to True.
        ylabel : bool
            If True, the ylabel is displayed. Defaults to True.
        legend : bool
            If True, a legend is displayed. Defaults to True.
        chance : bool | float. Defaults to None
            Plot chance level. If True, chance level is estimated from the type
            of scorer.
        label : str
            Score label used in the legend. Defaults to 'Classif. score'.

        Returns
        -------
        fig : instance of matplotlib.figure.Figure
            The figure.
        """
        # XXX JRK: need cleanup in viz
        self._prep_times()
        fig = plot_gat_times(self, train_time='diagonal', title=title,
                             xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, ax=ax,
                             show=show, color=color, xlabel=xlabel,
                             ylabel=ylabel, legend=legend, chance=chance,
                             label=label)
        self._clean_times()
        return fig

    def _prep_times(self):
        """Auxiliary function to allow compability with GAT"""
        self.test_times = 'diagonal'
        if hasattr(self, 'times'):
            self.train_times = self.times
        if hasattr(self, 'times_'):
            self.train_times_ = self.times_
            self.test_times_ = _DecodingTime()
            self.test_times_['slices'] = [[slic] for slic in
                                          self.train_times_['slices']]
            self.test_times_['times'] = [[tim] for tim in
                                         self.train_times_['times']]
        if hasattr(self, 'scores_'):
            self.scores_ = [[score] for score in self.scores_]
        if hasattr(self, 'y_pred_'):
            self.y_pred_ = [[y_pred] for y_pred in self.y_pred_]

    def _clean_times(self):
        """Auxiliary function to allow compability with GAT"""
        if hasattr(self, 'train_times'):
            self.times = self.train_times
        if hasattr(self, 'train_times_'):
            self.times_ = self.train_times_
        for attr in ['test_times', 'train_times',
                     'test_times_', 'train_times_']:
            if hasattr(self, attr):
                delattr(self, attr)
        if hasattr(self, 'y_pred_'):
            self.y_pred_ = [y_pred[0] for y_pred in self.y_pred_]
        if hasattr(self, 'scores_'):
            self.scores_ = [score[0] for score in self.scores_]


from RLToolbox.network.network import Network
import tensorflow as tf
import tensorlayer as tl
import numpy as np
from RLToolbox.network.alexNet.bvlc_alexnet import AlexNet as AlexNetKaffe

class AlexNet(Network):
    def __init__(self, scope, pms):
        super(AlexNet, self).__init__(scope, pms)
        with tf.variable_scope("%s_shared" % scope):
            self.obs = obs = tf.placeholder(
                tf.float32 , shape=[None] + [pms.obs_shape[0] , pms.obs_shape[1] , pms.obs_shape[2]] ,
                name="%s_obs" % scope)
            self.obs_target = obs_target = tf.placeholder(
                tf.float32 , shape=[None] + pms.obs_shape , name="%s_obs_target" % scope)
            self.action_n = tf.placeholder(tf.float32 , shape=[None , pms.action_shape] , name="%s_action" % scope)
            self.advant = tf.placeholder(tf.float32 , shape=[None] , name="%s_advant" % scope)
            self.old_dist_means_n = tf.placeholder(tf.float32 , shape=[None , pms.action_shape] ,
                                                   name="%s_oldaction_dist_means" % scope)
            self.old_dist_logstds_n = tf.placeholder(tf.float32 , shape=[None , pms.action_shape] ,
                                                     name="%s_oldaction_dist_logstds" % scope)

            self.network = AlexNetKaffe({'data': self.obs})
            self.action_dist_means_n = self.network.layers['train_fc11']
            self.N = tf.shape(obs)[0]
            self.action_dist_logstd_param = tf.Variable(
                (.01 * np.random.randn(1 , pms.action_shape)).astype(np.float32) , name="train_%s_policy_logstd" % scope)
            # self.action_dist_logstd_param = tf.maximum(self.action_dist_logstd_param, np.log(pms.min_std))
            self.action_dist_logstds_n = tf.tile(self.action_dist_logstd_param ,
                                                 tf.pack((tf.shape(self.action_dist_means_n)[0] , 1)))
            start_name = "%s_shared/%s" %(scope, "train")
            self.var_list = [v for v in tf.trainable_variables() if v.name.startswith(start_name)]

    def asyc_parameters(self, session=None):
        self.network.load("bvlc_alexnet_model.npy", session, scope="%s_shared" % self.scope, ignore_missing=True)