[Simnibs-discuss] Calculation Magnetic Field

Kristoffer Madsen kristofferm at drcmr.dk
Tue Mar 23 13:59:13 CET 2021


The few lines of Python code below should allow you to get the B field
by first order difference approximation (in file is A.nii.gz):

import numpy as np
import nibabel as nib
A=nib.load('A.nii.gz')
dx=np.diag(A.affine)[:3]/1000.
Av=A.get_fdata()
gx=np.gradient(Av[...,0],*dx)
gy=np.gradient(Av[...,1],*dx)
gz=np.gradient(Av[...,2],*dx)
cA=np.zeros_like(Av)
cA[...,0]=gz[1]-gy[2]
cA[...,1]=gx[2]-gz[0]
cA[...,2]=gy[0]-gx[1]
B=nib.Nifti1Image(cA[...,1],affine=A.affine)
B.to_filename('B.nii.gz')

something very similar can likely be done in Matlab but writing out
nifti volumes is a bit more complicated there.
this would give you B in a 3D volume (B.nii.gz) - if you want it on
the mesh you would have to interpolate it to the mesh positions (nodes
or elements depending on what you desire). If you just want a
visualization in the simNIBS gui - you could use the show dA/dt
functionality there (where you choose the B as the coil file).
Hope this helps a bit,
Kristoffer

On Sat, 20 Mar 2021 at 09:39, Christian Heinesch
<heinesch at rhrk.uni-kl.de> wrote:
>
> Hi Kristoffer,
>
>
>
> Thank you very much for your quick reply.
>
> This would like to graph the magnetic field (dB/dt) of different coils in TMS.
>
> For this I was advised in a previous e-mail to convert the simulation of the A-Field to a Nifti file, but I don’t know how to calculate the curl here and convert the Nifti file back.
>
> If you have any suggestions on how to compute and visualize the magnetic field, I would be very pleased.
>
> I’m open to any solution.
>
>
>
> Best,
>
> Christian Heinesch
>
>
>
> Von: Kristoffer Madsen
> Gesendet: Freitag, 19. März 2021 09:27
> An: Christian Heinesch
> Cc: discuss at simnibs.org
> Betreff: Re: [Simnibs-discuss] Calculation Magnetic Field
>
>
>
> Hi Christian, I still not entirely sure why you need to show the
>
> (dA/dt) / (dB/dt) field (which
>
> would be independent of the head model). However, do you only have a
>
> nifti file or can you
>
> use a dipole expansion .ccd file? From the dipole expansion you can
>
> easily calculate the B field at any desired point (this would be
>
> easier and you would simply need to calculate the field for all
>
> desired points in the mesh). If you do need it from the nifti file
>
> then I'd calculate the curl of dA/dt on the grid like you are doing
>
> and then interpolate to points in the mesh. I think I have some code
>
> to do this (likely python though) if that can be helpful for you.
>
> Best,
>
> Kristoffer
>
>
>
>
>
> On Fri, 19 Mar 2021 at 09:12, Christian Heinesch
>
> <heinesch at rhrk.uni-kl.de> wrote:
>
> >
>
> > Hello,
>
> >
>
> > I apologize for asking another question here in my concern.
>
> >
>
> >
>
> >
>
> > I want to determine the dB/dt field at TMS from the magnetic vector field dA/dt. For this I have successfully converted the gmsh file to a nifti file. Now I wanted to have the nifti file with the A field read into Matlab and then calculate the rotation of A here. However, the required volume is 4d of size 256x256x256x3 and has too many elements to display.
>
> >
>
> >
>
> >
>
> > Can you please help how I can calculate the magnetic field here and then convert it back to a gmsh file? I would be very happy to receive an answer.
>
> >
>
> >
>
> >
>
> > With kind regards
>
> >
>
> > Christian Heinesch
>
> >
>
> > _______________________________________________
>
> > Simnibs-discuss mailing list
>
> > Simnibs-discuss at drcmr.dk
>
> > https://mailman.drcmr.dk/mailman/listinfo/simnibs-discuss
>
>


More information about the Simnibs-discuss mailing list