Skip to content

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
@dataclass
class Experiment:
    """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 (str): Name of the experiment.
        savedir (pathlib.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.
    """

    name: str = field(default_factory=lambda: str(uuid.uuid4().hex[:8]))
    savedir: pathlib.Path = DEFAULT_RESULT_DIR
    auto_saving: bool = True

    timestamp: datetime = field(default_factory=datetime.now, init=False)
    _running: bool = field(default=False, init=False)
    _run_id: int = field(default=0, init=False)

    def __post_init__(self):
        """Post-initialization method for Experiment class.

        - Add timestamp to the experiment name.
        - Initialize the database instance.
        - Create the OMMX artifact builder.
        """
        # 実験名にタイムスタンプを追加
        self.experiment_name = f"{self.name}_{self.timestamp.strftime('%Y%m%d%H%M%S')}"
        # 保存ディレクトリのパスを設定
        self.savedir = pathlib.Path(self.savedir) / self.experiment_name
        # 自動保存が有効な場合、ディレクトリを作成
        if self.auto_saving:
            self.savedir.mkdir(exist_ok=True, parents=True)
        # データベースインスタンスの初期化
        self.dataspace = ExperimentDataSpace(self.name)

    def run(self) -> "Experiment":
        """Start a new experiment run.

        Returns:
            Experiment: Instance of the current experiment run.
        """
        self._running = True
        self._run_id = self.dataspace.add_run_datastore(
            DataStore(), with_save=self.auto_saving, save_dir=self.savedir
        )
        self._run_start_time = datetime.now()
        return self

    def close_run(self):
        """Close the current experiment run.

        This method should be called to properly end the current run.
        """
        run_time = datetime.now() - self._run_start_time
        elapsed_time = run_time.total_seconds()
        self.log_data(
            "elapsed_time",
            elapsed_time,
            "meta_data",
        )

        self._running = False
        self._run_id += 1

    def __enter__(self) -> "Experiment":
        """Enter method for the context manager.

        Returns:
            Experiment: Instance of the current experiment run.
        """
        if self._running:
            return self
        else:
            return self.run()

    def __exit__(self, exc_type, exc_value, traceback):
        """Exit method for the context manager.

        Args:
            exc_type: exception type
            exc_value: exception value
            traceback: traceback information
        """
        self.close_run()

    def save(self, path: typ.Optional[str | pathlib.Path] = None):
        """Save the experiment database to the disk."""
        if path is not None:
            self.dataspace.save_dir(path)
        else:
            self.dataspace.save_dir(self.savedir)

    def get_current_datastore(self) -> "DataStore":
        """Get the current DataStore object."""
        if self._running:
            return self.dataspace.run_datastores[self._run_id]
        else:
            return self.dataspace.experiment_datastore

    def log_data(
        self,
        name: str,
        data: typ.Any,
        storage_name: str,
    ):
        """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]`.

        Args:
            name: Name of the data
            data: Data object to be saved
            storage_name: Name of the storage object
        """
        if not self._running:
            self.dataspace.add_exp_data(
                name,
                data,
                storage_name,
                with_save=self.auto_saving,
                save_dir=self.savedir,
            )
        else:
            self.dataspace.add_run_data(
                self._run_id,
                name,
                data,
                storage_name,
                with_save=self.auto_saving,
                save_dir=self.savedir,
            )

    def _get_name_or_default(
        self, name: str | T, obj: typ.Optional[T], storage: dict
    ) -> tuple[str, T]:
        """Get the name or generate a default name if the object is provided.

        Args:
            name: Name of the object or the type of the object.
            obj: The object itself (optional).

        Returns:
            str: The name or a generated default name.
        """
        if not isinstance(name, str) and obj is None:
            return str(len(storage)), name
        elif isinstance(name, str) and (obj is not None):
            return name, obj
        else:
            raise ValueError(
                "Invalid arguments: name must be a string or obj must be provided."
            )

    def log_problem(
        self, problem_name: str | jm.Problem, problem: typ.Optional[jm.Problem] = None
    ):
        """Log an optimization problem to the experiment database.

        Args:
            problem_name: Name of the problem, or the problem object itself.
            problem: Problem object (if problem_name is a string).

        If a run is active, the problem is saved with the current run ID.
        """
        datastore = self.get_current_datastore()
        _name, _problem = self._get_name_or_default(
            problem_name, problem, datastore.problems
        )
        self.log_data(
            _name,
            _problem,
            "problems",
        )

    def log_instance(
        self,
        instance_name: str | ommx_v1.Instance,
        instance: typ.Optional[ommx_v1.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.

        Args:
            instance_name: Name of the instance, or the instance object itself.
            instance: Instance object (if instance_name is a string).

        Example:
            ```python
            import minto
            exp = minto.Experiment("exp1")
            instance = ommx_v1.Instance()
            exp.log_instance("instance1", instance)
            ```
        """
        datastore = self.get_current_datastore()
        _name, _instance = self._get_name_or_default(
            instance_name, instance, datastore.instances
        )
        self.log_data(
            _name,
            _instance,
            "instances",
        )

    def log_solution(
        self,
        solution_name: str | ommx_v1.Solution,
        solution: typ.Optional[ommx_v1.Solution] = None,
    ):
        """Log an optimization solution to the experiment database.

        Args:
            solution_name: Name of the solution, or the solution object itself.
            solution: Solution object (if solution_name is a string).

        If a run is active, the solution is saved with the current run ID.
        """
        datastore = self.get_current_datastore()
        _name, _solution = self._get_name_or_default(
            solution_name, solution, datastore.solutions
        )
        self.log_data(_name, _solution, "solutions")

    def log_parameter(
        self,
        name: str,
        value: float | int | str | list | dict | np.ndarray,
    ):
        """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.

        Args:
            name (str): Name of the parameter. Must be a string that uniquely identifies
                the parameter within the experiment.
            value (Union[float, int, str, list, dict, numpy.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)

        Raises:
            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:
            ```python
            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]))
            ```
        """
        if isinstance(value, (list, dict, np.ndarray)):
            # Check value is serializable
            try:
                from minto.v1.json_encoder import NumpyEncoder

                json.dumps(value, cls=NumpyEncoder)
            except TypeError:
                raise ValueError(f"Value is not serializable.")

            self.log_object(name, {"paramter_" + name: value})

        self.log_data(name, value, "parameters")

    def log_params(self, params: dict[str, float | int]):
        """Log multiple parameters to the experiment database.

        Args:
            params: Dictionary of parameter names and values.

        If a run is active, the parameters are saved with the current run ID.
        Else, they are saved as experiment-wide data.
        """
        for name, value in params.items():
            self.log_parameter(name, value)

    def log_object(self, name: str, value: dict[str, typ.Any]):
        """Log a custom object to the experiment database.

        Args:
            name: Name of the object
            value: Dictionary containing the object data

        If a run is active, the object is saved with the current run ID.
        """
        type_check([("name", name, str), ("value", value, dict)])
        self.log_data(name, value, "objects")

    def log_sampleset(
        self,
        name: str | ommx_v1.SampleSet | jm.SampleSet | jm.experimental.SampleSet,
        value: typ.Optional[ommx_v1.SampleSet] = None,
    ):
        """Log a SampleSet to the experiment or run database."""

        # If name is specified, use it as the name.
        # If name is not specified and a SampleSet is passed as the first argument,
        # use the variable name of the SampleSet as the name.
        datastore = self.get_current_datastore()
        _name, _value = self._get_name_or_default(name, value, datastore.samplesets)

        if isinstance(_value, (jm.SampleSet, jm.experimental.SampleSet)):
            _value = convert_sampleset_jijmodeling_to_ommx(_value)

        self.log_data(_name, _value, "samplesets")

    def log_solver(
        self,
        solver: typ.Callable[P, R],
        *,
        exclude_params: typ.Optional[list[str]] = None,
    ) -> typ.Callable[P, R]:
        """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`, or `jm.experimental.SampleSet`.
        - If the result is of type `ommx_v1.Solution`, it is logged as a solution.

        Args:
            solver: Solver function to be logged.

        Returns:
            typ.Callable[P, R]: Wrapped solver function.

        Example:
            ```python
            import minto
            exp = minto.Experiment("exp1")
            result = exp.log_solver(solver)(parameter=1)
            exp.dataspace.experiment_datastore.parameters
            # {'solver_name': 'solver', 'parameter': 1}
            ```
        """
        solver_name = solver.__name__
        self.log_parameter("solver_name", solver_name)

        exclude_params = exclude_params or []

        @wraps(solver)
        def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
            for name, value in kwargs.items():
                if name in exclude_params:
                    continue
                if isinstance(value, (int, float, str)):
                    self.log_parameter(name, value)
                elif isinstance(value, jm.Problem):
                    self.log_problem(name, value)
                elif isinstance(value, ommx_v1.Instance):
                    self.log_instance(name, value)

            result = solver(*args, **kwargs)

            if isinstance(
                result, (ommx_v1.SampleSet, jm.SampleSet, jm.experimental.SampleSet)
            ):
                self.log_sampleset(solver_name + "_result", result)
            elif isinstance(result, ommx_v1.Solution):
                self.log_solution(solver_name + "_result", result)
            return result

        return typ.cast(typ.Callable[P, R], wrapper)

    @classmethod
    def load_from_dir(cls, savedir: str | pathlib.Path) -> "Experiment":
        """Load an experiment from a directory containing saved data.

        Args:
            savedir: Directory path containing the saved experiment data.

        Returns:
            Experiment: Instance of the loaded experiment.
        """
        savedir = pathlib.Path(savedir)

        # check directory exists
        if not savedir.exists():
            raise FileNotFoundError(f"Directory not found: {savedir}")

        dataspace = ExperimentDataSpace.load_from_dir(savedir)
        exp_name = dataspace.experiment_datastore.meta_data["experiment_name"]
        experiment = cls(exp_name, auto_saving=False)
        experiment.dataspace = dataspace
        return experiment

    def save_as_ommx_archive(
        self, savefile: typ.Optional[str | pathlib.Path] = None
    ) -> ox_artifact.Artifact:
        """Save the experiment data as an OMMX artifact.

        Args:
            savefile: Path to save the OMMX artifact. If None, a default name is generated.
        """
        if savefile is None:
            # デフォルトのファイル名を生成(タイムスタンプ付き)
            savefile = (
                self.savedir
                / f"{self.name}_{self.timestamp.strftime('%Y%m%d%H%M%S')}.ommx"
            )
        builder = ox_artifact.ArtifactBuilder.new_archive_unnamed(savefile)
        self.dataspace.add_to_artifact_builder(builder)
        return builder.build()

    @classmethod
    def load_from_ommx_archive(cls, savefile: str | pathlib.Path) -> "Experiment":
        """Load an experiment from an OMMX artifact file.

        Args:
            savefile: Path to the OMMX artifact file.

        Returns:
            Experiment: Instance of the loaded experiment.
        """
        dataspace = ExperimentDataSpace.load_from_ommx_archive(savefile)
        exp_name = dataspace.experiment_datastore.meta_data["experiment_name"]
        experiment = cls(exp_name, auto_saving=False)
        experiment.dataspace = dataspace
        return experiment

    def get_run_table(self) -> pd.DataFrame:
        """Get the run data as a table.

        Returns:
            pd.DataFrame: DataFrame containing the run data.
        """
        run_table = create_table_from_stores(self.dataspace.run_datastores)
        run_table.index.name = "run_id"
        return run_table

    def get_experiment_tables(self) -> dict[str, pd.DataFrame]:
        """Get the experiment data as a table.

        Returns:
            pd.DataFrame: DataFrame containing the experiment data.
        """
        exp_table = create_table(self.dataspace.experiment_datastore)
        return exp_table

    def push_github(
        self,
        org: str,
        repo: str,
        name: typ.Optional[str] = None,
        tag: typ.Optional[str] = None,
    ) -> ox_artifact.Artifact:
        """Push the experiment data to a GitHub repository.

        Returns:
            ox_artifact.Artifact: OMMX artifact containing the experiment data.
        """
        builder = ox_artifact.ArtifactBuilder.for_github(
            org=org,
            repo=repo,
            name=name if name else self.name,
            tag=tag if tag else self.timestamp.strftime("%Y%m%d%H%M%S"),
        )
        self.dataspace.add_to_artifact_builder(builder)
        artifact = builder.build()
        artifact.push()
        return artifact

    @classmethod
    def load_from_registry(cls, imagename: str) -> "Experiment":
        """Load an experiment from a Docker registry.

        Args:
            imagename: Name of the Docker image containing the experiment data.

        Returns:
            Experiment: Instance of the loaded experiment.
        """
        artifact = ox_artifact.Artifact.load(imagename)
        dataspace = ExperimentDataSpace.load_from_ommx_artifact(artifact)
        exp_name = dataspace.experiment_datastore.meta_data["experiment_name"]
        experiment = cls(exp_name, auto_saving=False)
        experiment.dataspace = dataspace
        return experiment

    @classmethod
    def concat(
        cls,
        experiments: list["Experiment"],
        name: typ.Optional[str] = None,
        savedir: str | pathlib.Path = DEFAULT_RESULT_DIR,
        auto_saving: bool = True,
    ) -> "Experiment":
        """Concatenate multiple experiments into a single experiment.

        Args:
            experiments: List of Experiment instances to concatenate.
            name: Name of the concatenated experiment.
            savedir: Directory path for saving the concatenated experiment data.
            auto_saving: Flag to enable automatic saving of the concatenated experiment

        Example:
            ```python
            import minto
            exp1 = minto.Experiment("exp1")
            exp2 = minto.Experiment("exp2")
            exp3 = minto.Experiment("exp3")
            new_exp = minto.Experiment.concat([exp1, exp2, exp3])
            ```

        Returns:
            Experiment: Instance of the concatenated experiment.
        """

        name = name or uuid.uuid4().hex[:8]

        if len(experiments) == 0:
            raise ValueError("No experiments provided.")

        # check if dataspaces have the same experiment-wide data
        first_datastore = experiments[0].dataspace.experiment_datastore
        for experiment in experiments[1:]:
            datastore = experiment.dataspace.experiment_datastore
            if datastore.problems.keys() != first_datastore.problems.keys():
                raise ValueError("Experiments have different problems.")
            if datastore.instances.keys() != first_datastore.instances.keys():
                raise ValueError("Experiments have different instances.")
            if datastore.solutions.keys() != first_datastore.solutions.keys():
                raise ValueError("Experiments have different solutions.")
            if datastore.objects.keys() != first_datastore.objects.keys():
                raise ValueError("Experiments have different objects.")
            if datastore.parameters.keys() != first_datastore.parameters.keys():
                raise ValueError("Experiments have different parameters.")
            if datastore.meta_data.keys() != first_datastore.meta_data.keys():
                raise ValueError("Experiments have different meta data.")

        new_experiment = cls(name, savedir, auto_saving)
        new_experiment.dataspace.experiment_datastore = first_datastore

        for experiment in experiments:
            for datastore in experiment.dataspace.run_datastores:
                new_experiment._run_id = new_experiment.dataspace.add_run_datastore(
                    datastore,
                    with_save=new_experiment.auto_saving,
                    save_dir=new_experiment.savedir,
                )

        return new_experiment

__enter__()

Enter method for the context manager.

Returns:

Name Type Description
Experiment Experiment

Instance of the current experiment run.

Source code in minto/experiment.py
def __enter__(self) -> "Experiment":
    """Enter method for the context manager.

    Returns:
        Experiment: Instance of the current experiment run.
    """
    if self._running:
        return self
    else:
        return self.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
Source code in minto/experiment.py
def __exit__(self, exc_type, exc_value, traceback):
    """Exit method for the context manager.

    Args:
        exc_type: exception type
        exc_value: exception value
        traceback: traceback information
    """
    self.close_run()

__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
def __post_init__(self):
    """Post-initialization method for Experiment class.

    - Add timestamp to the experiment name.
    - Initialize the database instance.
    - Create the OMMX artifact builder.
    """
    # 実験名にタイムスタンプを追加
    self.experiment_name = f"{self.name}_{self.timestamp.strftime('%Y%m%d%H%M%S')}"
    # 保存ディレクトリのパスを設定
    self.savedir = pathlib.Path(self.savedir) / self.experiment_name
    # 自動保存が有効な場合、ディレクトリを作成
    if self.auto_saving:
        self.savedir.mkdir(exist_ok=True, parents=True)
    # データベースインスタンスの初期化
    self.dataspace = ExperimentDataSpace(self.name)

close_run()

Close the current experiment run.

This method should be called to properly end the current run.

Source code in minto/experiment.py
def close_run(self):
    """Close the current experiment run.

    This method should be called to properly end the current run.
    """
    run_time = datetime.now() - self._run_start_time
    elapsed_time = run_time.total_seconds()
    self.log_data(
        "elapsed_time",
        elapsed_time,
        "meta_data",
    )

    self._running = False
    self._run_id += 1

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
@classmethod
def concat(
    cls,
    experiments: list["Experiment"],
    name: typ.Optional[str] = None,
    savedir: str | pathlib.Path = DEFAULT_RESULT_DIR,
    auto_saving: bool = True,
) -> "Experiment":
    """Concatenate multiple experiments into a single experiment.

    Args:
        experiments: List of Experiment instances to concatenate.
        name: Name of the concatenated experiment.
        savedir: Directory path for saving the concatenated experiment data.
        auto_saving: Flag to enable automatic saving of the concatenated experiment

    Example:
        ```python
        import minto
        exp1 = minto.Experiment("exp1")
        exp2 = minto.Experiment("exp2")
        exp3 = minto.Experiment("exp3")
        new_exp = minto.Experiment.concat([exp1, exp2, exp3])
        ```

    Returns:
        Experiment: Instance of the concatenated experiment.
    """

    name = name or uuid.uuid4().hex[:8]

    if len(experiments) == 0:
        raise ValueError("No experiments provided.")

    # check if dataspaces have the same experiment-wide data
    first_datastore = experiments[0].dataspace.experiment_datastore
    for experiment in experiments[1:]:
        datastore = experiment.dataspace.experiment_datastore
        if datastore.problems.keys() != first_datastore.problems.keys():
            raise ValueError("Experiments have different problems.")
        if datastore.instances.keys() != first_datastore.instances.keys():
            raise ValueError("Experiments have different instances.")
        if datastore.solutions.keys() != first_datastore.solutions.keys():
            raise ValueError("Experiments have different solutions.")
        if datastore.objects.keys() != first_datastore.objects.keys():
            raise ValueError("Experiments have different objects.")
        if datastore.parameters.keys() != first_datastore.parameters.keys():
            raise ValueError("Experiments have different parameters.")
        if datastore.meta_data.keys() != first_datastore.meta_data.keys():
            raise ValueError("Experiments have different meta data.")

    new_experiment = cls(name, savedir, auto_saving)
    new_experiment.dataspace.experiment_datastore = first_datastore

    for experiment in experiments:
        for datastore in experiment.dataspace.run_datastores:
            new_experiment._run_id = new_experiment.dataspace.add_run_datastore(
                datastore,
                with_save=new_experiment.auto_saving,
                save_dir=new_experiment.savedir,
            )

    return new_experiment

get_current_datastore()

Get the current DataStore object.

Source code in minto/experiment.py
def get_current_datastore(self) -> "DataStore":
    """Get the current DataStore object."""
    if self._running:
        return self.dataspace.run_datastores[self._run_id]
    else:
        return self.dataspace.experiment_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
def get_experiment_tables(self) -> dict[str, pd.DataFrame]:
    """Get the experiment data as a table.

    Returns:
        pd.DataFrame: DataFrame containing the experiment data.
    """
    exp_table = create_table(self.dataspace.experiment_datastore)
    return exp_table

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
def get_run_table(self) -> pd.DataFrame:
    """Get the run data as a table.

    Returns:
        pd.DataFrame: DataFrame containing the run data.
    """
    run_table = create_table_from_stores(self.dataspace.run_datastores)
    run_table.index.name = "run_id"
    return run_table

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
@classmethod
def load_from_dir(cls, savedir: str | pathlib.Path) -> "Experiment":
    """Load an experiment from a directory containing saved data.

    Args:
        savedir: Directory path containing the saved experiment data.

    Returns:
        Experiment: Instance of the loaded experiment.
    """
    savedir = pathlib.Path(savedir)

    # check directory exists
    if not savedir.exists():
        raise FileNotFoundError(f"Directory not found: {savedir}")

    dataspace = ExperimentDataSpace.load_from_dir(savedir)
    exp_name = dataspace.experiment_datastore.meta_data["experiment_name"]
    experiment = cls(exp_name, auto_saving=False)
    experiment.dataspace = dataspace
    return experiment

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
@classmethod
def load_from_ommx_archive(cls, savefile: str | pathlib.Path) -> "Experiment":
    """Load an experiment from an OMMX artifact file.

    Args:
        savefile: Path to the OMMX artifact file.

    Returns:
        Experiment: Instance of the loaded experiment.
    """
    dataspace = ExperimentDataSpace.load_from_ommx_archive(savefile)
    exp_name = dataspace.experiment_datastore.meta_data["experiment_name"]
    experiment = cls(exp_name, auto_saving=False)
    experiment.dataspace = dataspace
    return experiment

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
@classmethod
def load_from_registry(cls, imagename: str) -> "Experiment":
    """Load an experiment from a Docker registry.

    Args:
        imagename: Name of the Docker image containing the experiment data.

    Returns:
        Experiment: Instance of the loaded experiment.
    """
    artifact = ox_artifact.Artifact.load(imagename)
    dataspace = ExperimentDataSpace.load_from_ommx_artifact(artifact)
    exp_name = dataspace.experiment_datastore.meta_data["experiment_name"]
    experiment = cls(exp_name, auto_saving=False)
    experiment.dataspace = dataspace
    return experiment

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
def log_data(
    self,
    name: str,
    data: typ.Any,
    storage_name: str,
):
    """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]`.

    Args:
        name: Name of the data
        data: Data object to be saved
        storage_name: Name of the storage object
    """
    if not self._running:
        self.dataspace.add_exp_data(
            name,
            data,
            storage_name,
            with_save=self.auto_saving,
            save_dir=self.savedir,
        )
    else:
        self.dataspace.add_run_data(
            self._run_id,
            name,
            data,
            storage_name,
            with_save=self.auto_saving,
            save_dir=self.savedir,
        )

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
def log_instance(
    self,
    instance_name: str | ommx_v1.Instance,
    instance: typ.Optional[ommx_v1.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.

    Args:
        instance_name: Name of the instance, or the instance object itself.
        instance: Instance object (if instance_name is a string).

    Example:
        ```python
        import minto
        exp = minto.Experiment("exp1")
        instance = ommx_v1.Instance()
        exp.log_instance("instance1", instance)
        ```
    """
    datastore = self.get_current_datastore()
    _name, _instance = self._get_name_or_default(
        instance_name, instance, datastore.instances
    )
    self.log_data(
        _name,
        _instance,
        "instances",
    )

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
def log_object(self, name: str, value: dict[str, typ.Any]):
    """Log a custom object to the experiment database.

    Args:
        name: Name of the object
        value: Dictionary containing the object data

    If a run is active, the object is saved with the current run ID.
    """
    type_check([("name", name, str), ("value", value, dict)])
    self.log_data(name, value, "objects")

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
def log_parameter(
    self,
    name: str,
    value: float | int | str | list | dict | np.ndarray,
):
    """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.

    Args:
        name (str): Name of the parameter. Must be a string that uniquely identifies
            the parameter within the experiment.
        value (Union[float, int, str, list, dict, numpy.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)

    Raises:
        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:
        ```python
        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]))
        ```
    """
    if isinstance(value, (list, dict, np.ndarray)):
        # Check value is serializable
        try:
            from minto.v1.json_encoder import NumpyEncoder

            json.dumps(value, cls=NumpyEncoder)
        except TypeError:
            raise ValueError(f"Value is not serializable.")

        self.log_object(name, {"paramter_" + name: value})

    self.log_data(name, value, "parameters")

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
def log_params(self, params: dict[str, float | int]):
    """Log multiple parameters to the experiment database.

    Args:
        params: Dictionary of parameter names and values.

    If a run is active, the parameters are saved with the current run ID.
    Else, they are saved as experiment-wide data.
    """
    for name, value in params.items():
        self.log_parameter(name, value)

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
def log_problem(
    self, problem_name: str | jm.Problem, problem: typ.Optional[jm.Problem] = None
):
    """Log an optimization problem to the experiment database.

    Args:
        problem_name: Name of the problem, or the problem object itself.
        problem: Problem object (if problem_name is a string).

    If a run is active, the problem is saved with the current run ID.
    """
    datastore = self.get_current_datastore()
    _name, _problem = self._get_name_or_default(
        problem_name, problem, datastore.problems
    )
    self.log_data(
        _name,
        _problem,
        "problems",
    )

log_sampleset(name, value=None)

Log a SampleSet to the experiment or run database.

Source code in minto/experiment.py
def log_sampleset(
    self,
    name: str | ommx_v1.SampleSet | jm.SampleSet | jm.experimental.SampleSet,
    value: typ.Optional[ommx_v1.SampleSet] = None,
):
    """Log a SampleSet to the experiment or run database."""

    # If name is specified, use it as the name.
    # If name is not specified and a SampleSet is passed as the first argument,
    # use the variable name of the SampleSet as the name.
    datastore = self.get_current_datastore()
    _name, _value = self._get_name_or_default(name, value, datastore.samplesets)

    if isinstance(_value, (jm.SampleSet, jm.experimental.SampleSet)):
        _value = convert_sampleset_jijmodeling_to_ommx(_value)

    self.log_data(_name, _value, "samplesets")

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
def log_solution(
    self,
    solution_name: str | ommx_v1.Solution,
    solution: typ.Optional[ommx_v1.Solution] = None,
):
    """Log an optimization solution to the experiment database.

    Args:
        solution_name: Name of the solution, or the solution object itself.
        solution: Solution object (if solution_name is a string).

    If a run is active, the solution is saved with the current run ID.
    """
    datastore = self.get_current_datastore()
    _name, _solution = self._get_name_or_default(
        solution_name, solution, datastore.solutions
    )
    self.log_data(_name, _solution, "solutions")

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, or jm.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
def log_solver(
    self,
    solver: typ.Callable[P, R],
    *,
    exclude_params: typ.Optional[list[str]] = None,
) -> typ.Callable[P, R]:
    """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`, or `jm.experimental.SampleSet`.
    - If the result is of type `ommx_v1.Solution`, it is logged as a solution.

    Args:
        solver: Solver function to be logged.

    Returns:
        typ.Callable[P, R]: Wrapped solver function.

    Example:
        ```python
        import minto
        exp = minto.Experiment("exp1")
        result = exp.log_solver(solver)(parameter=1)
        exp.dataspace.experiment_datastore.parameters
        # {'solver_name': 'solver', 'parameter': 1}
        ```
    """
    solver_name = solver.__name__
    self.log_parameter("solver_name", solver_name)

    exclude_params = exclude_params or []

    @wraps(solver)
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        for name, value in kwargs.items():
            if name in exclude_params:
                continue
            if isinstance(value, (int, float, str)):
                self.log_parameter(name, value)
            elif isinstance(value, jm.Problem):
                self.log_problem(name, value)
            elif isinstance(value, ommx_v1.Instance):
                self.log_instance(name, value)

        result = solver(*args, **kwargs)

        if isinstance(
            result, (ommx_v1.SampleSet, jm.SampleSet, jm.experimental.SampleSet)
        ):
            self.log_sampleset(solver_name + "_result", result)
        elif isinstance(result, ommx_v1.Solution):
            self.log_solution(solver_name + "_result", result)
        return result

    return typ.cast(typ.Callable[P, R], wrapper)

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
def push_github(
    self,
    org: str,
    repo: str,
    name: typ.Optional[str] = None,
    tag: typ.Optional[str] = None,
) -> ox_artifact.Artifact:
    """Push the experiment data to a GitHub repository.

    Returns:
        ox_artifact.Artifact: OMMX artifact containing the experiment data.
    """
    builder = ox_artifact.ArtifactBuilder.for_github(
        org=org,
        repo=repo,
        name=name if name else self.name,
        tag=tag if tag else self.timestamp.strftime("%Y%m%d%H%M%S"),
    )
    self.dataspace.add_to_artifact_builder(builder)
    artifact = builder.build()
    artifact.push()
    return artifact

run()

Start a new experiment run.

Returns:

Name Type Description
Experiment Experiment

Instance of the current experiment run.

Source code in minto/experiment.py
def run(self) -> "Experiment":
    """Start a new experiment run.

    Returns:
        Experiment: Instance of the current experiment run.
    """
    self._running = True
    self._run_id = self.dataspace.add_run_datastore(
        DataStore(), with_save=self.auto_saving, save_dir=self.savedir
    )
    self._run_start_time = datetime.now()
    return self

save(path=None)

Save the experiment database to the disk.

Source code in minto/experiment.py
def save(self, path: typ.Optional[str | pathlib.Path] = None):
    """Save the experiment database to the disk."""
    if path is not None:
        self.dataspace.save_dir(path)
    else:
        self.dataspace.save_dir(self.savedir)

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
Source code in minto/experiment.py
def save_as_ommx_archive(
    self, savefile: typ.Optional[str | pathlib.Path] = None
) -> ox_artifact.Artifact:
    """Save the experiment data as an OMMX artifact.

    Args:
        savefile: Path to save the OMMX artifact. If None, a default name is generated.
    """
    if savefile is None:
        # デフォルトのファイル名を生成(タイムスタンプ付き)
        savefile = (
            self.savedir
            / f"{self.name}_{self.timestamp.strftime('%Y%m%d%H%M%S')}.ommx"
        )
    builder = ox_artifact.ArtifactBuilder.new_archive_unnamed(savefile)
    self.dataspace.add_to_artifact_builder(builder)
    return builder.build()