{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp datasets.yoochoose"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Yoochoose\n",
    "> Yoochoose dataset.\n",
    "\n",
    "The dataset is session-based and each session contains a sequence of clicks and purchases. Since the Yoochoose dataset is too large, in some cases we only use its the most recent 1/64 fractions of the training sessions, denoted as Yoochoose 1/64.\n",
    "\n",
    "Raw data: [https://www.dropbox.com/sh/n281js5mgsvao6s/AADQbYxSFVPCun5DfwtsSxeda?dl=0](https://www.dropbox.com/sh/n281js5mgsvao6s/AADQbYxSFVPCun5DfwtsSxeda?dl=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "from nbdev.showdoc import *\n",
    "from fastcore.nb_imports import *\n",
    "from fastcore.test import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import datetime\n",
    "import os\n",
    "import os.path as osp\n",
    "\n",
    "from recohut.datasets.bases.common import SessionDataset\n",
    "from recohut.utils.common_utils import extract_zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class YoochooseDataset(SessionDataset):\n",
    "    data_id = '1UEcKC4EfgMVD2n_zBvAyp0vRNyv7ndSF'\n",
    "\n",
    "    def __init__(self,\n",
    "                 root,\n",
    "                 min_session_length: int = 2,\n",
    "                 min_item_support: int = 5,\n",
    "                 eval_sec: int = 86400,\n",
    "                 ):\n",
    "        super().__init__(root, min_session_length, min_item_support, eval_sec)\n",
    "\n",
    "    @property\n",
    "    def raw_file_names(self) -> str:\n",
    "        return 'rsc15-clicks.dat'\n",
    "\n",
    "    @property\n",
    "    def processed_file_names(self) -> str:\n",
    "        return ['yoochoose_train.txt','yoochoose_valid.txt']\n",
    "\n",
    "    def download(self):\n",
    "        from google_drive_downloader import GoogleDriveDownloader as gdd\n",
    "        from shutil import move, rmtree\n",
    "\n",
    "        path = osp.join(self.raw_dir, 'rsc15.zip')\n",
    "        gdd.download_file_from_google_drive(self.data_id, path)\n",
    "        extract_zip(path, self.raw_dir)\n",
    "        move(osp.join(self.raw_dir, 'rsc15', 'raw', self.raw_file_names),\n",
    "             osp.join(self.raw_dir, self.raw_file_names))\n",
    "        rmtree(osp.join(self.raw_dir, 'rsc15'))\n",
    "        os.unlink(path)\n",
    "\n",
    "    def process(self):\n",
    "        df = self.load_ratings_df()\n",
    "        if self.min_session_length is not None:\n",
    "            df = self.remove_short_sessions(df)\n",
    "        if self.min_item_support is not None:\n",
    "            df = self.remove_sparse_items(df)\n",
    "        train, test = self.split_df(df)\n",
    "        train.to_csv(self.processed_paths[0], sep=',', index=False)\n",
    "        test.to_csv(self.processed_paths[1], sep=',', index=False)\n",
    "\n",
    "    def load_ratings_df(self):\n",
    "        df = pd.read_csv(self.raw_paths[0], header=None, usecols=[0, 1, 2],\n",
    "                         dtype={0: np.int32, 1: str, 2: np.int64})\n",
    "        df.columns = ['uid', 'timestamp', 'sid']\n",
    "        df['timestamp'] = df['timestamp'].apply(lambda x: datetime.datetime.strptime(\n",
    "            x, '%Y-%m-%dT%H:%M:%S.%fZ').timestamp())\n",
    "        return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Set has 31637239 Events, 7966257 Sessions, and 37483 Items\n",
      "\n",
      "\n",
      "Validation Set has 71222 Events, 15324 Sessions, and 6751 Items\n",
      "\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "ds = YoochooseDataset(root='/content/yoochoose')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[01;34m/content/yoochoose\u001b[00m\n",
      "├── [995M]  \u001b[01;34mprocessed\u001b[00m\n",
      "│   ├── [993M]  yoochoose_train.txt\n",
      "│   └── [2.3M]  yoochoose_valid.txt\n",
      "└── [1.4G]  \u001b[01;34mraw\u001b[00m\n",
      "    └── [1.4G]  rsc15-clicks.dat\n",
      "\n",
      " 2.4G used in 2 directories, 3 files\n"
     ]
    }
   ],
   "source": [
    "!tree --du -h -C /content/yoochoose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sparsh A.\n",
      "\n",
      "Last updated: 2021-12-31 12:52:36\n",
      "\n",
      "recohut: 0.0.8\n",
      "\n",
      "Compiler    : GCC 7.5.0\n",
      "OS          : Linux\n",
      "Release     : 5.4.144+\n",
      "Machine     : x86_64\n",
      "Processor   : x86_64\n",
      "CPU cores   : 2\n",
      "Architecture: 64bit\n",
      "\n",
      "numpy  : 1.19.5\n",
      "IPython: 5.5.0\n",
      "pandas : 1.1.5\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#hide\n",
    "%reload_ext watermark\n",
    "%watermark -a \"Sparsh A.\" -m -iv -u -t -d -p recohut"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
