Skip to content

Commit

Permalink
Python SDK - Generate Name functionality for creating experiments. (#…
Browse files Browse the repository at this point in the history
…2272)

* added dco

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* updated condition

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* added exception to catch missing name and generateName

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* updated experiment_name in create_experiment

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* py sdk create_exp - added type validation

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* added dco

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* updated condition

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* added exception to catch missing name and generateName

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* updated experiment_name in create_experiment

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

* py sdk create_exp - added type validation

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>

---------

Signed-off-by: Bharath Krishna <bharathk005@gmail.com>
  • Loading branch information
bharathk005 committed Apr 2, 2024
1 parent 250e9d1 commit 36150bc
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions sdk/python/v1beta1/kubeflow/katib/api/katib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,44 @@ def create_experiment(

namespace = namespace or self.namespace

experiment_name = None
if type(experiment) == models.V1beta1Experiment:
if experiment.metadata.name is not None:
experiment_name = experiment.metadata.name
elif experiment.metadata.generate_name is not None:
experiment_name = experiment.metadata.generate_name
elif "name" in experiment["metadata"]:
experiment_name = experiment["metadata"]["name"]
elif "generate_name" in experiment["metadata"]:
experiment_name = experiment["metadata"]["generate_name"]

if experiment_name is None:
raise ValueError("Experiment must have a name or generateName")

try:
self.custom_api.create_namespaced_custom_object(
outputs = self.custom_api.create_namespaced_custom_object(
constants.KUBEFLOW_GROUP,
constants.KATIB_VERSION,
namespace,
constants.EXPERIMENT_PLURAL,
experiment,
)
experiment_name = outputs["metadata"]["name"] # if "generate_name" is used, "name" gets a prefix from server
except multiprocessing.TimeoutError:
raise TimeoutError(
f"Timeout to create Katib Experiment: {namespace}/{experiment.metadata.name}"
f"Timeout to create Katib Experiment: {namespace}/{experiment_name}"
)
except Exception as e:
if hasattr(e, "status") and e.status == 409:
raise Exception(
f"A Katib Experiment with the name {namespace}/{experiment.metadata.name} already exists."
f"A Katib Experiment with the name {namespace}/{experiment_name} already exists."
)
raise RuntimeError(
f"Failed to create Katib Experiment: {namespace}/{experiment.metadata.name}"
f"Failed to create Katib Experiment: {namespace}/{experiment_name}"
)

# TODO (andreyvelich): Use proper logger.
print(f"Experiment {namespace}/{experiment.metadata.name} has been created")
print(f"Experiment {namespace}/{experiment_name} has been created")

if self._is_ipython():
if self.in_cluster:
Expand All @@ -125,9 +140,9 @@ def create_experiment(
IPython.display.HTML(
"Katib Experiment {} "
'link <a href="/_/katib/#/katib/hp_monitor/{}/{}" target="_blank">here</a>'.format(
experiment.metadata.name,
experiment_name,
namespace,
experiment.metadata.name,
experiment_name,
)
)
)
Expand Down

0 comments on commit 36150bc

Please sign in to comment.