ArtifactBuilder

ArtifactBuilder#

class ArtifactBuilder#

Builder for OMMX Artifacts.

>>> builder = ArtifactBuilder.temp()
>>> artifact = builder.build()
>>> print(artifact.image_name)
ttl.sh/...-...-...-...-...:1h

add_annotation(key: str, value: str) None#

Add annotation to the artifact itself.

add_dataframe(df: Any, annotation_namespace: str = 'org.ommx.user.', annotations: Any) Descriptor#

Add a pandas DataFrame to the artifact with parquet format.

>>> import pandas as pd
>>> df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
>>> builder = ArtifactBuilder.temp()
>>> _desc = builder.add_dataframe(df, title="test_dataframe")
>>> artifact = builder.build()
>>> layer = artifact.layers[0]
>>> print(layer.media_type)
application/vnd.apache.parquet

add_instance(instance: Instance) Descriptor#

Add an Instance to the artifact with annotations.

>>> from ommx.v1 import Instance
>>> instance = Instance.empty()
>>> instance.title = "test instance"
>>> builder = ArtifactBuilder.temp()
>>> desc = builder.add_instance(instance)
>>> print(desc.annotations['org.ommx.v1.instance.title'])
test instance

add_json(obj: Any, annotation_namespace: str = 'org.ommx.user.', annotations: Any) Descriptor#

Add a JSON object to the artifact.

>>> obj = {"a": 1, "b": 2}
>>> builder = ArtifactBuilder.temp()
>>> _desc = builder.add_json(obj, title="test_json")
>>> artifact = builder.build()
>>> layer = artifact.layers[0]
>>> print(layer.media_type)
application/json

add_layer(media_type: str, blob: bytes, annotations: Mapping[str, str] = {}) Descriptor#

Low-level API to add any type of layer to the artifact with annotations.

Use add_instance() or other high-level methods if possible.

add_ndarray(array: Any, annotation_namespace: str = 'org.ommx.user.', annotations: Any) Descriptor#

Add a numpy ndarray to the artifact with npy format.

>>> import numpy as np
>>> array = np.array([1, 2, 3])
>>> builder = ArtifactBuilder.temp()
>>> _desc = builder.add_ndarray(array, title="test_array")
>>> artifact = builder.build()
>>> layer = artifact.layers[0]
>>> print(layer.media_type)
application/vnd.numpy
>>> print(layer.annotations)
{'org.ommx.user.title': 'test_array'}

add_parametric_instance(instance: ParametricInstance) Descriptor#

Add a ParametricInstance to the artifact with annotations.

add_sample_set(sample_set: SampleSet) Descriptor#

Add a SampleSet to the artifact with annotations.

add_solution(solution: Solution) Descriptor#

Add a Solution to the artifact with annotations.

build() Artifact#

Build the artifact.

for_github(org: str, repo: str, name: str, tag: str) ArtifactBuilder#

An alias for new() to create a new artifact in local registry with GitHub Container Registry image name.

This also sets the org.opencontainers.image.source annotation to the GitHub repository URL.

new(image_name: str) ArtifactBuilder#

Create a new artifact in local registry with a named image name.

>>> from ommx.testing import SingleFeasibleLPGenerator, DataType
>>> generator = SingleFeasibleLPGenerator(3, DataType.INT)
>>> instance = generator.get_v1_instance()
>>> import uuid
>>> image_name = f"ghcr.io/jij-inc/ommx/single_feasible_lp:{uuid.uuid4()}"
>>> builder = ArtifactBuilder.new(image_name)
>>> _desc = builder.add_instance(instance)
>>> artifact = builder.build()
>>> print(artifact.image_name)
ghcr.io/jij-inc/ommx/single_feasible_lp:...

new_archive(path: str | PathLike | Path, image_name: str) ArtifactBuilder#

Create a new artifact archive with a named image name.

new_archive_unnamed(path: str | PathLike | Path) ArtifactBuilder#

Create a new artifact archive with an unnamed image name.

This cannot be loaded into local registry nor pushed to remote registry.

>>> from ommx.testing import SingleFeasibleLPGenerator, DataType
>>> generator = SingleFeasibleLPGenerator(3, DataType.INT)
>>> instance = generator.get_v1_instance()
>>> import uuid
>>> filename = f"data/single_feasible_lp.ommx.{uuid.uuid4()}"
>>> builder = ArtifactBuilder.new_archive_unnamed(filename)
>>> _desc = builder.add_instance(instance)
>>> artifact = builder.build()
>>> print(artifact.image_name)
None

temp() ArtifactBuilder#

Create a new artifact as a temporary file.

Note that this is insecure and should only be used for testing.

>>> builder = ArtifactBuilder.temp()
>>> artifact = builder.build()
>>> print(artifact.image_name)
ttl.sh/...-...-...-...-...:1h