Experiment
Experiment
dataclass
¶
Class to manage optimization experiments.
This class provides a structured way to manage optimization experiments, including logging of problems, instances, solutions, and parameters. It supports both experiment-wide data and run-specific data, with automatic saving capabilities and artifact creation.
Attributes:
Name | Type | Description |
---|---|---|
name | str | Name of the experiment. |
savedir | Path | Directory path for saving experiment data. |
auto_saving | bool | Flag to enable automatic saving of experiment data. |
timestamp | datetime | Timestamp of the experiment creation. |
_running | bool | Flag to track the current run status. |
_run_id | int | ID of the current run. |
Properties
experiment_name (str): Full name of the experiment with timestamp. database (Database): Database instance for storing experiment data.
Source code in minto/experiment.py
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 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 330 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 365 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 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
|
__enter__()
¶
Enter method for the context manager.
Returns:
Name | Type | Description |
---|---|---|
Experiment | Experiment | Instance of the current experiment run. |
__exit__(exc_type, exc_value, traceback)
¶
Exit method for the context manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc_type | exception type | required | |
exc_value | exception value | required | |
traceback | traceback information | required |
__post_init__()
¶
Post-initialization method for Experiment class.
- Add timestamp to the experiment name.
- Initialize the database instance.
- Create the OMMX artifact builder.
Source code in minto/experiment.py
close_run()
¶
Close the current experiment run.
This method should be called to properly end the current run.
Source code in minto/experiment.py
concat(experiments, name=None, savedir=DEFAULT_RESULT_DIR, auto_saving=True)
classmethod
¶
Concatenate multiple experiments into a single experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiments | list[Experiment] | List of Experiment instances to concatenate. | required |
name | Optional[str] | Name of the concatenated experiment. | None |
savedir | str | Path | Directory path for saving the concatenated experiment data. | DEFAULT_RESULT_DIR |
auto_saving | bool | Flag to enable automatic saving of the concatenated experiment | True |
Example
import minto
exp1 = minto.Experiment("exp1")
exp2 = minto.Experiment("exp2")
exp3 = minto.Experiment("exp3")
new_exp = minto.Experiment.concat([exp1, exp2, exp3])
Returns:
Name | Type | Description |
---|---|---|
Experiment | Experiment | Instance of the concatenated experiment. |
Source code in minto/experiment.py
get_current_datastore()
¶
get_experiment_tables()
¶
Get the experiment data as a table.
Returns:
Type | Description |
---|---|
dict[str, DataFrame] | pd.DataFrame: DataFrame containing the experiment data. |
Source code in minto/experiment.py
get_run_table()
¶
Get the run data as a table.
Returns:
Type | Description |
---|---|
DataFrame | pd.DataFrame: DataFrame containing the run data. |
Source code in minto/experiment.py
load_from_dir(savedir)
classmethod
¶
Load an experiment from a directory containing saved data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
savedir | str | Path | Directory path containing the saved experiment data. | required |
Returns:
Name | Type | Description |
---|---|---|
Experiment | Experiment | Instance of the loaded experiment. |
Source code in minto/experiment.py
load_from_ommx_archive(savefile)
classmethod
¶
Load an experiment from an OMMX artifact file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
savefile | str | Path | Path to the OMMX artifact file. | required |
Returns:
Name | Type | Description |
---|---|---|
Experiment | Experiment | Instance of the loaded experiment. |
Source code in minto/experiment.py
load_from_registry(imagename)
classmethod
¶
Load an experiment from a Docker registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
imagename | str | Name of the Docker image containing the experiment data. | required |
Returns:
Name | Type | Description |
---|---|---|
Experiment | Experiment | Instance of the loaded experiment. |
Source code in minto/experiment.py
log_data(name, data, storage_name)
¶
Log data to the experiment database.
This method is not intended to be called directly by the user. Instead, users should use other log_*
methods. Other log_*
methods wrap this method to save data to the dataspace. If the current experiment is not running, the data is saved to .dataspace.experiment_datastore
. If the current experiment is running, the data is saved to .dataspace.run_datastores[self._run_id]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | Name of the data | required |
data | Any | Data object to be saved | required |
storage_name | str | Name of the storage object | required |
Source code in minto/experiment.py
log_instance(instance_name, instance=None)
¶
Log an optimization problem instance to the experiment database.
Logs an ommx.v1.Instance to the experiment database. If instance_name is not specified, it will be named sequentially as "0", "1", "2", etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_name | str | Instance | Name of the instance, or the instance object itself. | required |
instance | Optional[Instance] | Instance object (if instance_name is a string). | None |
Example
import minto
exp = minto.Experiment("exp1")
instance = ommx_v1.Instance()
exp.log_instance("instance1", instance)
Source code in minto/experiment.py
log_object(name, value)
¶
Log a custom object to the experiment database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | Name of the object | required |
value | dict[str, Any] | Dictionary containing the object data | required |
If a run is active, the object is saved with the current run ID.
Source code in minto/experiment.py
log_parameter(name, value)
¶
Log a parameter to the experiment database.
This method allows logging of both simple scalar values (float, int, str) and complex data structures (list, dict, numpy.ndarray) as parameters. Complex data structures are first checked for JSON serializability and then stored as objects with a "parameter_" prefix to distinguish them from regular parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | Name of the parameter. Must be a string that uniquely identifies the parameter within the experiment. | required |
value | Union[float, int, str, list, dict, ndarray] | Value of the parameter. Can be one of the following types: - float: Floating point numbers - int: Integer numbers - str: String values - list: Python lists (must be JSON serializable) - dict: Python dictionaries (must be JSON serializable) - numpy.ndarray: NumPy arrays (must be JSON serializable) | required |
Raises:
Type | Description |
---|---|
ValueError | If a complex data structure (list, dict, numpy.ndarray) is not JSON serializable. This can happen if the structure contains objects that cannot be converted to JSON (e.g., custom objects, functions). |
TypeError | If the name is not a string or if the value is not one of the supported types. |
Note
- For complex data structures (list, dict, numpy.ndarray), the value is stored both as a parameter and as an object with a "parameter_" prefix.
- If the experiment is running (i.e., within a run context), the parameter is saved with the current run ID. Otherwise, it is saved as experiment-wide data.
- NumPy arrays are converted to nested lists when serialized to JSON.
Example
exp = Experiment("example")
# Logging simple scalar values
exp.log_parameter("learning_rate", 0.001)
exp.log_parameter("batch_size", 32)
# Logging complex data structures
exp.log_parameter("layer_sizes", [64, 128, 64])
exp.log_parameter("model_config", {"activation": "relu", "dropout": 0.5})
exp.log_parameter("weights", np.array([1.0, 2.0, 3.0]))
Source code in minto/experiment.py
log_params(params)
¶
Log multiple parameters to the experiment database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params | dict[str, float | int] | Dictionary of parameter names and values. | required |
If a run is active, the parameters are saved with the current run ID. Else, they are saved as experiment-wide data.
Source code in minto/experiment.py
log_problem(problem_name, problem=None)
¶
Log an optimization problem to the experiment database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
problem_name | str | Problem | Name of the problem, or the problem object itself. | required |
problem | Optional[Problem] | Problem object (if problem_name is a string). | None |
If a run is active, the problem is saved with the current run ID.
Source code in minto/experiment.py
log_sampleset(name, value=None)
¶
Log a SampleSet to the experiment or run database.
Source code in minto/experiment.py
log_solution(solution_name, solution=None)
¶
Log an optimization solution to the experiment database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
solution_name | str | Solution | Name of the solution, or the solution object itself. | required |
solution | Optional[Solution] | Solution object (if solution_name is a string). | None |
If a run is active, the solution is saved with the current run ID.
Source code in minto/experiment.py
log_solver(solver, *, exclude_params=None)
¶
Log solver name and parameters to the dataspace.
When the wrapped solver function is called, the following actions are performed:
- The solver name is logged as a parameter with
.log_parameter("solver_name", solver_name)
. - Each keyword argument passed to the solver function is logged as a parameter if it is of type int, float, or str.
- If a keyword argument is of type
jm.Problem
, it is logged as a problem. - If a keyword argument is of type
ommx_v1.Instance
, it is logged as an instance. - After the solver function executes, the result is logged as a sampleset if it is of type
ommx_v1.SampleSet
,jm.SampleSet
, orjm.experimental.SampleSet
. - If the result is of type
ommx_v1.Solution
, it is logged as a solution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
solver | Callable[P, R] | Solver function to be logged. | required |
Returns:
Type | Description |
---|---|
Callable[P, R] | typ.Callable[P, R]: Wrapped solver function. |
Example
import minto
exp = minto.Experiment("exp1")
result = exp.log_solver(solver)(parameter=1)
exp.dataspace.experiment_datastore.parameters
# {'solver_name': 'solver', 'parameter': 1}
Source code in minto/experiment.py
push_github(org, repo, name=None, tag=None)
¶
Push the experiment data to a GitHub repository.
Returns:
Type | Description |
---|---|
Artifact | ox_artifact.Artifact: OMMX artifact containing the experiment data. |
Source code in minto/experiment.py
run()
¶
Start a new experiment run.
Returns:
Name | Type | Description |
---|---|---|
Experiment | Experiment | Instance of the current experiment run. |
Source code in minto/experiment.py
save(path=None)
¶
Save the experiment database to the disk.
save_as_ommx_archive(savefile=None)
¶
Save the experiment data as an OMMX artifact.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
savefile | Optional[str | Path] | Path to save the OMMX artifact. If None, a default name is generated. | None |