# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] cell_id="00000-c4b06e56-de4c-4779-8055-c006e6fa21c2" deepnote_cell_type="markdown"
# # Estimate user pp at time of play
# **Contributors:** <NAME>
#
# **Achievement:** Player pp at the time a score was set can be estimated by computing pp on all scores the player set before the play.
#
# **Requirements:**
#
# 1. *sql_migration/sql_to_mongo_migration.ipynb*

# + [markdown] cell_id="00001-ca48ee16-05aa-4880-a487-867808b32ae0" deepnote_cell_type="markdown"
# ## Introduction
# Except for very recent plays, a player's pp does not reflect their skill at the time a score was set. Therefore, the amount of useable data would increase significantly if an accurate estimate of the user's pp at the time of play can be calculated

# + cell_id="00002-687100a5-1adb-4d6b-a270-31a05cb14ef8" deepnote_cell_type="code"
import sys
sys.path.append('../..')
from pymongo import UpdateOne
from tqdm import tqdm
import numpy as np
from exploration.config import mongo_inst
from mlpp.data_collection.pp_calculation import fast_pp_hist

# + cell_id="00003-130d03aa-cfc7-4a1c-babb-0b95880355e3" deepnote_cell_type="code"
osu_db = mongo_inst['val_random_db']

# + cell_id="00004-4e04efa2-135b-4371-bfc5-c3f3ca17d060" deepnote_cell_type="code"
user_ids = list(map(lambda c: c['_id'], osu_db['osu_user_stats'].find({}, {})))

# + cell_id="00005-e96d072e-7122-40d3-af5d-804e1e4c2071" deepnote_cell_type="code"
cnt_bonus = []
curr = 1
for i in range(100000):
    curr *= .9994
    cnt_bonus.append(416.6667 * (1 - curr))
cnt_bonus = np.array(cnt_bonus)

# + [markdown] cell_id="00006-cd1fe097-2a65-41d4-911f-3ed61cb97cd5" deepnote_cell_type="markdown"
# ## Estimating scores for each Player
# fast_pp_history enables fast calculation of the player's estimated pp for a date-sorted list of scores. Score estimation calculations grouped by player.

# + cell_id="00007-b582f0d3-570f-4aff-b7df-0c0d7ecb2f3d" deepnote_cell_type="code"
for user_id in tqdm(user_ids):
    scores = list(osu_db['osu_scores_high'].find({'user_id': user_id}, {'pp': 1, 'date': 1}).sort('date'))
    score_hist = list(map(lambda x: x['pp'] if x['pp'] else 0, scores))
    pp_hist = np.array(fast_pp_hist(score_hist))
    bonus_pp_hist = pp_hist + cnt_bonus[:len(pp_hist)]

    score_updates = []
    for i in range(len(scores)):
        query = {'_id': scores[i]['_id']}
        update = {
            '$set': {
                'mlpp.est_user_raw_pp': pp_hist[i],
                'mlpp.est_user_pp': bonus_pp_hist[i]
            }
        }
        score_updates.append(UpdateOne(query, update))
    
    if len(score_updates) > 0:
        osu_db['osu_scores_high'].bulk_write(score_updates)

        osu_db['osu_user_stats'].update_one({'_id': user_id}, {
            '$set': {
                'mlpp': {
                    'est_current_raw_pp': pp_hist[-1],
                    'est_current_pp': bonus_pp_hist[-1]
                }
    if len(score_updates) > 0:
        osu_random_db['osu_scores_high'].bulk_write(score_updates)

    osu_random_db['osu_user_stats'].update_one({'_id': user_id}, {
        '$set': {
            'mlpp': {
                'est_current_raw_pp': pp_hist[-1],
                'est_current_pp': bonus_pp_hist[-1]
            }
        }
    })
    else:
        osu_db['osu_user_stats'].remove({'_id': user_id})

# + [markdown] created_in_deepnote_cell=true deepnote_cell_type="markdown" tags=[]
# <a style='text-decoration:none;line-height:16px;display:flex;color:#5B5B62;padding:10px;justify-content:end;' href='https://deepnote.com?utm_source=created-in-deepnote-cell&projectId=f93d0822-db5a-47ef-9a78-57b8adfbeb20' target="_blank">
# <img style='display:inline;max-height:16px;margin:0px;margin-right:7.5px;' src='data:image/svg+xml;base64,PD94bWwgdmV<KEY> > </img>
# Created in <span style='font-weight:600;margin-left:4px;'>Deepnote</span></a>

StarcoderdataJupyterScriptsDedupFiltered