Generate a Phantom and visualize the contrast at different TE values

Generate a Phantom and visualize the contrast at different TE values#

Example for generating a phantom and visualizing the contrast at different TE values.

from snake.core.phantom import Phantom
from snake.core.engine.utils import get_ideal_phantom
from snake.core.simulation import SimConfig, GreConfig

This notebook has interactive widgets, run it in google colab or locally to enjoy it fully

shape = (181, 217, 181)
TR = 100
TE = 25
FA = 3
field = "7T"  # "1T5"

sim_conf = SimConfig(shape, seq=GreConfig(TR=TR, TE=TE, FA=FA))
phantom = Phantom.from_brainweb(sub_id=4, sim_conf=sim_conf)

phantom

contrast_at_TE = get_ideal_phantom(phantom, sim_conf)

from snake.toolkit.plotting import axis3dcut
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
axis3dcut(contrast_at_TE.T, None, None, cuts=(60, 60, 60), ax=ax, width_inches=5)
fig

from ipywidgets import interact
Traceback (most recent call last):
  File "/home/runner/work/snake-fmri/snake-fmri/examples/anatomical/example_generate_phantom.py", line 26, in <module>
    phantom = Phantom.from_brainweb(sub_id=4, sim_conf=sim_conf)
  File "/home/runner/work/snake-fmri/snake-fmri/src/snake/core/phantom/static.py", line 112, in from_brainweb
    tissues_mask = get_mri(sub_id, contrast="fuzzy").astype(np.float32)
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/brainweb_dl/mri.py", line 173, in get_mri
    data = _get_mri_sub20(
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/brainweb_dl/mri.py", line 91, in _get_mri_sub20
    filename = get_brainweb20(
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/brainweb_dl/_brainweb.py", line 329, in get_brainweb20
    Parallel(n_jobs=-1, backend="threading")(
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/joblib/parallel.py", line 2007, in __call__
    return output if self.return_generator else list(output)
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/joblib/parallel.py", line 1650, in _get_outputs
    yield from self._retrieve()
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/joblib/parallel.py", line 1754, in _retrieve
    self._raise_error_fast()
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/joblib/parallel.py", line 1789, in _raise_error_fast
    error_job.get_result(self.timeout)
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/joblib/parallel.py", line 745, in get_result
    return self._return_or_raise()
  File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/joblib/parallel.py", line 763, in _return_or_raise
    raise self._result
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='brainweb.bic.mni.mcgill.ca', port=80): Max retries exceeded with url: /cgi/brainweb1/?do_download_alias=subject04_wht&format_value=raw_short&zip_value=gnuzip&download_for_real=%5BStart+download%21%5D (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f2a32253dc0>, 'Connection to brainweb.bic.mni.mcgill.ca timed out. (connect timeout=None)'))
fig = plt.figure()
sim_conf = SimConfig(shape, seq=GreConfig(TR=TR, TE=TE, FA=FA))

phantom1T5 = Phantom.from_brainweb(
    sub_id=4, sim_conf=sim_conf, tissue_file="tissue_1T5"
)
phantom7T = Phantom.from_brainweb(sub_id=4, sim_conf=sim_conf, tissue_file="tissue_7T")


def live_contrast(TE: float = 10, TR: float = 100, FA: float = 3, tissue_field="1T5"):
    [ax.remove() for ax in fig.get_axes()]
    ax = fig.subplots()
    sim_conf.seq.TE = TE
    sim_conf.seq.TR = TR
    sim_conf.seq.FA = FA
    if tissue_field == "1T5":
        phantom = phantom1T5
    elif tissue_field == "7T":
        phantom = phantom7T
    contrast_at_TE = get_ideal_phantom(phantom, sim_conf)
    axis3dcut(fig, ax, contrast_at_TE.T, None, None, cuts=(60, 60, 60), width_inches=5)
    fig.canvas.draw_idle()
    # if len(fig.get_axes()) >=5:
    #     fig.get_axes()[-1].remove()


interact(
    live_contrast,
    TE=(0, 100, 1),
    TR=(0, 1000, 1),
    FA=(0, 90, 1),
    tissue_field=["1T5", "7T"],
)

Total running time of the script: (2 minutes 14.852 seconds)

Gallery generated by Sphinx-Gallery