def run():
    running_number = 2
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"
    local_test_flag = False
    type = '_pid'
    icehockey_mdn_Qs_config_path = "../environment_settings/ice_hockey_predict_Qs_lstm{0}.yaml".format(str(type))
    icehockey_mdn_Qs_config = LSTMQsCongfig.load(icehockey_mdn_Qs_config_path)
    saved_network_dir, log_dir = get_model_and_log_name(config=icehockey_mdn_Qs_config,
                                                        model_catagoery='lstm_Qs',
                                                        running_number=running_number)

    if local_test_flag:
        data_store_dir = "/Users/liu/Desktop/Ice-hokcey-data-sample/feature-sample"
        dir_games_all = os.listdir(data_store_dir)
        training_dir_games_all = os.listdir(data_store_dir)
        validate_dir_games_all = os.listdir(data_store_dir)
        testing_dir_games_all = os.listdir(data_store_dir)
        tmp_testing_dir_games_all = os.listdir(data_store_dir)
        save_flag = False
    else:
        data_store_dir = icehockey_mdn_Qs_config.Learn.save_mother_dir + "/oschulte/Galen/Ice-hockey-data/2018-2019/"
        dir_games_all = os.listdir(data_store_dir)
        save_flag = True
        if running_number == 0:
            training_dir_games_all = dir_games_all[
                                     0: len(dir_games_all) / 5 * 4 - running_number * len(dir_games_all) / 5]
        else:
            training_dir_games_all = dir_games_all[
                                     0: len(dir_games_all) / 5 * 4 - running_number * len(dir_games_all) / 5] \
                                     + dir_games_all[-running_number * len(dir_games_all) / 5:]
        test_validate_dir_games_all = [item for item in dir_games_all if item not in training_dir_games_all]
        testing_dir_games_all = test_validate_dir_games_all[:len(test_validate_dir_games_all)/2]
        validate_dir_games_all = test_validate_dir_games_all[len(test_validate_dir_games_all) / 2:]
        tmp_testing_dir_games_all = testing_dir_games_all[-10:]  # TODO: it is a small running testing, not the real one
    # data_store_dir = '/cs/oschulte/xiangyus/2019-icehockey-data-preprocessed/2018-2019'
    # saved_network_dir = '/Local-Scratch/oschulte/Galen/icehockey-models/lstm_Qs_model_saved_NN/lstm_saved_networks_featurev1_Qs_batch32_iterate10_lr1e-05_v1_MaxTL10_LSTM512_dense256'
    number_of_total_game = len(dir_games_all)
    icehockey_mdn_Qs_config.Learn.number_of_total_game = number_of_total_game

    sess = tf.Session()
    model = TD_Prediction(config=icehockey_mdn_Qs_config)
    model()
    sess.run(tf.global_variables_initializer())

    if not local_test_flag:
        if not os.path.exists(saved_network_dir):
            os.mkdir(saved_network_dir)
        # save the training and testing dir list
        if os.path.exists(saved_network_dir + '/training_file_dirs_all.csv'):
            os.rename(saved_network_dir + '/training_file_dirs_all.csv',
                      saved_network_dir + '/bak_training_file_dirs_all_{0}.csv'
                      .format(datetime.date.today().strftime("%Y%B%d")))
        if os.path.exists(saved_network_dir + '/testing_file_dirs_all.csv'):
            os.rename(saved_network_dir + '/testing_file_dirs_all.csv',
                      saved_network_dir + '/bak_testing_file_dirs_all_{0}.csv'
                      .format(datetime.date.today().strftime("%Y%B%d")))
        if os.path.exists(saved_network_dir + '/validate_file_dirs_all.csv'):
            os.rename(saved_network_dir + '/validate_file_dirs_all.csv',
                      saved_network_dir + '/bak_validate_file_dirs_all_{0}.csv'
                      .format(datetime.date.today().strftime("%Y%B%d")))
        with open(saved_network_dir + '/training_file_dirs_all.csv', 'wb') as f:
            for dir in training_dir_games_all:
                f.write(dir + '\n')
        with open(saved_network_dir + '/validate_file_dirs_all.csv', 'wb') as f:
            for dir in validate_dir_games_all:
                f.write(dir + '\n')
        with open(saved_network_dir + '/testing_file_dirs_all.csv', 'wb') as f:
            for dir in testing_dir_games_all:
                f.write(dir + '\n')
    run_network(sess=sess, model=model, config=icehockey_mdn_Qs_config, log_dir=log_dir,
                save_network_dir=saved_network_dir, data_store=data_store_dir,
                training_dir_games_all=training_dir_games_all, testing_dir_games_all=tmp_testing_dir_games_all,
                player_id_cluster_dir=None, save_flag=save_flag)
    sess.close()
def run_calibration():
    model_type = 'lstm_Qs'
    player_info = '_pid'
    apply_old = False
    apply_difference = False
    running_numbers = [0, 1, 2, 3, 4]
    if model_type == 'cvrnn':
        model_number = 601
        embed_mode = '_embed_random'
        predicted_target = '_PlayerLocalId_predict_nex_goal'
        icehockey_config_path = "../../environment_settings/" \
                                "icehockey_cvrnn{0}_config{1}{2}.yaml" \
            .format(predicted_target, player_info, embed_mode)
        config = CVRNNCongfig.load(icehockey_config_path)
    elif model_type == 'cvae':
        model_number = 601
        embed_mode = ''
        predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
        icehockey_config_path = "../../environment_settings/icehockey_cvae_lstm{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVAECongfig.load(icehockey_config_path)
    elif model_type == 'vhe':
        model_number = 601
        embed_mode = ''
        predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
        icehockey_config_path = "../../environment_settings/icehockey_vhe_lstm{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVAECongfig.load(icehockey_config_path)
    elif model_type == 'encoder':
        model_number = 901
        embed_mode = ''
        predicted_target = '_PlayerLocalId_predict_next_goal'
        # player_id_cluster_dir = '../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
        icehockey_encoder_config_path = "../../environment_settings/" \
                                        "icehockey_stats_lstm_encoder{0}" \
                                        "_config{1}.yaml".format(predicted_target, player_info)
        config = EncoderConfig.load(icehockey_encoder_config_path)
    elif model_type == 'lstm_Qs':
        embed_mode = ''
        # 'model_2101_three_cut_lstm_next_Qs_featurev1_next_Qs_batch32_iterate10_lr1e-05_v1_MaxTL10_LSTM512_dense256'
        model_number = 901
        icehockey_config_path = "../../environment_settings/ice_hockey_predict_Qs_lstm{0}.yaml".format(
            player_info)
        config = LSTMQsCongfig.load(icehockey_config_path)
    else:
        raise ValueError('incorrect model type {0}'.format(model_type))
    calibration_features = ['period', 'scoreDifferential', 'zone', 'manpowerSituation', 'home_away']
    calibration_bins = {'period': {'feature_name': ('period'), 'range': (1, 2, 3, 4)},
                        'scoreDifferential': {'feature_name': ('scoreDifferential'), 'range': range(-10, 10)},
                        'zone': {'feature_name': ('zone'), 'range': ('dz', 'nz', 'oz')},
                        'manpowerSituation': {'feature_name': ('manpowerSituation'),
                                              'range': ('shortHanded', 'evenStrength', 'powerPlay')},
                        'home_away': {'feature_name': ('home_away'), 'range': (1, 0)},
                        # TODO: we must add the home/away label
                        }
    source_data_dir = '/Local-Scratch/oschulte/Galen/2018-2019/'
    model_data_store_dir = '/Local-Scratch/oschulte/Galen/Ice-hockey-data/2018-2019'


    Cali = Calibration(bins=calibration_bins, source_data_dir=source_data_dir,
                       calibration_features=calibration_features, config=config,
                       model_data_store_dir=model_data_store_dir, apply_old=apply_old,
                       apply_difference=apply_difference,
                       model_type=model_type, model_number=model_number,
                       player_info=player_info, calibration_type='next_goal',
                       # testing_dir_all=testing_dir_games_all[:1],
                       embed_mode=embed_mode,
                       focus_actions_list=[],
                       if_apply_cv=True,
                       running_numbers=running_numbers)
    Cali.construct_bin_dicts()
    for running_number in running_numbers:
        saved_network_dir, log_dir = get_model_and_log_name(config=config,
                                                            model_catagoery=model_type,
                                                            running_number=running_number)
        with open(saved_network_dir + '/testing_file_dirs_all.csv', 'rb') as f:
            # with open('../../sport_resource/ice_hockey_201819/' + '/testing_file_dirs_all.csv', 'rb') as f:
            testing_dir_all = f.readlines()
        testing_dir_games_all = []
        for testing_dir in testing_dir_all:
            testing_dir_games_all.append(str(int(testing_dir)) + '-playsequence-wpoi.json')
        Cali.aggregate_calibration_values(testing_dir_games_all=testing_dir_games_all, running_number=running_number)
    Cali.compute_distance()
    print Cali.save_calibration_dir
def run_calibration():
    model_type = 'cvrnn'
    player_info = '_box'
    apply_old = False
    apply_difference = False
    if model_type == 'cvrnn':
        model_number = 2101
        predicted_target = '_PlayerLocalId'
        icehockey_config_path = "../../environment_settings/icehockey_cvrnn{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVRNNCongfig.load(icehockey_config_path)
    elif model_type == 'lstm_Qs':
        model_number = 2101
        icehockey_config_path = "../../environment_settings/ice_hockey_predict_Qs_lstm{0}.yaml".format(
            player_info)
        config = LSTMQsCongfig.load(icehockey_config_path)
    else:
        raise ValueError('incorrect model type {0}'.format(model_type))
    calibration_features = [
        'period', 'scoreDifferential', 'zone', 'manpowerSituation', 'home_away'
    ]
    calibration_bins = {
        'period': {
            'feature_name': ('period'),
            'range': (1, 2, 3, 4)
        },
        'scoreDifferential': {
            'feature_name': ('scoreDifferential'),
            'range': range(-10, 10)
        },
        'zone': {
            'feature_name': ('zone'),
            'range': ('dz', 'nz', 'oz')
        },
        'manpowerSituation': {
            'feature_name': ('manpowerSituation'),
            'range': ('shortHanded', 'evenStrength', 'powerPlay')
        },
        'home_away': {
            'feature_name': ('home_away'),
            'range': (1, 0)
        },
        # TODO: we must add the home/away label
    }
    source_data_dir = '/Local-Scratch/oschulte/Galen/2018-2019/'
    model_data_store_dir = '/Local-Scratch/oschulte/Galen/Ice-hockey-data/2018-2019'
    Cali = Calibration(bins=calibration_bins,
                       source_data_dir=source_data_dir,
                       calibration_features=calibration_features,
                       config=config,
                       model_data_store_dir=model_data_store_dir,
                       apply_old=apply_old,
                       apply_difference=apply_difference,
                       model_type=model_type,
                       model_number=model_number,
                       player_info=player_info,
                       focus_actions_list=[])
    Cali.construct_bin_dicts()
    Cali.aggregate_calibration_values()
    Cali.compute_distance()
    print Cali.save_calibration_dir
        predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
        player_id_cluster_dir = '../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
        icehockey_config_path = "../environment_settings/icehockey_cvae_lstm{0}_config{1}.yaml".format(
            predicted_target, player_info)
        icehockey_model_config = CVAECongfig.load(icehockey_config_path)

    elif model_type == 'vhe':
        predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
        player_id_cluster_dir = '../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
        icehockey_config_path = "../environment_settings/icehockey_vhe_lstm{0}_config{1}.yaml".format(
            predicted_target, player_info)
        icehockey_model_config = CVAECongfig.load(icehockey_config_path)

    elif model_type == 'lstm_Qs':
        icehockey_config_path = "../environment_settings/ice_hockey_predict_Qs_lstm{0}.yaml".format(player_info)
        icehockey_model_config = LSTMQsCongfig.load(icehockey_config_path)
        player_id_cluster_dir = None

    elif model_type == 'encoder':
        predicted_target = '_PlayerLocalId_predict_next_goal'
        player_id_cluster_dir = '../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
        icehockey_encoder_config_path = "../environment_settings/" \
                                        "icehockey_stats_lstm_encoder{0}" \
                                        "_config{1}.yaml".format(predicted_target, player_info)
        icehockey_model_config = EncoderConfig.load(icehockey_encoder_config_path)

    elif model_type == 'lstm_diff':
        icehockey_config_path = "../environment_settings/ice_hockey_predict_score_diff_lstm{0}.yaml".format(player_info)
        icehockey_model_config = LSTMDiffCongfig.load(icehockey_config_path)
        player_id_cluster_dir = None
    else:
Beispiel #5
0
def run_calibration():
    model_type = 'cvrnn'
    player_info = '_box'
    apply_old = False
    apply_difference = True
    if model_type == 'cvrnn':
        model_number = 2101
        predicted_target = '_PlayerLocalId'
        icehockey_config_path = "../../environment_settings/icehockey_cvrnn{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVRNNCongfig.load(icehockey_config_path)

    elif model_type == 'lstm_diff':
        model_number = 2101
        icehockey_config_path = "../../environment_settings/ice_hockey_predict_score_diff_lstm{0}.yaml".format(
            player_info)
        config = LSTMQsCongfig.load(icehockey_config_path)
    else:
        raise ValueError('incorrect model type {0}'.format(model_type))
    calibration_features = [
        'period', 'scoreDifferential', 'zone', 'manpowerSituation', 'home_away'
    ]
    calibration_bins = {
        'period': {
            'feature_name': ('period'),
            'range': (1, 2, 3, 4)
        },
        'scoreDifferential': {
            'feature_name': ('scoreDifferential'),
            'range': range(-10, 10)
        },
        'zone': {
            'feature_name': ('zone'),
            'range': ('dz', 'nz', 'oz')
        },
        'manpowerSituation': {
            'feature_name': ('manpowerSituation'),
            'range': ('shortHanded', 'evenStrength', 'powerPlay')
        },
        'home_away': {
            'feature_name': ('home_away'),
            'range': (1, 0)
        },
        # TODO: we must add the home/away label
    }
    source_data_dir = '/Local-Scratch/oschulte/Galen/2018-2019/'
    model_data_store_dir = '/Local-Scratch/oschulte/Galen/Ice-hockey-data/2018-2019'

    testing_dir_games_all = []
    # with open('../../sport_resource/ice_hockey_201819/testing_file_dirs_all.csv', 'rb') as f:
    saved_network_dir, log_dir = get_model_and_log_name(
        config=config, model_catagoery=model_type)
    # with open(saved_network_dir + '/testing_file_dirs_all.csv', 'rb') as f:
    with open(
            '../../sport_resource/ice_hockey_201819/' +
            '/testing_file_dirs_all.csv', 'rb') as f:
        testing_dir_all = f.readlines()
    for testing_dir in testing_dir_all:
        testing_dir_games_all.append(
            str(int(testing_dir)) + '-playsequence-wpoi.json')

    Cali = Calibration(bins=calibration_bins,
                       source_data_dir=source_data_dir,
                       calibration_features=calibration_features,
                       config=config,
                       model_data_store_dir=model_data_store_dir,
                       apply_old=apply_old,
                       apply_difference=apply_difference,
                       model_type=model_type,
                       model_number=model_number,
                       player_info=player_info,
                       calibration_type='score_diff',
                       testing_dir_all=testing_dir_games_all,
                       focus_actions_list=[])
    Cali.construct_bin_dicts()
    Cali.aggregate_calibration_values()
    Cali.compute_distance()
    print Cali.save_calibration_dir