Optimization (tit.opt)¶
Leadfield (tit.opt.leadfield)¶
Leadfield matrix generator for optimization and other applications Integrates with SimNIBS to create leadfield matrices
LeadfieldGenerator ¶
LeadfieldGenerator(subject_dir, electrode_cap='EEG10-10', progress_callback=None, termination_flag=None)
Generate and load leadfield matrices for TI optimization
This class provides a unified interface for leadfield generation and management, supporting both HDF5 and NPY formats with consistent naming conventions.
Initialize leadfield generator
Args: subject_dir: Path to subject directory (m2m folder) or subject_id electrode_cap: Electrode cap type (e.g., 'EEG10-10', 'GSN-256') progress_callback: Optional callback function(message, type) for progress updates termination_flag: Optional callable that returns True if generation should be terminated
Source code in tit/opt/leadfield.py
24 25 26 27 28 29 30 31 32 33 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 | |
cleanup_old_simulations ¶
cleanup_old_simulations()
Clean up old SimNIBS simulation files, temporary directories, and ROI mesh files.
Source code in tit/opt/leadfield.py
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 117 118 119 120 | |
generate_leadfield ¶
generate_leadfield(output_dir=None, tissues=[1, 2], eeg_cap_path=None, cleanup=True)
Generate leadfield matrix using SimNIBS
Args: output_dir: Output directory for leadfield (default: subject_dir) tissues: Tissue types to include [1=GM, 2=WM] eeg_cap_path: Path to EEG cap CSV file (optional, will look in eeg_positions if not provided) cleanup: Whether to clean up old simulation files before running (default: True)
Returns: dict: Dictionary with path {'hdf5': hdf5_path}
Source code in tit/opt/leadfield.py
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 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
get_electrode_names_from_cap ¶
get_electrode_names_from_cap(cap_name=None)
Extract electrode names from an EEG cap CSV file using simnibs csv_reader.
Args: cap_name: Name of EEG cap (will look in subject_dir/eeg_positions/). If None, uses self.electrode_cap.
Returns: list: List of electrode names
Source code in tit/opt/leadfield.py
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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
list_available_leadfields ¶
list_available_leadfields(subject_id=None)
List available leadfield HDF5 files for a subject.
Args: subject_id: Subject ID (optional, will use self.subject_id if not provided)
Returns: list: List of tuples (net_name, hdf5_path, file_size_gb)
Source code in tit/opt/leadfield.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
list_available_leadfields_hdf5 ¶
list_available_leadfields_hdf5(subject_id=None)
List available leadfield HDF5 files for a subject.
Args: subject_id: Subject ID (optional, will use self.subject_id if not provided)
Returns: list: List of tuples (net_name, hdf5_path, file_size_gb)
Source code in tit/opt/leadfield.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 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 | |
Flex search (tit.opt.flex)¶
Flex-search optimization package for TI stimulation.
This package provides flexible optimization for temporal interference (TI) stimulation with support for different ROI definitions and optimization goals.
Modules: flex_config: Configuration and optimization setup flex_log: Logging utilities and progress tracking multi_start: Multi-start optimization logic and result management flex: Main optimization orchestration script
Note: roi module is now located at core.roi (shared across optimization approaches)
Main API (tit.opt.flex.flex)¶
Main flex-search script for TI stimulation optimization.
This script orchestrates the flexible search optimization process for temporal interference (TI) stimulation. It supports: - Multiple ROI methods (spherical, atlas-based, subcortical) - Multiple optimization goals (mean, max, focality) - Multi-start optimization for robust results - Optional electrode mapping to EEG cap positions
main ¶
main() -> int
Main entry point for flex-search optimization.
Returns: Exit code (0 for success, 1 for failure)
Source code in tit/opt/flex/flex.py
25 26 27 28 29 30 31 32 33 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 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 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 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 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
Config (tit.opt.flex.flex_config)¶
Configuration and optimization setup for flex-search.
This module handles: - Argument parsing - Optimization object configuration - Electrode setup - Output directory structure
build_optimization ¶
build_optimization(args: argparse.Namespace) -> opt_struct.TesFlexOptimization
Set up optimization object with all parameters.
Args: args: Parsed command line arguments
Returns: Configured SimNIBS optimization object
Raises: SystemExit: If required environment variables or files are missing
Source code in tit/opt/flex/flex_config.py
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 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 209 210 211 212 213 214 215 | |
configure_optimizer_options ¶
configure_optimizer_options(opt: opt_struct.TesFlexOptimization, args: argparse.Namespace, logger) -> None
Configure optimizer options for the optimization object.
Args: opt: SimNIBS optimization object args: Parsed command line arguments logger: Logger instance
Source code in tit/opt/flex/flex_config.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
parse_arguments ¶
parse_arguments() -> argparse.Namespace
Parse command line arguments.
Returns: Parsed arguments namespace
Source code in tit/opt/flex/flex_config.py
24 25 26 27 28 29 30 31 32 33 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
Ex search (tit.opt.ex)¶
TI Exhaustive Search Module
A streamlined implementation for TI exhaustive search simulations.
calculate_total_combinations ¶
calculate_total_combinations(e1_plus, e1_minus, e2_plus, e2_minus, current_ratios, all_combinations)
Calculate total number of montage combinations to be tested.
Args: e1_plus (list): E1+ electrode names e1_minus (list): E1- electrode names e2_plus (list): E2+ electrode names e2_minus (list): E2- electrode names current_ratios (list): List of (ch1_current, ch2_current) tuples all_combinations (bool): If True, test all valid electrode combinations
Returns: int: Total number of combinations to test
Source code in tit/opt/ex/logic.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
create_roi_from_coordinates ¶
create_roi_from_coordinates(subject_id: str, roi_name: str, x: float, y: float, z: float) -> Tuple[bool, str]
Create an ROI from custom coordinates.
Args: subject_id: Subject identifier roi_name: Name for the ROI file (without .csv extension) x, y, z: Coordinates in subject space (RAS)
Returns: Tuple of (success: bool, message: str)
Source code in tit/opt/ex/roi_utils.py
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 | |
create_roi_from_preset ¶
create_roi_from_preset(subject_id: str, roi_name: str, preset_key: str, presets: Optional[Dict[str, Dict]] = None) -> Tuple[bool, str]
Create an ROI from a preset.
Args: subject_id: Subject identifier roi_name: Name for the ROI file (without .csv extension) preset_key: Key of the preset to use presets: Optional preset dictionary (will load if not provided)
Returns: Tuple of (success: bool, message: str)
Source code in tit/opt/ex/roi_utils.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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
delete_roi ¶
delete_roi(subject_id: str, roi_name: str) -> Tuple[bool, str]
Delete an ROI file.
Args: subject_id: Subject identifier roi_name: Name of the ROI file to delete
Returns: Tuple of (success: bool, message: str)
Source code in tit/opt/ex/roi_utils.py
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 | |
generate_current_ratios ¶
generate_current_ratios(total_current, current_step, channel_limit)
Generate valid current ratio combinations for TI stimulation.
Args: total_current (float): Total current in milliamps current_step (float): Step size for current increments in milliamps channel_limit (float): Maximum current per channel in milliamps
Returns: tuple: (ratios, channel_limit_exceeded) - ratios: List of (ch1_current, ch2_current) tuples - channel_limit_exceeded: Boolean indicating if limit was exceeded
Source code in tit/opt/ex/logic.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
generate_montage_combinations ¶
generate_montage_combinations(e1_plus, e1_minus, e2_plus, e2_minus, current_ratios, all_combinations)
Generate electrode montage combinations for testing.
Args: e1_plus (list): E1+ electrode names e1_minus (list): E1- electrode names e2_plus (list): E2+ electrode names e2_minus (list): E2- electrode names current_ratios (list): List of (ch1_current, ch2_current) tuples all_combinations (bool): If True, generate all valid electrode combinations
Yields: tuple: (e1_plus, e1_minus, e2_plus, e2_minus, current_ch1, current_ch2)
Source code in tit/opt/ex/logic.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
get_available_rois ¶
get_available_rois(subject_id: str) -> List[str]
Get list of available ROIs for a subject.
Source code in tit/opt/ex/roi_utils.py
40 41 42 43 44 45 46 47 48 49 | |
get_roi_coordinates ¶
get_roi_coordinates(subject_id: str, roi_name: str) -> Optional[Tuple[float, float, float]]
Get coordinates for an ROI.
Args: subject_id: Subject identifier roi_name: Name of the ROI file
Returns: Tuple of (x, y, z) coordinates or None if not found
Source code in tit/opt/ex/roi_utils.py
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 | |
load_roi_presets ¶
load_roi_presets() -> Dict[str, Dict]
Load ROI presets from the roi_presets.json file.
Source code in tit/opt/ex/roi_utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |