pareto
tit.opt.flex.pareto ¶
Pareto sweep module for focality threshold grid optimization.
This module provides the data structures and functions needed to run a Cartesian-product sweep over (ROI%, non-ROI%) threshold combinations for TI stimulation focality optimization. After all combinations are run, the results are saved as JSON, a PNG scatter plot, and an ASCII summary table.
All thresholds are expressed as percentages of achievable_ROI_mean --
the mean field in the target ROI at the mean-optimal electrode configuration
obtained in step 1 of the adaptive workflow.
SweepPoint
dataclass
¶
SweepPoint(roi_pct: float, nonroi_pct: float, roi_threshold: float, nonroi_threshold: float, run_index: int, output_folder: str, focality_score: float | None = None, status: str = 'pending')
One (roi_pct, nonroi_pct) combination in the sweep grid.
Attributes:
| Name | Type | Description |
|---|---|---|
roi_pct |
float
|
ROI threshold expressed as a percentage (e.g. 80.0). |
nonroi_pct |
float
|
Non-ROI threshold expressed as a percentage (e.g. 20.0). |
roi_threshold |
float
|
Absolute ROI threshold in V/m (= roi_pct / 100 * achievable_ROI_mean). |
nonroi_threshold |
float
|
Absolute non-ROI threshold in V/m. |
run_index |
int
|
0-based index; determines the subfolder name. |
output_folder |
str
|
Absolute path to this run's output directory. |
focality_score |
float | None
|
optim_funvalue returned by SimNIBS (negative float;
values closer to 0 are better focality). |
status |
str
|
One of |
ParetoSweepConfig
dataclass
¶
ParetoSweepConfig(roi_pcts: list, nonroi_pcts: list, achievable_roi_mean: float, base_output_folder: str)
Configuration for a full Pareto threshold sweep.
Attributes:
| Name | Type | Description |
|---|---|---|
roi_pcts |
list
|
List of ROI% values to sweep (e.g. [80.0, 70.0]). |
nonroi_pcts |
list
|
List of non-ROI% values to sweep (e.g. [20.0, 30.0, 40.0]). |
achievable_roi_mean |
float
|
Mean field strength in V/m from step-1 mean opt. |
base_output_folder |
str
|
Parent directory for all sweep run subdirectories. |
ParetoSweepResult
dataclass
¶
ParetoSweepResult(config: ParetoSweepConfig, points: list)
Result container for a completed (or in-progress) Pareto sweep.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
ParetoSweepConfig
|
The configuration used for this sweep. |
points |
list
|
Ordered list of :class: |
compute_sweep_grid ¶
compute_sweep_grid(roi_pcts: list, nonroi_pcts: list, achievable_roi_mean: float, base_output_folder: str) -> list
Return the Cartesian product of (roi_pct, nonroi_pct) as SweepPoints.
Ordering: roi_pcts outer loop, nonroi_pcts inner loop.
Invalid combinations (nonroi_pct >= roi_pct) are included here;
call :func:validate_grid before starting any runs to reject them early.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roi_pcts
|
list
|
Sequence of ROI threshold percentages. |
required |
nonroi_pcts
|
list
|
Sequence of non-ROI threshold percentages. |
required |
achievable_roi_mean
|
float
|
Mean field strength (V/m) from step-1 mean opt. |
required |
base_output_folder
|
str
|
Parent directory; each point gets a numbered
subdirectory named |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of :class: |
Source code in tit/opt/flex/pareto.py
validate_grid ¶
Raise ValueError if any combination has nonroi_pct >= roi_pct.
Rejects the entire grid rather than silently skipping bad rows, so the user is aware of all problematic combinations before any subprocess starts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roi_pcts
|
list
|
Sequence of ROI threshold percentages. |
required |
nonroi_pcts
|
list
|
Sequence of non-ROI threshold percentages. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Lists all invalid (roi_pct, nonroi_pct) pairs. |
Source code in tit/opt/flex/pareto.py
build_focality_config ¶
build_focality_config(base_config: FlexConfig, point: SweepPoint) -> FlexConfig
Return a new FlexConfig with focality goal and thresholds from point.
Creates a shallow copy of base_config and sets:
goalto"focality"thresholdsto"<nonroi_threshold>,<roi_threshold>"output_foldertopoint.output_folder
This decouples the Pareto sweep from subprocess command-line building
and allows calling run_flex_search() directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_config
|
FlexConfig
|
The template FlexConfig (unchanged). |
required |
point
|
SweepPoint
|
The sweep point whose thresholds should be applied. |
required |
Returns:
| Type | Description |
|---|---|
FlexConfig
|
A new :class: |
Source code in tit/opt/flex/pareto.py
build_pareto_manifest_data ¶
build_pareto_manifest_data(result: ParetoSweepResult) -> dict
Build the pareto section for flex_meta.json from a sweep result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ParetoSweepResult
|
Completed pareto sweep result. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict suitable for the 'pareto_data' parameter of write_manifest(). |
Source code in tit/opt/flex/pareto.py
generate_pareto_plot ¶
generate_pareto_plot(result: ParetoSweepResult, output_path: str) -> None
Save a Pareto trade-off scatter plot as a PNG.
- x-axis: non-ROI threshold as % of achievable ROI mean
- y-axis: focality score (optim_funvalue; higher/closer to 0 = better)
- One line/series per unique ROI% value, coloured by series
- Each point labelled with
(roi%, nonroi%) - Points with
status != "done"are skipped
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ParetoSweepResult
|
The sweep result containing all :class: |
required |
output_path
|
str
|
Absolute path where the PNG should be written. |
required |
Source code in tit/opt/flex/pareto.py
generate_summary_text ¶
generate_summary_text(result: ParetoSweepResult) -> str
Return a formatted ASCII table summarising all sweep points.
Columns: ROI% | NonROI% | ROI thr (V/m) | NR thr (V/m) | Score | Status
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ParetoSweepResult
|
The sweep result to summarise. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Multi-line string ready to write to a text file or print. |
Source code in tit/opt/flex/pareto.py
save_results ¶
save_results(result: ParetoSweepResult, output_folder: str) -> tuple
Persist the sweep results to disk and promote the best run's output.
Steps:
- Copy the best run's optimizer output files into output_folder.
- Delete all numbered sweep subdirectories.
- Write
pareto_results.jsonandpareto_sweep_plot.png.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ParetoSweepResult
|
The completed (or partially completed) sweep result. |
required |
output_folder
|
str
|
Parent pareto-sweep directory (created if necessary). |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|