Skip to article frontmatterSkip to article content
# Prepare Python environment

import scipy.io as sio
from pathlib import Path

data_dir = Path("../../../data/04-B1-02-AFI")
data_file = "fig1.mat"

#Load either archived or generated plot variables
mat_contents = sio.loadmat(data_dir / data_file)

params = mat_contents["params"]
TR1_range = mat_contents["TR1_range"][0]
TR2_range = mat_contents["TR2_range"][0]
signal1_WM = mat_contents["signal1_WM"]
signal2_WM = mat_contents["signal2_WM"]
signal1_GM = mat_contents["signal1_GM"]
signal2_GM = mat_contents["signal2_GM"]
signal1_CSF = mat_contents["signal1_CSF"]
signal2_CSF = mat_contents["signal2_CSF"]

## Plot
# PYTHON CODE
# Module imports

import matplotlib.pyplot as plt
import chart_studio.plotly as py
import plotly.graph_objs as go
import numpy as np
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
config={'showLink': False, 'displayModeBar': False}
# PYTHON CODE

init_notebook_mode(connected=True)

data1_1 = [dict(
        visible = False,
        line=dict(color='royalblue'),
        x = params["EXC_FA"][0][0][0].astype(int),
        y = abs(np.squeeze(np.asarray(signal1_WM[ii]))),
        name = 'S<sub>1</sub>: T<sub>1</sub> = 0.9 s (White Matter)',
        text = 'S<sub>1</sub>: T<sub>1</sub> = 0.9 s (White Matter)',
        hoverinfo = 'x+y+text') for ii in range(len(TR1_range))]

data1_1[4]['visible'] = True

data1_2 = [dict(
        visible = False,
        line=dict(color='royalblue', dash='dash'),
        x = params["EXC_FA"][0][0][0],
        y = abs(np.squeeze(np.asarray(signal2_WM[ii]))),
        name = 'S<sub>2</sub>: T<sub>1</sub> = 0.9 s (White Matter)',
        text = 'S<sub>2</sub>: T<sub>1</sub> = 0.9 s (White Matter)',
        hoverinfo = 'x+y+text') for ii in range(len(TR1_range))]

data1_2[4]['visible'] = True

data2_1 = [dict(
        visible = False,
        line=dict(color='firebrick'),
        x = params["EXC_FA"][0][0][0],
        y = abs(np.squeeze(np.asarray(signal1_GM[ii]))),
        name = 'S<sub>1</sub>: T<sub>1</sub> = 1.5 s (Grey Matter)',
        text = 'S<sub>1</sub>: T<sub>1</sub> = 1.5 s (Grey Matter)',
        hoverinfo = 'x+y+text') for ii in range(len(TR1_range))]

data2_1[4]['visible'] = True

data2_2 = [dict(
        visible = False,
        line=dict(color='firebrick', dash='dash'),
        x = params["EXC_FA"][0][0][0],
        y = abs(np.squeeze(np.asarray(signal2_GM[ii]))),
        name = 'S<sub>2</sub>: T<sub>1</sub> = 1.5 s (Grey Matter)',
        text = 'S<sub>2</sub>: T<sub>1</sub> = 1.5 s (Grey Matter)',
        hoverinfo = 'x+y+text') for ii in range(len(TR1_range))]

data2_2[4]['visible'] = True

data3_1 = [dict(
        visible = False,
        line=dict(color='orange'),
        x = params["EXC_FA"][0][0][0],
        y = abs(np.squeeze(np.asarray(signal1_CSF[ii]))),
        name = 'S<sub>1</sub>: T<sub>1</sub> = 4.0 s (Cerebrospinal Fluid)',
        text = 'S<sub>1</sub>: T<sub>1</sub> = 4.0 s (Cerebrospinal Fluid)',
        hoverinfo = 'x+y+text') for ii in range(len(TR1_range))]

data3_1[4]['visible'] = True

data3_2 = [dict(
        visible = False,
        line=dict(color='orange', dash='dash'),
        x = params["EXC_FA"][0][0][0],
        y = abs(np.squeeze(np.asarray(signal2_CSF[ii]))),
        name = 'S<sub>2</sub>: T<sub>1</sub> = 4.0 s (Cerebrospinal Fluid)',
        text = 'S<sub>2</sub>: T<sub>1</sub> = 4.0 s (Cerebrospinal Fluid)',
        hoverinfo = 'x+y+text') for ii in range(len(TR1_range))]

data3_2[4]['visible'] = True

data = data1_1 + data1_2 + data2_1 +data2_2 + data3_1 + data3_2

steps = []
for i in range(len(TR1_range)):
    step = dict(
        method = 'restyle',  
        args = ['visible', [False] * len(data1_1)],
        label = str(TR1_range[i])
        )
    step['args'][1][i] = True # Toggle i'th trace to "visible"
    steps.append(step)

sliders=[
    dict(
        x = 0,
        y = -0.09,
        active = 3,
        currentvalue = {"prefix": "TR1 value (ms): <b>"},
        pad = {"t": 50, "b": 10},
        steps = steps)]

layout = go.Layout(
    width=650,
    height=520,
    margin=go.layout.Margin(
        l=80,
        r=40,
        b=60,
        t=10,
    ),
    annotations=[
        dict(
            x=0.5004254919715793,
            y=-0.18,
            showarrow=False,
            text='Excitation Flip Angle (°)',
            font=dict(
                family='Times New Roman',
                size=22
            ),
            xref='paper',
            yref='paper'
        ),
        dict(
            x=-0.15,
            y=0.5,
            showarrow=False,
            text='Long. Magnetization (M<sub>z</sub>)',
            font=dict(
                family='Times New Roman',
                size=22
            ),
            textangle=-90,
            xref='paper',
            yref='paper'
        ),
        dict(
            x=-0.007,
            y=-0.25,
            showarrow=False,
            text='TR<sub>2</sub> = 5TR<sub>1</sub>',
            font=dict(
                family='Times New Roman',
                size=12
            ),
            xref='paper',
            yref='paper'
        ),
    ],
    xaxis=dict(
        autorange=False,
        range=[0, params["EXC_FA"][0][0][0][-1]],
        showgrid=False,
        linecolor='black',
        linewidth=2
    ),
    yaxis=dict(
        autorange=True,
        showgrid=False,
        linecolor='black',
        linewidth=2
    ),
    legend=dict(
        x=0.5,
        y=0.9,
        traceorder='normal',
        font=dict(
            family='Times New Roman',
            size=12,
            color='#000'
        ),
        bordercolor='#000000',
        borderwidth=2
    ), 
    sliders=sliders
)

fig = dict(data=data, layout=layout)

iplot(fig, filename = 'basic-line', config = config)
Loading...