{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import json\n",
    "import polars as pl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filtered_ids_filepath = \"/home/christian/code/christian/metadata/v45_splits/ids_keep_sets_v12.json\"\n",
    "with open(filtered_ids_filepath, \"r\") as f:\n",
    "    filtered_ids = json.load(f)\n",
    "\n",
    "# convert to set\n",
    "filtered_ids_set = set()\n",
    "for dataset_name, ids in filtered_ids.items():\n",
    "    filtered_ids_set.update(ids)\n",
    "\n",
    "print(f\"{len(filtered_ids_set):,}\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_metas = pl.read_ndjson(\"/app/suno/data/auk_v0/metas_v2_tr.jsonl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# collect all ids that have non null overpaint_id \n",
    "overpaint_ids = train_metas.filter(pl.col(\"overpaint_id\").is_not_null())[\"id\"].to_list()\n",
    "print(overpaint_ids)\n",
    "\n",
    "output_filepath = \"/home/christian/code/christian/metadata/v45_splits/ids_keep_sets_v12.json\"\n",
    "with open(output_filepath, \"w\") as f:\n",
    "    json.dump(filtered_ids, f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filtered_metas = train_metas.filter(pl.col(\"id\").is_in(filtered_ids_set))\n",
    "print(f\"{len(filtered_metas):,}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# count the number of metas with tags and text\n",
    "tags_count = filtered_metas[\"tags\"].count()\n",
    "text_count = filtered_metas[\"text\"].count()\n",
    "print(f\"{tags_count:,}\")\n",
    "print(f\"{text_count:,}\")\n",
    "\n",
    "# count how many have at least one tag with more than 25 characters\n",
    "# The correct method is to use list.eval() with length() for string operations in polars expressions\n",
    "\n",
    "tags_count_gt_25 = (\n",
    "    filtered_metas\n",
    "    .filter(\n",
    "        pl.col(\"tags\").list.eval(\n",
    "            pl.element().str.len_chars() > 25\n",
    "        ).list.any()\n",
    "    )\n",
    "    .height\n",
    ")\n",
    "\n",
    "print(f\"{tags_count_gt_25:,}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check polars version\n",
    "print(pl.__version__)\n",
    "\n",
    "# check polars version\n",
    "print(pl.__version__)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx = 500_000\n",
    "youtube_id = filtered_metas[idx][\"id\"][0]\n",
    "tags = filtered_metas[idx][\"tags\"][0]\n",
    "text = filtered_metas[idx][\"text\"][0]\n",
    "print(youtube_id)\n",
    "print(len(tags))\n",
    "for tag in tags:\n",
    "    print(tag)\n",
    "print(\"\")\n",
    "print(text)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env",
   "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
