{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "UgqzEwle2xCd"
   },
   "source": [
    "## **IMPORT PACKAGES**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "3X7UsV832B6j"
   },
   "outputs": [],
   "source": [
    "from math import pi\n",
    "\n",
    "import numpy as np\n",
    "import pickle\n",
    "import math\n",
    "\n",
    "from pypulseq.Sequence.sequence import Sequence\n",
    "from pypulseq.calc_rf_center import calc_rf_center\n",
    "from pypulseq.calc_duration import calc_duration\n",
    "from pypulseq.make_adc import make_adc\n",
    "from pypulseq.make_delay import make_delay\n",
    "from pypulseq.make_sinc_pulse import make_sinc_pulse\n",
    "from pypulseq.make_trapezoid import make_trapezoid\n",
    "from pypulseq.opts import Opts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "UQ4AWw9l4et_"
   },
   "source": [
    "## **USER INPUTS**\n",
    "\n",
    "These parameters are typically on the user interface of the scanner computer console "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "namespace(t_BW_product_ex=3.8,\n",
       "          t_BW_product_ref=4.2,\n",
       "          t_ex=0.00205,\n",
       "          t_ref=0.00256,\n",
       "          dG=0.00025,\n",
       "          sl_nb=1.0,\n",
       "          sl_thkn=0.004,\n",
       "          sl_gap=100.0,\n",
       "          FoV_f=0.256,\n",
       "          FoV_ph=0.256,\n",
       "          Nf=16.0,\n",
       "          Np=16.0,\n",
       "          BW_pixel=500.0,\n",
       "          TE=0.015000000000000001,\n",
       "          TR=0.4,\n",
       "          FA=90.0,\n",
       "          gamma=42576000.0,\n",
       "          G_amp_max=37.8,\n",
       "          rf_raster_time=1e-06,\n",
       "          grad_raster_time=1e-05,\n",
       "          G_slew_max=121)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('C:\\MRI_seq_files_mess\\SE_pulsequence_parameters.pickle', 'rb') as f:\n",
    "    params = pickle.load(f, fix_imports=True)\n",
    "\n",
    "\n",
    "params.gamma = 42.576e6 #TODO add to GUI\n",
    "params.G_amp_max = 37.8*1e-3*params.gamma #TODO add to GUI\n",
    "\n",
    "dG = 256e-6 #TODO: get from GUI correct value!\n",
    "params.rf_raster_time = 1e-6  #TODO add to GUI\n",
    "params.grad_raster_time = 10e-6  #TODO add to GUI\n",
    "\n",
    "params.t_ex = np.ceil(params.t_ex / params.grad_raster_time) * params.grad_raster_time\n",
    "params.TE = np.ceil(params.TE / params.grad_raster_time) * params.grad_raster_time #TODO in GUI correct TE\n",
    "params.t_ref = np.ceil(params.t_ref / params.grad_raster_time) * params.grad_raster_time\n",
    "params.G_amp_max = 37.8 #TODO add to GUI\n",
    "params.G_slew_max = 121 #TODO add to GUI\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ssnNwiQH4q_0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User inputs setup\n"
     ]
    }
   ],
   "source": [
    "params.nsa = 1  # Number of averages\n",
    "n_slices = 1 #params.sl_nb  # Number of slices #TODO integer from GUI\n",
    "Nx = 32 #TODO integer from GUI\n",
    "Ny = 32 #TODO integer from GUI\n",
    "fov = 256e-3  # mm\n",
    "slice_thickness = params.sl_thkn  # s\n",
    "slice_gap = params.sl_gap  # s\n",
    "rf_flip = params.FA  # degrees\n",
    "rf_offset = 0\n",
    "print('User inputs setup')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "PeYeI0V45ZfD"
   },
   "source": [
    "## **SYSTEM LIMITS**\n",
    "Set the hardware limits and initialize sequence object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "XHs1LT965kqg"
   },
   "outputs": [],
   "source": [
    "# Set system limits\n",
    "system = Opts(\n",
    "    max_grad=params.G_amp_max,  #TODO add to GUI\n",
    "    grad_unit=\"mT/m\",\n",
    "    max_slew=params.G_slew_max,  #TODO add to GUI\n",
    "    slew_unit=\"T/m/s\",\n",
    "    rf_ringdown_time=20e-6,    #TODO add to GUI     \n",
    "    rf_dead_time=100e-6,     #TODO add to GUI       \n",
    "    adc_dead_time=10e-6,    #TODO add to GUI        \n",
    "    rf_raster_time = params.rf_raster_time,            \n",
    "    grad_raster_time = params.grad_raster_time,        \n",
    "    block_duration_raster = params.grad_raster_time,\n",
    "    adc_raster_time = 1/(params.BW_pixel*params.Nf)\n",
    ")\n",
    "seq = Sequence(system)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ee-xBrpa7Zyn"
   },
   "source": [
    "## **TIME CONSTANTS**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "u2dW2nRf7obq"
   },
   "outputs": [],
   "source": [
    "TE = 15e-3  # s\n",
    "TR = params.TR  # s\n",
    "tau = TE / 2  # s  #TODO - introduce in GUI\n",
    "readout_time = round(1/params.BW_pixel, 8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OTw7M03g79bH"
   },
   "source": [
    "## **RF**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "XDZyQrbL8I3Q"
   },
   "outputs": [],
   "source": [
    "rf90_phase = np.pi / 2\n",
    "rf180_phase = 0\n",
    "\n",
    "flip90 = round(params.FA * pi / 180, 3)\n",
    "flip180 = round(180 * pi / 180)\n",
    "rf90, gz90, _ = make_sinc_pulse(flip_angle = flip90, system = system, duration = params.t_ex, \n",
    "                                slice_thickness = params.sl_thkn, apodization=0.3, \n",
    "                                time_bw_product = round(params.t_BW_product_ex,8), return_gz = True)\n",
    "\n",
    "rf180, gz180, _ = make_sinc_pulse(flip_angle = flip180, system = system, duration = params.t_ref, \n",
    "                                  slice_thickness = params.sl_thkn, apodization=0.3, \n",
    "                                  time_bw_product = round(params.t_BW_product_ref,8), phase_offset=90 * pi/180, return_gz = True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Slice selection gradients**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# gz_reph rephase gradient after gz90\n",
    "t_gz_reph = np.ceil(params.t_ex / 2 / params.grad_raster_time) * params.grad_raster_time\n",
    "gz_reph = make_trapezoid(channel='z', system=system, area=-gz90.area / 2,\n",
    "                         duration=t_gz_reph)\n",
    "t_gz_spoil = np.ceil(params.t_ref / 2 / params.grad_raster_time) * params.grad_raster_time\n",
    "# gz_spoil spoil gradients around \n",
    "gz_spoil = make_trapezoid(channel='z', system=system, area=gz90.area*3,\n",
    "                          duration=t_gz_spoil*2.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "RFSHuUOG9LHK"
   },
   "source": [
    "## **READOUT gradients & events**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Q8p-CttI9dk9"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'grad_raster_time' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[20], line 4\u001b[0m\n\u001b[0;32m      2\u001b[0m k_width \u001b[38;5;241m=\u001b[39m Nx \u001b[38;5;241m*\u001b[39m delta_k\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# gx readout gradient\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m t_gx \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mceil(readout_time \u001b[38;5;241m/\u001b[39m \u001b[43mgrad_raster_time\u001b[49m) \u001b[38;5;241m*\u001b[39m grad_raster_time\n\u001b[0;32m      5\u001b[0m gx \u001b[38;5;241m=\u001b[39m make_trapezoid(channel\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx\u001b[39m\u001b[38;5;124m'\u001b[39m, system\u001b[38;5;241m=\u001b[39msystem, flat_area\u001b[38;5;241m=\u001b[39mk_width, \n\u001b[0;32m      6\u001b[0m                     flat_time\u001b[38;5;241m=\u001b[39mt_gx)\n\u001b[0;32m      7\u001b[0m \u001b[38;5;66;03m# gx_pre readout prephase gradient\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'grad_raster_time' is not defined"
     ]
    }
   ],
   "source": [
    "delta_k = 1 / fov\n",
    "k_width = Nx * delta_k\n",
    "# gx readout gradient\n",
    "t_gx = np.ceil(readout_time / grad_raster_time) * grad_raster_time\n",
    "gx = make_trapezoid(channel='x', system=system, flat_area=k_width, \n",
    "                    flat_time=t_gx)\n",
    "# gx_pre readout prephase gradient\n",
    "\n",
    "\n",
    "t_gx_pre = np.ceil(readout_time / 2 / grad_raster_time) * grad_raster_time\n",
    "gx_pre = make_trapezoid(channel='x', system=system, flat_area=k_width / 2, \n",
    "                        flat_time=t_gx_pre)\n",
    "\n",
    "t_gx_spoil = np.ceil(readout_time / 2 / grad_raster_time) * grad_raster_time\n",
    "gx_spoil = make_trapezoid(channel='x', system=system, area=gz90.area * 0.75,\n",
    "                          duration=t_gx_spoil)\n",
    "\n",
    "\n",
    "gx.rise_time = np.ceil(gx.rise_time / grad_raster_time) * grad_raster_time\n",
    "gx.flat_time = np.ceil(gx.flat_time / grad_raster_time) * grad_raster_time\n",
    "adc = make_adc(num_samples=Nx, duration=gx.flat_time, delay=gx.rise_time)\n",
    "#adc = make_adc(num_samples=Nx, duration=gx.flat_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "o829kzm8kVFB"
   },
   "source": [
    "## **PREPHASE AND REPHASE**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "m5zA1bMakTVs"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'grad_raster_time' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[21], line 6\u001b[0m\n\u001b[0;32m      1\u001b[0m phase_areas \u001b[38;5;241m=\u001b[39m (np\u001b[38;5;241m.\u001b[39marange(Ny) \u001b[38;5;241m-\u001b[39m (Ny \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2\u001b[39m)) \u001b[38;5;241m*\u001b[39m delta_k\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m#gz_reph = make_trapezoid(channel='z', system=system, area=-gz90.area / 2,\u001b[39;00m\n\u001b[0;32m      3\u001b[0m   \u001b[38;5;66;03m#                       duration=2.5e-3)\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m#gx_pre = make_trapezoid(channel='x', system=system, flat_area=k_width / 2, \u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m#                        flat_time=readout_time / 2)\u001b[39;00m\n\u001b[1;32m----> 6\u001b[0m t_gy_pre \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mceil(params\u001b[38;5;241m.\u001b[39mt_ex \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2\u001b[39m \u001b[38;5;241m/\u001b[39m \u001b[43mgrad_raster_time\u001b[49m) \u001b[38;5;241m*\u001b[39m grad_raster_time\n\u001b[0;32m      7\u001b[0m gy_pre \u001b[38;5;241m=\u001b[39m make_trapezoid(channel\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124my\u001b[39m\u001b[38;5;124m'\u001b[39m, system\u001b[38;5;241m=\u001b[39msystem, area\u001b[38;5;241m=\u001b[39mphase_areas[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m], \n\u001b[0;32m      8\u001b[0m                         duration\u001b[38;5;241m=\u001b[39mt_gy_pre)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'grad_raster_time' is not defined"
     ]
    }
   ],
   "source": [
    "phase_areas = (np.arange(Ny) - (Ny / 2)) * delta_k\n",
    "#gz_reph = make_trapezoid(channel='z', system=system, area=-gz90.area / 2,\n",
    "  #                       duration=2.5e-3)\n",
    "#gx_pre = make_trapezoid(channel='x', system=system, flat_area=k_width / 2, \n",
    "#                        flat_time=readout_time / 2)\n",
    "t_gy_pre = np.ceil(params.t_ex / 2 / grad_raster_time) * grad_raster_time\n",
    "gy_pre = make_trapezoid(channel='y', system=system, area=phase_areas[-1], \n",
    "                        duration=t_gy_pre)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "3F5JUpE9-4lo"
   },
   "source": [
    "## **DELAYS**\n",
    "Echo time (TE) and repetition time (TR). Here, TE is broken down into `delay1` and `delay2`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "aOKRJclb_mDQ"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'gx_pre' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[22], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m delay1 \u001b[38;5;241m=\u001b[39m tau \u001b[38;5;241m-\u001b[39m calc_duration(rf90) \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2\u001b[39m \u001b[38;5;241m-\u001b[39m calc_duration(\u001b[43mgx_pre\u001b[49m)\n\u001b[0;32m      2\u001b[0m delay1 \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m=\u001b[39m calc_duration(gz_spoil) \u001b[38;5;241m-\u001b[39m calc_duration(rf180) \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2\u001b[39m\n\u001b[0;32m      3\u001b[0m delay1 \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mceil(delay1\u001b[38;5;241m/\u001b[39mseq\u001b[38;5;241m.\u001b[39mgrad_raster_time)\u001b[38;5;241m*\u001b[39mseq\u001b[38;5;241m.\u001b[39mgrad_raster_time\n",
      "\u001b[1;31mNameError\u001b[0m: name 'gx_pre' is not defined"
     ]
    }
   ],
   "source": [
    "delay1 = tau - calc_duration(rf90) / 2 - calc_duration(gx_pre)\n",
    "delay1 -= calc_duration(gz_spoil) - calc_duration(rf180) / 2\n",
    "delay1 = np.ceil(delay1/seq.grad_raster_time)*seq.grad_raster_time\n",
    "delay1 = make_delay(delay1)\n",
    "\n",
    "delay2 = tau - calc_duration(rf180) / 2 - calc_duration(gz_spoil)\n",
    "delay2 -= calc_duration(gx) / 2\n",
    "delay2 = np.ceil(delay2/seq.grad_raster_time)*seq.grad_raster_time\n",
    "delay2 = make_delay(delay2)\n",
    "\n",
    "delay_TR = TR - calc_duration(rf90) / 2 - calc_duration(gx) / 2 - TE\n",
    "delay_TR -= calc_duration(gy_pre)\n",
    "delay_TR = np.ceil(delay_TR/seq.grad_raster_time)*seq.grad_raster_time\n",
    "delay_TR = make_delay(delay_TR)\n",
    "\n",
    "print(f'delay_1: {delay1}')\n",
    "print(f'delay_2: {delay1}')\n",
    "print(f'delay_TR: {delay_TR}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'delay_TR' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m calc_duration(\u001b[43mdelay_TR\u001b[49m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'delay_TR' is not defined"
     ]
    }
   ],
   "source": [
    "calc_duration(delay_TR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "6Dq4wT-UAEOR"
   },
   "source": [
    "## **CONSTRUCT SEQUENCE**\n",
    "Construct sequence for one phase encode and multiple slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "B8ZmVkkrAXnK"
   },
   "outputs": [],
   "source": [
    "# Prepare RF offsets. This is required for multi-slice acquisition\n",
    "\n",
    "phase_areas = (np.arange(Ny) - (Ny / 2)) * delta_k\n",
    "\n",
    "delta_z = n_slices * slice_gap\n",
    "#z = np.linspace((-delta_z / 2), (delta_z / 2), n_slices) + rf_offset\n",
    "\n",
    "for k in range(params.nsa):  # Averages\n",
    "  for j in range(n_slices):  # Slices\n",
    "     # Apply RF offsets\n",
    "     rf90.freq_offset = (gz90.amplitude * slice_thickness * (j - (n_slices - 1) / 2))\n",
    "     rf180.freq_offset = (gz180.amplitude * slice_thickness * (j - (n_slices - 1) / 2))\n",
    "     rf90.phase_offset = (rf90_phase - 2 * np.pi * rf90.freq_offset * calc_rf_center(rf90)[0])\n",
    "     rf180.phase_offset = (rf180_phase - 2 * np.pi * rf180.freq_offset * calc_rf_center(rf180)[0])\n",
    "\n",
    "     for i in range(Ny):  # Phase encodes\n",
    "      seq.add_block(rf90, gz90)\n",
    "      t_gy_pre = np.ceil(params.t_ex / 2 / grad_raster_time) * grad_raster_time\n",
    "      gy_pre = make_trapezoid(channel='y', system=system, \n",
    "                              area=phase_areas[-i -1], duration = t_gy_pre)\n",
    "      seq.add_block(gx_pre, gy_pre, gz_reph)\n",
    "      seq.add_block(delay1)\n",
    "      seq.add_block(gz_spoil)\n",
    "      seq.add_block(rf180, gz180)\n",
    "      seq.add_block(gz_spoil)\n",
    "      seq.add_block(delay2)\n",
    "      #seq.add_block(gx_spoil) \n",
    "      seq.add_block(gx, adc)\n",
    "      #seq.add_block(gx_spoil)\n",
    "      gy_pre = make_trapezoid(channel='y', system=system, \n",
    "                              area=-phase_areas[-j -1], duration = t_gy_pre)\n",
    "      seq.add_block(gy_pre, gz_spoil)\n",
    "      seq.add_block(delay_TR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m exc_pulse_offsets \u001b[38;5;241m=\u001b[39m (\u001b[43mnp\u001b[49m\u001b[38;5;241m.\u001b[39mlinspace(\u001b[38;5;241m0.0\u001b[39m,param\u001b[38;5;241m.\u001b[39msl_nb \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1.0\u001b[39m,np\u001b[38;5;241m.\u001b[39mint16(param\u001b[38;5;241m.\u001b[39msl_nb)) \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m0.5\u001b[39m\u001b[38;5;241m*\u001b[39m(np\u001b[38;5;241m.\u001b[39mdouble(param\u001b[38;5;241m.\u001b[39msl_nb)  \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1.0\u001b[39m))\u001b[38;5;241m*\u001b[39m(param\u001b[38;5;241m.\u001b[39msl_thkn\u001b[38;5;241m*\u001b[39m(\u001b[38;5;241m100.0\u001b[39m \u001b[38;5;241m+\u001b[39m param\u001b[38;5;241m.\u001b[39msl_gap)\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m100.0\u001b[39m)\u001b[38;5;241m*\u001b[39mgradient_slice_amp\n",
      "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "exc_pulse_offsets = (np.linspace(0.0,param.sl_nb - 1.0,np.int16(param.sl_nb)) - 0.5*(np.double(param.sl_nb)  - 1.0))*(param.sl_thkn*(100.0 + param.sl_gap)/100.0)*gradient_slice_amp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "l-YP9djBJCpC"
   },
   "source": [
    "## **PLOTTING TIMNG DIAGRAM**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "d_iCUR4nfoH9"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'seq' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mseq\u001b[49m\u001b[38;5;241m.\u001b[39mplot(time_range\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m0.000\u001b[39m, \u001b[38;5;241m0.040\u001b[39m))\n",
      "\u001b[1;31mNameError\u001b[0m: name 'seq' is not defined"
     ]
    }
   ],
   "source": [
    "seq.plot(time_range=(0.000, 0.040))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Timing check passed successfully\n"
     ]
    }
   ],
   "source": [
    "ok, error_report = seq.check_timing()\n",
    "if ok:\n",
    "    print(\"Timing check passed successfully\")\n",
    "else:\n",
    "    print(\"Timing check failed. Error listing follows:\")\n",
    "    [print(e) for e in error_report]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "fYNgdWc_KiK7"
   },
   "source": [
    "## **GENERATING `.SEQ` FILE**\n",
    "Uncomment the code in the cell below to generate a `.seq` file and download locally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "6iN0aeuuqKRe"
   },
   "outputs": [],
   "source": [
    "seq.write('C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t1_se\\\\t1_SE_matrx32x32_myGrad.seq')  # Save to disk\n",
    "# from google.colab import files\n",
    "# files.download('t2_se_pypulseq_colab.seq')  # Download locally\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "4Q0b5w-lKtfP"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<xml.etree.ElementTree.ElementTree at 0x25ec4bb64a0>,\n",
       " 'C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t1_se/t1_SE_matrx32x32_myGrad.xml')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from py2jemris.seq2xml import seq2xml\n",
    "seq2xml(seq, seq_name='t1_SE_matrx32x32_myGrad', out_folder='C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t1_se')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "write_t2_se.ipynb",
   "private_outputs": true,
   "provenance": []
  },
  "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
