{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Any\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def map_control_sliders_to_df(df: pd.DataFrame) -> pd.DataFrame:\n",
    "    \"\"\"Extracts 'style_weight' and 'weirdness_constraint' from the 'control_sliders' dict in the 'metadata' column\n",
    "    and adds them as new columns to the DataFrame.\n",
    "\n",
    "    Args:\n",
    "        df (pd.DataFrame): DataFrame with a 'metadata' column containing a 'control_sliders' dict.\n",
    "\n",
    "    Returns:\n",
    "        pd.DataFrame: DataFrame with added 'style_weight' and 'weirdness_constraint' columns.\n",
    "\n",
    "    Raises:\n",
    "        KeyError: If 'control_sliders', 'style_weight', or 'weirdness_constraint' are missing in any row.\n",
    "        TypeError: If the extracted values are not floats.\n",
    "\n",
    "    Example:\n",
    "        >>> import pandas as pd\n",
    "        >>> data = [{'metadata': {'control_sliders': {'style_weight': 0.89, 'weirdness_constraint': 0.8}}}]\n",
    "        >>> df = pd.DataFrame(data)\n",
    "        >>> df = map_control_sliders_to_df(df)\n",
    "        >>> df[['style_weight', 'weirdness_constraint']].iloc[0].tolist()\n",
    "        [0.89, 0.8]\n",
    "    \"\"\"\n",
    "    # Vectorized extraction for performance\n",
    "    sliders = df[\"metadata\"].map(lambda m: m.get(\"control_sliders\", {}))\n",
    "    style_weight = sliders.map(lambda s: s.get(\"style_weight\", None))\n",
    "    weirdness_constraint = sliders.map(lambda s: s.get(\"weirdness_constraint\", None))\n",
    "    audio_weight = sliders.map(lambda s: s.get(\"audio_weight\", None))\n",
    "\n",
    "    df[\"style_weight\"] = style_weight\n",
    "    df[\"weirdness_constraint\"] = weirdness_constraint\n",
    "    df[\"audio_weight\"] = audio_weight\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle(\"/home/tony/Data/Preference/crow_t1/interesting_clips_crow_t1_20250930.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = map_control_sliders_to_df(df)\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert list of pairs to a dataframe\n",
    "# Each row represents one pair with prefixed columns for each item\n",
    "pairs_data = []\n",
    "for pair in pairs:\n",
    "    item1, item2 = pair\n",
    "    row = {}\n",
    "    # Add item1 columns with prefix\n",
    "    for key, value in item1.items():\n",
    "        row[f'item1_{key}'] = value\n",
    "    # Add item2 columns with prefix\n",
    "    for key, value in item2.items():\n",
    "        row[f'item2_{key}'] = value\n",
    "    pairs_data.append(row)\n",
    "\n",
    "df_pairs = pd.DataFrame(pairs_data)\n",
    "print(f\"Pairs dataframe shape: {df_pairs.shape}\")\n",
    "df_pairs.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove rows where all three columns are NaN\n",
    "#df = df.dropna(subset=['style_weight', 'weirdness_constraint', 'audio_weight'], how='all')\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Condense dataframe into list of pairs\n",
    "# Keep specified columns for each pair\n",
    "columns_to_keep = ['pos_preference', 'neg_preference', 'style_weight', 'weirdness_constraint', 'audio_weight', 'metadata']\n",
    "\n",
    "pairs = []\n",
    "for i in range(0, len(df), 2):\n",
    "    if i + 1 < len(df):  # Make sure we have a complete pair\n",
    "        pair = (\n",
    "            df.iloc[i][columns_to_keep].to_dict(),\n",
    "            df.iloc[i + 1][columns_to_keep].to_dict()\n",
    "        )\n",
    "        pairs.append(pair)\n",
    "\n",
    "print(f\"Number of pairs: {len(pairs)}\")\n",
    "print(f\"First pair:\")\n",
    "print(pairs[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pos = df_clean[df_clean['pos_preference'] == 1]\n",
    "df_neg = df_clean[df_clean['neg_preference'] == 1]\n",
    "print(len(df_pos))\n",
    "print(len(df_neg))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_style = df_pos.dropna(subset=['style_weight'])['style_weight']\n",
    "neg_style = df_neg.dropna(subset=['style_weight'])['style_weight']\n",
    "pos_weirdness = df_pos.dropna(subset=['weirdness_constraint'])['weirdness_constraint']\n",
    "neg_weirdness = df_neg.dropna(subset=['weirdness_constraint'])['weirdness_constraint']\n",
    "pos_audio = df_pos.dropna(subset=['audio_weight'])['audio_weight']\n",
    "neg_audio = df_neg.dropna(subset=['audio_weight'])['audio_weight']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_style.hist(bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "neg_style.hist(bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_weirdness.hist(bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "neg_weirdness.hist(bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_audio.hist(bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "neg_audio.hist(bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_clean",
   "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.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
