|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": { |
| 7 | + "colab": { |
| 8 | + "base_uri": "https://localhost:8080/" |
| 9 | + }, |
| 10 | + "collapsed": true, |
| 11 | + "id": "GiA85Mp8NnVR", |
| 12 | + "outputId": "18f17ceb-69a3-4c39-a146-0c900d11441d" |
| 13 | + }, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "!pip install toolapi\n", |
| 17 | + "!wget -q --show-progress https://github.com/mrx-org/toolapi-py/releases/download/v0.1.0/gre.seq" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "metadata": { |
| 24 | + "id": "gXB-6EyNuDOO" |
| 25 | + }, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "# @title Define tools\n", |
| 29 | + "import toolapi\n", |
| 30 | + "from functools import cache\n", |
| 31 | + "\n", |
| 32 | + "def on_message(msg):\n", |
| 33 | + " print(f\"\\r > {msg}\", end=\"\")\n", |
| 34 | + " return True\n", |
| 35 | + "\n", |
| 36 | + "def call(url, **kwargs):\n", |
| 37 | + " from time import time\n", |
| 38 | + " start = time()\n", |
| 39 | + " result = toolapi.call(url, on_message, **kwargs)\n", |
| 40 | + " print(f\"\\n --- done ({time() - start:.2f} s) ---\")\n", |
| 41 | + " return result\n", |
| 42 | + "\n", |
| 43 | + "# .seq loader: uses disseqt to convert sequence to instant events\n", |
| 44 | + "def load_seq(path, exact_trajectories=True):\n", |
| 45 | + " with open(path) as f:\n", |
| 46 | + " file_content = f.read()\n", |
| 47 | + " return call(\n", |
| 48 | + " \"wss://tool-seqloader-flyio.fly.dev/tool\",\n", |
| 49 | + " seq_file=file_content,\n", |
| 50 | + " exact_trajectory=exact_trajectories\n", |
| 51 | + " )[\"seq\"]\n", |
| 52 | + "\n", |
| 53 | + "# phantom library: returns rescaled brainweb multi-tissue phantoms (slow)\n", |
| 54 | + "@cache\n", |
| 55 | + "def load_phantom(subject, res, slice):\n", |
| 56 | + " return call(\n", |
| 57 | + " \"wss://tool-phantomlib-flyio.fly.dev/tool\",\n", |
| 58 | + " subject=subject, # BrainWeb subject 1-20\n", |
| 59 | + " res_x=res[0], # X resolution (1-434)\n", |
| 60 | + " res_y=res[1], # Y resolution (1-362)\n", |
| 61 | + " res_z=res[2], # Z resolution (1-434)\n", |
| 62 | + " slice=slice, # Axial slice index\n", |
| 63 | + " )[\"phantom\"]\n", |
| 64 | + "\n", |
| 65 | + "# not-yet-quite segmented PDG\n", |
| 66 | + "def sim_spdg(sequence, phantom):\n", |
| 67 | + " return call(\n", |
| 68 | + " \"wss://tool-spdg-flyio.fly.dev/tool\",\n", |
| 69 | + " sequence=sequence,\n", |
| 70 | + " phantom=phantom,\n", |
| 71 | + " )[\"signal\"]" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": null, |
| 77 | + "metadata": { |
| 78 | + "colab": { |
| 79 | + "base_uri": "https://localhost:8080/", |
| 80 | + "height": 475 |
| 81 | + }, |
| 82 | + "id": "G-39fS3ygxen", |
| 83 | + "outputId": "e26eef10-da33-45a8-b8c1-00ec23cef04e" |
| 84 | + }, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "# @title **load .seq + load phantom + run sim** | _with tools hosted on fly.io_\n", |
| 88 | + "import numpy as np\n", |
| 89 | + "import matplotlib.pyplot as plt\n", |
| 90 | + "\n", |
| 91 | + "seq = load_seq(\"gre.seq\", exact_trajectories=False)\n", |
| 92 | + "phantom = load_phantom(4, (64, 64, 64), 30)\n", |
| 93 | + "signal = np.array(sim_spdg(seq, phantom))[0, :]\n", |
| 94 | + "\n", |
| 95 | + "# Reconstruction is done here (not via some tool)\n", |
| 96 | + "kspace = signal.reshape(256, 256)\n", |
| 97 | + "reco = np.fft.fftshift(np.fft.fft2(np.fft.fftshift(kspace)))\n", |
| 98 | + "\n", |
| 99 | + "plt.figure()\n", |
| 100 | + "plt.subplot(211)\n", |
| 101 | + "plt.plot(np.abs(signal))\n", |
| 102 | + "plt.grid()\n", |
| 103 | + "plt.subplot(223)\n", |
| 104 | + "plt.imshow(np.abs(reco), origin=\"lower\", vmin=0)\n", |
| 105 | + "plt.axis(\"off\")\n", |
| 106 | + "plt.subplot(224)\n", |
| 107 | + "plt.imshow(np.angle(reco), origin=\"lower\", vmin=-np.pi, vmax=np.pi, cmap=\"twilight\")\n", |
| 108 | + "plt.axis(\"off\")\n", |
| 109 | + "plt.show()\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "metadata": { |
| 114 | + "colab": { |
| 115 | + "provenance": [] |
| 116 | + }, |
| 117 | + "kernelspec": { |
| 118 | + "display_name": "Python 3", |
| 119 | + "name": "python3" |
| 120 | + }, |
| 121 | + "language_info": { |
| 122 | + "name": "python" |
| 123 | + } |
| 124 | + }, |
| 125 | + "nbformat": 4, |
| 126 | + "nbformat_minor": 0 |
| 127 | +} |
0 commit comments