{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\yazan\\anaconda3\\Lib\\site-packages\\paramiko\\transport.py:219: CryptographyDeprecationWarning: Blowfish has been deprecated\n",
      "  \"class\": algorithms.Blowfish,\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "import time\n",
    "import numpy as np\n",
    "from skimage import io\n",
    "import plotly.graph_objects as go\n",
    "from scipy.ndimage import uniform_filter1d\n",
    "from scipy.signal import savgol_filter\n",
    "from scipy import *\n",
    "from scipy.signal import *\n",
    "from util import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Shear_x001-x101_y010_Rot00.csv', 'Shear_x001-x101_y011_Rot00.csv', 'Shear_x001-x101_y012_Rot00.csv', 'Shear_x001-x101_y013_Rot00.csv', 'Shear_x001-x101_y014_Rot00.csv', 'Shear_x001-x101_y015_Rot00.csv', 'Shear_x001-x101_y016_Rot00.csv', 'Shear_x001-x101_y017_Rot00.csv', 'Shear_x001-x101_y018_Rot00.csv', 'Shear_x001-x101_y019_Rot00.csv', 'Shear_x001-x101_y020_Rot00.csv', 'Shear_x001-x101_y021_Rot00.csv', 'Shear_x001-x101_y022_Rot00.csv', 'Shear_x001-x101_y023_Rot00.csv', 'Shear_x001-x101_y024_Rot00.csv', 'Shear_x001-x101_y025_Rot00.csv', 'Shear_x001-x101_y026_Rot00.csv', 'Shear_x001-x101_y027_Rot00.csv', 'Shear_x001-x101_y028_Rot00.csv', 'Shear_x001-x101_y029_Rot00.csv', 'Shear_x001-x101_y030_Rot00.csv', 'Shear_x001-x101_y031_Rot00.csv', 'Shear_x001-x101_y032_Rot00.csv', 'Shear_x001-x101_y033_Rot00.csv', 'Shear_x001-x101_y034_Rot00.csv', 'Shear_x001-x101_y035_Rot00.csv', 'Shear_x001-x101_y036_Rot00.csv', 'Shear_x001-x101_y037_Rot00.csv', 'Shear_x001-x101_y038_Rot00.csv', 'Shear_x001-x101_y039_Rot00.csv', 'Shear_x001-x101_y040_Rot00.csv', 'Shear_x001-x101_y041_Rot00.csv', 'Shear_x001-x101_y042_Rot00.csv', 'Shear_x001-x101_y043_Rot00.csv', 'Shear_x001-x101_y044_Rot00.csv', 'Shear_x001-x101_y045_Rot00.csv', 'Shear_x001-x101_y046_Rot00.csv', 'Shear_x001-x101_y047_Rot00.csv', 'Shear_x001-x101_y048_Rot00.csv', 'Shear_x001-x101_y049_Rot00.csv', 'Shear_x001-x101_y050_Rot00.csv', 'Shear_x001-x101_y051_Rot00.csv', 'Shear_x001-x101_y052_Rot00.csv', 'Shear_x001-x101_y053_Rot00.csv', 'Shear_x001-x101_y054_Rot00.csv', 'Shear_x001-x101_y055_Rot00.csv', 'Shear_x001-x101_y056_Rot00.csv', 'Shear_x001-x101_y057_Rot00.csv', 'Shear_x001-x101_y058_Rot00.csv', 'Shear_x001-x101_y059_Rot00.csv', 'Shear_x001-x101_y060_Rot00.csv', 'Shear_x001-x101_y061_Rot00.csv']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\"\"\"\n",
    "vol = io.imread(\"https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attention-mri.tif\")\n",
    "volume = vol.T\n",
    "r, c = volume[0].shape\n",
    "\"\"\"\n",
    "\n",
    "# Import data\n",
    "dossier = \"dataverse_files\\Dataset\\Shear_Wave_Rot00_CSV_Data\"\n",
    "fichiers_selectionnes= fichiers_selectionnes = ['Shear_x001-x101_y{:03d}_Rot00.csv'.format(i) for i in range(10, 62)]\n",
    "print(fichiers_selectionnes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def butter_lowpass(cutoff, fs, order=5):\n",
    "    nyq = 0.5 * fs\n",
    "    normal_cutoff = cutoff / nyq\n",
    "    b, a = butter(order, normal_cutoff, btype='low', analog=False)\n",
    "    return b, a\n",
    "\n",
    "def butter_lowpass_filter(data, cutoff, fs, order=5):\n",
    "    b, a = butter_lowpass(cutoff, fs, order=order)\n",
    "    y = lfilter(b, a, data)\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_volume= np.array(lire_fichier_csv(dossier,fichiers_selectionnes))\n",
    "\n",
    "pre_traitement = abs(hilbert(pre_volume))\n",
    "fs = 600.0\n",
    "cutoff = 3.667\n",
    "filtered_m_hat = butter_lowpass_filter(pre_traitement, cutoff, fs)\n",
    "traitement = savgol_filter(pre_traitement, window_length=50, polyorder=5)\n",
    "\n",
    "volume = traitement[:, ::10, :]\n",
    "r, c = volume[0].shape\n",
    "nb_frames = volume.shape[0]\n",
    "\n",
    "z_scale = 6.7 / nb_frames\n",
    "\n",
    "#custom_colorscale = [(0, 'blue'), (0.5, 'cyan'), (1, 'red')]\n",
    "\n",
    "fig = go.Figure(frames=[go.Frame(data=go.Surface(\n",
    "    z=(z_scale * nb_frames - k * z_scale) * np.ones((r, c)),\n",
    "    surfacecolor=np.flipud(volume[nb_frames - 1 - k]),\n",
    "    cmin=0, cmax=20000,\n",
    "    colorscale='Jet'\n",
    "    ),\n",
    "    name=str(k) \n",
    "    )\n",
    "    for k in range(nb_frames)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
 
    "fig.add_trace(go.Surface(\n",
    "    z=z_scale * nb_frames * np.ones((r, c)),\n",
    "    surfacecolor=np.flipud(volume[nb_frames - 1]),\n",
    "    colorscale='Jet',\n",
    "    cmin=-20000, cmax=20000,\n",
    "    colorbar=dict(thickness=20, ticklen=4)\n",
    "    ))\n",
    "\n",
    "def frame_args(duration):\n",
    "    return {\n",
    "            \"frame\": {\"duration\": duration},\n",
    "            \"mode\": \"immediate\",\n",
    "            \"fromcurrent\": True,\n",
    "            \"transition\": {\"duration\": duration, \"easing\": \"linear\"},\n",
    "        }\n",
    "\n",
    "sliders = [\n",
    "            {\n",
    "                \"pad\": {\"b\": 10, \"t\": 60},\n",
    "                \"len\": 0.9,\n",
    "                \"x\": 0.1,\n",
    "                \"y\": 0,\n",
    "                \"steps\": [\n",
    "                    {\n",
    "                        \"args\": [[f.name], frame_args(0)],\n",
    "                        \"label\": str(k),\n",
    "                        \"method\": \"animate\",\n",
    "                    }\n",
    "                    for k, f in enumerate(fig.frames)\n",
    "                ],\n",
    "            }\n",
    "        ]\n",
    "\n",
    "# Layout\n",
    "fig.update_layout(\n",
    "         title='Slices in volumetric data',\n",
    "         width=600,\n",
    "         height=600,\n",
    "         scene=dict(\n",
    "                    zaxis=dict(range=[-0.1, z_scale * nb_frames], autorange=False),\n",
    "                    aspectratio=dict(x=1, y=1, z=1),\n",
    "                    ),\n",
    "         updatemenus = [\n",
    "            {\n",
    "                \"buttons\": [\n",
    "                    {\n",
    "                        \"args\": [None, frame_args(50)],\n",
    "                        \"label\": \"&#9654;\", # play symbol\n",
    "                        \"method\": \"animate\",\n",
    "                    },\n",
    "                    {\n",
    "                        \"args\": [[None], frame_args(0)],\n",
    "                        \"label\": \"&#9724;\", # pause symbol\n",
    "                        \"method\": \"animate\",\n",
    "                    },\n",
    "                ],\n",
    "                \"direction\": \"left\",\n",
    "                \"pad\": {\"r\": 10, \"t\": 70},\n",
    "                \"type\": \"buttons\",\n",
    "                \"x\": 0.1,\n",
    "                \"y\": 0,\n",
    "            }\n",
    "         ],\n",
    "         sliders=sliders\n",
    ")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = go.Figure()\n",
    "\n",
    "for k in range(nb_frames):\n",
    "    fig.add_trace(go.Surface(\n",
    "        z=np.ones((r, c)) * (z_scale * nb_frames - k * z_scale),\n",
    "        surfacecolor=np.flipud(volume[k]),\n",
    "        colorscale='Jet',\n",
    "        cmin=0, cmax=20000\n",
    "    ))\n",
    "\n",
    "fig.update_layout(\n",
    "    title='Volumetric Data Visualization',\n",
    "    scene=dict(\n",
    "        xaxis=dict(title='X'),\n",
    "        yaxis=dict(title='Y'),\n",
    "        zaxis=dict(title='Z'),\n",
    "    )\n",
    ")\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seuil = 10000\n",
    "indices = np.argwhere(volume > seuil)\n",
    "x, y, z = indices[:, 1], indices[:, 2], indices[:, 0] * z_scale  \n",
    "\n",
    "fig = go.Figure(data=[go.Scatter3d(\n",
    "    x=x,\n",
    "    y=y,\n",
    "    z=z,\n",
    "    mode='markers',\n",
    "    marker=dict(\n",
    "        size=2,\n",
    "        color=volume[indices[:, 0], indices[:, 1], indices[:, 2]], \n",
    "        colorscale='Jet',\n",
    "        opacity=0.8,\n",
    "        colorbar=dict(title='Intensité')\n",
    "    )\n",
    ")])\n",
    "fig.update_layout(\n",
    "    title='Volumetric Data Points',\n",
    "    scene=dict(\n",
    "        xaxis=dict(title='X'),\n",
    "        yaxis=dict(title='Y'),\n",
    "        zaxis=dict(title='Z'),\n",
    "    )\n",
    ")\n",
    "fig.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "pre_volume_transposed = np.transpose(pre_volume)\n",
    "\n",
   
    "pre_traitement = abs(hilbert(pre_volume_transposed))\n",
    "\n",
    "traitement = savgol_filter(pre_traitement, window_length=50, polyorder=5)\n",
    "\n",
    "volume = traitement[:, ::10, :]\n",
    "r, c = volume[0].shape\n",
    "\n",
   
    "nb_frames = volume.shape[0]\n",
    "\n",
   
    "z_scale = 6.7 / nb_frames\n",
    "\n",
    "fig = go.Figure(frames=[go.Frame(data=go.Surface(\n",
    "    z=(z_scale * nb_frames - k * z_scale) * np.ones((r, c)),\n",
    "    surfacecolor=np.flipud(volume[nb_frames - 1 - k]),\n",
    "    cmin=0, cmax=20000,\n",
    "    colorscale='Jet'\n",
    "    ),\n",
    "    name=str(k) \n",
    "    )\n",
    "    for k in range(nb_frames)])\n",
    "\n",
    
    "fig.add_trace(go.Surface(\n",
    "    z=z_scale * nb_frames * np.ones((r, c)),\n",
    "    surfacecolor=np.flipud(volume[nb_frames - 1]),\n",
    "    colorscale='Jet',\n",
    "    cmin=-20000, cmax=20000,\n",
    "    colorbar=dict(thickness=20, ticklen=4)\n",
    "    ))\n",
    "\n",
    "def frame_args(duration):\n",
    "    return {\n",
    "            \"frame\": {\"duration\": duration},\n",
    "            \"mode\": \"immediate\",\n",
    "            \"fromcurrent\": True,\n",
    "            \"transition\": {\"duration\": duration, \"easing\": \"linear\"},\n",
    "        }\n",
    "\n",
    "sliders = [\n",
    "            {\n",
    "                \"pad\": {\"b\": 10, \"t\": 60},\n",
    "                \"len\": 0.9,\n",
    "                \"x\": 0.1,\n",
    "                \"y\": 0,\n",
    "                \"steps\": [\n",
    "                    {\n",
    "                        \"args\": [[f.name], frame_args(0)],\n",
    "                        \"label\": str(k),\n",
    "                        \"method\": \"animate\",\n",
    "                    }\n",
    "                    for k, f in enumerate(fig.frames)\n",
    "                ],\n",
    "            }\n",
    "        ]\n",
    "\n",
    "# Layout\n",
    "fig.update_layout(\n",
    "         title='Slices in volumetric data',\n",
    "         width=600,\n",
    "         height=600,\n",
    "         scene=dict(\n",
    "                    zaxis=dict(range=[-0.1, z_scale * nb_frames], autorange=False),\n",
    "                    aspectratio=dict(x=1, y=1, z=1),\n",
    "                    xaxis=dict(title=\"X Axis\"),\n",
    "                    yaxis=dict(title=\"Y Axis\"),\n",
    "                    ),\n",
    "         updatemenus = [\n",
    "            {\n",
    "                \"buttons\": [\n",
    "                    {\n",
    "                        \"args\": [None, frame_args(50)],\n",
    "                        \"label\": \"&#9654;\", # play symbol\n",
    "                        \"method\": \"animate\",\n",
    "                    },\n",
    "                    {\n",
    "                        \"args\": [[None], frame_args(0)],\n",
    "                        \"label\": \"&#9724;\", # pause symbol\n",
    "                        \"method\": \"animate\",\n",
    "                    },\n",
    "                ],\n",
    "                \"direction\": \"left\",\n",
    "                \"pad\": {\"r\": 10, \"t\": 70},\n",
    "                \"type\": \"buttons\",\n",
    "                \"x\": 0.1,\n",
    "                \"y\": 0,\n",
    "            }\n",
    "         ],\n",
    "         sliders=sliders\n",
    ")\n",
    "\n",
    "fig.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
