{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ae3d722b",
   "metadata": {},
   "source": [
    "## FID sequence in PyPulseSeq from scratch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f2e88708",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypulseq as pp\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d8e6ae7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define scanner aprameters\n",
    "scanner_parameters = pp.Opts(max_grad = 32, grad_unit = 'mT/m', max_slew = 100, slew_unit = 'T/m/s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3180015c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the sequence\n",
    "fid_sequence = pp.Sequence(system = scanner_parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0db4226f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initintalize the RF pulse\n",
    "rf_pulses = [pp.make_block_pulse(flip_angle = 90, bandwidth = 50e3, duration = 1e-3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4cb0f233",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the ADC\n",
    "adc_blocks = [pp.make_adc(num_samples = 128,system = scanner_parameters, dwell = 1/(50e3))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a14be6bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make zero gradients\n",
    "total_duration = adc_blocks[0].duration\n",
    "nonzero_amplitude = 1e-12\n",
    "gx = pp.make_trapezoid(channel = 'x', duration = total_duration, flat_time = total_duration*0.5, rise_time = total_duration*0.25, amplitude = nonzero_amplitude, system = scanner_parameters)\n",
    "gy = pp.make_trapezoid(channel = 'y', duration = total_duration, flat_time = total_duration*0.5, rise_time = total_duration*0.25, amplitude = nonzero_amplitude, system = scanner_parameters)\n",
    "gz = pp.make_trapezoid(channel = 'z', duration = total_duration, flat_time = total_duration*0.5, rise_time = total_duration*0.25, amplitude = nonzero_amplitude, system = scanner_parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ee01edfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add blocks together\n",
    "fid_sequence.add_block(rf_pulses[0])\n",
    "fid_sequence.add_block(adc_blocks[0],gx,gy,gz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "501c9ac8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34216 characters]",
      "text/plain": [
       "<Figure size 640x480 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46320 characters]",
      "text/plain": [
       "<Figure size 640x480 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fid_sequence.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2f5b108e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fid_sequence.write('fid_local.seq')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5075fa19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'block_duration': 0.001,\n",
       " 'rf': namespace(type='rf',\n",
       "           signal=array([14323.94487827+0.j, 14323.94487827+0.j]),\n",
       "           t=array([0.   , 0.001]),\n",
       "           shape_dur=0.001,\n",
       "           delay=0.0,\n",
       "           freq_offset=0.0,\n",
       "           phase_offset=0.0,\n",
       "           dead_time=0,\n",
       "           ringdown_time=0,\n",
       "           use='undefined'),\n",
       " 'gx': None,\n",
       " 'gy': None,\n",
       " 'gz': None,\n",
       " 'adc': None}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fid_sequence.block_events\n",
    "blk = fid_sequence.get_block(1).__dict__\n",
    "blk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "428c9c81",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'type'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpy2jemris\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mseq2xml\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m seq2xml\n\u001b[1;32m----> 2\u001b[0m \u001b[43mseq2xml\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfid_sequence\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mseq_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mfid_local\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout_folder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m.\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mC:\\MRI_seq\\py2jemris\\seq2xml.py:112\u001b[0m, in \u001b[0;36mseq2xml\u001b[1;34m(seq, seq_name, out_folder)\u001b[0m\n\u001b[0;32m    110\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m key \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgx\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgy\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgz\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[0;32m    111\u001b[0m     g \u001b[38;5;241m=\u001b[39m blk[key]\n\u001b[1;32m--> 112\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtype\u001b[49m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrap\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m    113\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m g\u001b[38;5;241m.\u001b[39mamplitude \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m    114\u001b[0m             g_atom \u001b[38;5;241m=\u001b[39m ET\u001b[38;5;241m.\u001b[39mSubElement(C_block, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTRAPGRADPULSE\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'type'"
     ]
    }
   ],
   "source": [
    "from py2jemris.seq2xml import seq2xml\n",
    "seq2xml(fid_sequence, seq_name='fid_local', out_folder='.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bc500f9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,len(fid_sequence.block_events)+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8e7daae0-f9df-45ee-a5a3-f6d88dab2fe2",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Sequence' object has no attribute 'dict_block_events'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[24], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;28mlen\u001b[39m(\u001b[43mfid_sequence\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdict_block_events\u001b[49m)\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Sequence' object has no attribute 'dict_block_events'"
     ]
    }
   ],
   "source": [
    "range(1,len(fid_sequence.dict_block_events)+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47c6f51b-3f2a-4a26-8949-7ba11fb5fec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2jemris.seq2xml import seq2xml\n",
    "seq2xml(seq, seq_name='fid_local', out_folder='.')"
   ]
  }
 ],
 "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
