
    =                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z
 ddlmZ dd	lmZ ej                  fd
Z G d de      Zy)z4Utilities for querying hptuning-jobs in AI platform.    )absolute_import)division)unicode_literals)
list_pager)apis)messages)	constants)yamlc                     t        j                  t        j                  t        j                  |          }| t        j
                  k(  r|j                  j                  S |j                  j                  S N)	r   GetMessagesModuler	   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSION
GA_VERSION GoogleCloudAiplatformV1StudySpecAlgorithmValueValuesEnum%GoogleCloudAiplatformV1beta1StudySpec)versionr   s     6lib/googlecloudsdk/api_lib/ai/hp_tuning_jobs/client.pyGetAlgorithmEnumr      s[    ##I$B$B$-$E$Eg$NP(	$$$44MMM99RRR    c                   b    e Zd ZdZd Zd Zd Zd Z	 	 	 	 	 	 	 	 	 	 ddZddZ	dd	Z
dd
Zd Zy)HpTuningJobsClientzBClient used for interacting with HyperparameterTuningJob endpoint.c                     t        j                  t        j                  t        j                  |         }|j
                  | _        |j                  | _        || _	        t        j                  |   | _        y r   )r   GetClientInstancer	   r   r   MESSAGES_MODULE	_messages+projects_locations_hyperparameterTuningJobs_servicer   AI_PLATFORM_MESSAGE_PREFIX_message_prefix)selfr   clients      r   __init__zHpTuningJobsClient.__init__(   s]    ##I$B$B$-$E$Eg$NPF++DNFFDMDL$??HDr   c                 f    t        | j                  dj                  | j                  |      d      S )z(Returns the API messsages class by name.z{prefix}{name})prefixnameN)getattrr   formatr!   )r"   message_names     r   _GetMessagezHpTuningJobsClient._GetMessage0   s9     t';';%1 	  	348: :r   c                 $    | j                  d      S )z5Returns the HyperparameterTuningJob resource message.HyperparameterTuningJob)r+   r"   s    r   HyperparameterTuningJobMessagez1HpTuningJobsClient.HyperparameterTuningJobMessage8   s     566r   c                 8    | j                  d      j                  S )z,Returns enum message representing Algorithm.	StudySpec)r+   r   r.   s    r   AlgorithmEnumz HpTuningJobsClient.AlgorithmEnum=   s     K(AAAr   Nc                 6   | j                         }|r;t        j                  |      }|r$t        j                  || j                               }|j
                  s
|sd|_        n	|r||_        |j                  s
|sd|_        n	|r||_        |r||j                  _        |	r|	|j                  _	        |
r|
|j                  _
        |r||j                  _        |r||_        |r|j                  r||j                  _        | | j                  d      |      |_        |r||_        | j$                  t&        j(                  k(  r| j*                  j-                  ||      }n| j*                  j-                  ||      }| j.                  j1                  |      S )a1  Creates a hyperparameter tuning job with given parameters.

    Args:
      config_path: str, the file path of the hyperparameter tuning job
        configuration.
      display_name: str, the display name of the created hyperparameter tuning
        job.
      parent: str, parent of the created hyperparameter tuning job. e.g.
        /projects/xxx/locations/xxx/
      max_trial_count: int, the desired total number of Trials. The default
        value is 1.
      parallel_trial_count: int, the desired number of Trials to run in
        parallel. The default value is 1.
      algorithm: AlgorithmValueValuesEnum, the search algorithm specified for
        the Study.
      kms_key_name: str, A customer-managed encryption key to use for the
        hyperparameter tuning job.
      network: str, user network to which the job should be peered with
        (overrides yaml file)
      service_account: str, A service account (email address string) to use for
        the job.
      enable_web_access: bool, Whether to enable the interactive shell for the
        job.
      enable_dashboard_access: bool, Whether to enable the dashboard defined for
        the job.
      labels: LabelsValues, map-like user-defined metadata to organize the
        hp-tuning jobs.

    Returns:
      Created hyperparameter tuning job.
       EncryptionSpec)
kmsKeyName)parent.googleCloudAiplatformV1HyperparameterTuningJob)r7   3googleCloudAiplatformV1beta1HyperparameterTuningJob)r/   r
   	load_pathmessages_utilDictToMessageWithErrorCheckmaxTrialCountparallelTrialCounttrialJobSpecnetworkserviceAccountenableWebAccessenableDashboardAccessdisplayName	studySpec	algorithmr+   encryptionSpeclabelsr   r	   r   r   @AiplatformProjectsLocationsHyperparameterTuningJobsCreateRequestr   Create)r"   config_pathdisplay_namer7   max_trial_countparallel_trial_countrF   kms_key_namer@   service_accountenable_web_accessenable_dashboard_accessrH   job_specdatarequests                   r   rJ   zHpTuningJobsClient.CreateB   s   Z 224H^^K(d	 <<$5579 !!/ h	.h&&/C$%h!	$8h!&-h#-<h*.?h+4Kh1)hX''%.h" B 0 01A B!!#h ho||y+++__9A ` Cg __>F ` Hg ==((r   c                 p    | j                   j                  |      }| j                  j                  |      S Nr'   r   =AiplatformProjectsLocationsHyperparameterTuningJobsGetRequestr   Getr"   r'   rU   s      r   r[   zHpTuningJobsClient.Get   s4    nnZZ [ G==W%%r   c                 p    | j                   j                  |      }| j                  j                  |      S rW   )r   @AiplatformProjectsLocationsHyperparameterTuningJobsCancelRequestr   Cancelr\   s      r   r_   zHpTuningJobsClient.Cancel   s4    nn]] ^ G==((r   c                 ~    t        j                  | j                  | j                  j	                  |      dd|      S )N)r7   hyperparameterTuningJobspageSize)fieldbatch_size_attributelimit)r   YieldFromListr   r   >AiplatformProjectsLocationsHyperparameterTuningJobsListRequest)r"   re   regions      r   ListzHpTuningJobsClient.List   s@    ##	G	G 
H 
(' r   c                     | j                   j                  |      }| j                  j                  |      fd}|S )zReturns a function to decide if log fetcher should continue polling.

    Args:
      name: String id of job.

    Returns:
      A one-argument function decides if log fetcher should continue.
    rX   c                 ,    | dk  ryj                   d u S )Nr4   T)endTime)periods_without_logsresponses    r   ShouldContinuez;HpTuningJobsClient.CheckJobComplete.<locals>.ShouldContinue   s    		"%%r   rY   )r"   r'   rU   ro   rn   s       @r   CheckJobCompletez#HpTuningJobsClient.CheckJobComplete   sC     nnZZ [ G}}  )H&
 r   )
NNNNNNNFFNr   )NN)__name__
__module____qualname____doc__r$   r+   r/   r2   rJ   r[   r_   ri   rp    r   r   r   r   %   sV    JI:7
B #_)B&
)
r   r   N)rt   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   r   r;   googlecloudsdk.command_lib.air	   googlecloudsdk.corer
   BETA_VERSIONr   objectr   ru   r   r   <module>r}      sA    ; &  ' ' , A 3 $ '33 Sd dr   