Пример #1
0
def show_evaluation_hist(league_name):

    # requested params : [thr, field]
    args = request.json
    config = configs[league_name]['league']

    params = {**args, **config}
    params['save_dir'] = STATIC_DIR

    outcome, msg = check_league(league_name)
    if (outcome == False):
        response = make_response(msg, 404)

    else:
        model = models[league_name]
        feat_eng = configs[league_name]['feat_eng']

        testset, pred, true = real_case_inference(model, params, feat_eng)
        pred_df, outcome, fig = evaluate_results(true,
                                                 pred,
                                                 params,
                                                 plot=False)
        response = show_plot(fig)

    return response
Пример #2
0
def show_evaluation_hist_from_path(league_name):

    # requested params : [thr, field]
    args = request.json
    model_dir = args['model_dir']
    model_name = args['model_name']

    response = check_predict_paths(model_dir, model_name)
    if not response['check']:
        return make_response(response['msg'], 400)

    paths = response['paths']
    config, model = load_configs_from_paths(paths)
    league_params, data_params = config['league'], config['data']

    params = {**args, **league_params, **data_params}
    params['save_dir'] = STATIC_DIR

    outcome, msg = check_league(league_name)
    if (outcome == False):
        response = make_response(msg, 404)

    else:
        # model = models[league_name]
        feat_eng = config['feat_eng']

        testset, pred, true = real_case_inference(model, params, feat_eng)
        pred_df, outcome, fig = evaluate_results(true,
                                                 pred,
                                                 params,
                                                 plot=False)
        response = show_plot(fig)

    return response
Пример #3
0
def get_teams_from_league():
    """

    Requested Args:
        league_name: str

    Returns:
        response: dict { 'league_name': str,
                         'teams': list }
    """

    args = request.args
    league_name = args['league']

    outcome, msg = check_league(league_name)
    if not outcome:
        response = make_response(msg, 404)
    else:
        # teams = sorted(LEAGUE.TEAMS_LEAGUE[str(league_name).lower()])
        # response = make_response(jsonify({'league_name': league_name,
        #                                   'teams': teams}))

        teams = teams_extraction(league_name)
        teams.sort()

        response_dict = {'league_name': league_name, 'teams': teams}

        response = make_response(response_dict, 200)

    return response
Пример #4
0
def update_all_leagues():
    """
    Request Args:
                    - npm: int

    """
    npm = request.args['npm']

    assert int(npm) > 0, 'NPM must be greater than 0'

    response = {}

    for league_name in LEAGUE.LEAGUE_NAMES:
        response_league_name = check_league(league_name)
        response_league_data = check_data_league(
            league_name, npm) if response_league_name['check'] else {
                'check': False,
                'msg': ''
            }

        msg = f"League_name: {response_league_name['msg']} \nLeague_data: {response_league_data['msg']}"

        if not response_league_name['check'] or not response_league_data[
                'check']:
            response[league_name] = msg

        else:
            logger.info('--------------------------------------')
            logger.info(f'\nUpdating {league_name.upper()}\n')
            league_params = {}
            league_params['league_name'] = league_name
            league_params['n_prev_match'] = npm
            league_params['league_dir'] = DATA_DIR

            update_response = update_data_league(league_params)

            if update_response['check']:
                succ_msg = f'Successful Update: {league_name} - npm={npm}'
                logger.info(succ_msg)
                response[
                    league_name] = f'Successful Update: {league_name} -> npm = {npm}'

            else:
                fail_msg = f'Failed Update: {league_name} - npm={npm} \n {update_response["msg"]}'
                logger.error(fail_msg)
                response[
                    league_name] = f'Failed Update: {league_name} -> npm = {npm} : {fail_msg}'

    return make_response(response)
Пример #5
0
def get_round():
    """
    Requested Args:
        - league_name: str

    """

    args = request.args

    league_name = args['league_name']

    outcome, msg = check_league(league_name)
    if not outcome:
        response = make_response(msg, 404)
    else:
        pass
Пример #6
0
def create_league_data():
    """
    Create and Save league data for some league available

    Method = POST
    Body: { 'league_name': LEAGUE_NAME,
            'n_prev_match': NPM,
          }

    """

    params = request.json
    league_name = params['league_name']
    npm = params['n_prev_match']

    response_league_name = check_league(league_name)
    response_npm = check_npm(npm)

    if response_npm['check'] and response_league_name['check']:

        params['league_dir'] = DATA_DIR
        params['train'] = True

        try:
            set_start_method('spawn')
        except RuntimeError:
            pass

        params = check_data_params(params)
        p = Process(target=extract_data_league, args=(params, ))
        p.start()
        p.join()
        # league_df, preprocessed_data = extract_data_league(params)

        response = make_response(
            f'Successfully writing: {league_name.upper()}, npm = {npm}', 200)

    else:
        error_msg = f'League name : {response_league_name["msg"]}'
        error_msg += f'NPM : {response_npm["msg"]}'

        response = make_response(error_msg, 400)

    return response
Пример #7
0
def update_league(league_name):

    npm = request.args['npm']

    assert int(npm) > 0, 'NPM must be greater than 0'

    league_params = {}

    response_league_name = check_league(league_name)
    response_league_data = check_data_league(
        league_name, npm) if response_league_name['check'] else {
            'check': False,
            'msg': ''
        }

    msg = f"League_name: {response_league_name['msg']} \nLeague_data: {response_league_data['msg']}"

    if not response_league_name['check'] or not response_league_data['check']:
        response = make_response(msg, 404)
    else:
        league_params['league_name'] = league_name
        league_params['n_prev_match'] = npm
        league_params['league_dir'] = DATA_DIR

        response = update_data_league(league_params)

        if response['check']:
            succ_msg = f'Successful Update: {league_name} - npm={npm}'
            logger.info(succ_msg)
            response = make_response(
                f'Successful Update: {league_name} -> npm = {npm}', 200)
        else:
            fail_msg = f'Failed Update: {league_name} - npm={npm} \n {response["msg"]}'
            logger.error(fail_msg)
            response = make_response(
                f'Failed Update: {league_name} -> npm = {npm} \n {response["msg"]}',
                400)

        # UPDATE MATCHES LEAGUE
        Data_Api().write_league_matches(league_name, DATA_DIR)

    return response
Пример #8
0
def load_model_and_config(req_params, models, configs):

    model, config = None, None

    if ('league_name' in req_params.keys() and models is not None
            and configs is not None):
        league_name = req_params['league_name']
        outcome, msg = check_league(league_name)
        if (outcome == False):
            response = {'check': False, 'msg': msg}

        # league_params = configs[league_name]['league']
        # data_params = configs[league_name]['data']
        # model = models[league_name]
        # feat_eng = configs[league_name]['feat_eng']
        else:
            model, config = models[league_name], configs[league_name]
            response = {'check': True}

    elif ('model_dir' in req_params.keys()
          and 'model_name' in req_params.keys()):
        model_dir = req_params['model_dir']
        model_name = req_params['model_name']
        response = check_predict_paths(model_dir, model_name)

        if (response['check']):

            paths = response['paths']
            config, model = load_configs_from_paths(paths)
            # league_params = config['league']
            # data_params = config['data']
            # feat_eng = config['feat_eng']

    else:
        msg = 'Body does not provide the required params'
        response = {'check': False, 'msg': msg}

    return response, model, config