{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check Synthetic Samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chekc Using $\\ell_1$ Norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def evaluate_sample(run_name, original_data, mean, std, tol=1e-3):\n",
    "    samples = np.load(f\"results/samples/{run_name}.npy\")\n",
    "    # print(f\"Shape: {samples.shape}\")\n",
    "    count = 0\n",
    "    for i in range(samples.shape[0]):\n",
    "        synthetic_sample = samples[i] * std + mean\n",
    "        for j in range(original_data.shape[0]):\n",
    "            if np.abs(synthetic_sample - original_data[j]).sum() < tol:\n",
    "                count += 1\n",
    "                break\n",
    "    \n",
    "    return count / samples.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tolerance: 1, similarity: 0.0\n",
      "Tolerance: 10, similarity: 0.001\n",
      "Tolerance: 100, similarity: 0.263\n",
      "Tolerance: 1000, similarity: 0.95\n",
      "Tolerance: 2000, similarity: 0.9925\n",
      "Tolerance: 3000, similarity: 0.9985\n",
      "Tolerance: 4000, similarity: 0.999\n",
      "Tolerance: 5000, similarity: 1.0\n",
      "Tolerance: 10000, similarity: 1.0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "original_data = torch.load(\"data/hirid_heartrate_256_400.pt\").squeeze().numpy()\n",
    "for tol in [1, 10, 100, 1000, 2000, 3000, 4000, 5000, 10000]:\n",
    "    print(f\"Tolerance: {tol}, similarity: {evaluate_sample('ddpm_heartrate_1689398471_samples', original_data, mean = 83.69308471679688, std = 18.421707153320312, tol=tol)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tolerance: 1, similarity: 0.0\n",
      "Tolerance: 10, similarity: 0.006\n",
      "Tolerance: 100, similarity: 0.7055\n",
      "Tolerance: 1000, similarity: 0.988\n",
      "Tolerance: 2000, similarity: 0.9965\n",
      "Tolerance: 3000, similarity: 0.999\n",
      "Tolerance: 4000, similarity: 0.9995\n",
      "Tolerance: 5000, similarity: 1.0\n",
      "Tolerance: 10000, similarity: 1.0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "original_data = torch.load(\"data/hirid_heartrate_256_400.pt\").squeeze().numpy()\n",
    "for tol in [1, 10, 100, 1000, 2000, 3000, 4000, 5000, 10000]:\n",
    "    print(f\"Tolerance: {tol}, similarity: {evaluate_sample('ddpm_heartrate_1689398478_samples', original_data, mean = 83.69308471679688, std = 18.421707153320312, tol=tol)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tolerance: 1, similarity: 0.0\n",
      "Tolerance: 10, similarity: 0.0135\n",
      "Tolerance: 100, similarity: 0.8695\n",
      "Tolerance: 1000, similarity: 0.9945\n",
      "Tolerance: 2000, similarity: 0.998\n",
      "Tolerance: 3000, similarity: 0.999\n",
      "Tolerance: 4000, similarity: 0.9995\n",
      "Tolerance: 5000, similarity: 1.0\n",
      "Tolerance: 10000, similarity: 1.0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "original_data = torch.load(\"data/hirid_heartrate_256_400.pt\").squeeze().numpy()\n",
    "for tol in [1, 10, 100, 1000, 2000, 3000, 4000, 5000, 10000]:\n",
    "    print(f\"Tolerance: {tol}, similarity: {evaluate_sample('ddpm_heartrate_1689398481_samples', original_data, mean = 83.69308471679688, std = 18.421707153320312, tol=tol)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tolerance: 1, similarity: 0.0\n",
      "Tolerance: 10, similarity: 0.02\n",
      "Tolerance: 100, similarity: 0.9565\n",
      "Tolerance: 1000, similarity: 0.9985\n",
      "Tolerance: 2000, similarity: 1.0\n",
      "Tolerance: 3000, similarity: 1.0\n",
      "Tolerance: 4000, similarity: 1.0\n",
      "Tolerance: 5000, similarity: 1.0\n",
      "Tolerance: 10000, similarity: 1.0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "original_data = torch.load(\"data/hirid_heartrate_256_400.pt\").squeeze().numpy()\n",
    "for tol in [1, 10, 100, 1000, 2000, 3000, 4000, 5000, 10000]:\n",
    "    print(f\"Tolerance: {tol}, similarity: {evaluate_sample('ddpm_heartrate_1689398483_samples', original_data, mean = 83.69308471679688, std = 18.421707153320312, tol=tol)}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gen",
   "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.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
