
    g;                        S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSK	r	SSK
Jr  SSK
Jr   " S S\R                  R                  5      r " S S	\R                  R                  5      r " S
 S\R                  R                  5      r " S S\R                  R                  5      r " S S\R                  R                  5      r " S S\R                  R                  5      r " S S\R                  R                  5      r " S S\5      r\R                  R0                  R3                  \\5         " S S\5      r\R                  R0                  R3                  \\5        g)z0Classes for dealing with I/O from ML pipelines.
    N)	cStringIO)	_decoders)_filec                   *    \ rS rSrSrS rS rS rSrg)ExampleProtoCoder   z8A coder to encode and decode TensorFlow Example objects.c                 .    SS K nUR                  U l        g Nr   )
tensorflowtrain	_tf_train)selftfs     ,lib/third_party/ml_sdk/cloud/ml/io/coders.py__init__ExampleProtoCoder.__init__"   s    XXDN    c                 "    UR                  5       $ )zEncodes Tensorflow example object to a serialized string.

Args:
  example_proto: A Tensorflow Example object

Returns:
  String.
)SerializeToString)r   example_protos     r   encodeExampleProtoCoder.encode&   s     **,,r   c                 \    U R                   R                  5       nUR                  U5        U$ )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   decodeExampleProtoCoder.decode1   s)     nn$$&GN+Nr   )r   N	__name__
__module____qualname____firstlineno____doc__r   r   r   __static_attributes__ r   r   r   r      s    @	-r   r   c                   .    \ rS rSrSrSS jrS rS rSrg)		JsonCoder?   z1A coder to encode and decode JSON formatted data.Nc                     Xl         g N_indent)r   indents     r   r   JsonCoder.__init__B   s    Lr   c                 @    [         R                  " XR                  SS9$ )zbEncodes a python object into a JSON string.

Args:
  obj: python object.

Returns:
  JSON string.
),z: )r/   
separators)jsondumpsr.   r   objs     r   r   JsonCoder.encodeE   s     ::c,,;GGr   c                 .    [         R                  " U5      $ )zlDecodes a JSON string to a python object.

Args:
  json_string: A JSON string.

Returns:
  A python object.
)r4   loads)r   json_strings     r   r   JsonCoder.decodeQ   s     ::k""r   r-   r,   r    r'   r   r   r)   r)   ?   s    9
H	#r   r)   c                   J    \ rS rSrSr " S S\5      r   S
S jrS rS r	Sr
g	)CsvCoder]   z3A coder to encode and decode CSV formatted data.
  c                   0    \ rS rSrSrS rS rS rS rSr	g)	CsvCoder._WriterWrappera   z?A wrapper for csv.writer / csv.DictWriter to make it picklable.c                     XU4U l         [        5       U l        U(       a'  [        R                  " U R                  USUS9U l        g [        R                  " U R                  SUS9U l        g )N )lineterminator	delimiter)_stater   _buffercsv
DictWriter_writerwriter)r   column_namesrF   decode_to_dicts       r   r    CsvCoder._WriterWrapper.__init__d   sY    !n=dk[dl	~~LL	! zzLL!r   c                     U R                   R                  U5        U R                  R                  5       nU R                  R	                  S5        U R                  R                  S5        U$ r
   )rK   writerowrH   getvalueseektruncate)r   recordvalues      r   encode_record%CsvCoder._WriterWrapper.encode_records   sO    
llF#ll##%e
ll
llAlr   c                     U R                   $ r,   )rG   r   s    r   __getstate__$CsvCoder._WriterWrapper.__getstate__{   s    [[r   c                 "    U R                   " U6   g r,   )r   )r   states     r   __setstate__$CsvCoder._WriterWrapper.__setstate__~   s    
mmUr   )rH   rG   rK   N)
r!   r"   r#   r$   r%   r   rW   r[   r_   r&   r'   r   r   _WriterWrapperrA   a   s    I!r   ra   c                 j    [         R                  " XX4XV5      U l        U R                  UUUS9U l        g)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.
)rM   rF   rN   N)r   
CsvDecoder_decoderra   _encoder)r   rM   numeric_column_namesrF   rN   fail_on_errorskip_initial_spaces          r   r   CsvCoder.__init__   s@    " ((I+DM ''!% ( 'DMr   c                 8    U R                   R                  U5      $ )a  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.
)rd   r   )r   csv_lines     r   r   CsvCoder.decode   s    " ==))r   c                 8    U R                   R                  U5      $ )a7  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.
)re   rW   )r   python_datas     r   r   CsvCoder.encode   s     ==&&{33r   )rd   re   N)r2   TTF)r!   r"   r#   r$   r%   objectra   r   r   r   r&   r'   r   r   r>   r>   ]   s0    v @ DG26"''2*&4r   r>   c                   *    \ rS rSrSrS rS rS rSrg)	YamlCoder   z1A coder to encode and decode YAML formatted data.c                    [         R                  (       a+  [         R                  U l        [         R                  U l        g[        R                  " S5        [         R                  U l        [         R                  U l        g)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
SafeLoaderrZ   s    r   r   YamlCoder.__init__   sV     **d**dooEF //d//dr   c                 D    [         R                  " USSU R                  S9$ )zbEncodes a python object into a YAML string.

Args:
  obj: python object.

Returns:
  YAML string.
Fzutf-8)default_flow_styleencodingDumper)ru   dumprx   r6   s     r   r   YamlCoder.encode   s'     99   	" "r   c                 >    [         R                  " XR                  S9$ )zlDecodes a YAML string to a python object.

Args:
  yaml_string: A YAML string.

Returns:
  A python object.
)Loader)ru   loadrz   )r   yaml_strings     r   r   YamlCoder.decode   s     99[):):;;r   )rx   rz   Nr    r'   r   r   rr   rr      s    9*$"	<r   rr   c                   D    \ rS rSrSrS rS r\S 5       r\	S 5       r
Srg)	MetadataCoder   z.A coder to encode and decode CloudML metadata.c                 2    [        SS9R                  U5      $ )zbEncodes a python object into a YAML string.

Args:
  obj: python object.

Returns:
  JSON string.
   )r/   )r)   r   r6   s     r   r   MetadataCoder.encode   s     A%%c**r   c                 $    U R                  U5      $ )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   MetadataCoder.decode   s       11r   c                 P    [         R                  " U5      nU R                  U5      $ )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_filer   )clspathdatas      r   	load_fromMetadataCoder.load_from  s#     ??4 D%%r   c                      [        5       R                  U 5      $ ! [         a    [        5       R                  U 5      s $ f = fr,   )r)   r   
ValueErrorrr   )r   s    r   r   MetadataCoder._decode_internal  s<    1[00 1[001s    #A Ar'   N)r!   r"   r#   r$   r%   r   r   classmethodr   staticmethodr   r&   r'   r   r   r   r      s7    6	+	2 & & 1 1r   r   c                   $    \ rS rSrSrS rS rSrg)TrainingJobRequestCoderi  z-Custom coder for a TrainingJobRequest object.c                     0 nUR                  UR                  5        S H"  nX#   (       d  M  X#   R                  5       X#'   M$     [        R                  " U5      $ )zEncode a TrainingJobRequest to a JSON string.

Args:
  training_job_request: A TrainingJobRequest object.

Returns:
  A JSON string
timeoutpolling_interval)update__dict__total_secondsr4   r5   )r   training_job_requestdks       r   r   TrainingJobRequestCoder.encode  sQ     	AHH!**+ -	
t!!# - ::a=r   c                     [        5       n[        R                  " U5      nS H&  nX4   (       d  M  [        R                  " X4   S9X4'   M(     UR
                  R                  U5        U$ )zDecode a JSON string representing a TrainingJobRequest.

Args:
  training_job_request_string: A string representing a TrainingJobRequest.

Returns:
  TrainingJobRequest object.
r   seconds)TrainingJobRequestr4   r:   datetime	timedeltar   r   )r   training_job_request_stringrr   r   s        r   r   TrainingJobRequestCoder.decode1  s[     	A

./A -	
!!!$/ - JJaHr   r'   Nr!   r"   r#   r$   r%   r   r   r&   r'   r   r   r   r     s    5$r   r   c                   $    \ rS rSrSrS rS rSrg)TrainingJobResultCoderiF  z#Custom coder for TrainingJobResult.c                     0 nUR                  UR                  5        US   b!  [        5       nUR                  US   5      US'   [        R
                  " U5      $ )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   r4   r5   )r   training_job_resultr   coders       r   r   TrainingJobResultCoder.encodeI  sZ     	AHH ))* 		(%'e#ll1-?+@Aa::a=r   c                     [        5       n[        R                  " U5      nUS   b!  [        5       nUR	                  US   5      US'   UR
                  R                  U5        U$ )zDecode a string to a TrainingJobResult object.

Args:
  training_job_result_string: A string representing a TrainingJobResult.

Returns:
  A TrainingJobResult object.
r   )TrainingJobResultr4   r:   r   r   r   r   )r   training_job_result_stringr   r   r   s        r   r   TrainingJobResultCoder.decode[  sb     	A

-.A 		(%'e#ll1-?+@AaJJaHr   r'   Nr   r'   r   r   r   r   F  s    +$r   r   c                       \ rS rSrSrSSSSSS\R                  " SS9SSSSSSSSSS4S jr\S 5       r	S r
S	 rS
 rS rSrg)r   ip  zAThis class contains the parameters for running a training job.
  N   r   c                     Xl         X l        X0l        XPl        X@l        Xl        Xl        Xl        Xl        Xl	        Xl
        Xl        Xl        X`l        Xpl        UU l        UU l        g)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   TrainingJobRequest.__init__t  sf    Z KMM&$ O*K""L$ML,DM*Dr   c                     U R                   $ r,   )r   rZ   s    r   projectTrainingJobRequest.project  s    ;;r   c                 d    [        5       nUR                  R                  U R                  5        U$ )zReturn a copy of the object.)r   r   r   )r   r   s     r   copyTrainingJobRequest.copy  s%    AJJdmm$Hr   c                 J    S H  n[        X5      [        X5      :w  d  M    g   g)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__TrainingJobRequest.__eq__  s)    = 
	WQ]	*= r   c                     X:X  + $ r,   r'   r   r   s     r   __ne__TrainingJobRequest.__ne__  
    =r   c                     / nU R                   R                  5        H%  u  p#UR                  SR                  X#5      5        M'     SR                  SR	                  U5      5      $ )N{0}={1}zTrainingJobRequest({0}), r   	iteritemsappendformatjoinr   fieldsr   vs       r   __repr__TrainingJobRequest.__repr__  sQ    F'')mmI$$Q*+ *$++DIIf,=>>r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r!   r"   r#   r$   r%   r   r   r   propertyr   r   r   r   r   r&   r'   r   r   r   r   p  su      ! ( 2 22 ># ##=+~  ?r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   i  zResult of training a model.c                 <    S U l         S U l        S U l        S U l        g r,   r   training_job_metadataerrorr   rZ   s    r   r   TrainingJobResult.__init__  s%     D "&D DJ#Dr   c                 J    S H  n[        X5      [        X5      :w  d  M    g   g)Nr   FTr   r   s      r   r   TrainingJobResult.__eq__  s'    %		WQ]	*%
 r   c                     X:X  + $ r,   r'   r   s     r   r   TrainingJobResult.__ne__  r   r   c                     / nU R                   R                  5        H%  u  p#UR                  SR                  X#5      5        M'     SR                  SR	                  U5      5      $ )Nr   zTrainingJobResult({0})r   r   r   s       r   r   TrainingJobResult.__repr__  sQ    F'')mmI$$Q*+ *#**499V+<==r   )r   r   r   r   N)
r!   r"   r#   r$   r%   r   r   r   r   r&   r'   r   r   r   r     s    #$>r   r   )r%   rI   r   r4   r{   apache_beambeam	six.movesr   ru   google.cloud.ml.utilr   r   codersCoderr   r)   r>   rr   r   r   r   rp   r   registryregister_coderr   r'   r   r   <module>r     s!          * &)) @#!! #<[4t{{   [4|-<!! -<`.1DKK%% .1b'dkk// 'T'T[[.. 'T_? _?D    # #$68O P >  >F    # #$57M Nr   