def simulation(fnamehead, pathfem, pos_centre=[-74.296158, -10.213354, 28.307243], pos_ydir=[-74.217369, -37.293205, 20.05232], distance=4, current_change=1e6): # Initalize a session s = sim_struct.SESSION() # Name of head mesh s.fnamehead = fnamehead # Output folder s.pathfem = pathfem # Not to visualize results in gmsh when running simulations (else set to True) s.open_in_gmsh = False # Initialize a list of TMS simulations tmslist = s.add_tmslist() # Select coil. For full list of available coils, please see simnibs documentation tmslist.fnamecoil = 'Magstim_70mm_Fig8.nii.gz' # Initialize a coil position pos = tmslist.add_position() pos.centre = pos_centre # Place the coil over pos.pos_ydir = pos_ydir # Point the coil towards pos.distance = distance # Distance between coil and head pos.didt = current_change # Rate of change of current in the coil, in A/s. run_simnibs(s)
def createSim(coil_center, coil_direction, dir_name, cpus=1): # General Infoarmation S = sim_struct.SESSION() abPath = '/home/wq/下载/simnibs_examples/ernie/' S.fnamehead = abPath + 'ernie.msh' # head mesh S.pathfem = dir_name # Directory for the simulation # 定义线圈类型 tms = S.add_tmslist() tms.fnamecoil = 'Magstim_70mm_Fig8.nii.gz' # Choose a coil from the ccd-files folder # 定义线圈位置 pos = tms.add_position() # Place coil over the hand knob # Here, the hand knob is defined in MNI coordinates (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2034289/) # And transformed to subject coordinates # We can use positions in the cortex. SimNIBS will automatically project them to the skin surface # and add the specified distance pos.centre = simnibs.mni2subject_coords(coil_center, abPath + 'm2m_ernie') # Point the coil handle posteriorly, we just add 10 mm to the original M1 y coordinate pos.pos_ydir = simnibs.mni2subject_coords(coil_direction, abPath + 'm2m_ernie') pos.distance = 4 # 4 mm distance from coil surface to head surface # Run Simulation out = S.run(cpus, False, True) re = simnibs.read_msh(S.pathfem + '/ernie_TMS_1-0001_Magstim_70mm_Fig8_nii_scalar.msh') return re
def run_simulation(self, sim_output_name, fnamecoil='MagVenture_MC_B70.ccd', didt=1e6, overwrite=False): s = sim_struct.SESSION() s.fnamehead = os.path.join(self.mesh_dir, self.subject + '.msh') print('output dir: ', self.wf_base_dir) try: if overwrite and os.path.exists(os.path.join( self.wf_base_dir), 'simnibs_simulations', sim_output_name): os.rmdir( os.path.join(self.wf_base_dir, 'simnibs_simulations', sim_output_name)) os.makedirs( os.path.join(self.wf_base_dir, 'simnibs_simulations', sim_output_name)) except: raise OSError( 'simulation directory exists, overwrite=True to overwrite the results!' ) s.pathfem = os.path.join(self.wf_base_dir, 'simnibs_simulations', sim_output_name) s.map_to_vol = True s.map_to_surf = True tms = s.add_tmslist() tms.fnamecoil = os.path.join(Simnibs.SIMNIBSDIR, 'simnibs', 'ccd-files', fnamecoil) tms.add_positions_from_csv( os.path.join(self.wf_base_dir, 'simnibs_targets', self.csv_fname)) for p in tms.pos: p.didt = didt run_simnibs(s)
"""Script for simulating the TACS challenge montage. see @TACSchallenge on Twitter A. Thielscher, 2020 """ from copy import deepcopy from simnibs import sim_struct, run_simnibs ### general Information S = sim_struct.SESSION() S.fnamehead = 'ernie.msh' # head mesh S.pathfem = 'TACSchallenge' # Directory for the simulation S.map_to_surf = True S.open_in_gmsh = True # open results once they are ready # (set to False if you are annoyed by the popup windows) ### Define Condition A ConditionA = S.add_tdcslist() ### Set current flow though each channel: ### 2mA peak-to-peak --> 1 mA baseline-to-peak ### The third entry is a "pseudochannel" to which electrodes ### are connected that are not used in the simulated condition ConditionA.currents = [0.001, -0.001, 0.0] # Define occipital electrodes O1 = ConditionA.add_electrode() O1.channelnr = 1 # Connect the electrode to the first channel O1.centre = 'O1' # Place it over O1 O1.shape = 'ellipse' # Elliptical / circular shape
# In directory & files --> HARD CODED sub_dir = '/data/pt_neuam005/simnibs_TMS/subjects' coilnifti = '/data/pt_neuam005/simnibs_TMS/MagVenture_MC_B70.nii.gz' meshfile = os.path.join(sub_dir, sub, sub + '.msh') m2mfile = os.path.join(sub_dir, sub, 'm2m_' + sub) outfile = os.path.join(sub_dir, sub, 'simnibs_simulation') intFile = os.path.join(sub_dir, sub, 'tms_int_' + sub + '.txt') coordFile = os.path.join(sub_dir, sub, 'coordinates_' + sub + '.txt') tint = np.loadtxt(intFile).astype(float) coord = np.loadtxt(coordFile).astype(float)[1:].tolist()[0] # Initalize a session s = sim_struct.SESSION() s.fnamehead = meshfile s.subpath = m2mfile s.pathfem = outfile mesh = mesh_io.Msh(fn=s.fnamehead) # Initialize a list of TMS simulations tmslist = s.add_tmslist() tmslist.fnamecoil = coilnifti tmslist.anisotropy_type = 'scalar' # Initialize a TEMPORARY coil position to get params pos = tmslist.add_position() pos.centre = coord # entry coordinates