{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp datasets.diginetica"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Diginetica dataset\n",
    "> Diginetica 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 numpy as np\n",
    "\n",
    "from recohut.datasets.bases import common as base\n",
    "from recohut.utils.common_utils import download_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class DigineticaDataset(base.SessionDatasetv2):\n",
    "    url = 'https://github.com/RecoHut-Datasets/diginetica/raw/main/train-item-views.csv'\n",
    "\n",
    "    def __init__(self, root, column_names={'SESSION_ID':'sessionId',\n",
    "                                        'ITEM_ID': 'itemId',\n",
    "                                        'TIMEFRAME': 'timeframe',\n",
    "                                        'EVENT_DATE': 'eventdate'}):\n",
    "        super().__init__(root, column_names)\n",
    "\n",
    "    @property\n",
    "    def raw_file_names(self) -> str:\n",
    "        return 'train-item-views.csv'\n",
    "\n",
    "    def download(self):\n",
    "        path = download_url(self.url, self.raw_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading https://github.com/RecoHut-Datasets/diginetica/raw/main/train-item-views.csv\n",
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-- Reading data\n",
      "Splitting date 1464134400.0\n",
      "186670\n",
      "15979\n",
      "[('4737', 1451606400.0), ('4741', 1451606400.0), ('4742', 1451606400.0)]\n",
      "[('289', 1464220800.0), ('290', 1464220800.0), ('302', 1464220800.0)]\n",
      "-- Splitting train set and test set\n",
      "43098\n",
      "719470\n",
      "60858\n",
      "[[1], [3, 4], [3]] [1451606400.0, 1451606400.0, 1451606400.0] [2, 5, 4]\n",
      "[[21553, 20071, 8762, 21566, 6381], [21553, 20071, 8762, 21566], [21553, 20071, 8762]] [1464220800.0, 1464220800.0, 1464220800.0] [21566, 6381, 21566]\n",
      "avg length:  4.850942344040704\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n"
     ]
    }
   ],
   "source": [
    "ds = DigineticaDataset(root='/content/digidata')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class DigineticaDatasetv2(base.SessionGraphDataset):\n",
    "    train_url = \"https://github.com/RecoHut-Datasets/diginetica/raw/v2/train.txt\"\n",
    "    test_url = \"https://github.com/RecoHut-Datasets/diginetica/raw/v2/test.txt\"\n",
    "    all_train_seq_url = \"https://github.com/RecoHut-Datasets/diginetica/raw/v2/all_train_seq.txt\"\n",
    "\n",
    "    def __init__(self, root, shuffle=False, n_node=43097, is_train=True):\n",
    "        self.n_node = n_node\n",
    "        self.shuffle = shuffle\n",
    "        self.is_train = is_train\n",
    "        super().__init__(root, shuffle, n_node)\n",
    "\n",
    "    @property\n",
    "    def raw_file_names(self) -> str:\n",
    "        if self.is_train:\n",
    "            return ['train.txt', 'all_train_seq.txt']\n",
    "        return ['test.txt', 'all_train_seq.txt']\n",
    "\n",
    "    def download(self):\n",
    "        download_url(self.all_train_seq_url, self.raw_dir)\n",
    "        if self.is_train:\n",
    "            download_url(self.train_url, self.raw_dir)\n",
    "        else:\n",
    "            download_url(self.test_url, self.raw_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = '/content/diginetica'\n",
    "\n",
    "train_data = DigineticaDataset(root=root, shuffle=True, is_train=True)\n",
    "test_data = DigineticaDataset(root=root, shuffle=False, is_train=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sparsh A.\n",
      "\n",
      "Last updated: 2021-12-30 05:00:21\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",
      "recohut: 0.0.8\n",
      "IPython: 5.5.0\n",
      "numpy  : 1.19.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
}
