{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "54da5161",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import medmnist\n",
    "from medmnist import INFO, Evaluator\n",
    "from sklearn.utils import shuffle\n",
    "import seaborn as sns\n",
    "from models.classifier import Encoder, Regressor\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.autograd import Variable\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import Dataset\n",
    "import torch\n",
    "sns.set_style(\"whitegrid\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "59b53d56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(34581, 28, 28) (34581, 1)\n",
      "(17778, 28, 28) (17778, 1)\n",
      "(6491, 28, 28) (6491, 1)\n",
      "(58850, 28, 28) (58850, 1)\n",
      "-0.46570450924635015\n",
      "0.5342954907536499\n",
      "(11,)\n",
      "(11,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'OrganAmnist Distribution')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 10568 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = np.load('organamnist.npz')\n",
    "X = data['train_images']\n",
    "y = data['train_labels']\n",
    "print(X.shape, y.shape)\n",
    "X_2 = data['test_images']\n",
    "y_2 = data['test_labels']\n",
    "print(X_2.shape, y_2.shape)\n",
    "X_3 = data['val_images']\n",
    "y_3 = data['val_labels']\n",
    "print(X_3.shape, y_3.shape)\n",
    "\n",
    "X = np.append(X, X_2, axis=0)\n",
    "y = np.append(y, y_2, axis=0)\n",
    "X = np.append(X, X_3, axis=0)\n",
    "y = np.append(y, y_3, axis=0)\n",
    "print(X.shape, y.shape)\n",
    "\n",
    "X, y = shuffle(X, y)\n",
    "# Make X zero mean and unit std\n",
    "X = X/np.max(X)\n",
    "X = X - np.mean(X)\n",
    "print(np.min(X))\n",
    "print(np.max(X))\n",
    "\n",
    "vals = np.unique(y)\n",
    "totals = np.array([np.sum(np.where(y==i, 1, 0)) for i in range(0, 11)])\n",
    "print(vals.shape)\n",
    "print(totals.shape)\n",
    "plt.bar(vals, totals)\n",
    "plt.ylabel('Count')\n",
    "plt.xlabel('Label')\n",
    "plt.title('OrganAmnist Distribution')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8bc3c4fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_1, y_1 = X[:8400], y[:8400]\n",
    "X_2, y_2 = X[8400:8400*2], y[8400:8400*2]\n",
    "X_3, y_3 = X[8400*2:8400*3], y[8400*2:8400*3]\n",
    "X_4, y_4 = X[8400*3:8400*4], y[8400*3:8400*4]\n",
    "X_5, y_5 = X[8400*4:8400*5], y[8400*4:8400*5]\n",
    "X_6, y_6 = X[8400*5:8400*6], y[8400*5:8400*6]\n",
    "X_7, y_7 = X[8400*6:8400*7], y[8400*6:8400*7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f9386fd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8400, 28, 28)\n",
      "(6000, 28, 28) (2400, 28, 28)\n",
      "(6000, 1) (2400, 1)\n"
     ]
    }
   ],
   "source": [
    "# Domain 1 \n",
    "# Split into train and test sets \n",
    "print(X_1.shape)\n",
    "X_1_train, y_1_train = X_1[:6000], y_1[:6000]\n",
    "X_1_test, y_1_test = X_1[6000:], y_1[6000:]\n",
    "print(X_1_train.shape, X_1_test.shape)\n",
    "print(y_1_train.shape, y_1_test.shape)\n",
    "\n",
    "np.save('X_1_train', X_1_train)\n",
    "np.save('X_1_test', X_1_test)\n",
    "np.save('y_1_train', y_1_train)\n",
    "np.save('y_1_test', y_1_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0df41a9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8400, 28, 28)\n",
      "0.5342954907536499\n",
      "-0.46570450924635015\n",
      "0.4\n",
      "-0.3486494026662671\n",
      "(6000, 28, 28) (2400, 28, 28)\n",
      "(6000, 1) (6000, 1)\n"
     ]
    }
   ],
   "source": [
    "# Domain 2 - Decreased contrast\n",
    "# Split into train and test sets \n",
    "print(X_2.shape)\n",
    "print(X_2.max())\n",
    "print(X_2.min())\n",
    "X_2_shift = X_2/X_2.max() * 0.40\n",
    "\n",
    "print(X_2_shift.max())\n",
    "print(X_2_shift.min())\n",
    "\n",
    "X_2_train, y_2_train = X_2_shift[:6000], y_2[:6000]\n",
    "X_2_test, y_2_test= X_2_shift[6000:], y_2[6000:]\n",
    "print(X_2_train.shape, X_2_test.shape)\n",
    "print(y_2_train.shape, y_2_train.shape)\n",
    "\n",
    "np.save('X_2_train', X_2_train)\n",
    "np.save('X_2_test', X_2_test)\n",
    "np.save('y_2_train', y_2_train)\n",
    "np.save('y_2_test', y_2_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f5db4b8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8400, 28, 28)\n",
      "0.5342954907536499\n",
      "-0.46570450924635015\n",
      "1.1\n",
      "-0.9587858573322345\n",
      "(6000, 28, 28) (2400, 28, 28)\n",
      "(6000, 1) (2400, 1)\n"
     ]
    }
   ],
   "source": [
    "# Domain 3 - Increase contrast\n",
    "# Split into train and test sets \n",
    "print(X_3.shape)\n",
    "print(X_3.max())\n",
    "print(X_3.min())\n",
    "X_3_shift = X_3/X_3.max() * 1.1\n",
    "\n",
    "print(X_3_shift.max())\n",
    "print(X_3_shift.min())\n",
    "\n",
    "X_3_train, y_3_train = X_3_shift[:6000], y_3[:6000]\n",
    "X_3_test, y_3_test = X_3_shift[6000:], y_3[6000:]\n",
    "print(X_3_train.shape, X_3_test.shape)\n",
    "print(y_3_train.shape, y_3_test.shape)\n",
    "\n",
    "np.save('X_3_train', X_3_train)\n",
    "np.save('X_3_test', X_3_test)\n",
    "np.save('y_3_train', y_3_train)\n",
    "np.save('y_3_test', y_3_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "05c5f98a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6000, 28, 28) (2400, 28, 28)\n",
      "(6000, 1) (2400, 1)\n"
     ]
    }
   ],
   "source": [
    "# Domain 4 - noise gaussian blur\n",
    "from scipy.ndimage import gaussian_filter\n",
    "\n",
    "# Split into train and test sets \n",
    "X_4_blur = np.zeros_like(X_4)\n",
    "for i in range(0, X_4_blur.shape[0]):\n",
    "    sampl = np.random.uniform(low=0.25, high=0.75, size=(1,))\n",
    "    result = gaussian_filter(X_4[i], sigma=sampl[0])\n",
    "    X_4_blur[i] = result\n",
    "\n",
    "X_4_train, y_4_train = X_4_blur[:6000], y_4[:6000]\n",
    "X_4_test, y_4_test = X_4_blur[6000:], y_4[6000:]\n",
    "print(X_4_train.shape, X_4_test.shape)\n",
    "print(y_4_train.shape, y_4_test.shape)\n",
    "\n",
    "np.save('X_4_train', X_4_train)\n",
    "np.save('X_4_test', X_4_test)\n",
    "np.save('y_4_train', y_4_train)\n",
    "np.save('y_4_test', y_4_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b3747673",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8400, 28, 28)\n",
      "(6000, 28, 28) (2400, 28, 28)\n"
     ]
    }
   ],
   "source": [
    "# Domain 5\n",
    "# Split into train and test sets \n",
    "print(X_5.shape)\n",
    "noise = np.random.normal(scale=0.1, size=(X_5.shape))\n",
    "X_5_noise = X_5 + noise\n",
    "\n",
    "X_5_train, y_5_train = X_5_noise[:6000], y_5[:6000]\n",
    "X_5_test, y_5_test = X_5_noise[6000:], y_5[6000:]\n",
    "print(X_5_train.shape, X_5_test.shape)\n",
    "\n",
    "np.save('X_5_train', X_5_train)\n",
    "np.save('X_5_test', X_5_test)\n",
    "np.save('y_5_train', y_5_train)\n",
    "np.save('y_5_test', y_5_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60373a8a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d0c598f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65796a13",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
