Simulation (tit.sim)¶
Temporal Interference (TI) Simulation Module
This module provides a unified interface for running TI and mTI simulations. It automatically detects the simulation type based on montage configuration.
ConductivityType ¶
Bases: Enum
SimNIBS conductivity type enumeration.
ElectrodeConfig
dataclass
¶
ElectrodeConfig(shape: str = 'ellipse', dimensions: List[float] = (lambda: [8.0, 8.0])(), thickness: float = 4.0, sponge_thickness: float = 2.0)
Configuration for electrode properties.
IntensityConfig
dataclass
¶
IntensityConfig(pair1: float = 1.0, pair2: float = 1.0, pair3: float = 1.0, pair4: float = 1.0)
Configuration for current intensities in TI simulations.
Each pair requires one intensity value (in mA). SimNIBS automatically applies equal and opposite currents to the two electrodes in each pair. For example: pair1=2.0 means electrode1=+2.0mA and electrode2=-2.0mA
TI mode (2 pairs): Uses pair1 and pair2 mTI mode (4 pairs): Uses pair1, pair2, pair3, and pair4
from_string
classmethod
¶
from_string(intensity_str: str) -> IntensityConfig
Parse intensity from string format.
Formats: - "2.0" -> all pairs: 2.0 mA - "2.0,1.5" -> pair1: 2.0, pair2: 1.5 (both set to 1.0 for pair3/pair4) - "2.0,1.5,1.0,0.5" -> pair1: 2.0, pair2: 1.5, pair3: 1.0, pair4: 0.5
Args: intensity_str: Comma-separated intensity values
Returns: IntensityConfig object
Source code in tit/sim/config.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
MontageConfig
dataclass
¶
MontageConfig(name: str, electrode_pairs: List[Tuple[Union[str, List[float]], Union[str, List[float]]]], is_xyz: bool = False, eeg_net: Optional[str] = None)
ParallelConfig
dataclass
¶
ParallelConfig(enabled: bool = False, max_workers: int = 0)
Configuration for parallel simulation execution.
__post_init__ ¶
__post_init__()
Set max_workers to sensible default if auto-detect.
Source code in tit/sim/config.py
117 118 119 120 121 122 123 | |
get_memory_warning ¶
get_memory_warning() -> Optional[str]
Return memory warning if parallel execution may cause issues.
Source code in tit/sim/config.py
130 131 132 133 134 135 136 137 138 139 140 | |
PostProcessor ¶
PostProcessor(subject_id: str, conductivity_type: str, m2m_dir: str, logger)
Post-processor for TI simulation results.
Initialize post-processor.
Args: subject_id: Subject identifier conductivity_type: Conductivity type string m2m_dir: Path to m2m directory logger: Logger instance
Source code in tit/sim/post_processor.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
process_mti_results ¶
process_mti_results(hf_dir: str, ti_dir: str, mti_dir: str, mti_nifti_dir: str, hf_mesh_dir: str, hf_analysis_dir: str, documentation_dir: str, montage_name: str) -> str
Process 4-pair mTI simulation results with full pipeline.
Args: hf_dir: High-frequency output directory ti_dir: TI intermediate output directory mti_dir: mTI final output directory mti_nifti_dir: mTI NIfTI output directory hf_mesh_dir: High-frequency mesh output directory hf_analysis_dir: High-frequency analysis output directory documentation_dir: Documentation output directory montage_name: Montage name
Returns: Path to output mTI mesh file
Source code in tit/sim/post_processor.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
process_ti_results ¶
process_ti_results(hf_dir: str, output_dir: str, nifti_dir: str, surface_overlays_dir: str, hf_mesh_dir: str, hf_nifti_dir: str, hf_analysis_dir: str, documentation_dir: str, montage_name: str) -> str
Process 2-pair TI simulation results with full pipeline.
Args: hf_dir: High-frequency output directory (SimNIBS writes here) output_dir: TI mesh output directory nifti_dir: TI NIfTI output directory surface_overlays_dir: Surface overlays output directory hf_mesh_dir: High-frequency mesh output directory hf_nifti_dir: High-frequency NIfTI output directory hf_analysis_dir: High-frequency analysis output directory documentation_dir: Documentation output directory montage_name: Montage name
Returns: Path to output TI mesh file
Source code in tit/sim/post_processor.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
SessionBuilder ¶
SessionBuilder(config: SimulationConfig)
Builder class for constructing SimNIBS SESSION objects.
Initialize session builder.
Args: config: Simulation configuration
Source code in tit/sim/session_builder.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
build_session ¶
build_session(montage: MontageConfig, output_dir: str) -> sim_struct.SESSION
Build SimNIBS SESSION object for a montage.
Args: montage: Montage configuration output_dir: Output directory for simulation results
Returns: Configured SESSION object
Source code in tit/sim/session_builder.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
SimulationConfig
dataclass
¶
SimulationConfig(subject_id: str, project_dir: str, conductivity_type: ConductivityType, intensities: IntensityConfig, electrode: ElectrodeConfig, eeg_net: str = 'GSN-HydroCel-185.csv', map_to_surf: bool = True, map_to_vol: bool = True, map_to_mni: bool = True, map_to_fsavg: bool = False, tissues_in_niftis: str = 'all', open_in_gmsh: bool = False, parallel: ParallelConfig = ParallelConfig())
Main configuration for TI simulation.
__post_init__ ¶
__post_init__()
Convert string conductivity type to enum if needed.
Source code in tit/sim/config.py
160 161 162 163 164 165 | |
SimulationMode ¶
Bases: Enum
Simulation mode enumeration.
load_montages ¶
load_montages(montage_names: List[str], project_dir: str, eeg_net: str, include_flex: bool = True) -> List[MontageConfig]
Load all montages (regular + flex).
Args: montage_names: List of montage names to load project_dir: Project directory path eeg_net: EEG net name include_flex: Whether to include flex montages
Returns: List of MontageConfig objects
Source code in tit/sim/montage_loader.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
run_montage_visualization ¶
run_montage_visualization(montage_name: str, simulation_mode: SimulationMode, eeg_net: str, output_dir: str, project_dir: str, logger, electrode_pairs: Optional[List] = None) -> bool
Run montage visualization using visualize-montage.sh.
Args: montage_name: Name of the montage simulation_mode: TI or MTI mode eeg_net: EEG net name output_dir: Output directory for montage images project_dir: Project directory path logger: Logger instance electrode_pairs: Optional list of electrode pairs for direct visualization
Returns: True if successful, False otherwise
Source code in tit/sim/simulator.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
run_simulation ¶
run_simulation(config: SimulationConfig, montages: List[MontageConfig], logger=None, progress_callback: Optional[Callable[[int, int, str], None]] = None) -> List[dict]
Run TI/mTI simulations for given montages.
Supports both sequential and parallel execution based on config.parallel settings.
Args: config: Simulation configuration montages: List of montage configurations logger: Optional logger instance progress_callback: Optional callback for progress updates (current, total, montage_name)
Returns: List of dictionaries with simulation results
Source code in tit/sim/simulator.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
setup_montage_directories ¶
setup_montage_directories(montage_dir: str, simulation_mode: SimulationMode) -> dict
Create the complete directory structure for a montage simulation.
Args: montage_dir: Base montage directory simulation_mode: TI or MTI simulation mode
Returns: Dictionary of created directory paths
Source code in tit/sim/simulator.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
Config (tit.sim.config)¶
Configuration dataclasses for TI simulations.
ConductivityType ¶
Bases: Enum
SimNIBS conductivity type enumeration.
ElectrodeConfig
dataclass
¶
ElectrodeConfig(shape: str = 'ellipse', dimensions: List[float] = (lambda: [8.0, 8.0])(), thickness: float = 4.0, sponge_thickness: float = 2.0)
Configuration for electrode properties.
IntensityConfig
dataclass
¶
IntensityConfig(pair1: float = 1.0, pair2: float = 1.0, pair3: float = 1.0, pair4: float = 1.0)
Configuration for current intensities in TI simulations.
Each pair requires one intensity value (in mA). SimNIBS automatically applies equal and opposite currents to the two electrodes in each pair. For example: pair1=2.0 means electrode1=+2.0mA and electrode2=-2.0mA
TI mode (2 pairs): Uses pair1 and pair2 mTI mode (4 pairs): Uses pair1, pair2, pair3, and pair4
from_string
classmethod
¶
from_string(intensity_str: str) -> IntensityConfig
Parse intensity from string format.
Formats: - "2.0" -> all pairs: 2.0 mA - "2.0,1.5" -> pair1: 2.0, pair2: 1.5 (both set to 1.0 for pair3/pair4) - "2.0,1.5,1.0,0.5" -> pair1: 2.0, pair2: 1.5, pair3: 1.0, pair4: 0.5
Args: intensity_str: Comma-separated intensity values
Returns: IntensityConfig object
Source code in tit/sim/config.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
MontageConfig
dataclass
¶
MontageConfig(name: str, electrode_pairs: List[Tuple[Union[str, List[float]], Union[str, List[float]]]], is_xyz: bool = False, eeg_net: Optional[str] = None)
ParallelConfig
dataclass
¶
ParallelConfig(enabled: bool = False, max_workers: int = 0)
Configuration for parallel simulation execution.
__post_init__ ¶
__post_init__()
Set max_workers to sensible default if auto-detect.
Source code in tit/sim/config.py
117 118 119 120 121 122 123 | |
get_memory_warning ¶
get_memory_warning() -> Optional[str]
Return memory warning if parallel execution may cause issues.
Source code in tit/sim/config.py
130 131 132 133 134 135 136 137 138 139 140 | |
SimulationConfig
dataclass
¶
SimulationConfig(subject_id: str, project_dir: str, conductivity_type: ConductivityType, intensities: IntensityConfig, electrode: ElectrodeConfig, eeg_net: str = 'GSN-HydroCel-185.csv', map_to_surf: bool = True, map_to_vol: bool = True, map_to_mni: bool = True, map_to_fsavg: bool = False, tissues_in_niftis: str = 'all', open_in_gmsh: bool = False, parallel: ParallelConfig = ParallelConfig())
Main configuration for TI simulation.
__post_init__ ¶
__post_init__()
Convert string conductivity type to enum if needed.
Source code in tit/sim/config.py
160 161 162 163 164 165 | |
SimulationMode ¶
Bases: Enum
Simulation mode enumeration.
Simulator (tit.sim.simulator)¶
Unified TI/mTI simulation runner.
This module provides a single entry point for running both TI (2-pair) and mTI (4-pair) simulations. The simulation type is automatically detected based on montage configuration.
This refactored module includes all features from the original pipeline: - Montage visualization - SimNIBS simulation - Field extraction (GM/WM) - NIfTI transformation - T1 to MNI conversion - File organization
Parallel execution support: - Multiple montages can be simulated in parallel - Worker count is configurable (default: half of CPU cores)
create_simulation_config_file ¶
create_simulation_config_file(config: SimulationConfig, montage: MontageConfig, documentation_dir: str, logger) -> str
Create a config.json file with all simulation parameters.
This file is used by visualization tools to auto-populate parameters without requiring manual input.
Args: config: Simulation configuration montage: Montage configuration documentation_dir: Documentation directory path logger: Logger instance
Returns: Path to created config.json file
Source code in tit/sim/simulator.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
main ¶
main()
Command-line entry point for the simulator.
Parses command-line arguments, loads montages, runs simulations, and generates a completion report. This function is called when the module is executed directly or via the CLI wrapper script.
Command-line Arguments
- subject_id : str Subject identifier (e.g., '001')
- conductivity_str : str Conductivity type ('scalar', 'vn', 'dir', 'mc')
- project_dir : str Project directory path
- simulation_dir : str Simulation output directory (legacy, not used)
- mode : str Simulation mode (legacy, not used - auto-detected)
- intensity_str : str Current intensity string (e.g., '2.0' or '2.0,1.5' or '2.0,1.5,1.0,0.5')
- electrode_shape : str Electrode shape ('ellipse' or 'rect')
- dimensions : str Electrode dimensions as 'x,y' in mm (e.g., '8.0,8.0')
- thickness : float Gel thickness in mm
- eeg_net : str EEG cap name (e.g., 'EGI_template.csv') 11+. montage_names : str One or more montage names to simulate
Exit Codes
0 : All simulations completed successfully 1 : One or more simulations failed
Output
Creates simulation_completion_
Examples:
>>> python simulator.py 001 scalar /path/to/project /tmp TI 2.0 ellipse 8.0,8.0 4.0 EGI_template.csv montage1 montage2
Source code in tit/sim/simulator.py
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 | |
run_montage_visualization ¶
run_montage_visualization(montage_name: str, simulation_mode: SimulationMode, eeg_net: str, output_dir: str, project_dir: str, logger, electrode_pairs: Optional[List] = None) -> bool
Run montage visualization using visualize-montage.sh.
Args: montage_name: Name of the montage simulation_mode: TI or MTI mode eeg_net: EEG net name output_dir: Output directory for montage images project_dir: Project directory path logger: Logger instance electrode_pairs: Optional list of electrode pairs for direct visualization
Returns: True if successful, False otherwise
Source code in tit/sim/simulator.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
run_simulation ¶
run_simulation(config: SimulationConfig, montages: List[MontageConfig], logger=None, progress_callback: Optional[Callable[[int, int, str], None]] = None) -> List[dict]
Run TI/mTI simulations for given montages.
Supports both sequential and parallel execution based on config.parallel settings.
Args: config: Simulation configuration montages: List of montage configurations logger: Optional logger instance progress_callback: Optional callback for progress updates (current, total, montage_name)
Returns: List of dictionaries with simulation results
Source code in tit/sim/simulator.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
setup_montage_directories ¶
setup_montage_directories(montage_dir: str, simulation_mode: SimulationMode) -> dict
Create the complete directory structure for a montage simulation.
Args: montage_dir: Base montage directory simulation_mode: TI or MTI simulation mode
Returns: Dictionary of created directory paths
Source code in tit/sim/simulator.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
Session builder (tit.sim.session_builder)¶
SimNIBS session builder for TI simulations.
SessionBuilder ¶
SessionBuilder(config: SimulationConfig)
Builder class for constructing SimNIBS SESSION objects.
Initialize session builder.
Args: config: Simulation configuration
Source code in tit/sim/session_builder.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
build_session ¶
build_session(montage: MontageConfig, output_dir: str) -> sim_struct.SESSION
Build SimNIBS SESSION object for a montage.
Args: montage: Montage configuration output_dir: Output directory for simulation results
Returns: Configured SESSION object
Source code in tit/sim/session_builder.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
Post-processor (tit.sim.post_processor)¶
Post-processing utilities for TI simulations.
This module handles all post-simulation processing including: - TI/mTI field calculation - Field extraction (GM/WM mesh creation) - NIfTI transformation - T1 to MNI conversion - File organization
PostProcessor ¶
PostProcessor(subject_id: str, conductivity_type: str, m2m_dir: str, logger)
Post-processor for TI simulation results.
Initialize post-processor.
Args: subject_id: Subject identifier conductivity_type: Conductivity type string m2m_dir: Path to m2m directory logger: Logger instance
Source code in tit/sim/post_processor.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
process_mti_results ¶
process_mti_results(hf_dir: str, ti_dir: str, mti_dir: str, mti_nifti_dir: str, hf_mesh_dir: str, hf_analysis_dir: str, documentation_dir: str, montage_name: str) -> str
Process 4-pair mTI simulation results with full pipeline.
Args: hf_dir: High-frequency output directory ti_dir: TI intermediate output directory mti_dir: mTI final output directory mti_nifti_dir: mTI NIfTI output directory hf_mesh_dir: High-frequency mesh output directory hf_analysis_dir: High-frequency analysis output directory documentation_dir: Documentation output directory montage_name: Montage name
Returns: Path to output mTI mesh file
Source code in tit/sim/post_processor.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
process_ti_results ¶
process_ti_results(hf_dir: str, output_dir: str, nifti_dir: str, surface_overlays_dir: str, hf_mesh_dir: str, hf_nifti_dir: str, hf_analysis_dir: str, documentation_dir: str, montage_name: str) -> str
Process 2-pair TI simulation results with full pipeline.
Args: hf_dir: High-frequency output directory (SimNIBS writes here) output_dir: TI mesh output directory nifti_dir: TI NIfTI output directory surface_overlays_dir: Surface overlays output directory hf_mesh_dir: High-frequency mesh output directory hf_nifti_dir: High-frequency NIfTI output directory hf_analysis_dir: High-frequency analysis output directory documentation_dir: Documentation output directory montage_name: Montage name
Returns: Path to output TI mesh file
Source code in tit/sim/post_processor.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |