Skip to content

Commit 7106079

Browse files
Merge pull request #10 from qMRLab/mrathon-cylindersim
Mrathon cylindersim
2 parents 0fcf572 + 311b148 commit 7106079

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

cylinder_sim.ipynb

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "2c36e403-7e73-47f2-a06b-75830ec7be39",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import numpy as np\n",
11+
"from scipy.fft import fftn, ifftn, fftshift\n",
12+
"import plotly.express as px\n",
13+
"import plotly.graph_objects as go\n",
14+
"from plotly.subplots import make_subplots"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"id": "396e6242-f067-47f8-b0cd-7d228b25818a",
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"vol_size = (128, 128, 128)\n",
25+
"vol_centre = (vol_size[1] // 2, vol_size[2] // 2)\n",
26+
"offset = (10, 20)\n",
27+
"radius_inner = 10\n",
28+
"radius_outer = 40\n",
29+
"cylinder_base = np.zeros(vol_size)\n",
30+
"cylinder_internal = np.zeros(vol_size)\n",
31+
"\n",
32+
"x = np.arange(-vol_size[1]//2,vol_size[1]//2,dtype=float)\n",
33+
"y = x\n",
34+
"z = x\n",
35+
"X,Y,Z = np.meshgrid(x,y,z)\n",
36+
"\n",
37+
"cylinder_base[X**2+Y**2<=radius_outer**2] = 1\n",
38+
"\n",
39+
"cylinder_internal[(X-offset[0])**2+(Y-offset[1])**2<=radius_inner**2] += 0.1"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"id": "2921239d-062b-4f50-bb5b-fd568be46e9d",
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"kx = X\n",
50+
"ky = Y\n",
51+
"kz = Z\n",
52+
"KK = (kx**2 + ky**2 + kz**2)\n",
53+
"KK[KK==0] = np.nan\n",
54+
"dipole_kernel = 2./3 - (kx**2)/(KK)\n",
55+
"dipole_kernel[(kx**2 + ky**2 + kz**2) == 0] = 0"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"id": "aa56b729-59da-4fbb-b4cb-c9863d9ee04a",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"fieldmap = np.real(ifftn(fftshift(dipole_kernel) * (fftn(cylinder_internal))))"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"id": "17152f62-03e7-4d10-95a7-7ad3bae45c39",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"idx = 64\n",
76+
"\n",
77+
"fig = make_subplots(rows=1, cols=3, shared_xaxes=False, horizontal_spacing=0.1,\n",
78+
" subplot_titles=(\"Geometry\",\n",
79+
" \"Dipole kernel\",\n",
80+
" \"Fieldmap\"),\n",
81+
" specs=[[{\"type\": \"Heatmap\"}, {\"type\": \"Heatmap\"}, {\"type\": \"Heatmap\"}]])\n",
82+
"fig.add_trace(go.Heatmap(z=cylinder_base[:,:,idx]+cylinder_internal[:,:,64], colorscale='gray'), 1, 1)\n",
83+
"fig.add_trace(go.Heatmap(z=dipole_kernel[:,:,idx], colorscale='gray'), 1, 2)\n",
84+
"fig.add_trace(go.Heatmap(z=fieldmap[:,:,idx], colorscale='gray'), 1, 3)\n",
85+
"fig.show()"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "97876e43-7be3-41d1-b725-c2ca14b66d94",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"fig = go.Figure()\n",
96+
"\n",
97+
"for step in np.arange(10, 60, 20):\n",
98+
" fig.add_trace(\n",
99+
" go.Heatmap(\n",
100+
" visible=False,\n",
101+
" z=fieldmap*step % np.pi))\n",
102+
"\n",
103+
"fig.data[0].visible = True\n",
104+
"\n",
105+
"# Create and add slider\n",
106+
"steps = []\n",
107+
"for i in range(len(fig.data)):\n",
108+
" step = dict(\n",
109+
" method=\"update\",\n",
110+
" args=[{\"visible\": [False] * len(fig.data)},\n",
111+
" {\"title\": \"TE=\" + str(i) + \" ms\"}], # layout attribute\n",
112+
" )\n",
113+
" step[\"args\"][0][\"visible\"][i] = True # Toggle i'th trace to \"visible\"\n",
114+
" steps.append(step)\n",
115+
" \n",
116+
"print('ok')\n",
117+
"\n",
118+
"sliders = [dict(\n",
119+
" active=10,\n",
120+
" currentvalue={\"prefix\": \"Frequency: \"},\n",
121+
" pad={\"t\": 50},\n",
122+
" steps=steps\n",
123+
")]\n",
124+
"\n",
125+
"fig.update_layout(\n",
126+
" sliders=sliders\n",
127+
")\n",
128+
"\n",
129+
"fig.show()"
130+
]
131+
}
132+
],
133+
"metadata": {
134+
"kernelspec": {
135+
"display_name": "Python 3 (ipykernel)",
136+
"language": "python",
137+
"name": "python3"
138+
},
139+
"language_info": {
140+
"name": ""
141+
}
142+
},
143+
"nbformat": 4,
144+
"nbformat_minor": 5
145+
}

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
nibabel==5.2.1
2+
pandas==2.2.2
3+
plotly==5.22.0
4+
scipy==1.13.0

0 commit comments

Comments
 (0)