{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp datasets.lastfm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LastFM dataset\n",
    "> LastFM 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",
    "import os\n",
    "import os.path as osp\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Timedelta\n",
    "\n",
    "from recohut.datasets.bases.common import SessionDatasetv3\n",
    "from recohut.utils.common_utils import download_url, extract_tar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class LastfmDataset(SessionDatasetv3):\n",
    "    url = 'http://mtg.upf.edu/static/datasets/last.fm/lastfm-dataset-1K.tar.gz'\n",
    "\n",
    "    def __init__(self, root, interval=Timedelta(hours=8), n=40000):\n",
    "        use_cols = [0, 1, 2]\n",
    "        super().__init__(root, use_cols, interval, n)\n",
    "\n",
    "    @property\n",
    "    def raw_file_names(self) -> str:\n",
    "        return 'userid-timestamp-artid-artname-traid-traname.tsv'\n",
    "\n",
    "    def download(self):\n",
    "        path = download_url(self.url, self.raw_dir)\n",
    "        extract_tar(path, self.raw_dir)\n",
    "        from shutil import move, rmtree\n",
    "        move(osp.join(self.raw_dir, 'lastfm-dataset-1K', 'userid-timestamp-artid-artname-traid-traname.tsv'),\n",
    "                osp.join(self.raw_dir, 'userid-timestamp-artid-artname-traid-traname.tsv'))\n",
    "        rmtree(osp.join(self.raw_dir, 'lastfm-dataset-1K'))\n",
    "        os.unlink(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start preprocessing\n",
      "No. of Clicks: 3835706\n",
      "No. of Items: 38615\n",
      "saving dataset\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "ds = LastfmDataset(root='/content/lastfmdata')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[01;34m/content/lastfmdata\u001b[00m\n",
      "├── [ 18M]  \u001b[01;34mprocessed\u001b[00m\n",
      "│   ├── [   5]  all_train_seq.txt\n",
      "│   ├── [3.5M]  test.txt\n",
      "│   └── [ 14M]  train.txt\n",
      "└── [2.4G]  \u001b[01;34mraw\u001b[00m\n",
      "    └── [2.4G]  userid-timestamp-artid-artname-traid-traname.tsv\n",
      "\n",
      " 2.4G used in 2 directories, 4 files\n"
     ]
    }
   ],
   "source": [
    "!tree --du -h -C /content/lastfmdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sparsh A.\n",
      "\n",
      "Last updated: 2021-12-23 08:01:27\n",
      "\n",
      "recohut: 0.0.6\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",
      "IPython: 5.5.0\n",
      "numpy  : 1.19.5\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
}
