{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "from suno_utils.utils.text import (    \n",
    "    write_jsonl,\n",
    "    read_jsonl,\n",
    "    write_json,\n",
    "    read_json,\n",
    "    normalize_whitespace,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alignment_metas = read_jsonl(\"/home/christian/code/christian/metadata/genius_hq_alignments_v6.jsonl\", progress=True)\n",
    "metas = read_jsonl(\"/home/christian/code/christian/metadata/genius_hq_metas.jsonl\", progress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "metas_map = {}\n",
    "for meta in tqdm(metas):\n",
    "    metas_map[meta[\"id\"]] = meta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# filter metas to only include songs with alignments\n",
    "filtered_metas = []\n",
    "for meta in tqdm(metas):\n",
    "    if meta[\"id\"] in alignment_metas:\n",
    "        filtered_metas.append(meta)\n",
    "\n",
    "print(len(filtered_metas), len(metas))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first find 100 most common tags\n",
    "tag_counts = {}\n",
    "for alignment_meta in tqdm(alignment_metas):\n",
    "    meta_id, _ = alignment_meta\n",
    "    meta = metas_map[meta_id]\n",
    "    tags = meta[\"tags_text\"]\n",
    "    for tag in tags:\n",
    "        tag_counts[tag] = tag_counts.get(tag, 0) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sort tags by count\n",
    "sorted_tags = sorted(tag_counts.items(), key=lambda x: x[1], reverse=True)\n",
    "top_tags = [tag for tag, count in sorted_tags[:100]]\n",
    "print(top_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# now we want to sample 100k songs trying to balance the top 100 tags\n",
    "tag_counts = {tag: 0 for tag in top_tags}\n",
    "sampled_metas = []\n",
    "for alignment_meta in tqdm(alignment_metas):\n",
    "    meta_id, _ = alignment_meta\n",
    "    meta = metas_map[meta_id]\n",
    "    tags = meta[\"tags_text\"]\n",
    "    for tag in tags:\n",
    "        if tag in top_tags:\n",
    "            if tag_counts[tag] < 1000:\n",
    "                tag_counts[tag] += 1\n",
    "                sampled_metas.append(meta)\n",
    "                break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# validate that we have a balanced sample\n",
    "sampled_tag_counts = {tag: 0 for tag in top_tags}\n",
    "for meta in sampled_metas:\n",
    "    tags = meta[\"tags_text\"]\n",
    "    for tag in tags:\n",
    "        if tag in top_tags:\n",
    "            sampled_tag_counts[tag] += 1\n",
    "\n",
    "print(sampled_tag_counts)\n",
    "print(len(sampled_metas))\n",
    "\n",
    "# make a plot of the tag counts\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "tags = list(sampled_tag_counts.keys())\n",
    "counts = list(sampled_tag_counts.values())\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.barh(tags, counts)"
   ]
  },
  {
   "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
}
