
    g;                        d Z ddlZddlZddlZddlZddlZddlmZ ddl	Z	ddl
mZ ddl
mZ  G d dej                  j                        Z G d d	ej                  j                        Z G d
 dej                  j                        Z G d dej                  j                        Z G d dej                  j                        Z G d dej                  j                        Z G d dej                  j                        Z G d de      Zej                  j0                  j3                  ee        G d de      Zej                  j0                  j3                  ee       y)z0Classes for dealing with I/O from ML pipelines.
    N)	cStringIO)	_decoders)_filec                   "    e Zd ZdZd Zd Zd Zy)ExampleProtoCoderz8A coder to encode and decode TensorFlow Example objects.c                 .    dd l }|j                  | _        y Nr   )
tensorflowtrain	_tf_train)selftfs     ,lib/third_party/ml_sdk/cloud/ml/io/coders.py__init__zExampleProtoCoder.__init__"   s    XXDN    c                 "    |j                         S )zEncodes Tensorflow example object to a serialized string.

    Args:
      example_proto: A Tensorflow Example object

    Returns:
      String.
    )SerializeToString)r   example_protos     r   encodezExampleProtoCoder.encode&   s     **,,r   c                 \    | j                   j                         }|j                  |       |S )zDecodes a serialized string into a Tensorflow Example object.

    Args:
      serialized_str: string

    Returns:
      Tensorflow Example object.
    )r   ExampleParseFromString)r   serialized_strexamples      r   decodezExampleProtoCoder.decode1   s)     nn$$&GN+Nr   N__name__
__module____qualname____doc__r   r   r    r   r   r   r      s    @	-r   r   c                   $    e Zd ZdZddZd Zd Zy)	JsonCoderz1A coder to encode and decode JSON formatted data.Nc                     || _         y N)_indent)r   indents     r   r   zJsonCoder.__init__B   s	    DLr   c                 F    t        j                  || j                  d      S )zvEncodes a python object into a JSON string.

    Args:
      obj: python object.

    Returns:
      JSON string.
    ),z: )r'   
separators)jsondumpsr&   r   objs     r   r   zJsonCoder.encodeE   s     ::c$,,;GGr   c                 ,    t        j                  |      S )zDecodes a JSON string to a python object.

    Args:
      json_string: A JSON string.

    Returns:
      A python object.
    )r+   loads)r   json_strings     r   r   zJsonCoder.decodeQ   s     ::k""r   r%   r   r!   r   r   r#   r#   ?   s    9
H	#r   r#   c                   @    e Zd ZdZ G d de      Z	 	 	 ddZd Zd Zy)	CsvCoderz3A coder to encode and decode CSV formatted data.
  c                   (    e Zd ZdZd Zd Zd Zd Zy)CsvCoder._WriterWrapperz?A wrapper for csv.writer / csv.DictWriter to make it picklable.c                     |||f| _         t               | _        |r)t        j                  | j                  |d|      | _        y t        j                  | j                  d|      | _        y )N )lineterminator	delimiter)_stater   _buffercsv
DictWriter_writerwriter)r   column_namesr9   decode_to_dicts       r   r   z CsvCoder._WriterWrapper.__init__d   s[    !9n=dk[dl	~~LL	! zzLL!r   c                     | j                   j                  |       | j                  j                         }| j                  j	                  d       | j                  j                  d       |S r	   )r>   writerowr;   getvalueseektruncate)r   recordvalues      r   encode_recordz%CsvCoder._WriterWrapper.encode_records   sO    
llF#ll##%e
ll
llAlr   c                     | j                   S r%   )r:   r   s    r   __getstate__z$CsvCoder._WriterWrapper.__getstate__{   s    [[r   c                 "     | j                   |  y r%   )r   )r   states     r   __setstate__z$CsvCoder._WriterWrapper.__setstate__~   s    dmmUr   N)r   r   r   r    r   rI   rL   rO   r!   r   r   _WriterWrapperr5   a   s    I!r   rP   c                 t    t        j                  ||||||      | _        | j                  |||      | _        y)a  Initializes CsvCoder.

    Args:
      column_names: Tuple of strings. Order must match the order in the file.
      numeric_column_names: Tuple of strings. Contains column names that are
          numeric. Every name in numeric_column_names must also be in
          column_names.
      delimiter: A one-character string used to separate fields.
      decode_to_dict: Boolean indicating whether the docoder should generate a
          dictionary instead of a raw sequence. True by default.
      fail_on_error: Whether to fail if a corrupt row is found. Default is True.
      skip_initial_space: When True, whitespace immediately following the
          delimiter is ignored when reading.
    )r@   r9   rA   N)r   
CsvDecoder_decoderrP   _encoder)r   r@   numeric_column_namesr9   rA   fail_on_errorskip_initial_spaces          r   r   zCsvCoder.__init__   sH    " ((*I~)+DM ''!% ( 'DMr   c                 8    | j                   j                  |      S )aB  Decode csv line into a python dict.

    Args:
      csv_line: String. One csv line from the file.

    Returns:
      Python dict where the keys are the column names from the file. The dict
      values are strings or numbers depending if a column name was listed in
      numeric_column_names. Missing string columns have the value '', while
      missing numeric columns have the value None. If there is an error in
      parsing csv_line, a python dict is returned where every value is '' or
      None.

    Raises:
      Exception: The number of columns to not match.
    )rS   r   )r   csv_lines     r   r   zCsvCoder.decode   s    " ==))r   c                 8    | j                   j                  |      S )aS  Encode python dict to a csv-formatted string.

    Args:
      python_data: A python collection, depending on the value of decode_to_dict
          it will be a python dictionary where the keys are the column names or
          a sequence.

    Returns:
      A csv-formatted string. The order of the columns is given by column_names.
    )rT   rI   )r   python_datas     r   r   zCsvCoder.encode   s     ==&&{33r   N)r)   TTF)	r   r   r   r    objectrP   r   r   r   r!   r   r   r3   r3   ]   s0    v @ DG26"''2*&4r   r3   c                   "    e Zd ZdZd Zd Zd Zy)	YamlCoderz1A coder to encode and decode YAML formatted data.c                     t         j                  r+t         j                  | _        t         j                  | _        yt        j                  d       t         j                  | _        t         j                  | _        y)zTrying to use the efficient libyaml library to encode and decode yaml.

    If libyaml is not available than we fallback to use the native yaml library,
    use with caution; it is far less efficient, uses excessive memory, and leaks
    memory.
    zpCan't find libyaml so it is not used for YamlCoder, the implementation used is far slower and has a memory leak.N)
yaml__with_libyaml__CSafeDumper_safe_dumperCSafeLoader_safe_loaderloggingwarning
SafeDumper
SafeLoaderrK   s    r   r   zYamlCoder.__init__   sV     **d**dooEF //d//dr   c                 H    t        j                  |dd| j                        S )zvEncodes a python object into a YAML string.

    Args:
      obj: python object.

    Returns:
      YAML string.
    Fzutf-8)default_flow_styleencodingDumper)r`   dumprc   r-   s     r   r   zYamlCoder.encode   s'     99   	" "r   c                 D    t        j                  || j                        S )zDecodes a YAML string to a python object.

    Args:
      yaml_string: A YAML string.

    Returns:
      A python object.
    )Loader)r`   loadre   )r   yaml_strings     r   r   zYamlCoder.decode   s     99[):):;;r   Nr   r!   r   r   r^   r^      s    9*$"	<r   r^   c                   <    e Zd ZdZd Zd Zed        Zed        Z	y)MetadataCoderz.A coder to encode and decode CloudML metadata.c                 8    t        d      j                  |      S )zvEncodes a python object into a YAML string.

    Args:
      obj: python object.

    Returns:
      JSON string.
       )r'   )r#   r   r-   s     r   r   zMetadataCoder.encode   s     A%%c**r   c                 $    | j                  |      S )zDecodes a metadata string to a python object.

    Args:
      metadata_string: A metadata string, either in json or yaml format.

    Returns:
      A python object.
    )_decode_internal)r   metadata_strings     r   r   zMetadataCoder.decode   s       11r   c                 N    t        j                  |      }| j                  |      S )zReads a metadata file.

    Assums it's in json format by default and falls back to yaml format if that
    fails.

    Args:
      path: A metadata file path string.

    Returns:
      A decoded metadata object.
    )r   	load_filerx   )clspathdatas      r   	load_fromzMetadataCoder.load_from  s#     ??4 D%%r   c                     	 t               j                  |       S # t        $ r t               j                  |       cY S w xY wr%   )r#   r   
ValueErrorr^   )ry   s    r   rx   zMetadataCoder._decode_internal  s<    1[00 1[001s    "A A N)
r   r   r   r    r   r   classmethodr   staticmethodrx   r!   r   r   rt   rt      s7    6	+	2 & & 1 1r   rt   c                       e Zd ZdZd Zd Zy)TrainingJobRequestCoderz-Custom coder for a TrainingJobRequest object.c                     i }|j                  |j                         dD ]  }||   s	||   j                         ||<     t        j                  |      S )zEncode a TrainingJobRequest to a JSON string.

    Args:
      training_job_request: A TrainingJobRequest object.

    Returns:
      A JSON string
    timeoutpolling_interval)update__dict__total_secondsr+   r,   )r   training_job_requestdks       r   r   zTrainingJobRequestCoder.encode  sU     	AHH!**+ -	
1t!!#! - ::a=r   c                     t               }t        j                  |      }dD ]$  }||   s	t        j                  ||         ||<   & |j
                  j                  |       |S )zDecode a JSON string representing a TrainingJobRequest.

    Args:
      training_job_request_string: A string representing a TrainingJobRequest.

    Returns:
      TrainingJobRequest object.
    r   seconds)TrainingJobRequestr+   r0   datetime	timedeltar   r   )r   training_job_request_stringrr   r   s        r   r   zTrainingJobRequestCoder.decode1  s_     	A

./A -	
1!!!A$/! - JJaHr   Nr   r   r   r    r   r   r!   r   r   r   r     s    5$r   r   c                       e Zd ZdZd Zd Zy)TrainingJobResultCoderz#Custom coder for TrainingJobResult.c                     i }|j                  |j                         |d   !t               }|j                  |d         |d<   t	        j
                  |      S )zEncode a TrainingJobResult object into a JSON string.

    Args:
      training_job_result: A TrainingJobResult object.

    Returns:
      A JSON string
    training_request)r   r   r   r   r+   r,   )r   training_job_resultr   coders       r   r   zTrainingJobResultCoder.encodeI  sZ     	AHH ))* 		(%'e#ll1-?+@Aa::a=r   c                     t               }t        j                  |      }|d   !t               }|j	                  |d         |d<   |j
                  j                  |       |S )zDecode a string to a TrainingJobResult object.

    Args:
      training_job_result_string: A string representing a TrainingJobResult.

    Returns:
      A TrainingJobResult object.
    r   )TrainingJobResultr+   r0   r   r   r   r   )r   training_job_result_stringr   r   r   s        r   r   zTrainingJobResultCoder.decode[  sb     	A

-.A 		(%'e#ll1-?+@AaJJaHr   Nr   r!   r   r   r   r   F  s    +$r   r   c                       e Zd ZdZdddddd ej
                  d      ddddddddddfdZed        Zd Z	d Z
d	 Zd
 Zy)r   zAThis class contains the parameters for running a training job.
  N   r   c                     || _         || _        || _        || _        || _        || _        |	| _        |
| _        || _        || _	        || _
        || _        || _        || _        || _        || _        || _        y)a  Construct an instance of TrainingSpec.

    Args:
      parent: The project name. This is named parent because the parent object
          of jobs is the project.
      job_name: A job name. This must be unique within the project.
      job_args: Additional arguments to pass to the job.
      package_uris: A list of URIs to tarballs with the training program.
      python_module: The module name of the python file within the tarball.
      timeout: A datetime.timedelta expressing the amount of time to wait before
          giving up. The timeout applies to a single invocation of the process
          method in TrainModelDo. A DoFn can be retried several times before a
          pipeline fails.
      polling_interval: A datetime.timedelta to represent the amount of time to
          wait between requests polling for the files.
      scale_tier: Google Cloud ML tier to run in.
      hyperparameters: (Optional) Hyperparameter config to use for the job.
      region: (Optional) Google Cloud region in which to run.
      master_type: Master type to use with a CUSTOM scale tier.
      worker_type: Worker type to use with a CUSTOM scale tier.
      ps_type: Parameter Server type to use with a CUSTOM scale tier.
      worker_count: Worker count to use with a CUSTOM scale tier.
      ps_count: Parameter Server count to use with a CUSTOM scale tier.
      endpoint: (Optional) The endpoint for the Cloud ML API.
      runtime_version: (Optional) the Google Cloud ML runtime version to use.

    N)parentjob_namejob_argspython_modulepackage_uris
scale_tierhyperparametersregionmaster_typeworker_typeps_typeworker_countps_countr   r   endpointruntime_version)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                     r   r   zTrainingJobRequest.__init__t  s    Z DKDMDM&D$D DO*DDK"D"DDL$DDMDL,DDM*Dr   c                     | j                   S r%   )r   rK   s    r   projectzTrainingJobRequest.project  s    ;;r   c                 d    t               }|j                  j                  | j                         |S )zReturn a copy of the object.)r   r   r   )r   r   s     r   copyzTrainingJobRequest.copy  s%    AJJdmm$Hr   c                 H    dD ]  }t        | |      t        ||      k7  s y y)N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   FTgetattrr   ofs      r   __eq__zTrainingJobRequest.__eq__  s-    = 
q	WQ]	*= r   c                     | |k(   S r%   r!   r   r   s     r   __ne__zTrainingJobRequest.__ne__      qy=r   c                     g }| j                   j                         D ]&  \  }}|j                  dj                  ||             ( dj                  dj	                  |            S )N{0}={1}zTrainingJobRequest({0}), r   	iteritemsappendformatjoinr   fieldsr   vs       r   __repr__zTrainingJobRequest.__repr__  sU    F'')1mmI$$Q*+ *$++DIIf,=>>r   )r   r   r   r    r   r   r   propertyr   r   r   r   r   r!   r   r   r   r   p  sw      ! 2 2 22 ># ##=+~  ?r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   zResult of training a model.c                 <    d | _         d | _        d | _        d | _        y r%   r   training_job_metadataerrorr   rK   s    r   r   zTrainingJobResult.__init__  s%     D "&D DJ#Dr   c                 H    dD ]  }t        | |      t        ||      k7  s y y)Nr   FTr   r   s      r   r   zTrainingJobResult.__eq__  s+    %	q	WQ]	*%
 r   c                     | |k(   S r%   r!   r   s     r   r   zTrainingJobResult.__ne__  r   r   c                     g }| j                   j                         D ]&  \  }}|j                  dj                  ||             ( dj                  dj	                  |            S )Nr   zTrainingJobResult({0})r   r   r   s       r   r   zTrainingJobResult.__repr__  sU    F'')1mmI$$Q*+ *#**499V+<==r   N)r   r   r   r    r   r   r   r   r!   r   r   r   r     s    #$>r   r   )r    r<   r   r+   rf   apache_beambeam	six.movesr   r`   google.cloud.ml.utilr   r   codersCoderr   r#   r3   r^   rt   r   r   r\   r   registryregister_coderr   r!   r   r   <module>r      s!          * &)) @#!! #<[4t{{   [4|-<!! -<`.1DKK%% .1b'dkk// 'T'T[[.. 'T_? _?D    # #$68O P >  >F    # #$57M Nr   