{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "UgqzEwle2xCd"
   },
   "source": [
    "## **IMPORT PACKAGES**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "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",
    "import json as j\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.make_extended_trapezoid import make_extended_trapezoid\n",
    "from pypulseq.opts import Opts\n",
    "from pypulseq.align import align\n",
    "from pypulseq.traj_to_grad import traj_to_grad\n",
    "\n",
    "from pypulseq.utilities import phase_grad_utils as pgu\n",
    "\n",
    "from py2jemris.seq2xml import seq2xml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **temporal functions**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def TSE_k_space_fill(n_ex, ETL, k_steps, TE_eff_number):\n",
    "    #function defines phase encoding steps for k space filling in liner order \n",
    "    #with shifting according to the TE effective number\n",
    "    \n",
    "    k_space_list_with_zero = []\n",
    "    for i in range(ETL):\n",
    "        k_space_list_with_zero.append((ETL-1)*n_ex - i*n_ex)\n",
    "    #print(k_space_list_with_zero)\n",
    "    central_num = np.int32(k_steps/2)\n",
    "    #print(central_num)\n",
    "    index_central_line = k_space_list_with_zero.index(central_num)\n",
    "    shift = index_central_line - TE_eff_number + 1\n",
    "    \n",
    "    if shift>0:\n",
    "        a = k_space_list_with_zero[:shift]\n",
    "        b = k_space_list_with_zero[shift:]\n",
    "        k_space_list_with_zero = b+a\n",
    "    elif shift<0:\n",
    "        a = k_space_list_with_zero[:shift]\n",
    "        b = k_space_list_with_zero[shift:]\n",
    "        k_space_list_with_zero = b+a\n",
    "        \n",
    "    k_space_order_filing = [k_space_list_with_zero]\n",
    "    for i in range(n_ex-1):\n",
    "        k_space_list_temp = []\n",
    "        for k in k_space_list_with_zero:\n",
    "            k_space_list_temp.append(k+i+1)\n",
    "        k_space_order_filing.append(k_space_list_temp)\n",
    "        \n",
    "    return k_space_order_filing\n",
    "    "
   ]
  },
  {
   "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": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'G_amp_max': 1609372.8,\n",
       " 'G_slew_max': 5151696000.0,\n",
       " 'gamma': 42576000.0,\n",
       " 'grad_raster_time': 1e-05,\n",
       " 'rf_raster_time': 1e-06,\n",
       " 't_BW_product_ex': 3.55,\n",
       " 't_BW_product_ref': 3.55,\n",
       " 't_ex': 0.00298,\n",
       " 't_ref': 0.00333,\n",
       " 'rf_ringdown_time': [2e-05],\n",
       " 'rf_dead_time': [0.0001],\n",
       " 'adc_dead_time': [1e-05],\n",
       " 'aapodization': 0.27,\n",
       " 'dG': 0.00025,\n",
       " 'sl_nb': 1.0,\n",
       " 'sl_thkn': 0.005,\n",
       " 'sl_gap': 100.0,\n",
       " 'FoV_f': 0.032,\n",
       " 'FoV_ph': 0.032,\n",
       " 'Nf': 16.0,\n",
       " 'Np': 16.0,\n",
       " 'BW_pixel': 500.0,\n",
       " 'TE': 0.02,\n",
       " 'N_TE': 2.0,\n",
       " 'ES': 0.01,\n",
       " 'TR': 0.5,\n",
       " 'FA': 90.0,\n",
       " 'conct': 1.0,\n",
       " 'ETL': 16.0,\n",
       " 'Average': 5,\n",
       " 'delay_TR': 0.33484}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weightning = 'T1'\n",
    "\n",
    "if weightning == 'T1':\n",
    "    with open('C:\\MRI_seq_files_mess\\TSE_T1.json', 'rb') as f:\n",
    "        params = j.load(f)\n",
    "        \n",
    "elif weightning == 'T2':\n",
    "    with open('C:\\MRI_seq_files_mess\\TSE_T2.json', 'rb') as f:\n",
    "        params = j.load(f)\n",
    "        \n",
    "elif weightning == 'T1':\n",
    "    with open('C:\\MRI_seq_files_mess\\TSE_T1.json', 'rb') as f:\n",
    "        params = j.load(f)    \n",
    "else:\n",
    "    print('Please choose image weightning')\n",
    "    \n",
    "\n",
    "tau = params['TE'] / 2  # s  #TODO - introduce in GUI\n",
    "readout_time = round(1/params['BW_pixel'], 8)\n",
    "\n",
    "\n",
    "params['Average'] = 5\n",
    "params"
   ]
  },
  {
   "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": 18,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "XHs1LT965kqg"
   },
   "outputs": [],
   "source": [
    "# Set system limits\n",
    "scanner_parameters = Opts(\n",
    "    max_grad=37.8,  \n",
    "    grad_unit=\"mT/m\",\n",
    "    max_slew=121,  \n",
    "    slew_unit=\"T/m/s\",\n",
    "    rf_ringdown_time = params['rf_ringdown_time'],   \n",
    "    rf_dead_time = params['rf_dead_time'],          \n",
    "    adc_dead_time = params['adc_dead_time'],            \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(scanner_parameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OTw7M03g79bH"
   },
   "source": [
    "## **RF & basis gradients**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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, gz_ex, _ = make_sinc_pulse(flip_angle = flip90, system = scanner_parameters, 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, gz_ref, _ = make_sinc_pulse(flip_angle = flip180, system = scanner_parameters, 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",
    "# Prepare RF offsets. This is required for multi-slice acquisition\n",
    "pulse_offsets90 = (np.linspace(0.0,params['sl_nb'] - 1.0,np.int16(params['sl_nb'])) - 0.5*(np.double(params['sl_nb'])  - 1.0))*(params['sl_thkn']*(100.0 + params['sl_gap'])/100.0)*gz_ex.amplitude\n",
    "pulse_offsets180 = (np.linspace(0.0,params['sl_nb'] - 1.0,np.int16(params['sl_nb'])) - 0.5*(np.double(params['sl_nb'])  - 1.0))*(params['sl_thkn']*(100.0 + params['sl_gap'])/100.0)*gz_ref.amplitude\n",
    "\n",
    "\n",
    "# slice selective gradient drafts for complex gradient blocks\n",
    "t_exwd = params['t_ex'] + scanner_parameters.rf_ringdown_time + scanner_parameters.rf_dead_time\n",
    "t_refwd = params['t_ref'] + scanner_parameters.rf_ringdown_time + scanner_parameters.rf_dead_time\n",
    "\n",
    "gz90 = make_trapezoid(channel=\"z\", system=scanner_parameters, amplitude=gz_ex.amplitude,\n",
    "                         flat_time=t_exwd, rise_time=params['dG'])\n",
    "gz180 = make_trapezoid(channel=\"z\", system=scanner_parameters, amplitude=gz_ref.amplitude,\n",
    "                         flat_time=t_refwd, rise_time=params['dG'])\n",
    "\n",
    "# generate basic gx readout gradient - G_read\n",
    "k_read = np.double(params['Nf'])/np.double(params['FoV_f'])\n",
    "t_gx = np.ceil(readout_time / scanner_parameters.grad_raster_time) * scanner_parameters.grad_raster_time\n",
    "gx = make_trapezoid(channel='x', system=scanner_parameters, flat_area=k_read, \n",
    "                    flat_time=t_gx + 2*scanner_parameters.adc_dead_time)\n",
    "\n",
    "# generate gx spoiler gradient - G_crr\n",
    "gx_spoil = make_trapezoid(channel='x', system=scanner_parameters, area=gx.area, flat_time = params['dG'],  \n",
    "                         rise_time=params['dG'])\n",
    "\n",
    "\n",
    "# read prephase gradient - G_pre\n",
    "gx_pre = make_trapezoid(channel=\"x\", system=scanner_parameters, area=gx.area*1.50,\n",
    "                        rise_time=params['dG'])\n",
    "\n",
    "# rephase gradient draft after 90 RF pulse  - G_reph\n",
    "gz_reph = make_trapezoid(channel=\"z\", system=scanner_parameters, area=gz_ex.area*0.25,\n",
    "                        flat_time = calc_duration(gx_pre), rise_time=params['dG'])\n",
    "\n",
    "#spoil gradient around 180 RF pulse - G_crs\n",
    "t_gz_spoil = np.ceil(params['t_ref'] / 2 / scanner_parameters.grad_raster_time) * scanner_parameters.grad_raster_time\n",
    "gz_spoil = make_trapezoid(channel='z', system=scanner_parameters, area=gz90.area*0.75, rise_time=params['dG'], flat_time = params['dG'])\n",
    "\n",
    "#spoil gradient G_sps\n",
    "gz_cr = make_trapezoid(channel='z', system=scanner_parameters, area=gz90.area*4, rise_time=params['dG'])\n",
    "\n",
    "# Creation of ADC\n",
    "adc = make_adc(num_samples=params['Nf'], duration=t_gx, delay=scanner_parameters.adc_dead_time, system=scanner_parameters)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "o829kzm8kVFB"
   },
   "source": [
    "## **k-space filling quantification**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "m5zA1bMakTVs"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10]]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k_phase = np.double(params['Np'])/np.double(params['FoV_ph'])\n",
    "k_steps_PE = pgu.create_k_steps(k_phase, np.int16(params['Np'])) #list of phase encoding gradients\n",
    "\n",
    "n_ex = math.floor(params['Np'] / params['ETL']) # number of excitations \n",
    "k_space_order_filing = TSE_k_space_fill(n_ex, np.int32(params['ETL']), np.int32(params['Np']), np.int32(params['N_TE'])) #TODO: create function on different k space order filling\n",
    "k_space_order_filing\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "Np = 16 \n",
    "part_fourier_factor = 1\n",
    "\n",
    "num_k_lines = Np*part_fourier_factor\n",
    "\n",
    "\n",
    "np.int32(num_k_lines)\n",
    "if (np.int32(num_k_lines) % 2) == 0:\n",
    "    num_k_lines = np.int32(num_k_lines)\n",
    "else: \n",
    "    num_k_lines = np.int32(num_k_lines) + 1\n",
    "    \n",
    "num_k_lines"
   ]
  },
  {
   "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": 9,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "aOKRJclb_mDQ"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.12317999999999996"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "block_duration = 0\n",
    "block_duration = max(calc_duration(rf90), calc_duration(gz90))/2\n",
    "block_duration += max(calc_duration(gx_pre), calc_duration(gz_spoil))\n",
    "for i in range(np.int32(params['ETL'])-1):\n",
    "    block_duration +=  max(calc_duration(rf180), calc_duration(gz180))  \n",
    "    block_duration +=  max(calc_duration(gx_spoil), calc_duration(gz_spoil))  \n",
    "    block_duration +=  max(calc_duration(gx_spoil), calc_duration(adc))\n",
    "    block_duration +=  calc_duration(gz_spoil)\n",
    "block_duration +=  max(calc_duration(rf180), calc_duration(gz_spoil))  \n",
    "block_duration +=  max(calc_duration(gx_spoil), calc_duration(gz_spoil))  \n",
    "block_duration +=  max(calc_duration(gx_spoil), calc_duration(adc))\n",
    "block_duration +=  calc_duration(gz_cr)    \n",
    "    \n",
    "    \n",
    "    \n",
    "block_duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.37682000000000004"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.5 - block_duration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **CONSTRUCT CONCATINATIONS timings**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Quantification of Effective TE loop\n",
    "#eff_time = TE + calc_duration(gx) / 2 + max(calc_duration(gy_pre),calc_duration(gz_spoil)) + calc_duration(gx_spoil) + calc_duration(gz90) / 2\n",
    "eff_time = block_duration #equal to previous!\n",
    "\n",
    "max_slices_per_TR = np.floor(params['TR']/eff_time)\n",
    "required_concats = np.int32(np.ceil(params['sl_nb']/max_slices_per_TR))\n",
    "slice_list = list(range(np.int32(params['sl_nb'])))\n",
    "slice_list = [slice_list[x::required_concats] for x in range(required_concats)]\n",
    "    \n",
    "# Calculate the TR fillers\n",
    "tr_pauses = [(params['TR']/np.double(len(x))) - eff_time for x in slice_list]\n",
    "tr_pauses = [max(seq.grad_raster_time,seq.rf_raster_time)*np.floor(x/max(seq.grad_raster_time,seq.rf_raster_time)) for x in tr_pauses]\n",
    "    \n",
    "# Generate the TR fillers\n",
    "tr_fillers = [make_delay(x) for x in tr_pauses]\n"
   ]
  },
  {
   "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": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "curr_concat_0\n",
      "curr_slice_0\n",
      "phase step_9\n",
      "-31.25\n",
      "phase step_8\n",
      "0.0\n",
      "phase step_7\n",
      "31.25\n",
      "phase step_6\n",
      "62.5\n",
      "phase step_5\n",
      "93.75\n",
      "phase step_4\n",
      "125.0\n",
      "phase step_3\n",
      "156.25\n",
      "phase step_2\n",
      "187.5\n",
      "phase step_1\n",
      "218.75\n",
      "phase step_0\n",
      "250.0\n",
      "phase step_15\n",
      "-218.75\n",
      "phase step_14\n",
      "-187.5\n",
      "phase step_13\n",
      "-156.25\n",
      "phase step_12\n",
      "-125.0\n",
      "phase step_11\n",
      "-93.75\n",
      "phase step_10\n",
      "-62.5\n",
      "curr_concat_0\n",
      "curr_slice_0\n",
      "phase step_9\n",
      "-31.25\n",
      "phase step_8\n",
      "0.0\n",
      "phase step_7\n",
      "31.25\n",
      "phase step_6\n",
      "62.5\n",
      "phase step_5\n",
      "93.75\n",
      "phase step_4\n",
      "125.0\n",
      "phase step_3\n",
      "156.25\n",
      "phase step_2\n",
      "187.5\n",
      "phase step_1\n",
      "218.75\n",
      "phase step_0\n",
      "250.0\n",
      "phase step_15\n",
      "-218.75\n",
      "phase step_14\n",
      "-187.5\n",
      "phase step_13\n",
      "-156.25\n",
      "phase step_12\n",
      "-125.0\n",
      "phase step_11\n",
      "-93.75\n",
      "phase step_10\n",
      "-62.5\n",
      "curr_concat_0\n",
      "curr_slice_0\n",
      "phase step_9\n",
      "-31.25\n",
      "phase step_8\n",
      "0.0\n",
      "phase step_7\n",
      "31.25\n",
      "phase step_6\n",
      "62.5\n",
      "phase step_5\n",
      "93.75\n",
      "phase step_4\n",
      "125.0\n",
      "phase step_3\n",
      "156.25\n",
      "phase step_2\n",
      "187.5\n",
      "phase step_1\n",
      "218.75\n",
      "phase step_0\n",
      "250.0\n",
      "phase step_15\n",
      "-218.75\n",
      "phase step_14\n",
      "-187.5\n",
      "phase step_13\n",
      "-156.25\n",
      "phase step_12\n",
      "-125.0\n",
      "phase step_11\n",
      "-93.75\n",
      "phase step_10\n",
      "-62.5\n",
      "curr_concat_0\n",
      "curr_slice_0\n",
      "phase step_9\n",
      "-31.25\n",
      "phase step_8\n",
      "0.0\n",
      "phase step_7\n",
      "31.25\n",
      "phase step_6\n",
      "62.5\n",
      "phase step_5\n",
      "93.75\n",
      "phase step_4\n",
      "125.0\n",
      "phase step_3\n",
      "156.25\n",
      "phase step_2\n",
      "187.5\n",
      "phase step_1\n",
      "218.75\n",
      "phase step_0\n",
      "250.0\n",
      "phase step_15\n",
      "-218.75\n",
      "phase step_14\n",
      "-187.5\n",
      "phase step_13\n",
      "-156.25\n",
      "phase step_12\n",
      "-125.0\n",
      "phase step_11\n",
      "-93.75\n",
      "phase step_10\n",
      "-62.5\n",
      "curr_concat_0\n",
      "curr_slice_0\n",
      "phase step_9\n",
      "-31.25\n",
      "phase step_8\n",
      "0.0\n",
      "phase step_7\n",
      "31.25\n",
      "phase step_6\n",
      "62.5\n",
      "phase step_5\n",
      "93.75\n",
      "phase step_4\n",
      "125.0\n",
      "phase step_3\n",
      "156.25\n",
      "phase step_2\n",
      "187.5\n",
      "phase step_1\n",
      "218.75\n",
      "phase step_0\n",
      "250.0\n",
      "phase step_15\n",
      "-218.75\n",
      "phase step_14\n",
      "-187.5\n",
      "phase step_13\n",
      "-156.25\n",
      "phase step_12\n",
      "-125.0\n",
      "phase step_11\n",
      "-93.75\n",
      "phase step_10\n",
      "-62.5\n"
     ]
    }
   ],
   "source": [
    "#working version without concatinations\n",
    "\n",
    "for k in range(params['Average']):  # Averages\n",
    "    for curr_concat in range(required_concats): \n",
    "        for phase_steps in k_space_order_filing: # in stead of phase steps list of phase steps\n",
    "            for curr_slice in range(np.int32(params['sl_nb'])):  # Slices\n",
    "                # Apply RF offsets\n",
    "                n_echo_temp = 0\n",
    "                rf90.freq_offset = pulse_offsets90[curr_slice]\n",
    "                rf180.freq_offset = pulse_offsets180[curr_slice]\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",
    "                print('curr_concat_' + str(curr_concat))\n",
    "                print('curr_slice_' + str(curr_slice))\n",
    "                \n",
    "\n",
    "                seq.add_block(gz90, rf90)\n",
    "                seq.add_block(gz_reph, gx_pre)\n",
    "                for phase_step in phase_steps:\n",
    "                    print('phase step_' + str(phase_step))\n",
    "                    seq.add_block(gz180, rf180)      \n",
    "                    gy_pre = make_trapezoid(channel='y', system=scanner_parameters, \n",
    "                                            area=-k_steps_PE[phase_step], duration = calc_duration(gz_spoil),\n",
    "                                            rise_time=params['dG'])\n",
    "                    print(k_steps_PE[phase_step])\n",
    "                      \n",
    "                    seq.add_block(gz_spoil,gx_spoil,gy_pre)\n",
    "                    seq.add_block(gx, adc)\n",
    "                    n_echo_temp += 1                    \n",
    "                    gy_pre = make_trapezoid(channel='y', system=scanner_parameters, \n",
    "                                            area=k_steps_PE[phase_step], duration = calc_duration(gz_spoil),\n",
    "                                            rise_time=params['dG'])\n",
    "                    if n_echo_temp == np.int32(params['ETL']):\n",
    "                        seq.add_block(gz_cr,gx_spoil,gy_pre)\n",
    "                    else:\n",
    "                        seq.add_block(gz_spoil,gx_spoil,gy_pre)\n",
    "                seq.add_block(tr_fillers[curr_concat])\n",
    "    \n",
    "  \n",
    "     "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "l-YP9djBJCpC"
   },
   "source": [
    "## **PLOTTING TIMNG DIAGRAM**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "d_iCUR4nfoH9"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57428 characters]",
      "text/plain": [
       "<Figure size 640x480 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 61776 characters]",
      "text/plain": [
       "<Figure size 640x480 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "seq.plot(time_range=(-0.01, 0.8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "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": 15,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "6iN0aeuuqKRe"
   },
   "outputs": [],
   "source": [
    "if weightning == 'T1':\n",
    "    seq.write('C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t1_TSE\\\\t1_TSE_matrx16x16.seq')  # Save to disk\n",
    "    seq2xml(seq, seq_name='t1_TSE_matrx16x16_myGrad', out_folder='C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t1_TSE')\n",
    "    \n",
    "elif weightning == 'T2':\n",
    "    seq.write('C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t2_TSE\\\\t2_TSE_matrx16x16.seq')  # Save to disk\n",
    "    seq2xml(seq, seq_name='t2_TSE_matrx16x16_myGrad', out_folder='C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t2_TSE')\n",
    "    \n",
    "elif weightning == 'PD':\n",
    "    seq.write('C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\pd_TSE\\\\pd_TSE_matrx16x16.seq')  # Save to disk \n",
    "    seq2xml(seq, seq_name='t2_TSE_matrx16x16_myGrad', out_folder='C:\\\\MRI_seq\\\\new_MRI_pulse_seq\\\\t2_TSE')\n",
    "\n",
    "else:\n",
    "    print('Please choose image weightning')\n",
    "\n"
   ]
  }
 ],
 "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
}
