{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4fdb98fc-65bb-467e-be0c-168fee9b0fca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda:0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<contextlib.ExitStack at 0x249d580ff50>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "import io\n",
    "import cv2\n",
    "import copy\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "import torch.nn as nn\n",
    "import torch\n",
    "import torchvision\n",
    "from torch.utils.data import Dataset\n",
    "from torch import default_generator, randperm\n",
    "from PIL import Image\n",
    "#from torch._utils import _accumulate\n",
    "import csv\n",
    "from torch.utils.data.dataset import Subset\n",
    "from scipy import ndimage\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(device)\n",
    "batch_size = 16\n",
    "momentum=0.9\n",
    "lr = 1e-3\n",
    "import random\n",
    "sub_sample = 0.5\n",
    "import matplotlib\n",
    "import gc\n",
    "matplotlib.use('Agg')\n",
    "import matplotlib as mpl\n",
    "mpl.rcParams['agg.path.chunksize'] = 256*256\n",
    "plt.ioff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9267fbe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sig2pic_with_spec(path_to_data, filename, figsize=(16,16), dpi=16, resize = None):\n",
    "    try:\n",
    "        array = np.load(path_to_data+filename)\n",
    "\n",
    "        if resize != None:\n",
    "            resized_real = cv2.resize(array[0], resize)\n",
    "            resized_mag = cv2.resize(array[1], resize)\n",
    "            resized_spectr = cv2.resize(array[2], resize)\n",
    "            img = np.asarray(array, dtype=np.float32)\n",
    "            return img\n",
    "        \n",
    "        img = np.asarray(array, dtype=np.float32)\n",
    "        return img\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(str(e))\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "448da74a-e0ae-44d8-9877-8dd1f257a24f",
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_binaries = 'C:/Users/snytk/Lerning_NN_for_work/datasets/2.4_learning'\n",
    "path_to_pictures = 'C:/Users/snytk/Lerning_NN_for_work/datasets_jpg/2.4_jpg_learning'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ac4945a8-29c4-4da4-945f-08658953e3e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6f226f86-5d72-4573-8af6-750128b70263",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████| 8751/8751 [1:01:54<00:00,  2.36it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dir:  drone  finished!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████| 11202/11202 [1:19:30<00:00,  2.35it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dir:  noise  finished!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "size = (256,256)\n",
    "if not os.path.exists(path_to_pictures):\n",
    "    os.mkdir(path_to_pictures)\n",
    "for subdir in os.listdir(path_to_binaries):\n",
    "    filepath = path_to_binaries + '/' + subdir + '/'\n",
    "    if not os.path.exists(path_to_pictures +'/' + subdir):\n",
    "        os.mkdir(path_to_pictures + '/' + subdir)\n",
    "    files = os.listdir(filepath)\n",
    "    for file in tqdm(files):\n",
    "        savepath_real_png = path_to_pictures +'/' + subdir + '/' + file + '_real' + '.jpg' \n",
    "        savepath_imag_png = path_to_pictures +'/' + subdir + '/' + file + '_imag' + '.jpg' \n",
    "        savepath_spec_png = path_to_pictures +'/' + subdir + '/' + file + '_spec' + '.jpg'\n",
    "        if not os.path.exists(savepath_real_png):\n",
    "            img = sig2pic_with_spec(path_to_data=filepath, filename=file, resize = size)\n",
    "            gc.collect()\n",
    "            \n",
    "            try:\n",
    "                plt.imshow(img[0])\n",
    "                plt.savefig(savepath_real_png)\n",
    "                plt.clf()\n",
    "                plt.cla()\n",
    "                plt.close()\n",
    "                \n",
    "                plt.imshow(img[1])\n",
    "                plt.savefig(savepath_imag_png)\n",
    "                plt.clf()\n",
    "                plt.cla()\n",
    "                plt.close()\n",
    "\n",
    "                plt.imshow(img[2])\n",
    "                plt.savefig(savepath_spec_png)\n",
    "                plt.clf()\n",
    "                plt.cla()\n",
    "                plt.close()\n",
    "                \n",
    "            except Exception:\n",
    "                continue\n",
    "    print('Dir: ', subdir , ' finished!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfbd309d",
   "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.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
