{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1980b3fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xplain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bf05a546",
   "metadata": {},
   "outputs": [],
   "source": [
    "startup = 'Seasons'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "33be95d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Authentication successful!!!\n",
      "xplain session from Seasons.xstartup initiated\n"
     ]
    }
   ],
   "source": [
    "x=xplain.Xsession()\n",
    "x.startup(startup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b9689fdb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Authentication successful!!!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\anaconda\\lib\\site-packages\\urllib3\\connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'nba.xplain-data.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xplain session from Seasons.xstartup initiated\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\anaconda\\lib\\site-packages\\urllib3\\connectionpool.py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'nba.xplain-data.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "###alternative login method (type in user and password)\n",
    "# s = xplain.Xsession(url=\"https://nba.xplain-data.com\", user=\"me\",\n",
    "# password=\"secret\")\n",
    "\n",
    "# s.startup(startup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3a580d66",
   "metadata": {},
   "outputs": [],
   "source": [
    "def player_ranks(xsession,metric):\n",
    "    \n",
    "    rank_name = str(metric) + ' Rank'\n",
    "    ###build rank dimension\n",
    "    try:\n",
    "        xsession.run({\n",
    "        \"selections\": [],\n",
    "        \"dimensionClass\": \"RankDimension\",\n",
    "        \"method\": \"buildSortedSequence\",\n",
    "        \"rankDimensionName\": rank_name,\n",
    "        \"sortDimension\": {\n",
    "        \"dimension\": metric,\n",
    "        \"object\": \"Player Statistics\"\n",
    "        },\n",
    "        \"type\": \"INT\",\n",
    "        \"reverse\": True,\n",
    "        \"baseObject\": \"Team Season\"\n",
    "        })\n",
    "        \n",
    "    finally:\n",
    "    \n",
    "        xsession.run({\n",
    "        \"method\": \"setPropagateSelectionsUpward\",\n",
    "        \"propagateSelectionsUpward\": True\n",
    "        })\n",
    "    \n",
    "    \n",
    "        ###Get player df\n",
    "        query_config = xplain.Query_config()\n",
    "    \n",
    "        query_config.add_aggregation(object_name=\"Player Statistics\",dimension_name=\"Player Name\", type=\"COUNT\")\n",
    "        query_config.add_groupby(object_name=\"Player Statistics\",dimension_name=\"Player Name\",attribute_name=\"Player Name\",\n",
    "                        groupby_level_number=2)\n",
    "    \n",
    "        query_config.add_selection(object_name=\"Player Statistics\",\n",
    "                           dimension_name=rank_name,\n",
    "                           attribute_name=rank_name,\n",
    "                           selected_states=[\"]0,1]\"])\n",
    "    \n",
    "        df = xsession.execute_query(query_config)\n",
    "        df_ = df.sort_values('# Player Statistics',ascending=False)\n",
    "    \n",
    "        return(df_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2bfa4af7",
   "metadata": {},
   "outputs": [],
   "source": [
    "metric = '#Assists'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2bfd6421",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Player Name</th>\n",
       "      <th># Player Statistics</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2115</th>\n",
       "      <td>LeBron James</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1756</th>\n",
       "      <td>John Stockton</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3278</th>\n",
       "      <td>Tony Parker</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2276</th>\n",
       "      <td>Mark Jackson</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1387</th>\n",
       "      <td>Isiah Thomas</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1243</th>\n",
       "      <td>Glenn Robinson III</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1244</th>\n",
       "      <td>God Shammgod</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1245</th>\n",
       "      <td>Goga Bitadze</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1247</th>\n",
       "      <td>Gordan Giricek</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3526</th>\n",
       "      <td>null</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3527 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Player Name  # Player Statistics\n",
       "2115        LeBron James                   18\n",
       "1756       John Stockton                   18\n",
       "3278         Tony Parker                   15\n",
       "2276        Mark Jackson                   13\n",
       "1387        Isiah Thomas                   13\n",
       "...                  ...                  ...\n",
       "1243  Glenn Robinson III                    0\n",
       "1244        God Shammgod                    0\n",
       "1245        Goga Bitadze                    0\n",
       "1247      Gordan Giricek                    0\n",
       "3526                null                    0\n",
       "\n",
       "[3527 rows x 2 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "###How many times has a player lead their team in the metric above?\n",
    "player_ranks(x,metric)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
