Skip to content

Commit 7911a0a

Browse files
committed
move _trial_component_is_associated_to_trial to private and move it as a method under trial_component, add doc-string and unit-tests
1 parent f824ccc commit 7911a0a

File tree

3 files changed

+65
-23
lines changed

3 files changed

+65
-23
lines changed

src/sagemaker/experiments/run.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,29 +196,9 @@ def __init__(
196196
self.experiment_name,
197197
)
198198

199-
def search_trial_component_associated_trial():
200-
search_results = sagemaker_session.sagemaker_client.search(
201-
Resource="ExperimentTrialComponent",
202-
SearchExpression={
203-
"Filters": [
204-
{
205-
"Name": "TrialComponentName",
206-
"Operator": "Equals",
207-
"Value": str(self._trial_component.trial_component_name),
208-
},
209-
{
210-
"Name": "Parents.TrialName",
211-
"Operator": "Equals",
212-
"Value": str(self._trial.trial_name),
213-
},
214-
]
215-
},
216-
)
217-
if search_results["Results"]:
218-
return True
219-
return False
220-
221-
if not search_trial_component_associated_trial():
199+
if not _TrialComponent._trial_component_is_associated_to_trial(
200+
self._trial_component.trial_component_name, self._trial.trial_name, sagemaker_session
201+
):
222202
self._trial.add_trial_component(self._trial_component)
223203

224204
self._artifact_uploader = _ArtifactUploader(

src/sagemaker/experiments/trial_component.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,42 @@ def _load_or_create(
345345
run_tc = _TrialComponent.load(trial_component_name, sagemaker_session)
346346
is_existed = True
347347
return run_tc, is_existed
348+
349+
@classmethod
350+
def _trial_component_is_associated_to_trial(
351+
cls, trial_component_name, trial_name=None, sagemaker_session=None
352+
):
353+
"""Returns a bool based on if trial_component is already associated with the trial.
354+
355+
Args:
356+
trial_component_name (str): The name of the trial component.
357+
trial_name: (str): The name of the trial.
358+
sagemaker_session (sagemaker.session.Session): Session object which
359+
manages interactions with Amazon SageMaker APIs and any other
360+
AWS services needed.
361+
362+
Returns:
363+
bool: A boolean variable indicating whether the trial component is already
364+
associated with the trial.
365+
366+
"""
367+
search_results = sagemaker_session.sagemaker_client.search(
368+
Resource="ExperimentTrialComponent",
369+
SearchExpression={
370+
"Filters": [
371+
{
372+
"Name": "TrialComponentName",
373+
"Operator": "Equals",
374+
"Value": str(trial_component_name),
375+
},
376+
{
377+
"Name": "Parents.TrialName",
378+
"Operator": "Equals",
379+
"Value": str(trial_name),
380+
},
381+
]
382+
},
383+
)
384+
if search_results["Results"]:
385+
return True
386+
return False

tests/unit/sagemaker/experiments/test_trial_component.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,26 @@ def test_search(sagemaker_session):
396396
),
397397
]
398398
assert expected == list(_TrialComponent.search(sagemaker_session=sagemaker_session))
399+
400+
401+
def test_trial_component_is_associated_to_trial(sagemaker_session):
402+
obj = _TrialComponent(sagemaker_session, trial_component_name="tc-1")
403+
sagemaker_session.sagemaker_client.search.return_value = {
404+
"Results": [
405+
{
406+
"TrialComponent": {
407+
"Parents": [{"ExperimentName": "e-1", "TrialName": "t-1"}],
408+
"TrialComponentName": "tc-1",
409+
}
410+
}
411+
]
412+
}
413+
414+
assert obj._trial_component_is_associated_to_trial("tc-1", "t-1", sagemaker_session) is True
415+
416+
417+
def test_trial_component_is_not_associated_to_trial(sagemaker_session):
418+
obj = _TrialComponent(sagemaker_session, trial_component_name="tc-1")
419+
sagemaker_session.sagemaker_client.search.return_value = {"Results": []}
420+
421+
assert obj._trial_component_is_associated_to_trial("tc-1", "t-1", sagemaker_session) is False

0 commit comments

Comments
 (0)