{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52cafe51-05b0-4287-bf31-418090f929de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# COMPARE NO-ATR VS ATR (11H05 crosses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "76a37581-2e43-4fec-9886-0f0849b86602",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ethoscopy as etho\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy.stats import ttest_ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5450b41f-4a94-48f1-99c9-9a8a2862165e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing No-ATR Data...\n",
      "Processing ATR Data...\n"
     ]
    }
   ],
   "source": [
    "# 1. LOAD SAVED PICKLE FILES\n",
    "df_no_atr_raw = pd.read_pickle('/home/rdingjin/UASxGal4_2026-04-28/phase6.pkl')\n",
    "df_atr_raw = pd.read_pickle('/home/rdingjin/UASxGal4_2026-05-05/phase7(11H)1.pkl')\n",
    "\n",
    "# 2. HELPER FUNCTION\n",
    "def calculate_delta_sleep(df, base_start, base_end, stim_start, stim_end):\n",
    " \n",
    "    # --- BASELINE ---\n",
    "    base_df = df.t_filter(start_time=base_start, end_time=base_end).copy()\n",
    "    sleep_hours_base = (base_df.groupby('id')['asleep'].sum() * 10) / 3600\n",
    "    base_overall = (sleep_hours_base / ((base_end - base_start) / 24)).reset_index(name='Base_Sleep_24h')\n",
    "    \n",
    "    base_df['zt_hour'] = (base_df['t'] / 3600) % 24\n",
    "    base_df['Phase'] = np.where(base_df['zt_hour'] < 12, 'Day', 'Night')\n",
    "    base_phase = base_df.groupby(['id', 'Phase']).agg(\n",
    "        total_sleep_epochs=('asleep', 'sum'), total_tracking_epochs=('t', 'count')\n",
    "    ).reset_index()\n",
    "    \n",
    "    base_phase['Sleep_Hours'] = (base_phase['total_sleep_epochs'] * 10) / 3600\n",
    "    base_phase['Tracking_Hours'] = (base_phase['total_tracking_epochs'] * 10) / 3600\n",
    "    base_phase['Base_Sleep_12h'] = (base_phase['Sleep_Hours'] / base_phase['Tracking_Hours']) * 12\n",
    "    base_pivot = base_phase.pivot(index='id', columns='Phase', values='Base_Sleep_12h').reset_index()\n",
    "    base_pivot.columns = ['id', 'Base_Day_12h', 'Base_Night_12h']\n",
    "    baseline_stats = pd.merge(base_overall, base_pivot, on='id')\n",
    "    \n",
    "    # --- STIMULUS ---\n",
    "    stim_df = df.t_filter(start_time=stim_start, end_time=stim_end).copy()\n",
    "    sleep_hours_stim = (stim_df.groupby('id')['asleep'].sum() * 10) / 3600\n",
    "    stim_overall = (sleep_hours_stim / ((stim_end - stim_start) / 24)).reset_index(name='Stim_Sleep_24h')\n",
    "    \n",
    "    stim_df['zt_hour'] = (stim_df['t'] / 3600) % 24\n",
    "    stim_df['Phase'] = np.where(stim_df['zt_hour'] < 12, 'Day', 'Night')\n",
    "    stim_phase = stim_df.groupby(['id', 'Phase']).agg(\n",
    "        total_sleep_epochs=('asleep', 'sum'), total_tracking_epochs=('t', 'count')\n",
    "    ).reset_index()\n",
    "    \n",
    "    stim_phase['Sleep_Hours'] = (stim_phase['total_sleep_epochs'] * 10) / 3600\n",
    "    stim_phase['Tracking_Hours'] = (stim_phase['total_tracking_epochs'] * 10) / 3600\n",
    "    stim_phase['Stim_Sleep_12h'] = (stim_phase['Sleep_Hours'] / stim_phase['Tracking_Hours']) * 12\n",
    "    stim_pivot = stim_phase.pivot(index='id', columns='Phase', values='Stim_Sleep_12h').reset_index()\n",
    "    stim_pivot.columns = ['id', 'Stim_Day_12h', 'Stim_Night_12h']\n",
    "    stim_stats = pd.merge(stim_overall, stim_pivot, on='id')\n",
    "    \n",
    "    # --- MERGE & CALCULATE DELTA ---\n",
    "    combined = pd.merge(baseline_stats, stim_stats, on='id')\n",
    "    unique_meta = df.meta['machine_name'][~df.meta.index.duplicated(keep='first')]\n",
    "    combined['Machine'] = combined['id'].map(unique_meta)\n",
    "    combined['Tube'] = combined['id'].apply(lambda x: str(x).split('|')[-1])\n",
    "    combined['Fly_Label'] = combined['Machine'] + \" - Fly \" + combined['Tube']\n",
    "    combined['Delta_24h'] = combined['Stim_Sleep_24h'] - combined['Base_Sleep_24h']\n",
    "    combined['Delta_Day'] = combined['Stim_Day_12h'] - combined['Base_Day_12h']\n",
    "    combined['Delta_Night'] = combined['Stim_Night_12h'] - combined['Base_Night_12h']\n",
    "    \n",
    "    return combined[['Fly_Label', 'Delta_24h', 'Delta_Day', 'Delta_Night']].dropna()\n",
    "\n",
    "# 3. PROCESS GROUPS\n",
    "print(\"Processing No-ATR Data...\")\n",
    "delta_no_atr = calculate_delta_sleep(df_no_atr_raw, base_start=0, base_end=48, stim_start=48, stim_end=72)\n",
    "delta_no_atr['Condition'] = 'No-ATR'\n",
    "\n",
    "print(\"Processing ATR Data...\")\n",
    "delta_atr = calculate_delta_sleep(df_atr_raw, base_start=0, base_end=48, stim_start=48, stim_end=72)\n",
    "delta_atr['Condition'] = 'ATR'\n",
    "\n",
    "master_delta = pd.concat([delta_no_atr, delta_atr], ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "642d6bbc-3674-4c1e-9a41-88928ee407f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. MELT DATA FOR PLOTTING AND STATS\n",
    "melted_delta = master_delta.melt(\n",
    "    id_vars=['Fly_Label', 'Condition'],\n",
    "    value_vars=['Delta_24h', 'Delta_Day', 'Delta_Night'],\n",
    "    var_name='Phase', \n",
    "    value_name='Sleep_Change'\n",
    ")\n",
    "# Clean up phase names\n",
    "melted_delta['Phase'] = melted_delta['Phase'].map({'Delta_24h': '24h Total', 'Delta_Day': 'Day', 'Delta_Night': 'Night'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fb18dd18-a4f4-4f06-a088-e2cf6b5a677c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==================================================\n",
      "--- SCIPY T-TESTS (No-ATR vs ATR Delta) ---\n",
      "==================================================\n",
      "24h Total Phase Comparison:\n",
      "  No-ATR Δ Mean: 1.24 hours\n",
      "  ATR Δ Mean:    -5.42 hours\n",
      "  P-value:       1.3586e-13\n",
      "\n",
      "Day Phase Comparison:\n",
      "  No-ATR Δ Mean: 0.07 hours\n",
      "  ATR Δ Mean:    -2.21 hours\n",
      "  P-value:       2.5060e-07\n",
      "\n",
      "Night Phase Comparison:\n",
      "  No-ATR Δ Mean: 0.22 hours\n",
      "  ATR Δ Mean:    -3.86 hours\n",
      "  P-value:       9.3420e-12\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 5. STATISTICAL COMPARISON \n",
    "print(\"\\n\" + \"=\"*50)\n",
    "print(\"--- SCIPY T-TESTS (No-ATR vs ATR Delta) ---\")\n",
    "print(\"=\"*50)\n",
    "\n",
    "p_values = {}\n",
    "phases = ['24h Total', 'Day', 'Night']\n",
    "\n",
    "for phase in phases:\n",
    "    group_no_atr = melted_delta[(melted_delta['Phase'] == phase) & (melted_delta['Condition'] == 'No-ATR')]['Sleep_Change']\n",
    "    group_atr = melted_delta[(melted_delta['Phase'] == phase) & (melted_delta['Condition'] == 'ATR')]['Sleep_Change']\n",
    "    \n",
    "    # Run Independent T-Test\n",
    "    t_stat, p_val = ttest_ind(group_no_atr, group_atr)\n",
    "    p_values[phase] = p_val\n",
    "    \n",
    "    print(f\"{phase} Phase Comparison:\")\n",
    "    print(f\"  No-ATR Δ Mean: {group_no_atr.mean():.2f} hours\")\n",
    "    print(f\"  ATR Δ Mean:    {group_atr.mean():.2f} hours\")\n",
    "    print(f\"  P-value:       {p_val:.4e}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c11c05b3-a8b5-4e86-86bf-bae60ec24103",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_50430/3429110790.py:14: FutureWarning: \n",
      "\n",
      "Setting a gradient palette using color= is deprecated and will be removed in v0.14.0. Set `palette='dark:black'` for the same effect.\n",
      "\n",
      "  sns.stripplot(\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 59120 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 6. FINAL COMPARISON PLOT\n",
    "plt.figure(figsize=(8, 6))\n",
    "sns.set_theme(style=\"ticks\")\n",
    "\n",
    "# Plot Boxplot\n",
    "ax = sns.boxplot(\n",
    "    data=melted_delta, x='Phase', y='Sleep_Change', hue='Condition', \n",
    "    palette={'No-ATR': '#cccccc', 'ATR': '#f1c232'}, order=phases,\n",
    "    width=0.6, gap=0.2, boxprops={'edgecolor': 'black', 'alpha': 0.7},\n",
    "    medianprops={'color': 'black', 'linewidth': 1.5}, showfliers=False\n",
    ")\n",
    "\n",
    "# Plot internal points\n",
    "sns.stripplot(\n",
    "    data=melted_delta, x='Phase', y='Sleep_Change', hue='Condition', \n",
    "    order=phases, dodge=True,\n",
    "    color='black', alpha=0.5, size=4, jitter=0.05, legend=False\n",
    ")\n",
    "\n",
    "# Add Median Labels\n",
    "medians = melted_delta.groupby(['Phase', 'Condition'], sort=False)['Sleep_Change'].median()\n",
    "offsets = {'No-ATR': -0.4, 'ATR': 0.4}\n",
    "\n",
    "for i, phase in enumerate(phases):\n",
    "    for condition in ['No-ATR', 'ATR']:\n",
    "        val = medians[(phase, condition)]\n",
    "        ax.text(i + offsets[condition], val, f\"{val:.1f}h\", \n",
    "                ha='center', va='bottom', fontsize=9)\n",
    "\n",
    "# Add Significance Brackets\n",
    "y_max = melted_delta['Sleep_Change'].max()\n",
    "y_min = melted_delta['Sleep_Change'].min()\n",
    "y_range = y_max - y_min\n",
    "y_line = y_max + (y_range * 0.05)\n",
    "h = y_range * 0.02\n",
    "\n",
    "for i, phase in enumerate(phases):\n",
    "    p = p_values[phase]\n",
    "    if p < 0.05:\n",
    "        star = '****' if p < 0.0001 else ('***' if p < 0.001 else ('**' if p < 0.01 else '*'))\n",
    "        x1, x2 = i - 0.2, i + 0.2\n",
    "        plt.plot([x1, x1, x2, x2], [y_line, y_line+h, y_line+h, y_line], lw=1.5, c='k')\n",
    "        plt.text((x1+x2)*.5, y_line+h, star, ha='center', va='bottom', fontsize=12)\n",
    "\n",
    "plt.axhline(0, color='black', linestyle='--', linewidth=1.5, alpha=0.7)\n",
    "sns.despine()\n",
    "plt.ylabel('Change in average time spent asleep (Δ hours)', fontsize=12)\n",
    "plt.xlabel('')\n",
    "plt.xlim(-0.5, 2.5) \n",
    "\n",
    "plt.legend(title='', frameon=False, loc='center left', bbox_to_anchor=(1, 0.5))\n",
    "plt.ylim(y_min - (y_range * 0.1), y_max + (y_range * 0.25))\n",
    "plt.tight_layout()\n",
    "\n",
    "plt.savefig(\"ATR_vs_NoATR_Delta.png\", dpi=600, bbox_inches='tight')\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
