{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp datasets.music30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Music30 dataset\n",
    "> Music30 dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "from nbdev.showdoc import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "from typing import List, Optional, Callable, Union, Any, Tuple\n",
    "\n",
    "import os\n",
    "import os.path as osp\n",
    "from collections.abc import Sequence\n",
    "import sys\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import timezone, datetime, timedelta\n",
    "import time\n",
    "\n",
    "from recohut.datasets.base import SessionDataset\n",
    "from recohut.utils.common_utils import download_url, extract_zip, makedirs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class Music30Dataset(SessionDataset):\n",
    "    url = 'https://github.com/RecoHut-Datasets/30music/raw/v1/30music.zip'\n",
    "    \n",
    "    def __init__(self, root, process_method, min_session_length=2, min_item_support=2,\n",
    "                 num_slices=5, days_offset=0, days_shift=95, days_train=90, days_test=5):\n",
    "        min_date = session_length = None\n",
    "        super().__init__(root, process_method, min_date, session_length, \n",
    "                         min_session_length, min_item_support, num_slices, days_offset, \n",
    "                         days_shift, days_train, days_test)\n",
    "    \n",
    "    @property\n",
    "    def raw_file_names(self) -> str:\n",
    "        return '30music-200ks.csv'\n",
    "\n",
    "    @property\n",
    "    def processed_file_names(self) -> str:\n",
    "        return 'dataset.pkl'\n",
    "\n",
    "    def download(self):\n",
    "        path = download_url(self.url, self.raw_dir)\n",
    "        extract_zip(path, self.raw_dir)\n",
    "        from shutil import move, rmtree\n",
    "        move(osp.join(self.raw_dir, '30music', 'raw', '30music-200ks.csv'),\n",
    "             osp.join(self.raw_dir, '30music-200ks.csv'))\n",
    "        rmtree(osp.join(self.raw_dir, '30music'))\n",
    "        os.unlink(path)\n",
    "\n",
    "    def load(self):\n",
    "        #load csv\n",
    "        data = pd.read_csv(osp.join(self.raw_dir,self.raw_file_names), sep='\\t')\n",
    "        data.sort_values(by=['SessionId','Time'], inplace=True)\n",
    "        \n",
    "        #output\n",
    "        data_start = datetime.fromtimestamp(data.Time.min(), timezone.utc)\n",
    "        data_end = datetime.fromtimestamp(data.Time.max(), timezone.utc)\n",
    "        \n",
    "        print('Loaded data set\\n\\tEvents: {}\\n\\tSessions: {}\\n\\tItems: {}\\n\\tSpan: {} / {}\\n\\n'.\n",
    "            format(len(data), data.SessionId.nunique(), data.ItemId.nunique(), data_start.date().isoformat(), data_end.date().isoformat()))\n",
    "    \n",
    "        self.data = data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading https://github.com/RecoHut-Datasets/30music/raw/v1/30music.zip\n",
      "Extracting /content/music30/raw/30music.zip\n",
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded data set\n",
      "\tEvents: 3707857\n",
      "\tSessions: 200000\n",
      "\tItems: 1203432\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Filtered data set\n",
      "\tEvents: 2953382\n",
      "\tSessions: 190216\n",
      "\tItems: 452855\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Full train set\n",
      "\tEvents: 2892862\n",
      "\tSessions: 186627\n",
      "\tItems: 450895\n",
      "Test set\n",
      "\tEvents: 54606\n",
      "\tSessions: 3468\n",
      "\tItems: 35100\n",
      "Train set\n",
      "\tEvents: 2847481\n",
      "\tSessions: 183674\n",
      "\tItems: 449290\n",
      "Validation set\n",
      "\tEvents: 41785\n",
      "\tSessions: 2852\n",
      "\tItems: 29293\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "musicdata = Music30Dataset(root='/content/music30', process_method='last')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded data set\n",
      "\tEvents: 3707857\n",
      "\tSessions: 200000\n",
      "\tItems: 1203432\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Filtered data set\n",
      "\tEvents: 2149666\n",
      "\tSessions: 165766\n",
      "\tItems: 139016\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Full train set\n",
      "\tEvents: 2105847\n",
      "\tSessions: 162634\n",
      "\tItems: 138861\n",
      "Test set\n",
      "\tEvents: 41871\n",
      "\tSessions: 3091\n",
      "\tItems: 23508\n",
      "Train set\n",
      "\tEvents: 2073194\n",
      "\tSessions: 160047\n",
      "\tItems: 138755\n",
      "Validation set\n",
      "\tEvents: 31937\n",
      "\tSessions: 2564\n",
      "\tItems: 20210\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "musicdata = Music30Dataset(root='/content/music30', process_method='last')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[01;34m/content/music30\u001b[00m\n",
      "├── [157M]  \u001b[01;34mprocessed\u001b[00m\n",
      "│   ├── [1.6M]  events_test.txt\n",
      "│   ├── [ 78M]  events_train_full.txt\n",
      "│   ├── [ 77M]  events_train_tr.txt\n",
      "│   └── [1.2M]  events_train_valid.txt\n",
      "└── [137M]  \u001b[01;34mraw\u001b[00m\n",
      "    └── [137M]  30music-200ks.csv\n",
      "\n",
      " 295M used in 2 directories, 5 files\n"
     ]
    }
   ],
   "source": [
    "!tree --du -h -C /content/music30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded data set\n",
      "\tEvents: 3707857\n",
      "\tSessions: 200000\n",
      "\tItems: 1203432\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Filtered data set\n",
      "\tEvents: 2149666\n",
      "\tSessions: 165766\n",
      "\tItems: 139016\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Full train set\n",
      "\tEvents: 2073194\n",
      "\tSessions: 160047\n",
      "\tItems: 138755\n",
      "Test set\n",
      "\tEvents: 73532\n",
      "\tSessions: 5652\n",
      "\tItems: 36423\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "!rm /content/music30/processed/*\n",
    "musicdata = Music30Dataset(root='/content/music30', process_method='days_test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[01;34m/content/music30\u001b[00m\n",
      "├── [ 79M]  \u001b[01;34mprocessed\u001b[00m\n",
      "│   ├── [2.7M]  events_test.txt\n",
      "│   └── [ 77M]  events_train_full.txt\n",
      "└── [137M]  \u001b[01;34mraw\u001b[00m\n",
      "    └── [137M]  30music-200ks.csv\n",
      "\n",
      " 217M used in 2 directories, 3 files\n"
     ]
    }
   ],
   "source": [
    "!tree --du -h -C /content/music30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded data set\n",
      "\tEvents: 3707857\n",
      "\tSessions: 200000\n",
      "\tItems: 1203432\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n",
      "Filtered data set\n",
      "\tEvents: 2149666\n",
      "\tSessions: 165766\n",
      "\tItems: 139016\n",
      "\tSpan: 2014-01-20 / 2015-01-20\n",
      "\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "!rm /content/music30/processed/*\n",
    "musicdata = Music30Dataset(root='/content/music30', process_method='slice')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[01;34m/content/music30\u001b[00m\n",
      "├── [4.0K]  \u001b[01;34mprocessed\u001b[00m\n",
      "└── [137M]  \u001b[01;34mraw\u001b[00m\n",
      "    └── [137M]  30music-200ks.csv\n",
      "\n",
      " 137M used in 2 directories, 1 file\n"
     ]
    }
   ],
   "source": [
    "!tree --du -h -C /content/music30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sparsh A.\n",
      "\n",
      "Last updated: 2021-12-22 09:23:49\n",
      "\n",
      "recohut: 0.0.5\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",
      "pandas : 1.1.5\n",
      "IPython: 5.5.0\n",
      "numpy  : 1.19.5\n",
      "sys    : 3.7.12 (default, Sep 10 2021, 00:21:48) \n",
      "[GCC 7.5.0]\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
}
