Skip to content

Commit ad1cbf3

Browse files
committed
ENH: Use the attenuation distance to plot the DWI signal
Use the attenuation distance to plot the DWI signal.
1 parent ec05d16 commit ad1cbf3

File tree

1 file changed

+93
-7
lines changed

1 file changed

+93
-7
lines changed

docs/notebooks/dwi_simulated_gp.ipynb

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@
6565
" gtab, mevals, S0=S0, angles=angles, fractions=fractions, snr=snr\n",
6666
" )\n",
6767
"\n",
68-
" grad = np.vstack([gtab.bvecs.T, gtab.bvals])\n",
69-
"\n",
70-
" return signal, sticks, grad"
68+
" return signal, sticks, gtab"
7169
],
7270
"id": "a0f5bab019855954",
7371
"outputs": [],
@@ -84,12 +82,96 @@
8482
"cell_type": "code",
8583
"source": [
8684
"hsph_dirs = 90\n",
87-
"signal, sticks, grad = create_multitensor_dmri_signal(hsph_dirs)"
85+
"signal, sticks, gtab = create_multitensor_dmri_signal(hsph_dirs)"
8886
],
8987
"id": "e9545781fe5cf3b8",
9088
"outputs": [],
9189
"execution_count": null
9290
},
91+
{
92+
"metadata": {},
93+
"cell_type": "markdown",
94+
"source": "Define the method that computes the signal attenuation.",
95+
"id": "40b8ba2f818a49f8"
96+
},
97+
{
98+
"metadata": {},
99+
"cell_type": "code",
100+
"source": [
101+
"def diffusion_distance(bvecs, Q):\n",
102+
" # Borrowed from https://github.com/arokem/osmosis/blob/72d848ddc172ce6a72baba4c0d4cf3675db1a381/osmosis/tensor.py#L233-L248\n",
103+
" # sphADC = np.dot(np.dot(bvecs.T, np.matrix(Q).getI()), bvecs)\n",
104+
" sphADC = np.dot(np.dot(bvecs.T, np.linalg.inv(Q)), bvecs)\n",
105+
" dist = np.diag(1 / np.sqrt(sphADC))\n",
106+
"\n",
107+
" return dist"
108+
],
109+
"id": "d40db09fa860c25e",
110+
"outputs": [],
111+
"execution_count": null
112+
},
113+
{
114+
"metadata": {},
115+
"cell_type": "markdown",
116+
"source": "Perform the DTI fit on the signal and compute the signal attenuation along each diffusion-encoding gradient direction.",
117+
"id": "25f32e883f520b86"
118+
},
119+
{
120+
"metadata": {},
121+
"cell_type": "code",
122+
"source": [
123+
"import dipy.reconst.dti as dti\n",
124+
"\n",
125+
"dti_model = dti.TensorModel(gtab)\n",
126+
"dti_fit = dti_model.fit(signal)\n",
127+
"\n",
128+
"distance = diffusion_distance(gtab.bvecs.T, dti_fit.quadratic_form)"
129+
],
130+
"id": "ba6f132d088a1c0a",
131+
"outputs": [],
132+
"execution_count": null
133+
},
134+
{
135+
"metadata": {},
136+
"cell_type": "markdown",
137+
"source": "We now define a function to plot the DWI signal.",
138+
"id": "e9c2295d90cfbfc1"
139+
},
140+
{
141+
"metadata": {},
142+
"cell_type": "code",
143+
"source": [
144+
"from matplotlib import pyplot as plt \n",
145+
"%matplotlib inline\n",
146+
"\n",
147+
"def plot_dwi(dwi_data, bvecs, b0s_mask, voxel_idx, b0_idx, distance):\n",
148+
"\n",
149+
" s0_voxel = dwi_data[voxel_idx[0], voxel_idx[1], voxel_idx[2], b0_idx]\n",
150+
" s_voxel = dwi_data[voxel_idx[0], voxel_idx[1], voxel_idx[2], ~b0s_mask]\n",
151+
"\n",
152+
" # Scale gradient vector values with the distance\n",
153+
" # s_normal = s_voxel/s0_voxel\n",
154+
" # scaled_vectors = bvecs[:, ~b0s_mask] / s_normal[np.newaxis, :]\n",
155+
" scaled_vectors = bvecs[:, ~b0s_mask] / distance[~b0s_mask]\n",
156+
"\n",
157+
" # Plot the DWI signal as a 3D point cloud\n",
158+
" fig = plt.figure()\n",
159+
" ax = fig.add_subplot(111, projection=\"3d\")\n",
160+
"\n",
161+
" _ = ax.scatter(scaled_vectors[0, :], scaled_vectors[1, :], scaled_vectors[2, :], c=\"blue\", marker=\"*\")\n",
162+
"\n",
163+
" # Set labels\n",
164+
" ax.set_xlabel(\"X\")\n",
165+
" ax.set_ylabel(\"Y\")\n",
166+
" ax.set_zlabel(\"Z\")\n",
167+
" ax.set_title(\"Normalized dMRI signal\")\n",
168+
"\n",
169+
" return fig"
170+
],
171+
"id": "1ed5c04d4b49d8d9",
172+
"outputs": [],
173+
"execution_count": null
174+
},
93175
{
94176
"metadata": {},
95177
"cell_type": "markdown",
@@ -99,13 +181,17 @@
99181
{
100182
"metadata": {},
101183
"cell_type": "code",
102-
"execution_count": null,
103184
"source": [
104185
"voxel_idx = [0, 0, 0]\n",
105-
"dwi_data = signal[np.newaxis, np.newaxis, np.newaxis, :]"
186+
"dwi_data = signal[np.newaxis, np.newaxis, np.newaxis, :]\n",
187+
"# There is only one b0 volume in the simulated signal\n",
188+
"b0_idx = 0\n",
189+
"_ = plot_dwi(dwi_data, gtab.bvecs.T, gtab.b0s_mask, voxel_idx, b0_idx, distance)\n",
190+
"plt.show()"
106191
],
107192
"id": "c07f103d9bd347cc",
108-
"outputs": []
193+
"outputs": [],
194+
"execution_count": null
109195
},
110196
{
111197
"metadata": {},

0 commit comments

Comments
 (0)