{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-16T10:04:44.511122Z",
     "start_time": "2025-04-16T10:04:44.012385Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "from flandre.beamformer.dist import direct_dist\n",
    "from flandre.beamformer.das import gen_pwi\n",
    "from flandre.utils.Config import DeviceConfig, ImagingConfig\n",
    "from flandre.utils.RfMat import RfMat\n",
    "from flandre.utils.RfMeta import RfFrameMeta, RfSequenceMeta\n",
    "from flandre.utils.ScanData import ScanData\n",
    "from pathlib import Path\n",
    "import scipy\n",
    "import cv2\n",
    "import cupy as cp\n",
    "import cupyx.scipy.fft\n",
    "from matplotlib import pyplot as plt"
   ],
   "id": "5518f8a97320440e",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lambda/source/scarlet/flandre/.venv/lib/python3.12/site-packages/cupyx/jit/_interface.py:173: FutureWarning: cupyx.jit.rawkernel is experimental. The interface can change in the future.\n",
      "  cupy._util.experimental('cupyx.jit.rawkernel')\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-13T05:31:08.792101Z",
     "start_time": "2025-04-13T05:31:08.334419Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "dc = DeviceConfig(v2=1540, rows=1490)\n",
    "pwi, _ = gen_pwi(direct_dist(dc, p=cp), dc)\n",
    "\n",
    "input_path = Path('/run/media/lambda/b86dccdc-f134-464b-a310-6575ee9ae85c/uf/cap-30/328_parallel.bin')\n",
    "input_sd = ScanData.from_file(input_path, (256, 1502), p=cp)\n",
    "plt.figure(figsize=(40, 20))\n",
    "dctm = cupyx.scipy.fft.dctn(input_sd.m)\n",
    "dctm = cupyx.scipy.fft.dctn(input_sd.m)\n",
    "\n",
    "# dctm[mask[:,:,3]!=0] = 0\n",
    "dctm[:30, :800] = 0\n",
    "m2 = cupyx.scipy.fft.idctn(dctm)\n",
    "input_sd.m = m2\n",
    "\n",
    "input_sd = input_sd.dct(80, 1500)\n",
    "# pass.hib().abs().filter_min(300)\n",
    "plt.imshow(input_sd.m.get()[:, 600:], cmap='grey')\n",
    "plt.imshow(pwi(input_sd.m).get(), cmap='grey')"
   ],
   "id": "734b0e53cbb44961",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7d27773fb2f0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 4000x2000 with 1 Axes>"
      ],
      "image/png": "[encoded data, 462736 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-13T06:12:42.465079Z",
     "start_time": "2025-04-13T06:12:42.168296Z"
    }
   },
   "source": [
    "input_path = Path('/run/media/lambda/b86dccdc-f134-464b-a310-6575ee9ae85c/uf/cap-30/328_parallel.bin')\n",
    "input_bytes = input_path.read_bytes()\n",
    "mat = np.frombuffer(input_bytes, dtype=np.int16).copy()\n",
    "mat = mat.reshape((256, 1502))\n",
    "mat = cp.asarray(mat)\n",
    "\n",
    "dc = DeviceConfig(v2=1540, rows=1490)\n",
    "pwi, _ = gen_pwi(direct_dist(dc, p=cp), dc)\n",
    "\n",
    "rfmat = RfMat(mat, RfFrameMeta(), RfSequenceMeta(shape=(256, 1502)))\n",
    "rfmat = rfmat.dct(80, 1500)\n",
    "rfmat = rfmat.call(pwi)\n",
    "rfmat = rfmat.call(cp.asarray, order='C')\n",
    "rfmat = rfmat.argrelextrema()\n",
    "rfmat = rfmat.conv_guass(b=18 * 0.01)\n",
    "rfmat = rfmat.cpu()\n",
    "\n",
    "plt.figure(figsize=(40, 20))\n",
    "plt.imshow(rfmat.m, cmap='grey')\n",
    "\n"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7d276b189a00>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 4000x2000 with 1 Axes>"
      ],
      "image/png": "[encoded data, 389672 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-16T10:06:08.722349Z",
     "start_time": "2025-04-16T10:06:08.281251Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "from flandre.utils.RfSequence import RfSequence\n",
    "\n",
    "rff = RfSequence('/mnt/16T/private_dataset/us/steel-top,U=30,M=PWI,S=(256 1502).zip')\n",
    "RfMat.from_rf_frame(rff.frames[0]).time_gain_compensation_linear_max(5.2).jupyter()"
   ],
   "id": "4acfc7303fa58cf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 4000x2000 with 1 Axes>"
      ],
      "image/png": "[encoded data, 1041536 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-16T10:03:23.103198Z",
     "start_time": "2025-04-16T10:03:23.018519Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "75e9c5f82736241",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'rff' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[2], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m RfMat\u001B[38;5;241m.\u001B[39mfrom_rf_frame(\u001B[43mrff\u001B[49m\u001B[38;5;241m.\u001B[39mframes[\u001B[38;5;241m0\u001B[39m])\u001B[38;5;241m.\u001B[39mjupyter()\n",
      "\u001B[0;31mNameError\u001B[0m: name 'rff' is not defined"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
