{
 "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 0x25775eabcd0>"
      ]
     },
     "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",
    "import torchsig.utils as u\n",
    "import torchsig.transforms.transforms as T\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": 2,
   "id": "4848b066-2e09-4c1c-b8fa-8e3fa84d907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = T.Spectrogram(nperseg=256)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9267fbe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sig2pic_with_spec(path_to_data, filename, specT=None,figsize=(16,16), dpi=16, resize = None):\n",
    "    try:\n",
    "        if specT is None:\n",
    "            specT = T.Spectrogram(nperseg=256)\n",
    "        with open(path_to_data + filename, 'rb') as file:\n",
    "            tmp = np.frombuffer(file.read(), dtype=np.complex64)\n",
    "        signal = tmp\n",
    "        spectr = np.array(specT(signal)['data']['samples'][:,:figsize[0] * dpi])\n",
    "        mag = np.abs(signal)\n",
    "        real = signal.real\n",
    "\n",
    "        fig2 = plt.figure(figsize = figsize)\n",
    "        plt.axes(ylim=(-1, 1))\n",
    "\n",
    "        plt.plot(real, color='black')\n",
    "        plt.gca().set_axis_off()\n",
    "        plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)\n",
    "        plt.margins(0,0)\n",
    "        buf2 = io.BytesIO()\n",
    "        fig2.savefig(buf2, format=\"png\", dpi=dpi)\n",
    "        buf2.seek(0)\n",
    "        img_arr2 = np.frombuffer(buf2.getvalue(), dtype=np.uint8)\n",
    "        buf2.close()\n",
    "        img2 = cv2.imdecode(img_arr2, 1)\n",
    "        img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)\n",
    "        plt.clf()\n",
    "        plt.cla()\n",
    "        plt.close()\n",
    "        plt.close(fig2)\n",
    "\n",
    "        fig3 = plt.figure(figsize = figsize)\n",
    "        plt.axes(ylim=(-1, 1))\n",
    "\n",
    "        plt.plot(mag, color='black')\n",
    "        plt.gca().set_axis_off()\n",
    "        plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)\n",
    "        plt.margins(0,0)\n",
    "        buf3 = io.BytesIO()\n",
    "        fig3.savefig(buf3, format=\"png\", dpi=dpi)\n",
    "        buf3.seek(0)\n",
    "        img_arr3 = np.frombuffer(buf3.getvalue(), dtype=np.uint8)\n",
    "        buf3.close()\n",
    "        img3 = cv2.imdecode(img_arr3, 1)\n",
    "        img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)\n",
    "        plt.clf()\n",
    "        plt.cla()\n",
    "        plt.close()\n",
    "        plt.close(fig3)\n",
    "\n",
    "        if resize != None:\n",
    "            resized_real = cv2.resize(img2, resize)\n",
    "            resized_mag = cv2.resize(img3, resize)\n",
    "            resized_spectr = cv2.resize(spectr, resize)\n",
    "            img = np.asarray([resized_real, resized_mag, resized_spectr], dtype=np.float32)\n",
    "            return img\n",
    "        img = np.asarray([img2, img3, spectr], dtype=np.float32)\n",
    "        return img\n",
    "    except Exception as e:\n",
    "        print(str(e))\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "448da74a-e0ae-44d8-9877-8dd1f257a24f",
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_binaries = '//192.168.11.63/data/DATASETS/Energomash/2400'\n",
    "path_to_pictures = '//192.168.11.63/data/DATASETS/Energomash/2400_jpg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ac4945a8-29c4-4da4-945f-08658953e3e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6f226f86-5d72-4573-8af6-750128b70263",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                                                          | 0/965 [00:00<?, ?it/s]C:\\Users\\snytk\\miniconda3\\envs\\python311\\Lib\\site-packages\\scipy\\signal\\_spectral_py.py:1936: RuntimeWarning: overflow encountered in multiply\n",
      "  result = np.conjugate(result) * result\n",
      "C:\\Users\\snytk\\miniconda3\\envs\\python311\\Lib\\site-packages\\scipy\\signal\\_spectral_py.py:1938: RuntimeWarning: invalid value encountered in multiply\n",
      "  result *= scale\n",
      "100%|████████████████████████████████████████████████████████████████████████████████| 965/965 [28:11<00:00,  1.75s/it]"
     ]
    },
    {
     "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 = path_to_pictures +'/' + subdir + '/' + file + '.npy'\n",
    "        savepath_real_png = path_to_pictures +'/' + subdir + '/' + file + '_real' + '.png' \n",
    "        savepath_imag_png = path_to_pictures +'/' + subdir + '/' + file + '_imag' + '.png' \n",
    "        savepath_spec_png = path_to_pictures +'/' + subdir + '/' + file + '_spec' + '.png'\n",
    "        if not os.path.exists(savepath):\n",
    "            img = sig2pic_with_spec(path_to_data=filepath, filename=file, specT=s, resize = size)\n",
    "            gc.collect()\n",
    "            try:\n",
    "                \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",
    "                np.save(savepath, img)\n",
    "                \n",
    "            except Exception:\n",
    "                continue\n",
    "    print('Dir: ', subdir , ' finished!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "106b1add",
   "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
}
