Python API
PSPSO 1.0 exposes one typed Python API. Use optimize() for concise notebook
work and PSPSOOptimizer when you need to retain or inspect the optimizer
instance.
Notebook Helper
pspso.optimize
optimize(
X: Any,
y: Any,
*,
estimator: str
| EstimatorConfig
| Callable[..., Any] = "svm",
search_space: SearchSpace
| Mapping[str, ParameterSpec]
| None = None,
config: OptimizationConfig | None = None,
X_validation: Any | None = None,
y_validation: Any | None = None,
progress_callback: ProgressCallback | None = None,
should_cancel: Callable[[], bool] | None = None,
tracking: TrackingConfig | None = None,
) -> OptimizationResult
Optimize an estimator directly from Python or a notebook.
Tracking is disabled unless tracking is supplied. Tracked calls write to
the same versioned workspace used by the dashboard and CLI, but execute in
the current Python process and do not require FastAPI.
Source code in pspso/api.py
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 | |
pspso.TrackingConfig
dataclass
TrackingConfig(
workspace: str | Path | None = None,
experiment_name: str = "Notebook experiment",
experiment_id: str | None = None,
run_name: str | None = None,
tags: tuple[str, ...] = tuple(),
snapshot_data: bool = False,
)
Opt-in persistence settings for an in-process Python optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace
|
str | Path | None
|
Directory containing the 1.0 tracking database and artifacts. |
None
|
experiment_name
|
str
|
Name used when creating a new experiment. |
'Notebook experiment'
|
experiment_id
|
str | None
|
Existing experiment to receive the run. |
None
|
run_name
|
str | None
|
Optional human-readable label stored in the run specification. |
None
|
tags
|
tuple[str, ...]
|
Tags applied when a new experiment is created. |
tuple()
|
snapshot_data
|
bool
|
Save the supplied training and validation objects with the run. |
False
|
Optimizer
pspso.PSPSOOptimizer
PSPSOOptimizer(
estimator: str
| EstimatorConfig
| Callable[..., Any] = "svm",
search_space: SearchSpace
| Mapping[str, ParameterSpec]
| None = None,
config: OptimizationConfig | None = None,
)
Hyperparameter optimizer with PSO, grid, and random strategies.
Source code in pspso/optimizer.py
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 | |
optimize
optimize(
X: Any,
y: Any,
X_val: Any | None = None,
y_val: Any | None = None,
progress_callback: ProgressCallback | None = None,
should_cancel: Callable[[], bool] | None = None,
defer_terminal_event: bool = False,
) -> OptimizationResult
Run the configured hyperparameter optimization.
Source code in pspso/optimizer.py
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 | |
Search Space
pspso.SearchSpace
SearchSpace(params: Mapping[str, ParameterSpec])
Ordered collection of tunable hyperparameters.
Source code in pspso/search_space.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
grid_size
property
grid_size: int
Return the exact number of combinations produced by :meth:iter_grid.
to_schema
to_schema() -> dict[str, dict[str, Any]]
Return the canonical JSON representation used by API v1.
Source code in pspso/search_space.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
pspso.Choice
dataclass
Choice(values: Sequence[Any])
A categorical hyperparameter.
Source code in pspso/search_space.py
19 20 21 22 | |
pspso.IntRange
dataclass
IntRange(low: int, high: int)
An integer hyperparameter.
pspso.FloatRange
dataclass
FloatRange(low: float, high: float, precision: int = 2)
A continuous numeric hyperparameter rounded to a fixed precision.
pspso.LogFloatRange
dataclass
LogFloatRange(low: float, high: float, precision: int = 2)
Config And Results
pspso.OptimizationConfig
dataclass
OptimizationConfig(
task: TaskName = "regression",
metric: MetricName = "rmse",
strategy: StrategyName = "pso",
validation_size: float = 0.2,
test_size: float = 0.0,
random_state: int | None = None,
n_particles: int = 5,
n_iterations: int = 10,
pso_options: Mapping[str, float] = (
lambda: {"c1": 1.49618, "c2": 1.49618, "w": 0.7298}
)(),
pso_topology: Literal["global", "local"] = "global",
max_trials: int | None = None,
timeout_seconds: float | None = None,
early_stopping_rounds: int | None = None,
evaluation_protocol: EvaluationProtocol = "holdout",
cv_folds: int = 5,
shuffle_folds: bool = True,
time_series: bool = False,
time_gap: int = 0,
stratify: bool = True,
positive_label: str | int | float | bool | None = None,
decision_threshold: float = 0.5,
refit_best: bool = True,
trial_workers: int = 1,
verbose: int = 0,
)
Runtime settings for an optimization run.
pspso.EstimatorConfig
dataclass
EstimatorConfig(
name: str | None = None,
factory: Callable[..., Any] | None = None,
fixed_params: Mapping[str, Any] = dict(),
)
Estimator name/factory plus parameters fixed across all trials.
pspso.OptimizationResult
dataclass
OptimizationResult(
best_params: Mapping[str, Any] | None,
best_cost: float | None,
best_metric: float | None,
model: Any,
duration: float,
trials: list[TrialResult],
strategy: str,
task: str,
metric_name: str,
failures: list[str] = list(),
best_position: list[float] | None = None,
optimizer_state: dict[str, Any] = dict(),
run_id: str | None = None,
experiment_id: str | None = None,
artifacts: Mapping[str, str] = dict(),
)
Final result returned by the modern optimizer.
evaluate
evaluate(X: Any, y: Any) -> dict[str, Any]
Evaluate the fitted best model with the run's task and metric.
Source code in pspso/config.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
predict
predict(X: Any) -> Any
Predict with the fitted best model.
Source code in pspso/config.py
232 233 234 235 236 237 | |
predict_proba
predict_proba(X: Any) -> Any
Return probabilities when the fitted best model supports them.
Source code in pspso/config.py
239 240 241 242 243 244 245 246 | |
summary
summary() -> str
Return a compact human-readable summary of the optimization.
Source code in pspso/config.py
185 186 187 188 189 190 191 192 193 194 | |
trials_as_rows
trials_as_rows() -> list[dict[str, Any]]
Return JSON-serializable trial dictionaries.
Source code in pspso/config.py
286 287 288 289 | |
trials_frame
trials_frame()
Return trial results as a pandas DataFrame.
Source code in pspso/config.py
219 220 221 222 223 224 225 226 227 228 229 230 | |
pspso.TrialResult
dataclass
TrialResult(
trial_id: int,
params: Mapping[str, Any],
cost: float | None,
metric: float | None,
train_metric: float | None,
status: Literal["completed", "failed"],
duration: float,
error: str | None = None,
iteration: int | None = None,
particle_index: int | None = None,
strategy_slot: int | None = None,
worker_slot: int | None = None,
validation_metrics: Mapping[str, float | None] = dict(),
train_metrics: Mapping[str, float | None] = dict(),
metric_std: float | None = None,
fold_metrics: list[dict[str, Any]] = list(),
failure_category: str | None = None,
model: Any = None,
)
A single evaluated parameter set.
Model Discovery
pspso.list_estimators
list_estimators(task: str | None = None) -> list[str]
List canonical estimator IDs, optionally filtered by task.
Source code in pspso/estimators.py
262 263 264 265 266 267 268 | |
pspso.get_estimator_info
get_estimator_info(name: str) -> dict[str, Any]
Return capabilities, dependency status, and typed defaults for an estimator.
Source code in pspso/estimators.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
Recipe Registration
pspso.register_recipe
register_recipe(
name: str,
factory: Callable[..., Any],
*,
tasks: list[str],
description: str,
fixed_params: Mapping[str, Mapping[str, Any]]
| None = None,
search_spaces: Mapping[
str, SearchSpace | Mapping[str, ParameterSpec]
]
| None = None,
) -> None
Register a trusted recipe with typed task-specific search spaces.
Source code in pspso/recipes.py
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 | |